In an earlier article, we looked at how to mount hard disks in Linux after partitioning and formatting them. We say that using the “lsblk” command, we can get the device names as well as the point on the filesystem where they are mounted. For example, here is my /dev/sda1 partition mounted to “/mount/first-partition”:
This happens at boot time due to an entry in my /etc/fstab file like this:
/dev/sda1 /mount/first-partition ext4 defaults 0 0
It’s a very simple command. It tells Linux that the device called “/dev/sda1” has to be mounted to the specified location. Here, “/dev/sda1” is a 10GB partition on the “/dev/sda” disk as we can see from the lsblk command.
Adding a New Hard Disk (Volume)
Everything seems to be working and fine. That is, until I add a second volume to my Linux droplet on DigitalOcean.
Now when I restart my Linux server, it hangs on boot. When I view the console output, here is what I see:
It seems the system can’t find the /dev/sda1″ device anymore! When I troubleshoot from the console, this is the output of lsblk:
Something strange seems to have happened. My earlier volume was called “/dev/sda” and the partitions were named “/dev/sda1” and “/dev/sda2”. But after the reboot and the new volume, it seems to have changed to “/dev/sdb1” and “/dev/sdb2”!
And when the system read my /etc/fstab file, it obviously couldn’t find the partitions it was referring to, and threw an error.
Hard Disks Change Names all the Time
This is a perfect example of how hard disk names change when you add or remove drives. If you have scripts or some other reference to them using the standard “/dev/sda” or “/dev/vda” or whatever file names, it could lead to some very nasty outcomes. You might end up formatting the wrong disk, or partitioning a device that still has critical data on it! What we need is a more consistent way to name devices that doesn’t change depending on factors like the number of disks etc.
For that, I prefer to use the disk “by-id”.
Using “by-id” to Refer to Hard Disks is Preferable
The names “/dev/sda1” are merely placeholders for the underlying hardware. In the “/dev/disk/” folder on your Linux installation, you’ll find a list of symbolic links pointing to locations like “/dev/sda” etc. Like this:
In the screenshot, you can see that “scsi-0DO_Volume_volume-nyc1-01-part1” refers to “/dev/sdb1” – which is the partition we’re interested in mounting. So we can use that name instead of “/dev/sdb1”. And this doesn’t change depending on how many volumes exist, or whether some hard disk has been removed. Referring to this name instead of “/dev/sdb1” is much safer, and consistent. You don’t have to worry about the system not finding your hard disk!
So now the command for mounting the partition changes from:
mount -o defaults /dev/sdb1 /mount/first-partition
mount -o defaults /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 /mount/first-partition
You can see in the screenshot below, that the second command ends up mounting “/dev/sdb1” just like the first command:
That’s pretty cool! In the same way, you can make a reference to this location in your /etc/fstab as well. The line to append in fstab would change from:
/dev/sdb1 /mount/first-partition ext4 defaults 0 0
/dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01-part1 /mount/first-partition ext4 defaults 0 0
The second line is definitely a bit more unwieldy, but it’s safer and more consistent. That’s what really matters. And this is what you should do when you’re making a hard reference to a device in a script or in a file. Find the actual ID of the disk or the partition, and use that instead of the dynamically generated “/dev/sda” or “/dev/vda” files.