永続的なブロックデバイスの命名

提供: ArchWiki
2015年7月3日 (金) 18:05時点におけるKusakata (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

関連記事

この記事ではブロックデバイスに永続的な名前を使う法法を説明します。永続的な命名は udev によって可能になったものであり、バスによる命名と比べて複数の利点があります。使用しているマシンに複数の SATA, SCSI, IDE ディスクコントローラが存在する場合、それぞれのデバイスノードが追加される順番は一定しません。そのため、起動するたびに /dev/sda/dev/sdb といったデバイス名は入れ替わる可能性があり、下手をすると、システムが起動できなくなったり、カーネルパニックが発生したり、ブロックデバイスが表示されなくなってしまいます。永続的な命名によってこれらの問題は解決します。

ノート:
  • 永続的な命名には制約が存在しますが、それはこの記事では扱っていません。例えば、mkinitcpio が命名方法をサポートしていても、systemd が起動時に処理できる命名法には制約が存在することがあります (例: FS#42884)。
  • LVM2 を使用している場合、LVM は自動的にデバイス名を管理するのでこの記事を読む必要はありません。

永続的な命名の方法

永続的な命名には4つの異なる形式があります: by-label, by-uuid, by-id と by-pathGUID Partition Table (GPT) のディスクを使用する場合、さらに by-partlabelby-partuuid の形式も利用可能です。また、Udev による固定デバイス名を使うこともできます。

以下のセクションでは4つの異なる命名規則がどのようなものであるか、またどうやって使うのかを説明しています。

lsblk -f コマンドを使うことで永続的な名前をグラフィカルに表示することができます:

$ 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]

GPT を使っている場合、代わりに blkid コマンドを使います。blkid コマンドはスクリプトで処理しやすいように出力しますが、人間が読むときは読みづらいかもしれません。

$ 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

大抵のファイルシステムはラベルを設定することができます。ラベルが存在するパーティションは /dev/disk/by-label ディレクトリに掲載されます。あなたがパーティションにラベルを付加したり取り除いたりすると、このディレクトリが動的に作成・破壊されます。

$ 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

ファイルシステムのラベルは変更することが可能です。一般的に使われているファイルシステムのラベルを変更する方法は以下の通りになります:

swap 
swaplabel -L <label> /dev/XXX (util-linux を使用)
ext2/3/4 
e2label /dev/XXX <label> (e2fsprogs を使用)
btrfs 
btrfs filesystem label /dev/XXX <label> (btrfs-progs を使用)
reiserfs 
reiserfstune -l <label> /dev/XXX (reiserfsprogs を使用)
jfs 
jfs_tune -L <label> /dev/XXX (jfsutils を使用)
xfs 
xfs_admin -L <label> /dev/XXX (xfsprogs を使用)
fat/vfat 
dosfslabel /dev/XXX <label> (dosfstools を使用)
fat/vfat 
mlabel -i /dev/XXX ::<label> (mtools を使用)
ntfs 
ntfslabel /dev/XXX <label> (ntfs-3g を使用)
zfs 
このファイルシステムは /dev/disk/by-label をサポートしていません。ただし #by-partlabel を使うことはできます。
ノート:
  • root パーティションのファイルシステムラベルを変更する場合、先にパーティションをアンマウントしなければならないので、"ライブ" GNU/Linux ディストリビューションから行う必要があります。
  • Labels have to be unambiguous to prevent any possible conflicts.
  • Labels can be up to 16 characters long.
  • Since the label is a property of the filesystem, it is not suitable for addressing a single RAID device persistently.

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 much less likely that name collisions occur than with labels. Further, it is generated automatically on creation of the filesystem. It will, for example, stay unique even if the device is plugged into another system (which may perhaps have a device with the same label).

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).

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

by-id と by-path

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), so they are linked to the hardware controlling the device. This implies different levels of persistence: the by-path will already change when the device is plugged into a different port of the controller, the by-id will change when the device is plugged into a port of a hardware controller subject to another subsystem. [1] Thus, both are not suitable to achieve persistent naming tolerant to hardware changes.

However, both provide important information to find a particular device in a large hardware infrastructure. For example, if you do not manually assign persistent labels (by-label or by-partlabel) and keep a directory with hardware port usage, by-id and by-path can be used to find a particular device.[2] [3]

by-partlabel

ノート: この方法は GUID Partition Table (GPT) のディスクでだけ使えます。

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

See also Wikipedia:GUID Partition Table#Partition entries.

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
ノート:
  • 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

ノート: この方法は GUID Partition Table (GPT) のディスクでだけ使えます。

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

Wikipedia:ja:GUIDパーティションテーブル#パーティションエントリ (LBA 2〜33) も見て下さい。

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

Udev によるデバイス名の固定

Udev#固定デバイス名の設定 を見て下さい。

永続的な命名の使用

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

fstab

次の記事を参照してください: fstab#UUID

ブートマネージャ

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