Wednesday, March 3, 2010

Labeling a Linux partition - Volume Labels

Here we shall discuss about labeling a Linux partition and its advantages. 

You might have seen labeled partitions if you have opened and viewed the /etc/fstab file. There you can see that the 'root', 'home', 'boot, and other system partitions are labeled and are mounted using the label rather than referring the device name. The advantage is that, the root partition will be the same even if the device name got changed in an unlikely event during the system startup. The volume labels make the partition retain a consistent name regardless of where they are connected and what else are connected. And for your information such an unlikely event of changing the device name is quiet often if you are mounting mounting multiple iSCSI drives to your system. 

Hope you know about the iSCSI target and initiator stuffs, it is the low cost, high efficient alternative for costly storage solutions. Hope I could give you an elaborate post about iSCSI soon coz that too is one of my hot favorite topic.  However here we will discuss about labeling the Linux partition labeling!!!!!


For labeling a Linux partition you can use e2label command, tune2fs or else mke2fs command. Each will support ext2 and ext3 filesystems. e2label can be used fro displaying the filesystem label also. Each volume label can have a maximum of 16 characters. And this label will be retained to the filesystem or partition even if you reconnected the hard disk to another controller or even another computer!!!!
NOTE: You need root privilages to use these commands. In debian/ubuntu based systems you can sudo command.
Usage
The syntax of the commands are as follows:

e2label /device/name label
tune2fs -L label /device/name

For example, if you want to create a label '/storage' to the filesystem /dev/sda5 type in the command as follows:

# e2label /dev/sda5 /storage
or
# tune2fs -L /storage /dev/sda5

Both the commands can be used for labeling the partition as '/storage' and the label stays with in that partition. 

Now lets check the usage of the mke2fs command.
It is just like the tune2fs command:

# mke2fs -L /storage /dev/sda5
or 
# mke2fs /storage /dev/sda5 (NOTE: -L not mandatory.)

However the difference is that the mke2fs command will first format the file system and applies the label. That is you will lose all the data in that drive. So it is always recommended to use the e2label or tune2fs for labeling unless you are very particular about it.

Application
Now that you have labeled your partition you might wonder how could you use it!!!!
Yea that's what I am going to describe to you.

As I explained earlier we are using the volume label for creating a persistent name to the filesystem. And as you can guess, it is of utmost useful while mounting the filesystem during system startup. So you can edit the /etc/fstab to use the label instead of the device name for mounting the partition. Lets take our labeled partition /dev/sda5 as example here.

Open the /etc/fstab file in your favourite editor.As I have been using the partition earlier I have edited and added the entry of /dev/sda5 file system in the fstab for auto-mounting the fs during the system startup. Also I am mounting the filesystem to /storage directory.  It looks like this:

/dev/sda5        /storage        ext3      defaults      1      2

Now that we have labeled the partition we can change the device name as follows:

LABEL=/storage     /storage      ext3      defaults     1      2

Now during the system startup the fifth partition on the first hard-disk (dev/sda5) will be mounted to the /storage folder irrespective of the partition number or drive it is on.

Displaying the current label 

You can use the e2label for displaying the label of a file system.

# e2label /dev/sda5

This will return you:

/storage

The current label of the filesystem!!!!!

Removing label
Now if you want to remove the label just use the tune2fs command as follows:

# tune2fs -L "" /dev/sda5

This will remove the label for the partition. After removing this do not forget to edit the fstab file!!!!!

Hope you enjoyed this article too... Please post you comments if you have any and please let me know if you need to know about any topics on Linux. It will be my pleasure to post a new article for you in this blog [:)].

Cheers,
Jayakrishnan. L

9 comments:

  1. awesome, just what i needed!!!

    ReplyDelete
  2. Good to hear that you found my article interesting!!!!

    ReplyDelete
  3. thanks a ton !!!!!!

    ReplyDelete
  4. Did not know the purpose of e2label and filesystem label until I read this post...

    Thanks

    ReplyDelete
  5. e2label show blank output.... So does it mean by volume is not mounted

    ReplyDelete
    Replies
    1. e2label labels the partition and hence it does not require the volume to be mounted. If e2label does not displays anything, then there wont be any label to the file system !!

      Could you paste the entire command and its output ? then we can get to a conclusion !!

      Delete
  6. LABEL=/storage /storage ext3 defaults 1 2
    its worng should be:

    LABEL=storage /storage ext3 defaults 0 0

    so it will not block the system from booting if file system check is stuck and the dash after the LABEL=/ will give error message:

    # echo "LABEL=/storage /mnt ext4 defaults 0 0" >> /etc/fstab
    # vi /etc/fstab
    # mount -a
    mount: can't find LABEL=/storage

    ReplyDelete
    Replies
    1. Are you sure about this? I have already labelled the filesystem as "/storage".

      The error you are getting could because you have not labelled the filesystem properly. Do check the label of the filesystem using:

      $ e2label fileSystem

      Delete