将 VHD 上传到 Azure,或将托管磁盘复制到其他区域 - Azure PowerShellUpload a VHD to Azure or copy a managed disk to another region - Azure PowerShell

本文介绍如何使用 AzCopy 将 VHD 从本地计算机上传到 Azure 托管磁盘,或将托管磁盘复制到其他区域。This article explains how to either upload a VHD from your local machine to an Azure managed disk or copy a managed disk to another region, using AzCopy. 此过程(直接上传)还允许你将最大大小为 32 TiB 的 VHD 直接上传到托管磁盘。This process, direct upload, also enables you to upload a VHD up to 32 TiB in size directly into a managed disk. 目前,标准 HDD、标准 SSD 和高级 SSD 托管磁盘支持直接上传。Currently, direct upload is supported for standard HDD, standard SSD, and premium SSD managed disks. 超级磁盘尚不支持此功能。It isn't supported for ultra disks, yet.

若要为 Azure 中的 IaaS VM 提供备份解决方案,建议使用直接上传方法将客户备份还原到托管磁盘。If you're providing a backup solution for IaaS VMs in Azure, we recommend using direct upload to restore customer backups to managed disks. 从 Azure 外部的源上传 VHD 时,速度取决于本地带宽。When uploading a VHD from a source external to Azure, speeds would depend on your local bandwidth. 从 Azure VM 上传或复制时,带宽将与标准 HDD 相同。When uploading or copying from an Azure VM, then your bandwidth would be the same as standard HDDs.


入门Getting started

如果希望通过 GUI 上传磁盘,可以使用 Azure 存储资源管理器。If you'd prefer to upload disks through a GUI, you can do so using Azure Storage Explorer. 有关详细信息,请参阅:使用 Azure 存储资源管理器管理 Azure 托管磁盘For details refer to: Use Azure Storage Explorer to manage Azure managed disks

若要将 VHD 上传到 Azure,需要创建一个针对此上传过程配置的空托管磁盘。To upload your VHD to Azure, you'll need to create an empty managed disk that is configured for this upload process. 在创建托管磁盘之前,应了解有关这些磁盘的一些附加信息。Before you create one, there's some additional information you should know about these disks.

这种托管磁盘有两种独特的状态:This kind of managed disk has two unique states:

  • ReadToUpload,表示磁盘已做好上传准备,但尚未生成安全访问签名 (SAS)。ReadToUpload, which means the disk is ready to receive an upload but, no secure access signature (SAS) has been generated.
  • ActiveUpload,表示磁盘已做好上传准备,并且已生成 SAS。ActiveUpload, which means that the disk is ready to receive an upload and the SAS has been generated.


在任一状态下,无论实际磁盘类型是什么,都会按标准 HDD 定价对托管磁盘计费。While in either of these states, the managed disk will be billed at standard HDD pricing, regardless of the actual type of disk. 例如,P10 将按 S10 计费。For example, a P10 will be billed as an S10. 在对托管磁盘调用 revoke-access 之前(将磁盘附加到 VM 需要执行此调用),都是如此。This will be true until revoke-access is called on the managed disk, which is required in order to attach the disk to a VM.

创建空托管磁盘Create an empty managed disk

在创建要上传的空标准 HDD 之前,需要获取要上传的 VHD 的文件大小(以字节为单位)。Before you can create an empty standard HDD for uploading, you'll need the file size of the VHD you want to upload, in bytes. 可以使用示例代码来这样做,但若要自己操作,可以使用 $vhdSizeBytes = (Get-Item "<fullFilePathHere>").lengthThe example code will get that for you but, to do it yourself you can use: $vhdSizeBytes = (Get-Item "<fullFilePathHere>").length. 指定 -UploadSizeInBytes 参数时将使用此值。This value is used when specifying the -UploadSizeInBytes parameter.

