Partition and Format Storage Device in Linux

When you attach a new storage device, you will need to first create a new partition and format it with a filesystem before you can make use of it. To view all attached devices, issue the following lsblk command:

[email protected]:~# lsblk -f
NAME    FSTYPE  LABEL           UUID                                 MOUNTPOINT
sr0     iso9660 config-2        2020-02-05-14-34-50-00               
vda                                                                  
├─vda1  ext4    cloudimg-rootfs 5de0cac7-17b6-4a0f-8715-54cfaaa0a9a9 /
├─vda14                                                              
└─vda15 vfat    UEFI            0564-2E15                            /boot/efi
vdb                                                                  
vdc

Here we have two newly attached drives, vdb and vdc.

Create a new Partition

First we need to give the drive a label. Here we use GPT but msdos is also an option.

[email protected]:~# parted /dev/vdb mklabel gpt

The below will create one partition on /dev/vdb using 100% of the available space:

[email protected]:~# parted -a opt /dev/vdb mkpart primary ext4 0% 100%

This creates a new partition, /dev/vdb1:

[email protected]:~# lsblk -f /dev/vdb
NAME   FSTYPE LABEL UUID MOUNTPOINT
vdb                      
└─vdb1

Create a Filesystem on Partition

The below command creates an ext4 filesystem:

[email protected]:~# mkfs.ext4 -L shared-storage /dev/vdb1

Make sure you run this against vdb1 (the partition) and not vdb (the root drive). The -L option can be used to pass a partition label.

[email protected]:~# lsblk -f /dev/vdb
NAME   FSTYPE LABEL          UUID                                 MOUNTPOINT
vdb                                                               
└─vdb1 ext4   shared-storage 2e7a0866-1164-44a8-9b6e-e2c4315e4e96

Mount the new Filesystem

Create the directory where you want to mount the drive:

[email protected]:~# mkdir -v /mnt/shared-storage
mkdir: created directory '/mnt/shared-storage'

Confirm you can mount with the default values:

[email protected]:~# mount -o defaults /dev/vdb1 /mnt/shared-storage

You should now see it mounted under MOUNTPOINT:

[email protected]:~# lsblk -f /dev/vdb
NAME   FSTYPE LABEL          UUID                                 MOUNTPOINT
vdb                                                               
└─vdb1 ext4   shared-storage 2e7a0866-1164-44a8-9b6e-e2c4315e4e96 /mnt/shared-storage

If you view the directory contents, you should see the lost+found folder. If not, check it mounted successfully.

To mount it permanently and at boot, edit the /etc/fstab file:

[email protected]:~# vim /etc/fstab

And add an entry like the below:

UUID=2e7a0866-1164-44a8-9b6e-e2c4315e4e96	/mnt/shared-storage	ext4 defaults 0 2

Make sure you replace the above UUID with the UUID of your drive. You can get this information from lsblk:

[email protected]:~# lsblk -f /dev/vdb
NAME   FSTYPE LABEL          UUID                                 MOUNTPOINT
vdb                                                               
└─vdb1 ext4   shared-storage 2e7a0866-1164-44a8-9b6e-e2c4315e4e96 /mnt/shared-storage

To check the entry in fstab works, first un-mount the drive if necessary:

[email protected]:~# umount -v /mnt/shared-storage 
umount: /mnt/shared-storage unmounted

Running the below command will read /etc/fstab and try and mount everything listed within it:

[email protected]:~# mount -a

If that worked, the partition should now be mounted again:

[email protected]:~# lsblk -f /dev/vdb
NAME   FSTYPE LABEL          UUID                                 MOUNTPOINT
vdb                                                               
└─vdb1 ext4   shared-storage 2e7a0866-1164-44a8-9b6e-e2c4315e4e96 /mnt/shared-storage

Your new disk and/or partition is now ready for use!

Be the first to comment

Leave a Reply