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

使用 Azure CLI 管理 Azure 磁盘Manage Azure disks with the Azure CLI

Azure 虚拟机使用磁盘来存储 VM 操作系统、应用程序和数据。Azure virtual machines use disks to store the VMs operating system, applications, and data. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置。When creating a VM it is important to choose a disk size and configuration appropriate to the expected workload. 本教程介绍如何部署和管理 VM 磁盘。This tutorial covers deploying and managing VM disks. 学习内容:You learn about:

  • OS 磁盘和临时磁盘OS disks and temporary disks
  • 数据磁盘数Data disks
  • 标准磁盘和高级磁盘Standard and Premium disks
  • 磁盘性能Disk performance
  • 附加和准备数据磁盘Attaching and preparing data disks
  • 调整磁盘大小Resizing disks
  • 磁盘快照Disk snapshots

启动 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它预安装有常用 Azure 工具并将其配置与帐户一起使用。It has common Azure tools preinstalled and configured to use with your account. 请直接单击“复制”按钮对代码进行复制,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Just click the Copy button to copy the code, paste it into the Cloud Shell, and then press enter to run it. 可通过多种方式来启动 Cloud Shell:There are a few ways to launch the Cloud Shell:

单击代码块右上角的“试用”。Click Try It in the upper right corner of a code block. 本文中的 Cloud Shell
在浏览器中打开 Cloud Shell。Open Cloud Shell in your browser. https://shell.azure.com/bashhttps://shell.azure.com/bash
单击 Azure 门户右上角菜单上的“Cloud Shell”按钮。Click the Cloud Shell button on the menu in the upper right of the Azure portal. 门户中的 Cloud Shell

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.4 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.4 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLI 2.0If you need to install or upgrade, see Install Azure CLI 2.0.

默认 Azure 磁盘Default Azure disks

创建 Azure 虚拟机后,会自动向此虚拟机附加两个磁盘。When an Azure virtual machine is created, two disks are automatically attached to the virtual machine.

操作系统磁盘 - 操作系统磁盘大小可达 1 TB,并可托管 VM 操作系统。Operating system disk - Operating system disks can be sized up to 1 terabyte, and hosts the VMs operating system. 默认情况下,OS 磁盘标记为“/dev/sda”。The OS disk is labeled /dev/sda by default. 已针对 OS 性能优化了 OS 磁盘的磁盘缓存配置。The disk caching configuration of the OS disk is optimized for OS performance. 由于此配置,OS 磁盘不应托管应用程序或数据。Because of this configuration, the OS disk should not host applications or data. 对于应用程序和数据,请使用数据磁盘,本文后面会对其进行详细介绍。For applications and data, use data disks, which are detailed later in this article.

临时磁盘- 临时磁盘使用 VM 所在的 Azure 主机上的固态驱动器。Temporary disk - Temporary disks use a solid-state drive that is located on the same Azure host as the VM. 临时磁盘具有高性能,可用于临时数据处理等操作。Temp disks are highly performant and may be used for operations such as temporary data processing. 但是,如果将 VM 移动到新的主机,临时磁盘上存储的数据都会被删除。However, if the VM is moved to a new host, any data stored on a temporary disk is removed. 临时磁盘的大小由 VM 大小决定。The size of the temporary disk is determined by the VM size. 临时磁盘标记为“/dev/sdb”,且装载点为 /mnt。Temporary disks are labeled /dev/sdb and have a mountpoint of /mnt.

临时磁盘大小Temporary disk sizes

TypeType VM 大小VM Size 临时磁盘大小上限 (GB)Max temp disk size (GB)
常规用途General purpose A 和 D 系列A and D series 800800
计算优化Compute optimized F 系列F series 800800
内存优化Memory optimized D 和 G 系列D and G series 61446144
存储优化Storage optimized L 系列L series 56305630
GPUGPU N 系列N series 14401440
高性能High performance A 和 H 系列A and H series 20002000

Azure 数据磁盘Azure data disks

可添加额外的数据磁盘,用于安装应用程序和存储数据。Additional data disks can be added for installing applications and storing data. 在任何需要持久和灵敏数据存储的情况下,都应使用数据磁盘。Data disks should be used in any situation where durable and responsive data storage is desired. 每个数据磁盘的最大容量为 1 TB。Each data disk has a maximum capacity of 1 terabyte. 虚拟机的大小决定可附加到 VM 的数据磁盘数。The size of the virtual machine determines how many data disks can be attached to a VM. 对于每个 VM vCPU,都可以附加两个数据磁盘。For each VM vCPU, two data disks can be attached.