现在,请在本地 shell 上创建一个要上传的空的标准 HDD,方法是:在 -CreateOption 参数中指定 Upload 设置,并在 New-AzDiskConfig cmdlet 中指定 -UploadSizeInBytes 参数。Now, on your local shell, create an empty standard HDD for uploading by specifying the Upload setting in the -CreateOption parameter as well as the -UploadSizeInBytes parameter in the New-AzDiskConfig cmdlet. 然后调用 New-AzDisk 来创建磁盘。Then call New-AzDisk to create the disk.

替换 <yourdiskname><yourresourcegroupname><yourregion>,然后运行以下命令:Replace <yourdiskname>, <yourresourcegroupname>, and <yourregion> then run the following commands:


如果要创建 OS 磁盘,请将 -HyperVGeneration 添加到 New-AzDiskConfigIf you are creating an OS disk, add -HyperVGeneration '' to New-AzDiskConfig.

$vhdSizeBytes = (Get-Item "<fullFilePathHere>").length

$diskconfig = New-AzDiskConfig -SkuName 'Standard_LRS' -OsType 'Windows' -UploadSizeInBytes $vhdSizeBytes -Location '<yourregion>' -CreateOption 'Upload'

New-AzDisk -ResourceGroupName '<yourresourcegroupname' -DiskName '<yourdiskname>' -Disk $diskconfig

若要上传高级 SSD 或标准 SSD,请将 Standard_LRS 替换为 Premium_LRSStandardSSD_LRSIf you would like to upload either a premium SSD or a standard SSD, replace Standard_LRS with either Premium_LRS or StandardSSD_LRS.

现在,你已创建了一个针对上传过程配置的空托管磁盘,可以将 VHD 上传到其中了。Now that you've created an empty managed disk that is configured for the upload process, you can upload a VHD to it. 若要将 VHD 上传到磁盘,需要一个可写的 SAS,以便将此磁盘作为上传目标引用。To upload a VHD to the disk, you'll need a writeable SAS, so that you can reference it as the destination for your upload.

若要生成空托管磁盘的可写 SAS,请替换 <yourdiskname><yourresourcegroupname>,然后使用以下命令:To generate a writable SAS of your empty managed disk, replace <yourdiskname>and <yourresourcegroupname>, then use the following commands:

$diskSas = Grant-AzDiskAccess -ResourceGroupName '<yourresourcegroupname>' -DiskName '<yourdiskname>' -DurationInSecond 86400 -Access 'Write'

$disk = Get-AzDisk -ResourceGroupName '<yourresourcegroupname>' -DiskName '<yourdiskname>'

上传 VHDUpload a VHD

生成空托管磁盘的 SAS 后,可以使用该 SAS 将托管磁盘设置为上传命令的目标。Now that you have a SAS for your empty managed disk, you can use it to set your managed disk as the destination for your upload command.

使用 AzCopy v10 并指定生成的 SAS URI,将本地 VHD 文件上传到托管磁盘。Use AzCopy v10 to upload your local VHD file to a managed disk by specifying the SAS URI you generated.

此上传过程的吞吐量与相应标准 HDD 的吞吐量相同。This upload has the same throughput as the equivalent standard HDD. 例如,如果大小相当于 S4,则最高吞吐量为 60 MiB/秒。For example, if you have a size that equates to S4, you will have a throughput of up to 60 MiB/s. 但是,如果大小相当于 S70,则最高吞吐量为 500 MiB/秒。But, if you have a size that equates to S70, you will have a throughput of up to 500 MiB/s.

AzCopy.exe copy "c:\somewhere\mydisk.vhd" $diskSas.AccessSAS --blob-type PageBlob

上传完成后,如果你不再需要将更多数据写入磁盘,请吊销 SAS。After the upload is complete, and you no longer need to write any more data to the disk, revoke the SAS. 吊销 SAS 会更改托管磁盘的状态,使你可以将磁盘附加到 VM。Revoking the SAS will change the state of the managed disk and allow you to attach the disk to a VM.

