[Ubuntu] Check and format existing hdd/partition via command line

I want to add new hdd into my existing server, here is how to add it and make the partition and format it to ext4 filesystem.

First check the drive

Check the drive location:

   sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL

would show the following:

	NAME   FSTYPE   SIZE MOUNTPOINT LABEL
	sda           111.8G
	|-sda1 ext4    95.9G /
	|-sda2            1K
	|-sda5 swap    15.9G [SWAP]
	sdb           465.8G
	|-sdb1 ntfs   465.8G

In my case sdb is new added drive.

Prepare the drive

Using fdisk command:

   sudo fdisk /dev/sdb

Now create a new partition, type:

   n

Choose partition type (p for primary), type:

   p

Enter the partition number.

   1

It will ask you to set the start and end cylinders on the disk. Just hit enter for each to accept the defaults. Change the system type to Linux, type:

   t

and

   83

Write the changes to new disk.

   w

Format to ext4

Format the new drive into ext4 filesystem.

   sudo mkfs.ext4 /dev/sdb1

Mount the drive

Now the new partition has been created, you need to mount it somewhere to use it. Here I’m using /mnt/data/. First create the directory:

   sudo mkdir /mnt/data/

and mount it into that directory:

   sudo mount /dev/sdb1 /mnt/data/

Mounting the new drive automatically at booting

If you wish to mount the filesystem automatically each time the server boots, adjust the /etc/fstab file:

   sudo nano /etc/fstab

Add below configuration to the last line.

   /dev/sdb1 /mnt/data ext4 defaults 0 2

End.