每个 VM 的最大数据磁盘数Max data disks per VM

TypeType VM 大小VM Size 每个 VM 的最大数据磁盘数Max data disks per VM
常规用途General purpose A 和 D 系列A and D series 3232
计算优化Compute optimized F 系列F series 3232
内存优化Memory optimized D 和 G 系列D and G series 6464
存储优化Storage optimized L 系列L series 6464
GPUGPU N 系列N series 4848
高性能High performance A 和 H 系列A and H series 3232

VM 磁盘类型VM disk types

Azure 提供两种类型的磁盘。Azure provides two types of disk.

标准磁盘Standard disk

标准存储受 HDD 支持,可以在确保性能的同时提供经济高效的存储。Standard Storage is backed by HDDs, and delivers cost-effective storage while still being performant. 标准磁盘适用于经济高效的开发和测试工作负荷。Standard disks are ideal for a cost effective dev and test workload.

高级磁盘Premium disk

高级磁盘由基于 SSD 的高性能、低延迟磁盘提供支持。Premium disks are backed by SSD-based high-performance, low-latency disk. 完美适用于运行生产工作负荷的 VM。Perfect for VMs running production workload. 高级存储支持 DS 系列、DSv2 系列、GS 系列和 FS 系列 VM。Premium Storage supports DS-series, DSv2-series, GS-series, and FS-series VMs. 高级磁盘分为 3 种类型(P10、P20 和 P30),磁盘大小决定磁盘类型。Premium disks come in three types (P10, P20, P30), the size of the disk determines the disk type. 选择时,磁盘大小值舍入为下一类型。When selecting, a disk size the value is rounded up to the next type. 例如,如果磁盘大小小于 128 GB,则磁盘类型为 P10。For example, if the disk size is less than 128 GB, the disk type is P10. 如果磁盘大小介于 129 GB 和 512 GB 之间,则大小为 P20。If the disk size is between 129 GB and 512 GB, the size is a P20. 如果超过 512 GB,则大小为 P30。Anything over 512 GB, the size is a P30.

高级磁盘性能Premium disk performance

高级存储磁盘类型Premium storage disk type P10P10 P20P20 P30P30
磁盘大小(向上舍入)Disk size (round up) 128 GB128 GB 512 GB512 GB 1,024 GB (1 TB)1,024 GB (1 TB)
每个磁盘的最大 IOPSMax IOPS per disk 500500 2,3002,300 5,0005,000
每个磁盘的吞吐量Throughput per disk 100 MB/秒100 MB/s 150 MB/秒150 MB/s 200 MB/秒200 MB/s

尽管上表确定了每个磁盘的最大 IOPS,但还可通过条带化多个数据磁盘实现更高级别的性能。While the above table identifies max IOPS per disk, a higher level of performance can be achieved by striping multiple data disks. 例如,Standard_GS5 VM 最多可实现 80,000 IOPS。For instance, a Standard_GS5 VM can achieve a maximum of 80,000 IOPS. 若要详细了解每个 VM 的最大 IOPS,请参阅 Linux VM 大小For detailed information on max IOPS per VM, see Linux VM sizes.

创建并附加磁盘Create and attach disks

可创建磁盘,并将其附加到新建 VM 或现有 VM。Data disks can be created and attached at VM creation time or to an existing VM.

在 VM 创建时附加磁盘Attach disk at VM creation

使用 az group create 命令创建资源组。Create a resource group with the az group create command.

az group create --name myResourceGroupDisk --location eastus

使用 az vm create 命令创建 VM。Create a VM using the az vm create command. --datadisk-sizes-gb 参数用于指定应创建并附加到虚拟机的附加磁盘。The --datadisk-sizes-gb argument is used to specify that an additional disk should be created and attached to the virtual machine. 若要创建并附加多个磁盘,请使用空格分隔的磁盘大小值列表。To create and attach more than one disk, use a space-delimited list of disk size values. 在以下示例中,创建的 VM 具有两个均为 128 GB 的数据磁盘。In the following example, a VM is created with two data disks, both 128 GB. 因为磁盘大小为 128 GB,所以这两个磁盘都配置为 P10,每个磁盘最多提供 500 IOPS。Because the disk sizes are 128 GB, these disks are both configured as P10s, which provide maximum 500 IOPS per disk.

az vm create \
  --resource-group myResourceGroupDisk \
  --name myVM \
  --image UbuntuLTS \
  --size Standard_DS2_v2 \
  --data-disk-sizes-gb 128 128 \
  --generate-ssh-keys

