Persistent block device naming

From ArchWiki
Revision as of 14:39, 6 June 2014 by Kynikos (Talk | contribs) (Static device names with Udev: Udev#Setting_static_device_names has relevant subsections, see https://wiki.archlinux.org/index.php?title=Persistent_block_device_naming&diff=prev&oldid=186846 , ?title=Udev&diff=186842&oldid=186750 + related)

Jump to: navigation, search

Related articles

This article describes how to use persistent names for your block devices. This has been made possible by the introduction of udev and has some advantages over bus-based naming. If your machine has more than one SATA, SCSI or IDE disk controller, the order in which their corresponding device nodes are added is arbitrary. This may result in device names like /dev/sda and /dev/sdb switching around on each boot, culminating in an unbootable system, kernel panic, or a block device disappearing. Persistent naming solves these issues.

Note: If you are using LVM2, this article is not relevant as LVM takes care of this automatically.

Persistent naming methods

There are four different schemes for persistent naming: by-label, by-uuid, by-id and by-path. For those using disks with GUID Partition Table (GPT), two additional schemes can be used by-partlabel and by-partuuid. You can also use static device names by using Udev.

The following sections describes what the different persistent naming methods are and how they are used.

The lsblk -f command can be used for viewing graphically the first persistent schemes:

$ lsblk -f
NAME   FSTYPE LABEL  UUID                                 MOUNTPOINT
sda                                                       
├─sda1 vfat          CBB6-24F2                            /boot
├─sda2 ext4   SYSTEM 0a3407de-014b-458b-b5c1-848e92a327a3 /
├─sda3 ext4   DATA   b411dc99-f0a0-4c87-9e05-184977be8539 /home
└─sda4 swap          f9fe0b69-a280-415d-a03a-a32752370dee [SWAP]

For those using GPT, use the blkid command instead. The latter is more convenient for scripts, but more difficult to read.

$ blkid
/dev/sda1: UUID="CBB6-24F2" TYPE="vfat" PARTLABEL="EFI SYSTEM PARTITION" PARTUUID="d0d0d110-0a71-4ed6-936a-304969ea36af" 
/dev/sda2: LABEL="SYSTEM" UUID="0a3407de-014b-458b-b5c1-848e92a327a3" TYPE="ext4" PARTLABEL="GNU/LINUX" PARTUUID="98a81274-10f7-40db-872a-03df048df366" 
/dev/sda3: LABEL="DATA" UUID="b411dc99-f0a0-4c87-9e05-184977be8539" TYPE="ext4" PARTLABEL="HOME" PARTUUID="7280201c-fc5d-40f2-a9b2-466611d3d49e" 
/dev/sda4: UUID="f9fe0b69-a280-415d-a03a-a32752370dee" TYPE="swap" PARTLABEL="SWAP" PARTUUID="039b6c1c-7553-4455-9537-1befbc9fbc5b"

by-label

Almost every filesystem type can have a label. All your partitions that have one are listed in the /dev/disk/by-label directory. This directory is created and destroyed dynamically, depending on whether you have partitions with labels attached.

$ ls -l /dev/disk/by-label
 
total 0
lrwxrwxrwx 1 root root 10 May 27 23:31 DATA -> ../../sda3
lrwxrwxrwx 1 root root 10 May 27 23:31 SYSTEM -> ../../sda2

The labels of your filesystems can be changed. Following are some methods for changing labels on common filesystems:

swap 
swaplabel -L <label> /dev/XXX using util-linux
ext2/3/4 
e2label /dev/XXX <label> using e2fsprogs
btrfs 
btrfs filesystem label /dev/XXX <label> using btrfs-progs
reiserfs 
reiserfstune -l <label> /dev/XXX using reiserfsprogs
jfs 
jfs_tune -L <label> /dev/XXX using jfsutils
xfs 
xfs_admin -L <label> /dev/XXX using xfsprogs
fat/vfat 
dosfslabel /dev/XXX <label> using dosfstools
fat/vfat 
mlabel -i /dev/XXX ::<label> using mtools
ntfs 
ntfslabel /dev/XXX <label> using ntfs-3g
Note:
  • Changing the filesystem label of the root partition has to be done from a "live" GNU/Linux distribution because the partition needs to be unmounted first.
  • Labels have to be unambiguous to prevent any possible conflicts;
  • Labels can be up to 16 characters long.

by-uuid

UUID is a mechanism to give each filesystem a unique identifier. These identifiers are generated by filesystem utilities (e.g. mkfs.*) when the partition gets formatted and are designed so that collisions are unlikely. All GNU/Linux filesystems (including swap and LUKS headers of raw encrypted devices) support UUID. FAT and NTFS filesystems (fat and windows labels above) do not support UUID, but are still listed in /dev/disk/by-uuid with a shorter UID (unique identifier):

$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 May 27 23:31 0a3407de-014b-458b-b5c1-848e92a327a3 -> ../../sda2
lrwxrwxrwx 1 root root 10 May 27 23:31 b411dc99-f0a0-4c87-9e05-184977be8539 -> ../../sda3
lrwxrwxrwx 1 root root 10 May 27 23:31 CBB6-24F2 -> ../../sda1
lrwxrwxrwx 1 root root 10 May 27 23:31 f9fe0b69-a280-415d-a03a-a32752370dee -> ../../sda4

The advantage of using the UUID method is that it is less likely that you have name collisions than with labels. The disadvantage is that UUIDs make long code lines hard to read and break formatting in many configuration files (e.g. fstab or crypttab). Also every time a partition is resized or reformatted a new UUID is generated and configs have to get adjusted (manually).

Tip: In case your swap partition does not have an UUID assigned, you will need to reset the swap partition using mkswap utility.

by-id and by-path

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: Elements provided in the Talk page (since 2011) seems to clearly indicate that by-id and by-path are valid ways to state persistent device names. Were they not, this section should be marked for deletion so as to not bring up confusion in this page dedicated to persistent device naming. (Discuss in Talk:Persistent block device naming#by-path and by-id unsuitable?)

by-id creates a unique name depending on the hardware serial number, by-path depending on the shortest physical path (according to sysfs). Both contain strings to indicate which subsystem they belong to (i.e. -ide- for by-path, and -ata- for by-id) and thus are not suitable for solving the problems mentioned in the beginning of this article. They will not be discussed any further here.

by-partlabel

Note: This method only concerns disks with GUID Partition Table (GPT).

Partition labels can be defined in the header of the partition entry on GPT disks. Template:Wikipedia

This method is very similar to the filesystem labels, excepted that the dynamic directory is /dev/disk/by-partlabel.

ls -l /dev/disk/by-partlabel/
total 0
lrwxrwxrwx 1 root root 10 May 27 23:31 EFI\x20SYSTEM\x20PARTITION -> ../../sda1
lrwxrwxrwx 1 root root 10 May 27 23:31 GNU\x2fLINUX -> ../../sda2
lrwxrwxrwx 1 root root 10 May 27 23:31 HOME -> ../../sda3
lrwxrwxrwx 1 root root 10 May 27 23:31 SWAP -> ../../sda4
Note:
  • GPT partition labels have also to be different to avoid conflicts. To change your partition label, you can use gdisk or the ncurse-based version cgdisk. Both are available from the gptfdisk package. See Partitioning#Partitioning_tools.
  • According to the specification, GPT partition labels can be up to 72 characters long.

by-partuuid

Note: This method only concerns disks with GUID Partition Table (GPT).

Like GPT partition labels, GPT partition UUID are defined in the partition entry on GPT disks. Template:Wikipedia

The dynamic directory is similar to other methods and, like UUID filesystems, using UUIDs is prefered over labels.

ls -l /dev/disk/by-partuuid/
total 0
lrwxrwxrwx 1 root root 10 May 27 23:31 039b6c1c-7553-4455-9537-1befbc9fbc5b -> ../../sda4
lrwxrwxrwx 1 root root 10 May 27 23:31 7280201c-fc5d-40f2-a9b2-466611d3d49e -> ../../sda3
lrwxrwxrwx 1 root root 10 May 27 23:31 98a81274-10f7-40db-872a-03df048df366 -> ../../sda2
lrwxrwxrwx 1 root root 10 May 27 23:31 d0d0d110-0a71-4ed6-936a-304969ea36af -> ../../sda1

Static device names with Udev

See Udev#Setting static device names.

Using persistent naming

There are various applications that can be configured using persistent naming. Following are some examples of how to configure them.

fstab

See the main article: fstab#UUID

Boot managers

To use persistent names in your boot manager, the following prerequisites must be met:

  • You are using a mkinitcpio initial RAM disk image
  • You have udev enabled in /etc/mkinitcpio.conf

In the above example, /dev/sda1 is the root partition. In the GRUB grub.cfg file, the linux line looks like this:

linux /boot/vmlinuz-linux root=/dev/sda1 rw quiet

Depending on which naming scheme you would prefer, change it to one of the following:

linux /boot/vmlinuz-linux root=/dev/disk/by-label/root_myhost rw quiet

or:

linux /boot/vmlinuz-linux root=UUID=2d781b26-0285-421a-b9d0-d4a0d3b55680 rw quiet

If you are using LILO, then do not try this with the root=... configuration option; it will not work. Use append="root=..." or addappend="root=..." instead. Read the LILO man page for more information on append and addappend.

There is an alternative way to use the label embedded in the filesystem. For example if (as above) the filesystem in /dev/sda1 is labeled root_myhost, you would give this line to GRUB:

linux /boot/vmlinuz-linux root=LABEL=root_myhost rw quiet