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

本文介绍如何使用 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.

先决条件Prerequisites

入门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. 为此,可以使用 wc -c <yourFileName>.vhdls -al <yourFileName>.vhdTo get that, you can use either wc -c <yourFileName>.vhd or ls -al <yourFileName>.vhd. 指定 --upload-size-bytes 参数时将使用此值。This value is used when specifying the --upload-size-bytes parameter.

disk create cmdlet 中同时指定 --for-upload 参数和 --upload-size-bytes 参数,创建要上传的空标准 HDD:Create an empty standard HDD for uploading by specifying both the --for-upload parameter and the --upload-size-bytes parameter in a disk create cmdlet:

<yourdiskname><yourresourcegroupname><yourregion> 替换为所选值。Replace <yourdiskname>, <yourresourcegroupname>, <yourregion> with values of your choosing. --upload-size-bytes 参数包含示例值 34359738880,请将其替换为适合你的值。The --upload-size-bytes parameter contains an example value of 34359738880, replace it with a value appropriate for you.

提示

如果要创建 OS 磁盘,请将--hyper-v-generation 添加到 az disk createIf you are creating an OS disk, add --hyper-v-generation to az disk create.

az disk create -n <yourdiskname> -g <yourresourcegroupname> -l <yourregion> --for-upload --upload-size-bytes 34359738880 --sku standard_lrs

若要上传高级 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. 目前不支持超级磁盘。Ultra disks are not supported for now.

现在,你已创建了一个针对上传过程配置的空托管磁盘,可以将 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 command:

az disk grant-access -n <yourdiskname> -g <yourresourcegroupname> --access-level Write --duration-in-seconds 86400

示例返回值:Sample returned value:

{
  "accessSas": "https://md-impexp-t0rdsfgsdfg4.blob.core.chinacloudapi.cn/w2c3mj0ksfgl/abcd?sv=2017-04-17&sr=b&si=600a9281-d39e-4cc3-91d2-923c4a696537&sig=xXaT6mFgf139ycT87CADyFxb%2BnPXBElYirYRlbnJZbs%3D"
}

上传 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" "sas-URI" --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 use the following command to make the disk usable:

az disk revoke-access -n <yourdiskname> -g <yourresourcegroupname>

复制托管磁盘Copy a managed disk

直接上传还能简化复制托管磁盘的过程。Direct upload also simplifies the process of copying a managed disk. 可以在同一区域中进行复制,或者跨区域复制(复制到另一区域)。You can either copy within the same region or cross-region (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><yourTargetLocationHere>(例如,位置值为 chinanorth2)替换为自己的值,然后运行以下脚本来复制托管磁盘。Replace the <sourceResourceGroupHere>, <sourceDiskNameHere>, <targetDiskNameHere>, <targetResourceGroupHere>, 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 磁盘,请将--hyper-v-generation 添加到 az disk createIf you are creating an OS disk, add --hyper-v-generation to az disk create.

sourceDiskName = <sourceDiskNameHere>
sourceRG = <sourceResourceGroupHere>
targetDiskName = <targetDiskNameHere>
targetRG = <targetResourceGroupHere>
targetLocale = <yourTargetLocationHere>

sourceDiskSizeBytes= $(az disk show -g $sourceRG -n $sourceDiskName --query '[diskSizeBytes]' -o tsv)

az disk create -g $targetRG -n $targetDiskName -l $targetLocale --for-upload --upload-size-bytes $(($sourceDiskSizeBytes+512)) --sku standard_lrs

targetSASURI = $(az disk grant-access -n $targetDiskName -g $targetRG  --access-level Write --duration-in-seconds 86400 -o tsv)

sourceSASURI=$(az disk grant-access -n $sourceDiskName -g $sourceRG --duration-in-seconds 86400 --query [accessSas] -o tsv)

.\azcopy copy $sourceSASURI $targetSASURI --blob-type PageBlob

az disk revoke-access -n $sourceDiskName -g $sourceRG

az disk revoke-access -n $targetDiskName -g $targetRG

后续步骤Next steps

成功将 VHD 上传到托管磁盘后,可将该磁盘作为数据磁盘附加到现有的 VM,或者将该磁盘作为 OS 磁盘附加到 VM,以创建新的 VM。Now that you've successfully uploaded a VHD to a managed disk, you can attach the disk as a data disk to an existing VM or attach the disk to a VM as an OS disk, to create a new VM.