logo
Published on Debian/Ubuntu Tips & Tricks (http://www.debuntu.org)

How-to get your removable device mounted under an explicit and persistent name

By chantra
Created 2006-12-27 14:48

You might have wondered how comes that your mp3 player is automatically mounted under a nice name like JUKEBOX for instance, while you usb stick simply get a name like USB_BAR and USB_BAR-1... for its partitions.
This is actually due to hal automatically mounting the device.
This tutorial will show you how to give a label to your partitions in order to have your removable devices mounted under an explicit location such as: /media/red-usb-disk or /media/my-big-fat-partition.

When automatically mounting a device, it happens that hal already know about this device, in which case, the device is going to be mounted under, let say for an Ipod, /media/Ipod. But if you have an external hard-drive that you connect through usb, chances are that your external hard-drive partitions are mounted under /media/usbdisk, /media/usbdisk-1 and so on.
And actually, partition one might be mounted under usbdisk-1 on day and usbdisk the day after :s.
Imagine you stock all you music on your external hardrive. Today, you hardrive get mounted under /media/usbdisk and you create a playlist. Tomorrow, when you plug you hardrive, your music partition might get mounted under /media/usbdisk-1, you start your music player, this one kept your last playlist in memory, but you simply can't replay it because the files have moved from /media/usbdisk to /media/usbdisk-1 :(.
This is where labeling a partition will become handy.

ext2 and ext3 [1]

  • reiserfs [1]
  • vfat [1]
  • XFS [1]: not tested, need your feedback
  • JFS [1]: not tested, need your feedback
  • Because linux comes with a whole range of file systems, we are going to need different tools depending on which filesystem you are using.
    From now on, I will suppose you know which file system your device is formated to. If you don't know yet, simply plug you device in order to get it mounted, the run:

    $ df -T

    This will output something like:

    .....
    .....
    /dev/scd0 iso9660 3011040 3011040 0 100% /media/cdrom0
    /dev/sdb1 vfat 244480 20756 223724 9% /media/USBDRIVE

    You can find the file system type on the second column: here iso9660 for the cdrom and vfat for the usb disk. The device name is found in the first column: here /dev/scd0 and /dev/sdb1

    From now on, we are going to work on device /dev/sdaX, you will have to adjust this in accordance with the device you want to rename. Let's get into it now :).

    back to file systems menu [1]

    In order to change the label of an ext2 or ext3 partition, you will need to use: e2fsprogs program utilities. If it is not yet install on your computer, run the following command to install it:

    $ sudo apt-get install e2fsprogs

    Retrieving the existing label

    In order to retrieve the existing label of your partition, simply run:

    $ sudo e2label /dev/sdaX
    my label

    Setting a new label

    To set up a new label, you simply have to append the label name at the end of the command line, so it will look like:

    $ sudo e2label /dev/sdaX "my new label"

    ext2/3 label has to be at most 16 characters long, if longer, label will be truncated

    In order to confirm that your changes where properly applied, you can retrieve the partition's label with:

    $ sudo e2label /dev/sdaX
    my new label

    The new label should be output on the next line.

    Deleting an existing label

    Label can be deleted by supplying an empty label to e2label with this command line:

    $ sudo e2label /dev/sdaX ""

    back to file systems menu [1]

    First of all, you need to have reiserfsprogs package installed. If it not yet present on your machine, please run:

    $ sudo apt-get install reiserfsprogs

    While working on a reiserfs partition, the partition needs to be unmounted.

    Now that you made sure that your partition is unmounted, let see how it works.

    Retrieving the existing label

    To retrieve the existing label, run:

    $ sudo reiserfstune /dev/sdaX | grep LABEL
    ...
    LABEL:

    The label is going to be apended to "LABEL: ", if there is no label yet, only "LABEL: " will appear.

    Setting a new label

    To set up a new label, you will need to use the -l switch like:

    $ sudo reiserfstune /dev/sdaX -l "my new label"

    Check for the "LABEL: " entry in the output, this one should now print:

    LABEL: my new label

    Reiserfs label has to be at most 16 characters long, if longer, label will be truncated

    Removing an existing label

    To do so, simply supply an empty label with this command line:

    $ sudo reiserfstune /dev/sdaX -l ""

    back to file systems menu [1]

    This one is a bit more tricky as you can't simply use the command line, but you will need to edit a file in order to let the tool know the name of the device.

    The package we are going to use here is mtools. If the package is not installed on your machine, please run:

    $ sudo apt-get install mtools

    Informing mtools about your device

    mtools needs to be told an "windows like" device name (something like G:) to match a linux device name. To do so, create and edit file ~/.mtoolsrc and add:

    drive i: file="/dev/sdaX"

    Where i: is the "windows name" and /dev/sdaX is the linux file system associated to it.

    Retrieving the existing label

    Now, you can use mlabel, provided by mtools package to retrieve the existing label. To do so, trigger the folowin command:

    $ mlabel -s i:

    Depending if the device already had a label or not, mlabel will output either:

    Volume has no label

    or

    Volume label is MY LABEL

    Setting a new label

    Setting a new label is done via the following command line:

    $ mlabel i:"my new label"

    Deleting an existing label

    Deleting an existing label can be done with:

    $ mlabel -c i:

    Doing so, you won't be prompt and the label will be removed.

    back to file systems menu [1]

    The tool used here is xfs_admin provided by xfsprogs package, so in the first place you need to have it installed:

    $ sudo apt-get install xfsprogs

    Retrieving an existing label

    To retrieve an existing label, run:

    $ sudo xfs_admin -l /dev/sdaX

    Setting a new label

    A new label can be set with this command line:

    $ sudo xfs_admin -L "my new label" /dev/sdaX

    XFS file system can only hold a label of at most 12 characters, if the label supplied is longer, the label will be truncated and a warning will be printed.

    Deleting an existing label

    To delete a label, simply supplied an empty label with this command:

    $ sudo xfs_admin -L "" /dev/sdaX

    back to file systems menu [1]

    Here, we are going to use jfs_tune provided by the jfsutils package. Install it with:

    $ sudo apt-get install jfsutils

    Retrieving an existing label

    An existing label can be retrieve with:

    $ sudo jfs_tune -l /dev/sdaX | grep label
    Volume label: 'mylabel'

    Setting a new label

    A new label can be set with the following command line:

    $ sudo jfs_tune -L "my new label" /dev/sdaX

    JFS file system can only hold a label of at most 16 characters, if the label supplied is longer, the label will be truncated and a warning will be printed.

    Deleting a label

    A label should be deletable using the following command:

    $ sudo jfs_tune -L "" /dev/sdaX

    Conclusion

    By using labels, you will be able to have your removable device mounted under persistent names as well as more user friendly names.
    For instance, having to usb stick plugged in, it will be easier for you to manipulate datas on the right device, if one is mounted under /media/blue-usbstick and the other one under /media/red-usbstick instead or /dev/disk1 and /dev/disk2 (mainly when those names can be swapped depending which device you plugged in first).


    Source URL:
    http://www.debuntu.org/device-partition-labeling