将磁盘添加到 Linux VMAdd a disk to a Linux VM

本文介绍了如何将持久性磁盘附加到 VM 以便持久保存数据 - 即使 VM 由于维护或调整大小而重新预配。This article shows you how to attach a persistent disk to your VM so that you can preserve your data - even if your VM is reprovisioned due to maintenance or resizing.


在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

将新磁盘附加到 VMAttach a new disk to a VM

如果只需要在 VM 上添加新的空数据磁盘,请使用 az vm disk attach 命令以及 --new 参数。If you want to add a new, empty data disk on your VM, use the az vm disk attach command with the --new parameter. 以下示例创建一个名为“myDataDisk”且大小为 50 GB 的磁盘:The following example creates a disk named myDataDisk that is 50 Gb in size:

az vm disk attach \
   -g myResourceGroup \
   --vm-name myVM \
   --name myDataDisk \
   --new \
   --size-gb 50

附加现有磁盘Attach an existing disk

若要附加现有磁盘,请查找磁盘 ID 并将该 ID 传递到 az vm disk attach 命令。To attach an existing disk, find the disk ID and pass the ID to the az vm disk attach command. 以下示例查询 myResourceGroup 中名为 myDataDisk 的磁盘,然后将其附加到名为 myVM 的 VM:The following example queries for a disk named myDataDisk in myResourceGroup, then attaches it to the VM named myVM:

diskId=$(az disk show -g myResourceGroup -n myDataDisk --query 'id' -o tsv)

az vm disk attach -g myResourceGroup --vm-name myVM --name $diskId

连接到 Linux VM 以装入新磁盘Connect to the Linux VM to mount the new disk

若要对新磁盘进行分区、格式化和装载,以便 Linux VM 可以使用它,请通过 SSH 登录到 VM。To partition, format, and mount your new disk so your Linux VM can use it, SSH into your VM. 有关详细信息,请参阅如何在 Azure 中将 SSH 用于 LinuxFor more information, see How to use SSH with Linux on Azure. 以下示例使用公共 DNS 条目 mypublicdns.chinanorth.cloudapp.chinacloudapi.cn 和用户名 azureuser 连接到一个 VM:The following example connects to a VM with the public DNS entry of mypublicdns.chinanorth.cloudapp.chinacloudapi.cn with the username azureuser:

ssh azureuser@mypublicdns.chinanorth.cloudapp.chinacloudapi.cn

连接到 VM 后就可以附加磁盘了。Once connected to your VM, you're ready to attach a disk. 首先,使用 dmesg 来查找磁盘(用于发现新磁盘的方法可能各不相同)。First, find the disk using dmesg (the method you use to discover your new disk may vary). 以下示例使用 dmesg 来筛选 SCSI 磁盘:The following example uses dmesg to filter on SCSI disks:

dmesg | grep SCSI

输出类似于以下示例:The output is similar to the following example:

[    0.294784] SCSI subsystem initialized
[    0.573458] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 252)
[    7.110271] sd 2:0:0:0: [sda] Attached SCSI disk
[    8.079653] sd 3:0:1:0: [sdb] Attached SCSI disk
[ 1828.162306] sd 5:0:0:0: [sdc] Attached SCSI disk


建议你使用适用于你的发行版的最新版 fdisk 或 parted。It is recommended that you use the latest versions of fdisk or parted that are available for your distro.

此处,sdc 是我们需要的磁盘。Here, sdc is the disk that we want. 使用 parted 对磁盘进行分区,如果磁盘大小为 2TiB 或更大,则必须使用 GPT 进行分区,如果小于 2TiB,则可以使用 MBR 或 GPT 进行分区。Partition the disk with parted, if the disk size is 2 tebibytes (TiB) or larger then you must use GPT partitioning, if it is under 2TiB, then you can use either MBR or GPT partitioning. 如果使用 MBR 分区,则可以使用 fdiskIf you're using MBR partitioning, you can use fdisk. 将其设置为分区 1 中的主磁盘,并接受其他默认值。Make it a primary disk on partition 1, and accept the other defaults. 以下示例在 /dev/sdc 上启动 fdisk 进程:The following example starts the fdisk process on /dev/sdc:

sudo fdisk /dev/sdc

使用 n 命令添加新分区。Use the n command to add a new partition. 在此示例中,我们还选择主分区的 p 并接受其余默认值。In this example, we also choose p for a primary partition and accept the rest of the default values. 输出将类似于以下示例:The output will be similar to the following example:

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x2a59b123.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759

通过键入 p 打印分区表并使用 w 将该表写入磁盘,然后退出。Print the partition table by typing p and then use w to write the table to disk and exit. 输出应类似于以下示例:The output should look similar to the following example:

Command (m for help): p

Disk /dev/sdc: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2a59b123

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    10485759     5241856   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

使用以下命令更新内核:Use the below command to update the kernel:


现在,使用 mkfs 命令将文件系统写入到该分区。Now, write a file system to the partition with the mkfs command. 指定文件系统类型和设备名称。Specify your filesystem type and the device name. 以下示例在通过前面的步骤创建的 /dev/sdc1 分区中创建 ext4 文件系统:The following example creates an ext4 filesystem on the /dev/sdc1 partition that was created in the preceding steps:

sudo mkfs -t ext4 /dev/sdc1

输出类似于以下示例:The output is similar to the following example:

mke2fs 1.42.9 (4-Feb-2014)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310464 blocks
65523 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

现在,使用 mkdir 创建一个目录来装载文件系统。Now, create a directory to mount the file system using mkdir. 以下示例在 /datadrive 处创建一个目录:The following example creates a directory at /datadrive:

sudo mkdir /datadrive

然后,使用 mount 来装载文件系统。Use mount to then mount the filesystem. 以下示例将 /dev/sdc1 分区装载到 /datadrive 装入点:The following example mounts the /dev/sdc1 partition to the /datadrive mount point:

sudo mount /dev/sdc1 /datadrive

若要确保在重新引导后自动重新装载驱动器,必须将其添加到 /etc/fstab 文件。To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file. 此外,强烈建议在 /etc/fstab 中使用 UUID(全局唯一标识符)来引用驱动器而不是只使用设备名称(例如 /dev/sdc1)。It is also highly recommended that the UUID (Universally Unique IDentifier) is used in /etc/fstab to refer to the drive rather than just the device name (such as, /dev/sdc1). 如果 OS 在启动过程中检测到磁盘错误,使用 UUID 可以避免将错误的磁盘装载到给定位置。If the OS detects a disk error during boot, using the UUID avoids the incorrect disk being mounted to a given location. 然后为剩余的数据磁盘分配这些设备 ID。Remaining data disks would then be assigned those same device IDs. 若要查找新驱动器的 UUID,请使用 blkid 实用工具:To find the UUID of the new drive, use the blkid utility:

sudo blkid

输出与以下示例类似:The output looks similar to the following example:

/dev/sda1: UUID="11111111-1b1b-1c1c-1d1d-1e1e1e1e1e1e" TYPE="ext4"
/dev/sdb1: UUID="22222222-2b2b-2c2c-2d2d-2e2e2e2e2e2e" TYPE="ext4"
/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="ext4"


错误地编辑 /etc/fstab 文件可能会导致系统无法引导。Improperly editing the /etc/fstab file could result in an unbootable system. 如果没有把握,请参考分发的文档来获取有关如何正确编辑该文件的信息。If unsure, refer to the distribution's documentation for information on how to properly edit this file. 另外,建议在编辑前备份 /etc/fstab 文件。It is also recommended that a backup of the /etc/fstab file is created before editing.

接下来,在文本编辑器中打开 /etc/fstab 文件,如下所示:Next, open the /etc/fstab file in a text editor as follows:

sudo vi /etc/fstab

在此示例中,使用在之前的步骤中创建的 /dev/sdc1 设备的 UUID 值并使用装入点 /datadrive。In this example, use the UUID value for the /dev/sdc1 device that was created in the previous steps, and the mountpoint of /datadrive. 将以下行添加到 /etc/fstab 文件的末尾:Add the following line to the end of the /etc/fstab file:

UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e   /datadrive   ext4   defaults,nofail   1   2


之后,在不编辑 fstab 的情况下删除数据磁盘可能会导致 VM 无法启动。Later removing a data disk without editing fstab could cause the VM to fail to boot. 大多数分发版都提供 nofail 和/或 nobootwait fstab 选项。Most distributions provide either the nofail and/or nobootwait fstab options. 这些选项使系统在磁盘无法装载的情况下也能启动。These options allow a system to boot even if the disk fails to mount at boot time. 有关这些参数的详细信息,请查阅分发文档。Consult your distribution's documentation for more information on these parameters.

