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

Azure 虚拟机使用磁盘来存储 VM 操作系统、应用程序和数据。Azure virtual machines use disks to store the VMs operating system, applications, and data. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置。When creating a VM, it's 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

启动 Azure 本地 PowerShellLaunch Azure local PowerShell

打开 Azure Powershell 控制台,并以管理员权限运行下面列出的脚本。Open the Azure Powershell console and run the scripts listed below with Administrator priviledge.

默认 Azure 磁盘Default Azure disks

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

操作系统磁盘 - 操作系统磁盘大小最大可达 4 TB,并可托管 VM 操作系统。Operating system disk - Operating system disks can be sized up to 4 terabytes, and hosts the VMs operating system. 如果从 Azure 市场映像创建新的虚拟机 (VM),通常为 127 GB(但某些映像的 OS 磁盘更小)。If you create a new virtual machine (VM) from an Azure Marketplace image, the typically 127 GB (but some images have smaller OS disk sizes). OS 磁盘默认分配有一个 C: 驱动器号。The OS disk is assigned a drive letter of C: by default. 已针对 OS 性能优化了 OS 磁盘的磁盘缓存配置。The disk caching configuration of the OS disk is optimized for OS performance. OS 磁盘不得承载应用程序或数据。The OS disk should not host applications or data. 对于应用程序和数据,请使用数据磁盘,详情请参见本文稍后部分。For applications and data, use a data disk, which is 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. 临时磁盘默认分配有一个 D: 驱动器号。Temporary disks are assigned a drive letter of D: by default.

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 needed. 虚拟机的大小决定可附加到 VM 的数据磁盘数。The size of the virtual machine determines how many data disks can be attached to a VM.

VM 磁盘类型VM disk types

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

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

高级磁盘 - 由基于 SSD 的高性能、低延迟磁盘提供支持。Premium disks - backed by SSD-based, high-performance, low-latency disk. 完美适用于运行生产工作负荷的 VM。Perfect for VMs running production workload. 大小名称中带有“S”的 VM 大小通常支持高级存储。VM sizes with an S in the size name, typically support Premium Storage. 例如,DS 系列、DSv2 系列和 FS 系列 VM 都支持高级存储。For example, DS-series, DSv2-series, and FS-series VMs support premium storage. 选择磁盘大小时,大小值将向上舍入到下一类型。When you select a disk size, the value is rounded up to the next type. 例如,如果磁盘大小大于 64 GB,但小于 128 GB,则磁盘类型为 P10。For example, if the disk size is more than 64 GB, but less than 128 GB, the disk type is P10.

高级 SSD 大小Premium SSD sizes  P1P1 P2P2 P3P3 P4P4 P6P6 P10P10 P15P15 P20P20 P30P30 P40P40 P50P50 P60P60 P70P70 P80P80
磁盘大小 (GiB)Disk size in GiB 44 88 1616 3232 6464 128128 256256 512512 1,0241,024 2,0482,048 4,0964,096 8,1928,192 16,38416,384 32,76732,767
每个磁盘预配的 IOPSProvisioned IOPS per disk 120120 120120 120120 120120 240240 500500 1,1001,100 2,3002,300 5,0005,000 7,5007,500 7,5007,500 16,00016,000 18,00018,000 20,00020,000
每个磁盘预配的吞吐量Provisioned Throughput per disk 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 25 MB/秒25 MB/sec 50 MB/秒50 MB/sec 100 MB/秒100 MB/sec 125 MB/秒125 MB/sec 150 MB/秒150 MB/sec 200 MB/秒200 MB/sec 250 MB/秒250 MB/sec 250 MB/秒250 MB/sec 500 MB/秒500 MB/sec 750 MB/秒750 MB/sec 900 MB/秒900 MB/sec
每个磁盘最大的突发 IOPSMax burst IOPS per disk 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500 3,5003,500
每个磁盘最大的突发吞吐量Max burst throughput per disk 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec 170 MB/秒170 MB/sec
最大突发持续时间Max burst duration 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min 30 分钟30 min
符合预留条件Eligible for reservation No No No No No No No No 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year 是,最多一年Yes, up to one year