替换 <yourdiskname><yourresourcegroupname>,然后运行以下命令:Replace <yourdiskname>and <yourresourcegroupname>, then run the following command:

Revoke-AzDiskAccess -ResourceGroupName '<yourresourcegroupname>' -DiskName '<yourdiskname>'

复制托管磁盘Copy a managed disk

直接上传还能简化复制托管磁盘的过程。Direct upload also simplifies the process of copying a managed disk. 可以在同一区域中进行复制,或者将托管磁盘复制到其他区域。You can either copy within the same region or copy your managed disk to another region.

以下脚本可自动完成此操作,此过程类似于前面所述的步骤,但由于处理的是现有磁盘,因此存在一些差异。The follow script will do this for you, the process is similar to the steps described earlier, with some differences, since you're working with an existing disk.


提供 Azure 中托管磁盘的磁盘大小(以字节为单位)时,需要添加 512 偏移量。You need to add an offset of 512 when you're providing the disk size in bytes of a managed disk from Azure. 这是因为,Azure 在返回磁盘大小时会省略脚注。This is because Azure omits the footer when returning the disk size. 如果不添加此偏移量,复制将会失败。The copy will fail if you do not do this. 以下脚本中已添加此偏移量。The following script already does this for you.

请将 <sourceResourceGroupHere><sourceDiskNameHere><targetDiskNameHere><targetResourceGroupHere><yourOSTypeHere><yourTargetLocationHere>(例如,位置值为 chinanorth2)替换为自己的值,然后运行以下脚本来复制托管磁盘。Replace the <sourceResourceGroupHere>, <sourceDiskNameHere>, <targetDiskNameHere>, <targetResourceGroupHere>, <yourOSTypeHere> and <yourTargetLocationHere> (an example of a location value would be chinanorth2) with your values, then run the following script in order to copy a managed disk.


如果要创建 OS 磁盘,请将 -HyperVGeneration 添加到 New-AzDiskConfigIf you are creating an OS disk, add -HyperVGeneration '' to New-AzDiskConfig.

$sourceRG = <sourceResourceGroupHere>
$sourceDiskName = <sourceDiskNameHere>
$targetDiskName = <targetDiskNameHere>
$targetRG = <targetResourceGroupHere>
$targetLocate = <yourTargetLocationHere>
#Expected value for OS is either "Windows" or "Linux"
$targetOS = <yourOSTypeHere>

$sourceDisk = Get-AzDisk -ResourceGroupName $sourceRG -DiskName $sourceDiskName

# Adding the sizeInBytes with the 512 offset, and the -Upload flag
$targetDiskconfig = New-AzDiskConfig -SkuName 'Standard_LRS' -osType $targetOS -UploadSizeInBytes $($sourceDisk.DiskSizeBytes+512) -Location $targetLocate -CreateOption 'Upload'

$targetDisk = New-AzDisk -ResourceGroupName $targetRG -DiskName $targetDiskName -Disk $targetDiskconfig

$sourceDiskSas = Grant-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName -DurationInSecond 86400 -Access 'Read'

$targetDiskSas = Grant-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName -DurationInSecond 86400 -Access 'Write'

azcopy copy $sourceDiskSas.AccessSAS $targetDiskSas.AccessSAS --blob-type PageBlob

Revoke-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName

Revoke-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName 

后续步骤Next steps

成功将 VHD 上传到托管磁盘后,即可将磁盘附加到 VM 并开始使用它。Now that you've successfully uploaded a VHD to a managed disk, you can attach your disk to a VM and begin using it.

若要了解如何将数据磁盘附加到 VM,请参阅有关此主题的文章:使用 PowerShell 将数据磁盘附加到 Windows VMTo learn how to attach a data disk to a VM, see our article on the subject: Attach a data disk to a Windows VM with PowerShell. 若要将磁盘用作 OS 磁盘,请参阅从专用磁盘创建 Windows VMTo use the disk as the OS disk, see Create a Windows VM from a specialized disk.