教程 - 使用 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 PowerShell


本文进行了更新,以便使用新的 Azure PowerShell Az 模块This article has been updated to use the new Azure PowerShell Az module. AzureRM 将继续获取关键的 bug 修复更新,但新功能将只出现在 Az 模块中。AzureRM will continue to get critical bugfix updates, but new features will be in the Az module only.

  • 若要在本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az module locally, see Install Azure PowerShell.
  • 如果在本地安装 Az 模块,可通过运行 Enable-AzureRmAlias 来启用 AzureRM 兼容性。If you install the Az module locally, you can enable the AzureRM compatibility by running Enable-AzureRmAlias.

默认 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. 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 vCPU,都可以附加四个数据磁盘。For each VM vCPU, four data disks can be attached.

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. 高级存储支持 DS 系列、DSv2 系列和 FS 系列 VM。Premium Storage supports DS-series, DSv2-series, and FS-series VMs. 高级磁盘分为五种类型(P10、P20、P30、P40、P50),磁盘大小决定了磁盘类型。Premium disks come in five types (P10, P20, P30, P40, P50), 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;如果大小在 129 GB 到 512 GB 之间,则磁盘类型为 P20。For example, if the size is below 128 GB the disk type is P10, or between 129 GB and 512 GB the disk is P20.

高级磁盘性能Premium disk performance

高级 SSD 大小Premium SSD sizes  P4P4 P6P6 P10P10 P15P15 P20P20 P30P30 P40P40 P50P50 P60P60 P70P70 P80P80
磁盘大小 (GiB)Disk size in GiB 3232 6464 128128 256256 512512 1,0241,024 2,0482,048 4,0954,095 8,1928,192 16,38416,384 32,76732,767
每个磁盘的 IOPSIOPS per disk 最多 120Up to 120 最多 240Up to 240 最大 500Up to 500 最多 1,100Up to 1,100 最多 2,300Up to 2,300 最多 5,000Up to 5,000 最多 7,500Up to 7,500 最多 7,500Up to 7,500 最多 16,000Up to 16,000 最多 18,000Up to 18,000 最多 20,000Up to 20,000
每个磁盘的吞吐量Throughput per disk 最多 25 MiB/秒Up to 25 MiB/sec 最多 50 MiB/秒Up to 50 MiB/sec 最多 100 MiB/秒Up to 100 MiB/sec 最多 125 MiB/秒Up to 125 MiB/sec 最多 150 MiB/秒Up to 150 MiB/sec 最多 200 MiB/秒Up to 200 MiB/sec 最多 250 MiB/秒Up to 250 MiB/sec 最多 250 MiB/秒Up to 250 MiB/sec 最多 500 MiB/秒Up to 500 MiB/sec 最多 750 MiB/秒Up to 750 MiB/sec 最多 900 MiB/秒Up to 900 MiB/sec

尽管上表确定了每个磁盘的最大 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.