预配高级存储磁盘时,可以获得该磁盘的容量、IOPS 和吞吐量保证,这与标准存储不同。When you provision a premium storage disk, unlike standard storage, you are guaranteed the capacity, IOPS, and throughput of that disk. 例如,如果创建 P50 磁盘,Azure 将为此磁盘预配 4,095-GB 存储容量、7,500 IOPS 和 250-MB/秒的吞吐量。For example, if you create a P50 disk, Azure provisions 4,095-GB storage capacity, 7,500 IOPS, and 250-MB/s throughput for that disk. 应用程序可以使用全部或部分容量与性能。Your application can use all or part of the capacity and performance. 高级 SSD 磁盘的设计目的是在 99.9% 的时间内提供较低的个位数毫秒延迟以及上表所述的目标 IOPS 和吞吐量。Premium SSD disks are designed to provide low single-digit millisecond latencies and target IOPS and throughput described in the preceding table 99.9% of the time.

尽管上表确定了每个磁盘的最大 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 附加 64 个数据磁盘。For instance, 64 data disks can be attached to Standard_GS5 VM. 如果这些磁盘的大小都为 P30,则最大可实现 80,000 IOPS。If each of these disks is sized as a P30, a maximum of 80,000 IOPS can be achieved. 若要详细了解每个 VM 的最大 IOPS,请参阅 VM 类型和大小For detailed information on max IOPS per VM, see VM types and sizes.

创建并附加磁盘Create and attach disks

若要完成本教程中的示例,必须现有一个虚拟机。To complete the example in this tutorial, you must have an existing virtual machine. 需要时,使用以下命令创建虚拟机。If needed, create a virtual machine with the following commands.

使用 Get-Credential 设置虚拟机上管理员帐户所需的用户名和密码:Set the username and password needed for the administrator account on the virtual machine with Get-Credential:

使用 New-AzVM 创建虚拟机。Create the virtual machine with New-AzVM. 系统将提示你输入 VM 的管理员帐户的用户名和密码。You'll be prompted to enter a username and password for the administrators account for the VM.

New-AzVm `
    -ResourceGroupName "myResourceGroupDisk" `
    -Name "myVM" `
    -Location "China East" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" 

使用 New-AzDiskConfig 创建初始配置。Create the initial configuration with New-AzDiskConfig. 以下示例配置大小为 128 GB 的磁盘。The following example configures a disk that is 128 gigabytes in size.

$diskConfig = New-AzDiskConfig `
    -Location "ChinaEast" `
    -CreateOption Empty `
    -DiskSizeGB 128

使用 New-AzDisk 命令创建数据磁盘。Create the data disk with the New-AzDisk command.

$dataDisk = New-AzDisk `
    -ResourceGroupName "myResourceGroupDisk" `
    -DiskName "myDataDisk" `
    -Disk $diskConfig

使用 Get-AzVM 命令获取要向其添加数据磁盘的虚拟机。Get the virtual machine that you want to add the data disk to with the Get-AzVM command.

$vm = Get-AzVM -ResourceGroupName "myResourceGroupDisk" -Name "myVM"

使用 Add-AzVMDataDisk 命令向虚拟机配置添加数据磁盘。Add the data disk to the virtual machine configuration with the Add-AzVMDataDisk command.

$vm = Add-AzVMDataDisk `
    -VM $vm `
    -Name "myDataDisk" `
    -CreateOption Attach `
    -ManagedDiskId $dataDisk.Id `
    -Lun 1

使用 Update-AzVM 命令更新虚拟机。Update the virtual machine with the Update-AzVM command.

Update-AzVM -ResourceGroupName "myResourceGroupDisk" -VM $vm

准备数据磁盘Prepare data disks

将磁盘附加到虚拟机后,需要将操作系统配置为使用该磁盘。Once a disk has been attached to the virtual machine, the operating system needs to be configured to use the disk. 以下示例演示如何手动配置添加到 VM 的第一个磁盘。The following example shows how to manually configure the first disk added to the VM. 还可使用自定义脚本扩展自动执行此过程。This process can also be automated using the custom script extension.

手动配置Manual configuration

创建与虚拟机的 RDP 连接。Create an RDP connection with the virtual machine. 打开 PowerShell 并运行此脚本。Open up PowerShell and run this script.

Get-Disk | Where partitionstyle -eq 'raw' |
    Initialize-Disk -PartitionStyle MBR -PassThru |
    New-Partition -AssignDriveLetter -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "myDataDisk" -Confirm:$false

验证数据磁盘Verify the data disk

若要验证是否已附加数据磁盘,请查看附加 DataDisksStorageProfileTo verify that the data disk is attached, view the StorageProfile for the attached DataDisks.


输出应该类似于以下示例:The output should look something like this example:

Name            : myDataDisk
DiskSizeGB      : 128
Lun             : 1
Caching         : None
CreateOption    : Attach
SourceImage     :
VirtualHardDisk :

后续步骤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

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