Labeling disks in Linux

There is something interesting under the hood in Linux, and that is the idea of mounting disks via a label, rather than just using a device path. The reason the idea of mounting disks by label came about, is that sometimes the device name or path can change from one boot up to the next, and this would really spell trouble for your system. Imagine if all of a sudden, your root file system mounted up as your home directory. Not fun.

With this in mind, some really smart people came up with the idea of assigning labels to disks, and mounting by the label, instead of the device name. This way, even if the device name changes, it will still mount up in the same place.

*UPDATE*

Here are some tips for Ubuntu and Debian, since they do things a little differently.

Basically, instead of using the e2label command and mounting via the label, you get the UUID for the drive and mount it by referencing that UUID.  This actually could be a better way to do it since labels can change.  I don’t know if UUIDs can change, but given what a UUID is, I would think that it wouldn’t, or at least wouldn’t be easy to.

Simply run the vol_id command like this:

vol_id /dev/hda1

You should get a result like this: 65468819-0195-49c5-a658-e9bd14170a28

Then add this to the /etc/fstab:

UUID=65468819-0195-49c5-a658-e9bd14170a28  /data_drive           ext3    relatime        0       2

There you go, you can mount it by mounting /data_drive, it’s that easy!  Enjoy!

*END UPDATE*

In case you might not know already, all entries for disks to be mounted at startup are in the file /etc/fstab. What do these labels look like in your fstab? Well, take a look here:

LABEL=/ / ext3 defaults 1 1

Since this is being displayed in your web browser, the extra white spaces won’t be showing up. You will see more space between the items above in the actual fstab file. In the old days, in place of the LABEL= entry, there would have been a device path like /dev/hda1. Now though, you can mount via the label and not worry if /dev/hda1 changes to /dev/hdb2 because you plugged in a USB device or something. It mounts by the label assigned to it, instead of the device name.

What’s that? How do we manage the label? I am so glad you asked! The answer is simple, but yet it’s not. Basically it depends on what file system you formatted the disk with. For the purposes of this article, I will stick with the two most popular (from my travels anyway), and that is ext2 and reiser.

For ext2 file systems, you use the command e2label to view and set the file system label. So, if you want to view the label assigned to the device /dev/hda1, it would look like this:

e2label /dev/hda1

and on my dev box it returns simply:

/

because it’s the root file system. If we take a look at the entry in my /etc/fstab, the entry to mount it looks like this (just like above):

LABEL=/ / ext3 defaults 1 1

Notice above that the file system is actually ext3, and not ext2. For anyone who doesn’t know, ext3 is really ext2 with journaling turned on, so the e2label command applies to it as well. Journaling simply means that the file system logs all changes to a journal before actually committing the changes to the disk. This means that you are much less likely to suffer from disk corruption in the event of a hard stop, power off, etc. The journal can simply be replayed when it comes back up, therefore catching up on any changes that did not get committed. It’s a very cool and highly recommended feature, plus who wants to sit through those lengthy fsck sessions?

Back to labels, if you want to set the label, you use the e2label command still, you simply specify the desired disk label after the path. If I were setting the label on the device in question, it would look like this:

e2label /dev/hda1 /

That tells the e2label command to set the label to “/”, whereas when that option is left off, it returns the current label. Nifty, huh?

If you are working with reiserfs, the command is similar and looks like this:

reiserfstune --label / /dev/hda1

This will accomplish the same thing, it sets the label “/” to /dev/hda1. To my knowledge, there is no way to view the current label with reiserfstune, but you can simply set it to what you want.

There you have it, it’s just that easy. So go forth and mount up some disks with full confidence!

2 thoughts on “Labeling disks in Linux

  1. Pingback: Labeling disks in Linux | Solarum - Information For Everyone

  2. Pingback: Labeling disks in Linux | Solarum - Information For Everyone

Leave a Reply

Your email address will not be published. Required fields are marked *