Linux Essentials – Formatting & Mounting Storage Volumes

Storage volumes are essential to our Linux servers and workstations, but how do we format and mount additional storage volumes? In this episode of Linux Essentials, I’ll walk you through the process of formatting additional storage volumes and mounting them in your Linux instance.

Commands used in this video

The lsblk command will list block devices that are directly attached to your computer. So if you add a second hard disk, or attach an external hard disk or flash drive, it should show up in the results:

lsblk

You can also use the fdisk command to view storage devices. Normally fdisk is used to create partitions, but fdisk -l as root or with sudo will show more information:

sudo fdisk -l

You can even see the disk model in the output so, you can tell which is which.

The mount command can be used to view storage devices that are currently mounted:

mount

But that output can be a bit noisy. To narrow it down, combine it with grep:

mount | grep sdb

To unmount a volume, use the umount command:

umount /dev/sdb1

To prepare a new block storage device for use, we can partition it with the fdisk command (note, this will erase all data on the device):

fdisk /dev/sdb

In the fdisk console, we can type p to print a list of current partitions on the volume:

p

When it comes to formatting the device, the filesystem type you choose for the partition will determine which operating systems will be able to use the device. For a small flash drive on Windows, we should probably format it with exfat. Often, we won’t be able to create an exfat filesystem by default. Most Linux distributions lack the tools required to do this. But we can install the required packages so that we’ll be able to do it.

On Debian/Ubuntu:

sudo apt install exfat-utils exfat-fuse

For Fedora, we can use this command to get exfat support installed:

sudo dnf install fuse-exfat exfat-utils

That should work on CentOS as well, but you may need to add an additional repository to have access to those packages. RPM Fusion should do the trick on Fedora, and the Epel repository on CentOS might be needed on that distro.

Anyway, with exfat support now available, we should be able to create a new filesystem on the flash drive:

sudo mkfs.exfat -n "label here" /dev/device

Now the device should be available to us, and in the case of desktop Linux, will mount and be available automatically when we plug it in.

The df command can be used to view how much disk space is free:

df -h

Note that the -h option is for human readable, it makes it easier to understand the sizes.

For most cases, the ext4 filesystem is a good idea when formatting a disk for exclusive use in Linux.

mkfs.ext4 /dev/sdb1

And now we have a new Linux filesystem available to us.

Now, in the case of a Linux server or any Linux installation that doesn’t automount storage, we’ll need to mount it manually. This is also the same process on headless Linux server. But there’s two directories we can use for this, and which one we use depends on the use-case.

/media is generally for storage devices that come and go. A flash drive is a good example of this, as-is a backup disk that won’t always be attached.

The /mnt directory is generally for storage devices that will either be around for a long time, or expected to be around forever. Something you’ll generally expect to have available.

The way mounting works, is that we attach a storage device to a directory. We can create a subdirectory for this task. For example, I can create a diretory under /mnt for this disk:

sudo mkdir /mnt/disk1

You can name the directory whatever you want.

Then, we can mount the disk:

sudo mount /dev/sdb1 /mnt/disk1

Once it’s mounted, we should see that reflected in the lsblk command:

lsblk

We can also check the available space on our devices, and we should see it in the list:

df -h

When we’re finished, we can unmount the disk:

umount /mnt/disk1

My favorite utility for checking disk space is the ncdu command. It’s great. It’s something I always make sure to have on all my Linux installations. And it’s a good idea to install it as soon as possible. If you run out of space, then you won’t have any space available to install ncdu. So it’s best to install it now.

sudo apt install ncdu

For Fedora and other rpm-based distributions, you can install it with dnf:

sudo dnf install ncdu

Keep in mind that you may need to add an additional repository on non-Debian or Ubuntu systems to be able to install it.

To use it, you can simply run it. I suggest that you use sudo with this command though, not because it’s required, but instead because ncdu will only have access to scan the areas of the disk that the user has access to. root has access to everything:

sudo ncdu

If you want to restrict ncdu to local storage devices, and not external devices or network shares you may have mounted, add the -x option:

ncdu -x /

Start the discussion at community.learnlinux.tv