排查 Linux VM 设备名更改问题Troubleshoot Linux VM device name changes

本文解释重启 Linux VM 或重新附加数据磁盘后,设备名为何发生更改。This article explains why device names change after you restart a Linux VM or reattach the data disks. 此外,本文还提供针对此问题的解决方案。The article also provides solutions for this problem.

症状Symptoms

在 Azure 中运行 Linux VM 时,可能会遇到以下问题:You may experience the following problems when running Linux VMs in Azure:

  • 重启后 VM 无法引导。The VM fails to boot after a restart.
  • 分离再重新附加数据磁盘时,磁盘的设备名发生更改。When data disks are detached and reattached, the disk device names are changed.
  • 使用设备名引用磁盘的应用程序或脚本会失败,因为设备名已发生更改。An application or script that references a disk by using the device name fails because the device name has changed.

原因Cause

不保证 Linux 中的设备路径在重启后保持一致。Device paths in Linux aren't guaranteed to be consistent across restarts. 设备名由主要编号(字母)和次要编号组成。Device names consist of major numbers (letters) and minor numbers. 当 Linux 存储设备驱动程序检测到新设备时,驱动程序会将可用范围内的主要和次要设备编号分配给该设备。When the Linux storage device driver detects a new device, the driver assigns major and minor numbers from the available range to the device. 移除某个设备后,其设备编号将被释放,供重复使用。When a device is removed, the device numbers are freed for reuse.

之所以发生该问题,是因为 Linux 中的设备扫描由 SCSI 子系统计划以异步方式进行。The problem occurs because device scanning in Linux is scheduled by the SCSI subsystem to happen asynchronously. 因此,设备路径名称会在重启后发生变化。As a result, a device path name can vary across restarts.

解决方案Solution

若要解决此问题,请使用持久命名。To resolve this problem, use persistent naming. 有四个方法可使用持久命名:按文件系统标签、按 UUID、按 ID 或按路径。There are four ways to use persistent naming: by filesystem label, by UUID, by ID, or by path. 我们建议对 Azure Linux VM 使用文件系统标签或 UUID。We recommend using the filesystem label or UUID for Azure Linux VMs.

大多数分发都提供 fstab nofail 或 nobootwait 参数 。Most distributions provide the fstab nofail or nobootwait parameters. 在启动时若无法装载磁盘,这些参数可使系统启动。These parameters enable a system to boot when the disk fails to mount at startup. 有关这些参数的详细信息,请查看分发文档。Check your distribution documentation for more information about these parameters. 有关在添加数据磁盘时如何将 Linux VM 配置为使用 UUID 的信息,请参阅连接到 Linux VM 以装载新磁盘For information on how to configure a Linux VM to use a UUID when you add a data disk, see Connect to the Linux VM to mount the new disk.

在 VM 上安装 Azure Linux 代理后,该代理使用 Udev 规则在 /dev/disk/azure 路径下构造一组符号链接。When the Azure Linux agent is installed on a VM, the agent uses Udev rules to construct a set of symbolic links under the /dev/disk/azure path. 应用程序和脚本使用 Udev 规则来识别附加到 VM 的磁盘,以及磁盘类型和磁盘 LUN。Applications and scripts use Udev rules to identify disks that are attached to the VM, along with the disk type and disk LUNs.

识别磁盘 LUNIdentify disk LUNs

应用程序使用 LUN 来查找所有附加的磁盘,并构造符号链接。Applications use LUNs to find all of the attached disks and to construct symbolic links. Azure Linux 代理包含一些 Udev 规则,用以设置从 LUN 到设备的符号链接:The Azure Linux agent includes Udev rules that set up symbolic links from a LUN to the devices:

$ tree /dev/disk/azure

/dev/disk/azure
├── resource -> ../../sdb
├── resource-part1 -> ../../sdb1
├── root -> ../../sda
├── root-part1 -> ../../sda1
└── scsi1
    ├── lun0 -> ../../../sdc
    ├── lun0-part1 -> ../../../sdc1
    ├── lun1 -> ../../../sdd
    ├── lun1-part1 -> ../../../sdd1
    ├── lun1-part2 -> ../../../sdd2
    └── lun1-part3 -> ../../../sdd3

Linux 来宾帐户中的 LUN 信息通过使用 lsscsi 或类似工具进行检索:LUN information from the Linux guest account is retrieved by using lsscsi or a similar tool:

$ sudo lsscsi

[1:0:0:0] cd/dvd Msft Virtual CD/ROM 1.0 /dev/sr0