将磁盘附加到现有 VMAttach disk to existing VM

要创建新磁盘并将其附加到现有虚拟机,请使用 az vm disk attach 命令。To create and attach a new disk to an existing virtual machine, use the az vm disk attach command. 以下示例创建大小为 128 GB 的高级磁盘,并将其附加到上一步创建的 VM 中。The following example creates a premium disk, 128 gigabytes in size, and attaches it to the VM created in the last step.

az vm disk attach --vm-name myVM --resource-group myResourceGroupDisk --disk myDataDisk --size-gb 128 --sku Premium_LRS --new 

准备数据磁盘Prepare data disks

将磁盘附加到虚拟机后,需要将操作系统配置为使用该磁盘。Once a disk has been attached to the virtual machine, the operating system needs to be configured to use the disk. 以下示例演示如何手动配置磁盘。The following example shows how to manually configure a disk. 还可使用 cloud-init 自动执行此过程,后面的教程对此进行了介绍。This process can also be automated using cloud-init, which is covered in a later tutorial.

手动配置Manual configuration

创建与虚拟机的 SSH 连接。Create an SSH connection with the virtual machine. 将示例 IP 地址替换为虚拟机的公共 IP 地址。Replace the example IP address with the public IP of the virtual machine.

ssh 52.174.34.95

使用 fdisk 对磁盘进行分区。Partition the disk with fdisk.

(echo n; echo p; echo 1; echo ; echo ; echo w) | sudo fdisk /dev/sdc

使用 mkfs 命令将文件系统写入分区。Write a file system to the partition by using the mkfs command.

sudo mkfs -t ext4 /dev/sdc1

装载新磁盘使其在操作系统中可访问。Mount the new disk so that it is accessible in the operating system.

sudo mkdir /datadrive && sudo mount /dev/sdc1 /datadrive

现在可以通过 datadrive 装入点访问磁盘,可运行 df -h 命令对此进行验证。The disk can now be accessed through the datadrive mountpoint, which can be verified by running the df -h command.

df -h

输出显示新驱动器装载在 /datadrive 上。The output shows the new drive mounted on /datadrive.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        30G  1.4G   28G   5% /
/dev/sdb1       6.8G   16M  6.4G   1% /mnt
/dev/sdc1        50G   52M   47G   1% /datadrive

要确保在重启后重新装载驱动器,必须将其添加到 /etc/fstab 文件。To ensure that the drive is remounted after a reboot, it must be added to the /etc/fstab file. 为此,请使用 blkid 实用工具获取磁盘的 UUID。To do so, get the UUID of the disk with the blkid utility.

sudo -i blkid

输出显示驱动器的 UUID,在本例中为 /dev/sdc1The output displays the UUID of the drive, /dev/sdc1 in this case.

/dev/sdc1: UUID="33333333-3b3b-3c3c-3d3d-3e3e3e3e3e3e" TYPE="ext4"

在 /etc/fstab 文件中添加类似于以下内容的行。Add a line similar to the following to the /etc/fstab file.

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

配置磁盘后,请关闭 SSH 会话。Now that the disk has been configured, close the SSH session.

exit

调整 VM 磁盘大小Resize VM disk

部署 VM 后,可增加操作系统磁盘或任何附加数据磁盘的大小。Once a VM has been deployed, the operating system disk or any attached data disks can be increased in size. 需要更多存储空间或更高级别的性能(P10、P20、P30)时,增加磁盘大小很有用。Increasing the size of a disk is beneficial when needing more storage space or a higher level of performance (P10, P20, P30). 请注意,不能降低磁盘大小。Note, disks cannot be decreased in size.

增加磁盘大小之前,需要磁盘 ID 或名称。Before increasing disk size, the Id or name of the disk is needed. 使用 az disk list 命令返回资源组中的所有磁盘。Use the az disk list command to return all disks in a resource group. 记下要调整大小的磁盘名称。Take note of the disk name that you would like to resize.

az disk list -g myResourceGroupDisk --query '[*].{Name:name,Gb:diskSizeGb,Tier:accountType}' --output table

此外,必须解除分配 VM。The VM must also be deallocated. 使用 az vm deallocate 命令停止和解除分配 VM。Use the az vm deallocate command to stop and deallocate the VM.

az vm deallocate --resource-group myResourceGroupDisk --name myVM

使用 az disk update 命令调整磁盘大小。Use the az disk update command to resize the disk. 本示例将名为“myDataDisk”的磁盘的大小调整为 1 TB。This example resizes a disk named myDataDisk to 1 terabyte.

