Pages

Friday, October 4, 2013

Using UUID's and label for partitions

Using label and UUID can be come very handy when you're using multiple disks or change disks frequently (for any reason). Imagine your machine is powered on. Then you connect the first external disk which will appear as /dev/sdb for eg. /usr. The second external disk that will be connected appears as /dev/sdc for eg. /opt. If you disconnect both disks and connect the second disk for /opt first it will appear as /dev/sdb which was the device entry for /usr before. One chance to avoid that is mounting disks by UUID's. Each partition on a disk has it's own UUID. The UUID can be listed with blkid or lsblk:

# blkid /dev/sda1
/dev/sda1: UUID="3a0b12a5-0cb1-43df-a241-00aa7935e68b" TYPE="swap"
# blkid /dev/sda2
/dev/sda2: UUID="42fbe1d9-ceea-4fd4-9a80-0e20c78f4360" TYPE="ext4"

# lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda
|-sda1 swap         3a0b12a5-0cb1-43df-a241-00aa7935e68b [SWAP]
`-sda2 ext4         0df31c26-9b8c-41dd-87f1-a5e87e9fa0cb /
sdb
`-sdb1 ext4         1c4cf247-1f12-4b3e-b01f-25d785b20f78 /usr
sdc
`-sdc1 ext4         a50bb1f7-2983-4ea4-ae7c-537ec83e2e2e /opt


To generate a new UUID use uuidgen:

# uuidgen
112e76b0-f0e7-4a99-8202-6e94d12e8095


To set a new UUID to a partition tune2fs has to be used:

# tune2fs -U `uuidgen` /dev/sda2
tune2fs 1.42.6 (21-Sep-2012)


Then check for the new UUID:

# blkid /dev/sda2
/dev/sda2: UUID="ed881c6b-f06f-4739-92bd-ffeeaa8849db" TYPE="ext4"


All partitions are available as UUID under /dev/disk/by-uuid/:

# ls -lah /dev/disk/by-uuid/ed881c6b-f06f-4739-92bd-ffeeaa8849db
... /dev/disk/by-uuid/ed881c6b-f06f-4739-92bd-ffeeaa8849db -> ../../sda2


In this case it doesn't matter if the partition appears as /dev/sdb1, /dev/sdc1 or anything else. The uuid will always be same and the partition can be mounted via the UUID. But there is an easier way and that is labeling the partition. Depending on the partition type (swap, ext4, etc) you need various tools to label a partition. For labeling the swap partition you can use swaplabel:

# swaplabel -L swap /dev/sda1
# lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda
|-sda1 swap   swap  3a0b12a5-0cb1-43df-a241-00aa7935e68b [SWAP]
`-sda2 ext4         ed881c6b-f06f-4739-92bd-ffeeaa8849db /
...


For labeling ext4 partitions you can use e2label:

# e2label /dev/sda2 root
# e2label /dev/sdb1 usr
# e2label /dev/sdc1 opt


Then check that label were set by running blkid or lsblk again:

# blkid /dev/sda2
/dev/sda2: LABEL="root" UUID="ed881c6b-f06f-4739-92bd-ffeeaa8849db" TYPE="ext4"
# lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda
|-sda1 swap   swap  3a0b12a5-0cb1-43df-a241-00aa7935e68b [SWAP]
`-sda2 ext4   root  0df31c26-9b8c-41dd-87f1-a5e87e9fa0cb /
sdb
`-sdb1 ext4   usr   1c4cf247-1f12-4b3e-b01f-25d785b20f78 /usr
sdc
`-sdc1 ext4   opt   a50bb1f7-2983-4ea4-ae7c-537ec83e2e2e /opt


All disk will appear as label under /dev/disk/by-label/ now:

# ls -lah /dev/disk/by-label/
total 0
drwxr-xr-x 2 root root 140 Oct  4 12:15 ./
drwxr-xr-x 6 root root 120 Oct  4  2013 ../
lrwxrwxrwx 1 root root  10 Oct  4 12:13 swap -> ../../sda1
lrwxrwxrwx 1 root root  10 Oct  4 12:15 root -> ../../sda2
lrwxrwxrwx 1 root root  10 Oct  4 12:15 usr -> ../../sdb1
lrwxrwxrwx 1 root root  10 Oct  4 12:15 opt -> ../../sdc1


Now you can replace all device entries with new label name, eg.:

# vi /etc/fstab
#/dev/sda1      swap    swap    defaults        0       0
#/dev/sda2      /       ext4    defaults        1       1
#/dev/sdb1      /usr    ext4    defaults        1       1
#/dev/sdc1      /opt    ext4    defaults        1       1
/dev/disk/by-label/swap swap    swap    defaults        0       0
/dev/disk/by-label/root /       ext4    defaults        1       1
/dev/disk/by-label/usr  /usr    ext4    defaults        1       1
/dev/disk/by-label/opt  /opt    ext4    defaults        1       1

...

And for lilo you may use:

# vi /etc/lilo.conf
...
image = /boot/vmlinuz
  root = /dev/disk/by-label/root
  label = Linux
  read-only
...


Links:
https://wiki.archlinux.org/index.php/Persistent_block_device_naming