[2:0:0:0] disk Msft Virtual Disk 1.0 /dev/sda

[3:0:1:0] disk Msft Virtual Disk 1.0 /dev/sdb

[5:0:0:0] disk Msft Virtual Disk 1.0 /dev/sdc

[5:0:0:1] disk Msft Virtual Disk 1.0 /dev/sdd

来宾 LUN 信息与 Azure 订阅元数据一起用于在包含分区数据的 Azure 存储中查找 VHD。The guest LUN information is used with Azure subscription metadata to locate the VHD in Azure Storage that contains the partition data. 例如,可以使用 az CLI:For example, you can use the az CLI:

$ az vm show --resource-group testVM --name testVM | jq -r .storageProfile.dataDisks
[
{
"caching": "None",
  "createOption": "empty",
"diskSizeGb": 1023,
  "image": null,
"lun": 0,
"managedDisk": null,
"name": "testVM-20170619-114353",
"vhd": {
  "uri": "https://testVM.blob.core.chinacloudapi.cn/vhd/testVM-20170619-114353.vhd"
}
},
{
"caching": "None",
"createOption": "empty",
"diskSizeGb": 512,
"image": null,
"lun": 1,
"managedDisk": null,
"name": "testVM-20170619-121516",
"vhd": {
  "uri": "https://testVM.blob.core.chinacloudapi.cn/vhd/testVM-20170619-121516.vhd"
  }
  }
]

使用 blkid 发现文件系统 UUIDDiscover filesystem UUIDs by using blkid

应用程序和脚本读取 blkid 的输出或类似的信息源,以在 /dev 路径中构造符号链接。Applications and scripts read the output of blkid, or similar sources of information, to construct symbolic links in the /dev path. 该输出显示所有附加到 VM 的磁盘的 UUID 及与其关联的设备文件:The output shows the UUIDs of all disks that are attached to the VM and their associated device file:

$ sudo blkid -s UUID

/dev/sr0: UUID="120B021372645f72"
/dev/sda1: UUID="52c6959b-79b0-4bdd-8ed6-71e0ba782fb4"
/dev/sdb1: UUID="176250df-9c7c-436f-94e4-d13f9bdea744"
/dev/sdc1: UUID="b0048738-4ecc-4837-9793-49ce296d2692"

Azure Linux 代理 Udev 规则在 /dev/disk/azure 路径下构造一组符号链接:The Azure Linux agent Udev rules construct a set of symbolic links under the /dev/disk/azure path:

$ ls -l /dev/disk/azure

total 0
lrwxrwxrwx 1 root root  9 Jun  2 23:17 resource -> ../../sdb
lrwxrwxrwx 1 root root 10 Jun  2 23:17 resource-part1 -> ../../sdb1
lrwxrwxrwx 1 root root  9 Jun  2 23:17 root -> ../../sda
lrwxrwxrwx 1 root root 10 Jun  2 23:17 root-part1 -> ../../sda1

应用程序可使用此链接来识别启动盘设备和资源(临时)磁盘。Applications use the links to identify the boot disk device and the resource (ephemeral) disk. 在 Azure 中,应用程序应搜索 /dev/disk/azure/root-part1 或 /dev/disk/azure-resource-part1 路径来发现这些分区。In Azure, applications should look in the /dev/disk/azure/root-part1 or /dev/disk/azure-resource-part1 paths to discover these partitions.

blkid 列表中的任何其他分区都驻留在数据磁盘上。Any additional partitions from the blkid list reside on a data disk. 应用程序维护这些分区的 UUID,并在运行时使用路径来发现设备名:Applications maintain the UUID for these partitions and use a path to discover the device name at runtime:

$ ls -l /dev/disk/by-uuid/b0048738-4ecc-4837-9793-49ce296d2692

lrwxrwxrwx 1 root root 10 Jun 19 15:57 /dev/disk/by-uuid/b0048738-4ecc-4837-9793-49ce296d2692 -> ../../sdc1

获取最新的 Azure 存储规则Get the latest Azure Storage rules

若要获取最新的 Azure 存储规则,请运行以下命令:To get the latest Azure Storage rules, run the following commands:

# sudo curl -o /etc/udev/rules.d/66-azure-storage.rules https://raw.githubusercontent.com/Azure/WALinuxAgent/master/config/66-azure-storage.rules
# sudo udevadm trigger --subsystem-match=block

另请参阅See also

有关详细信息,请参阅以下文章:For more information, see the following articles: