您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

将磁盘添加到 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.

使用托管磁盘Use managed disks

Azure 托管磁盘通过管理与 VM 磁盘关联的存储帐户简化了 Azure VM 的磁盘管理。Azure Managed Disks simplifies disk management for Azure VMs by managing the storage accounts associated with the VM disks. 你只需指定所需的类型(“高级”或“标准”)和磁盘大小,Azure 将创建和管理磁盘。You only have to specify the type (Premium or Standard) and the size of disk you need, and Azure creates and manages the disk for you. 有关详细信息,请参阅托管磁盘概述For more information, see Managed Disks overview.

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

如果只需要 VM 上的新磁盘,请使用 az vm disk attach 命令以及 --new 参数。If you just need a new disk on your VM, use the az vm disk attach command with the --new parameter. 如果 VM 位于某个可用性区域中,则会自动在与 VM 相同的区域中创建磁盘。If your VM is in an Availability Zone, the disk is automatically created in the same zone as the VM. 有关详细信息,请参阅可用性区域概述For more information, see Overview of Availability Zones. 以下示例创建一个名为 myDataDisk 且大小为 50 GB 的磁盘:The following example creates a disk named myDataDisk that is 50Gb in size:

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

附加现有磁盘Attach an existing disk

在许多情况下,In many cases. 你会附加已创建的磁盘。you attach disks that have already been created. 若要附加现有磁盘,请查找磁盘 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:

# find the disk id
diskId=$(az disk show -g myResourceGroup -n myDataDisk --query 'id' -o tsv)
az vm disk attach -g myResourceGroup --vm-name myVM --disk $diskId

输出类似于以下形式(可将 -o table 选项用于任何命令来设置输出格式):The output looks something like the following (you can use the -o table option to any command to format the output in):

{
  "accountType": "Standard_LRS",
  "creationData": {
    "createOption": "Empty",
    "imageReference": null,
    "sourceResourceId": null,
    "sourceUri": null,
    "storageAccountId": null
  },
  "diskSizeGb": 50,
  "encryptionSettings": null,
  "id": "/subscriptions/<guid>/resourceGroups/rasquill-script/providers/Microsoft.Compute/disks/myDataDisk",
  "location": "westus",
  "name": "myDataDisk",
  "osType": null,
  "ownerId": null,
  "provisioningState": "Succeeded",
  "resourceGroup": "myResourceGroup",
  "tags": null,
  "timeCreated": "2017-02-02T23:35:47.708082+00:00",
  "type": "Microsoft.Compute/disks"
}

使用非托管磁盘Use unmanaged disks

非托管磁盘需要额外的开销来创建和管理基础存储帐户。Unmanaged disks require additional overhead to create and manage the underlying storage accounts. 非托管磁盘是在与 OS 磁盘相同的存储帐户中创建的。Unmanaged disks are created in the same storage account as your OS disk. 若要创建并附加非托管磁盘,请使用 az vm unmanaged-disk attach 命令。To create and attach an unmanaged disk, use the az vm unmanaged-disk attach command. 以下示例将一个 50GB 的非托管磁盘附加到名为 myResourceGroup 的资源组中名为 myVM 的 VM:The following example attaches a 50GB unmanaged disk to the VM named myVM in the resource group named myResourceGroup:

az vm unmanaged-disk attach -g myResourceGroup -n myUnmanagedDisk --vm-name myVM \
  --new --size-gb 50

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

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

ssh azureuser@mypublicdns.westus.cloudapp.azure.com

连接到 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

此处,sdc 是我们需要的磁盘。Here, sdc is the disk that we want. 使用 fdisk 对磁盘进行分区,将其设置为分区 1 中的主磁盘,并接受其他默认值。Partition the disk with fdisk, 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

输出类似于以下示例:The output is 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 来创建分区,如下所示:Create the partition by typing p at the prompt as follows:

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.

现在,使用 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 -i 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 值并使用装入点 /datadriveIn this example, we 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 errors(由于 FSTAB 错误而无法通过 SSH 连接到 Linux VM)中所述的行为Without 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:

    UbuntuUbuntu

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

    RHEL/CentOSRHEL/CentOS

    sudo yum install util-linux
    sudo fstrim /datadrive
    

故障排除Troubleshooting

将数据磁盘添加到 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. 如果使用 azure vm disk attach-new 命令并指定 LUN (--lun) 来手动添加磁盘,而不是让 Azure 平台确定适当的 LUN,则请注意,LUN 0 已经有磁盘或者将有磁盘。If you are adding a disk manually using the azure 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 accessbile 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

  • 请记住,除非将该信息写入 fstab 文件,否则即使重新启动 VM,新磁盘也无法供 VM 使用。Remember, that your new disk is not available to the VM if it reboots unless you write that information to your fstab file.
  • 为确保正确配置 Linux VM,请查看有关优化 Linux 计算机性能的建议。To ensure your Linux VM is configured correctly, review the Optimize your Linux machine performance recommendations.
  • 可以添加更多的磁盘来扩展存储容量,配置 RAID 来提高性能。Expand your storage capacity by adding additional disks and configure RAID for additional performance.