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

教程 - 使用 Azure CLI 管理 Azure 磁盘Tutorial - Manage Azure disks with the Azure CLI

Azure 虚拟机 (VM) 使用磁盘来存储操作系统、应用程序和数据。Azure virtual machines (VMs) use disks to store the operating system, applications, and data. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置。When you create VM, it is important to choose a disk size and configuration appropriate to the expected workload. 本教程演示如何部署和管理 VM 磁盘。This tutorial shows you how to deploy and manage 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 磁盘Default Azure disks

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

操作系统磁盘 - 操作系统磁盘大小可达 2 TB,并可托管 VM 操作系统。Operating system disk - Operating system disks can be sized up to 2 TB, 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 be used for applications or data. 对于应用程序和数据,请使用数据磁盘,本教程后面会对其进行详细介绍。For applications and data, use data disks, which are detailed later in this tutorial.

临时磁盘- 临时磁盘使用 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.

Azure 数据磁盘Azure data disks

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

VM 磁盘类型VM disk types

Azure 提供两种类型的磁盘:标准磁盘和高级磁盘。Azure provides two types of disks, standard and Premium.

标准磁盘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. 选择磁盘大小时,大小值将舍入为下一类型。When you select 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。Over, 512 GB, the size is a P30.

高级磁盘性能Premium disk performance

高级存储磁盘类型Premium storage disk type P4P4 P6P6 P10P10 P20P20 P30P30 P40P40 P50P50 p60p60
磁盘大小(向上舍入)Disk size (round up) 32 GiB32 GiB 64 GiB64 GiB 128 GiB128 GiB 512 GiB512 GiB 1,024 GiB (1 TiB)1,024 GiB (1 TiB) 2,048 GiB (2 TiB)2,048 GiB (2 TiB) 4,095 GiB (4 TiB)4,095 GiB (4 TiB) 8,192 GiB (8 TiB)8,192 GiB (8 TiB)
每个磁盘的最大 IOPSMax IOPS per disk 120120 240240 500500 2,3002,300 5,0005,000 7,5007,500 7,5007,500 12,50012,500
每个磁盘的吞吐量Throughput per disk 25 MB/秒25 MB/s 50 MB/秒50 MB/s 100 MB/秒100 MB/s 150 MB/秒150 MB/s 200 MB/秒200 MB/s 250 MB/秒250 MB/s 250 MB/秒250 MB/s 480 MB/秒480 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.

启动 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,只需要从代码块的右上角选择“试一试”。To open the Cloud Shell, just select Try it from the upper right corner of a code block. 也可以通过转到 https://shell.azure.com/powershell 在单独的浏览器标签页中启动 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/powershell. 选择“复制”以复制代码块,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.

创建并附加磁盘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. 下面的示例创建名为 myVM 的 VM,添加名为 azureuser 的用户帐户,并生成 SSH 密钥(如果这些密钥不存在)。The following example creates a VM named myVM, adds a user account named azureuser, and generates SSH keys if they do not exist. --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 \
  --generate-ssh-keys \
  --data-disk-sizes-gb 128 128

将磁盘附加到现有 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 \
    --resource-group myResourceGroupDisk \
    --vm-name myVM \
    --disk myDataDisk \
    --size-gb 128 \
    --sku Premium_LRS \

准备数据磁盘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.

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


使用 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.


拍摄磁盘快照Snapshot a disk

创建磁盘快照时,Azure 会创建磁盘的只读时间点副本。When you take a disk snapshot, Azure creates a read only, point-in-time copy of the disk. Azure VM 快照可用于快速保存配置更改前 VM 所处的状态。Azure VM snapshots are useful to quickly save the state of a VM before you make configuration changes. 如果出现问题或错误,则可使用快照还原 VM。In the event of an issue or error, VM can be restored using a snapshot. VM 具有多个磁盘时,将分别对每个磁盘进行快照。When a VM has more than one disk, a snapshot is taken of each disk independently of the others. 若要执行应用程序一致性备份,请考虑在创建磁盘快照之前停止 VM。To take application consistent backups, consider stopping the VM before you take 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 you create a virtual machine disk snapshot, the ID or name of the disk is needed. 使用 az vm show 命令返回磁盘 ID。Use the az vm show command to return the disk ID. 在此示例中,磁盘 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 \
    --resource-group 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')].[id]" \
   -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.