az disk update --name myDataDisk --resource-group myResourceGroupDisk --size-gb 1023

完成调整大小操作后,启动 VM。Once the resize operation has completed, start the VM.

az vm start --resource-group myResourceGroupDisk --name myVM

如果已调整操作系统磁盘的大小,则会自动扩展分区。If you’ve resized the operating system disk, the partition is automatically expanded. 如果已调整数据磁盘的大小,则需在 VM 操作系统中扩展任何当前分区。If you have resized a data disk, any current partitions need to be expanded in the VMs operating system.

拍摄 Azure 磁盘快照Snapshot Azure disks

拍摄磁盘快照可创建磁盘的只读时间点副本。Taking a disk snapshot creates a read only, point-in-time copy of the disk. Azure VM 快照可用于快速保存配置更改前 VM 所处的状态。Azure VM snapshots are useful for quickly saving the state of a VM before making configuration changes. 如果已证实不需要更改配置,可使用此快照还原 VM 状态。In the event the configuration changes prove to be undesired, VM state can be restored using the snapshot. VM 具有多个磁盘时,则拍摄的每个磁盘快照都与其他磁盘快照无关。When a VM has more than one disk, a snapshot is taken of each disk independently of the others. 为了获取应用程序一致的备份,请考虑在拍摄磁盘快照之前停止 VM。For taking application consistent backups, consider stopping the VM before taking disk snapshots. 或者,使用 Azure 备份服务,以便在 VM 运行时执行自动备份。Alternatively, use the Azure Backup service, which enables you to perform automated backups while the VM is running.

创建快照Create snapshot

创建虚拟机磁盘快照前,需要磁盘 ID 或名称。Before creating a virtual machine disk snapshot, the Id or name of the disk is needed. 使用 az vm show 命令返回磁盘 ID。在此示例中,磁盘 ID 存储在变量中,以便能够在稍后的步骤中使用。Use the az vm show command to return the disk id. In this example, the disk id is stored in a variable so that it can be used in a later step.

osdiskid=$(az vm show -g myResourceGroupDisk -n myVM --query "storageProfile.osDisk.managedDisk.id" -o tsv)

获取虚拟机磁盘 ID 后,使用以下命令可创建磁盘快照。Now that you have the id of the virtual machine disk, the following command creates a snapshot of the disk.

az snapshot create -g myResourceGroupDisk --source "$osdiskid" --name osDisk-backup

从快照创建磁盘Create disk from snapshot

然后,可将此快照转换为可用于重新创建虚拟机的磁盘。This snapshot can then be converted into a disk, which can be used to recreate the virtual machine.

az disk create --resource-group myResourceGroupDisk --name mySnapshotDisk --source osDisk-backup

从快照还原虚拟机Restore virtual machine from snapshot

若要演示如何还原虚拟机,请删除现有虚拟机。To demonstrate virtual machine recovery, delete the existing virtual machine.

az vm delete --resource-group myResourceGroupDisk --name myVM

从快照磁盘创建新虚拟机。Create a new virtual machine from the snapshot disk.

az vm create --resource-group myResourceGroupDisk --name myVM --attach-os-disk mySnapshotDisk --os-type linux

重新附加数据磁盘Reattach data disk

需要将所有数据磁盘重新附加到虚拟机。All data disks need to be reattached to the virtual machine.

先使用 az disk list 命令找到数据磁盘名称。First find the data disk name using the az disk list command. 此示例将磁盘名称放在名为“datadisk”的变量中,会在下一步中使用该变量。This example places the name of the disk in a variable named datadisk, which is used in the next step.

datadisk=$(az disk list -g myResourceGroupDisk --query "[?contains(name,'myVM')].[name]" -o tsv)

使用 az vm disk attach 命令附加磁盘。Use the az vm disk attach command to attach the disk.

az vm disk attach –g myResourceGroupDisk –-vm-name myVM –-disk $datadisk

后续步骤Next steps

本教程中介绍了以下 VM 磁盘主题:In this tutorial, you learned about VM disks topics such as:

  • OS 磁盘和临时磁盘OS disks and temporary disks
  • 数据磁盘数Data disks
  • 标准磁盘和高级磁盘Standard and Premium disks
  • 磁盘性能Disk performance
  • 附加和准备数据磁盘Attaching and preparing data disks
  • 调整磁盘大小Resizing disks
  • 磁盘快照Disk snapshots

转到下一教程,了解如何自动配置 VM。Advance to the next tutorial to learn about automating VM configuration.