#19: Label partitions
On Linux devices and their partitions are named automatically when being plugged in. They have a base name (for SCSI devices it's sd
), a device identifier (a lowercase letter) and a number for every partition. So normal SCSI devices could have entries like this
/dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1
for two disks: one with three partitions, another with just a single partition. These names depend on the order in which the devices are plugged in, so they are not static at all.
To identify disks, you can either plug them in and watch the changes in /dev/
or have a look at /dev/disk/by-id/
or /dev/disk/by-uuid/
where Udev creates symlinks for all disks currently plugged in. Each disk has a UUID (linked in /dev/disk/by-uuid/
) and a type specific (not unique) ID (linked in /dev/disk/by-id/
), which consists of things like the manufacturer's name, the serial number etc.
Neither the ID nor the UUID are always clear and you might still find yourself struggling with finding out the name of the device currently attached to your computer. One way to avoid this is to name your partitions. You can do this by using either e2label
or tune2fs
(for Ext2/3/4 file systems). To give the partition /dev/sdb1
the name documents
, run either of these commands:
e2label /dev/sdb1 documents
tune2fs -L documents /dev/sdb1
You can also make use of mke2fs
to label your partitions, but that would reformat your disk. Thus, this is only useful when naming fresh partitions directly without running a second command. For instance
mke2fs -t ext4 -L multimedia /dev/sdc1
# Or shorter:
mke2fs -t ext4 multimedia /dev/sdc1
That would create a new Ext4 file system with the label multimedia
on partition /dev/sdc1
.
Once you have labeled your partition, you can reference it using this name. You no longer need to bother about the real device name. E.g. to reference our two labeled partitions in /etc/fstab
, write:
# /etc/fstab (snippet):
# ...
LABEL=documents /mnt/documents ext4 defaults 1 1
LABEL=multimedia /mnt/multimedia ext4 defaults 1 1
# ...
Or to mount these disks manually, run
mount -L documents /mnt/documents
mount -L multimedia /mnt/multimedia
Of course, labeling with e2*
-like tools only works for Ext file systems, but there are similar tools for other file systems. For example ntfslabel
for NTFS (from package ntfsprogs
), dosfslabel
for FAT (package dosfstools
), xfs_admin -L
(package xfsprogs
) for XFS and many more.
Actually, you can also reference your disks by UUIDs instead of labels but not all file systems support per-partition UUIDs, for some you have to edit them manually in a hexeditor (e.g. FAT and NTFS). A UUID consists of a series of hexadecimal digits separated by hyphens, e.g., 345ae2f-56235de244-a52fc-52395
. You can set the UUID for an Ext file system with tune2fs -U <uuid>
where <uuid> is either a custom UUID, clear
to clear/remove the UUID, random
to generate a random UUID or time
to generate a UUID based on the current time.
tune2fs -U 123-456 /dev/sdc1
creates a new UUID for our multimedia partition. You can now mount this partition either in your /etc/fstab
# /etc/fstab (snippet):
# ...
UUID=123-456 /mnt/multimedia defaults 1 1
# ...
or manually with mount
mount -U 123-456 /mnt/multimedia
But there's also another less known way to name partitions. This method “names” partitions without naming them. The command-line tool devlabel
creates a symlink to a partition by referencing its UUID (and if not available the SCSI or IDE identifier of the disk). devlabel
is not a standard tool on most Linux distributions and must be installed before. As an example, let us create a symlink to our documents partition by executing
devlabel -d /dev/sdb1 -s /dev/documents
Now we can mount /dev/documents
as usual. That's it, hope you learned a thing or two.
Read more about labeling partitions:
- About.com: tune2fs man page
- About.com: e2label man page
- devlabel project page
- About.com: devlabel man page
- Freshmeat.net: devlabel
RT @reflinux: #Advent series "24 Short #Linux #Hints", day 19: #Label #partitions http://bit.ly/eDBZf6