Linux: how to add a filesystem to fstab the right way
Searching the web for examples on how to mount drives on Linux returns a ton of results, most of those examples mounts the disk using the…
Searching the web for examples on how to mount drives on Linux returns a ton of results, most of those examples mounts the disk using the device name, it works but there is a problem, if the device name is /dev/sdb1 means that this is the first partition of the second hard drive but there no guarantee that /dev/sdb1 will always be /dev/sdb1. Depending on the order of how you connected the hard drives to your motherboard, it may change, using USB external drives can make device naming even more complicated. If that happens, your hard drives may be mounted in the wrong mount points resulting data loss or data corruption.
The solution to this is to mount the disks using their labels or their UUID, i prefer using labels because gives me the option to set a descriptive label, to see the labels of the disks we use the blkid command.root@DietPi:/mnt# blkid
/dev/mmcblk0p1: UUID="2565-1BA0" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="aad71f1f-01"
/dev/mmcblk0p2: UUID="14fcf30d-e3ea-40e7-be42-2304f593b40e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="aad71f1f-02"
/dev/sda2: LABEL="Seagate Expansion Drive" BLOCK_SIZE="512" UUID="A87028CA7028A0D4" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="8da870fd-6821-44b7-b59f-ac5734264780"
/dev/sda1: PARTLABEL="Microsoft reserved partition" PARTUUID="5ab9b52d-a126-4f4b-be48-b840da9526f2"
We can set the label of a device using e2label for ext3 and ext4 filesystems and ntfslabel for ntfs filesystems, in this example we will use an ntfs filesystem.ntfslabel /dev/sda2 "EXT_DRIVE_1"
Checking again with blkid we can see that the label for device /dev/sda2 is now “EXT_DRIVE_1”root@DietPi:~# blkid
/dev/mmcblk0p1: UUID="2565-1BA0" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="aad71f1f-01"
/dev/mmcblk0p2: UUID="14fcf30d-e3ea-40e7-be42-2304f593b40e" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="aad71f1f-02"
/dev/sda2: LABEL="EXT_DRIVE_1" BLOCK_SIZE="512" UUID="A87028CA7028A0D4" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="8da870fd-6821-44b7-b59f-ac5734264780"
/dev/sda1: PARTLABEL="Microsoft reserved partition" PARTUUID="5ab9b52d-a126-4f4b-be48-b840da9526f2"
I will mount my drive under the /mnt/ntfs directory, it cab be any directory you like. The command to mount the drive is mount and the syntax is
mount -t <FILE_SYSTEM_TYPE> -L <LABEL> <DIRECTORY_TO_MOUNT_DRIVE>
Lets try to mount the drive and verify with df that is mounted.root@DietPi:~# mount -t ntfs -L EXT_DRIVE_1 /mnt/ntfs
root@DietPi:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 3.3G 25G 12% /
devtmpfs 183M 0 183M 0% /dev
tmpfs 216M 0 216M 0% /dev/shm
tmpfs 87M 1.6M 85M 2% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.0G 0 1.0G 0% /tmp
tmpfs 50M 56K 50M 1% /var/log
/dev/mmcblk0p1 127M 50M 77M 40% /boot
/dev/sda2 7.3T 77G 7.3T 2% /mnt/ntfs
From the output of df we can see that is mounted, to un-mount the disk we use the umountroot@DietPi:~# umount /mnt/ntfs
To make the disk mount permanent after every boot we need to create a mount entry to /etc/fstab the syntax for the entry is
LABEL=<LABEL_NAME> <DIRECTORY_TO_MOUNT> <FILE_SYSTEM_TYPE> <OPTIONS>
i will not explain the options part of the command, you can find various documentation on the web for this.
insert the following line to the end of /etc/fstabLABEL=EXT_DRIVE_1 /mnt/ntfs ntfs permissions,defaults 0 2
Now try to mount all filesystems of /etc/fstab# mount -a
To verify the results you can use the df command, you should see the drive mounted under the selected directory, now after reboot the drive will mounted under this directory.root@DietPi:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 29G 3.3G 25G 12% /
devtmpfs 183M 0 183M 0% /dev
tmpfs 216M 0 216M 0% /dev/shm
tmpfs 87M 1.6M 85M 2% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.0G 0 1.0G 0% /tmp
tmpfs 50M 56K 50M 1% /var/log
/dev/mmcblk0p1 127M 50M 77M 40% /boot
/dev/sda2 7.3T 77G 7.3T 2% /mnt/ntfs
I hope you found this article useful :)