即使文件系统已损坏或磁盘在引导时不存在, nofail 选项也能确保 VM 启动。The nofail option ensures that the VM starts even if the filesystem is corrupt or the disk does not exist at boot time. 如果不使用此选项,可能会遇到 Cannot SSH to Linux VM due to FSTAB errorsWithout this option, you may encounter behavior as described in Cannot SSH to Linux VM due to FSTAB errors

Azure 中对 Linux 的 TRIM/UNMAP 支持TRIM/UNMAP support for Linux in Azure

某些 Linux 内核支持 TRIM/UNMAP 操作以放弃磁盘上未使用的块。Some Linux kernels support TRIM/UNMAP operations to discard unused blocks on the disk. 此功能主要用于标准存储中,如果你创建大型文件后又将其删除,则该功能将通知 Azure 已删除的页不再有效并且可以丢弃,可以节省成本。This feature is primarily useful in standard storage to inform Azure that deleted pages are no longer valid and can be discarded, and can save money if you create large files and then delete them.

在 Linux VM 中有两种方法可以启用 TRIM 支持。There are two ways to enable TRIM support in your Linux VM. 与往常一样,有关建议的方法,请参阅分发:As usual, consult your distribution for the recommended approach:

  • /etc/fstab 中使用 discard 装载选项,例如:Use the discard mount option in /etc/fstab, for example:

    UUID=33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e   /datadrive   ext4   defaults,discard   1   2
  • 在某些情况下,discard 选项可能会影响性能。In some cases, the discard option may have performance implications. 此处,还可以从命令行手动运行 fstrim 命令,或将其添加到 crontab 以定期运行:Alternatively, you can run the fstrim command manually from the command line, or add it to your crontab to run regularly:


    sudo apt-get install util-linux
    sudo fstrim /datadrive


    sudo yum install util-linux
    sudo fstrim /datadrive


将数据磁盘添加到 Linux VM 时,如果 LUN 0 位置没有磁盘,则你可能会遇到错误。When adding data disks to a Linux VM, you may encounter errors if a disk does not exist at LUN 0. 如果使用 az vm disk attach -new 命令并指定 LUN (--lun) 来手动添加磁盘,而不是让 Azure 平台确定适当的 LUN,则请注意,LUN 0 已经有磁盘或者将有磁盘。If you are adding a disk manually using the az vm disk attach -new command and you specify a LUN (--lun) rather than allowing the Azure platform to determine the appropriate LUN, take care that a disk already exists / will exist at LUN 0.

请考虑以下示例,其中显示了 lsscsi 输出的代码片段:Consider the following example showing a snippet of the output from lsscsi:

[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 0 和 LUN 1(lsscsi 中的第一列输出了详细信息 [host:channel:target:lun])。The two data disks exist at LUN 0 and LUN 1 (the first column in the lsscsi output details [host:channel:target:lun]). 两个磁盘都应该可从 VM 内部访问。Both disks should be accessible from within the VM. 如果手动指定了要在 LUN 1 位置添加第一个磁盘并在 LUN 2 位置添加第二个磁盘,则可能无法从 VM 内部正常查看这些磁盘。If you had manually specified the first disk to be added at LUN 1 and the second disk at LUN 2, you may not see the disks correctly from within your VM.


在这些示例中,Azure host 值为 5,但此值可能根据所选存储类型的不同而异。The Azure host value is 5 in these examples, but this may vary depending on the type of storage you select.

此磁盘行为不是 Azure 的问题,而是因为 Linux 内核遵循了 SCSI 规范。This disk behavior is not an Azure problem, but the way in which the Linux kernel follows the SCSI specifications. 当 Linux 内核在 SCSI 总线中扫描附加的设备时,必须能够在 LUN 0 位置找到设备,系统才能继续扫描是否有其他设备。When the Linux kernel scans the SCSI bus for attached devices, a device must be found at LUN 0 in order for the system to continue scanning for additional devices. 因此:As such:

  • 在添加数据磁盘之后,请查看 lsscsi 的输出,验证 LUN 0 位置是否有磁盘。Review the output of lsscsi after adding a data disk to verify that you have a disk at LUN 0.
  • 如果磁盘未在 VM 内正确显示,请验证 LUN 0 位置是否有磁盘。If your disk does not show up correctly within your VM, verify a disk exists at LUN 0.

后续步骤Next steps