你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本文提供了两个用于从快照创建托管磁盘的Azure CLI脚本。 第一个脚本使用平台管理的密钥创建托管磁盘,第二个脚本使用客户管理的密钥创建托管磁盘。 从各自的快照创建 OS 和数据磁盘后,可以使用磁盘在现有 VM 上创建 VM 或还原数据。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
Prerequisites
在 Azure Cloud Shell 中使用 Bash 环境。 有关详细信息,请参阅 Azure Cloud Shell 入门。
如果要在本地运行 CLI 引用命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。
如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅 使用 Azure CLI 向 Azure 进行身份验证。
如果系统发出提示,则在首次使用时安装 Azure CLI 扩展。 有关扩展的详细信息,请参阅 使用和管理 Azure CLI 中的扩展。
运行 az version 以查找所安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade。
从快照创建托管磁盘
启动 Azure Cloud Shell
Azure Cloud Shell 是免费的交互式 shell,可以使用它运行本文中的步骤。 它预安装有常用 Azure 工具并将其配置与帐户一起使用。
若要打开 Cloud Shell,只需要从代码块的右上角选择“试一试”。 也可以通过转到 https://shell.azure.com 在单独的浏览器标签页中启动 Cloud Shell。
当 Cloud Shell 打开时,请验证是否为环境选择了“Bash”。 后续会话将在 Bash 环境中使用 Azure CLI,选择“复制”以复制代码块,将其粘贴到 Cloud Shell 中,然后按 Enter 来运行它。
登录 Azure
Cloud Shell 会在登录时使用的初始帐户下自动进行身份验证。 使用以下脚本通过其他订阅登录,将 subscriptionId 替换为你的 Azure 订阅 ID。
如果没有 Azure 帐户,请在开始前创建一个免费帐户。
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
具有平台管理的密钥的磁盘
使用此脚本创建包含平台管理的密钥的托管磁盘。 提供订阅、资源组、源快照、新磁盘名称、磁盘大小和存储类型。 对于高级 SSD v2 和超级磁盘,还需指定可用区。 该脚本设置订阅、获取快照资源 ID,并从同一位置的快照创建托管磁盘。
#Provide the subscription Id of the subscription where you want to create Managed Disks
subscriptionId="<subscriptionId>"
#Provide the name of your resource group
resourceGroupName=myResourceGroupName
#Provide the name of the snapshot that will be used to create Managed Disks
snapshotName=mySnapshotName
#Provide the name of the new Managed Disks that will be create
diskName=myDiskName
#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128
#Provide the storage type for Managed Disk. Acceptable values are Standard_LRS, Premium_LRS, PremiumV2_LRS, StandardSSD_LRS, UltraSSD_LRS, Premium_ZRS, and StandardSSD_ZRS.
storageType=Premium_LRS
#Required for Premium SSD v2 and Ultra Disks
#Provide the Availability Zone you'd like the disk to be created in, default is 1
zone=1
#Set the context to the subscription Id where Managed Disk will be created
az account set --subscription $subscriptionId
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)
#Create a new Managed Disks using the snapshot Id
#Note that managed disk will be created in the same location as the snapshot
#If you're creating a Premium SSD v2 or an Ultra Disk, add "--zone $zone" to the end of the command
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --size-gb $diskSize --source $snapshotId
具有客户管理的密钥的磁盘
使用此脚本创建包含客户管理的密钥的托管磁盘。 除了订阅、资源组、快照和新磁盘的相关值外,还请提供目标磁盘加密集及其所属的资源组。 该脚本获取快照和磁盘加密集信息,然后使用指定的磁盘加密集从该快照创建托管磁盘。
#Provide the subscription Id of the subscription where you want to create managed disks
subscriptionId="<subscriptionId>"
#Provide the name of your resource group
resourceGroupName=myResourceGroupName
#Provide the name of the snapshot that will be used to create managed disks
snapshotName=mySnapshotName
#Provide the name of the new managed disks that will be create
diskName=myDiskName
#Provide the name of the target disk encryption set
diskEncryptionSetName=myName
#Provide the target disk encryption set resource group
diskEncryptionResourceGroup=myGroup
#Required for Premium SSD v2 and Ultra Disks
#Provide the Availability Zone you'd like the disk to be created in, default is 1
zone=1
#Set the context to the subscription Id where managed disk will be created
az account set --subscription $subscriptionId
#Get the snapshot ID
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)
#Get the disk encryption set ID
diskEncryptionSetId=$(az disk-encryption-set show --name $diskEncryptionSetName --resource-group $diskEncryptionResourceGroup --query [id] -o tsv)
#Create a new managed disk using the snapshot ID
#The managed disk is created in the same location as the snapshot
#If you're creating a Premium SSD v2 or an Ultra Disk, add "--zone $zone" to the end of the command
az disk create -g $resourceGroupName -n $diskName --source $snapshotId --disk-encryption-set $diskEncryptionSetId
性能影响 - 后台复制过程
从快照创建托管磁盘时,会启动后台复制过程。 你可以在此过程运行时将磁盘附加到 VM,但此操作会影响性能(4k 磁盘会受到读取影响,512e 受到读写影响)且延迟较高、IOPS 和吞吐量较低,直至后台复制完成。 对于超级磁盘和高级 SSD v2,请使用以下命令检查 completionPercent 属性。 当值达到 100时,后台副本将完成。
重要说明
不能使用以下部分来获取除超级磁盘或高级 SSD v2 以外的磁盘类型的后台复制过程状态。 其他磁盘类型始终会报告 100%。
subscriptionId=yourSubscriptionID
resourceGroupName=yourResourceGroupName
diskName=yourDiskName
az account set --subscription $subscriptionId
az disk show -n $diskName -g $resourceGroupName --query [completionPercent] -o tsv
清理资源
运行以下命令以删除资源组及其包含的所有资源。
az group delete --name myResourceGroupName
Azure CLI命令参考
这些脚本使用以下Azure CLI命令创建托管磁盘、监视后台复制过程和清理资源。
| 命令 | 说明 |
|---|---|
| az account set | 设置在其中创建托管磁盘的订阅。 |
| az snapshot show | 获取源快照资源 ID。 |
| az disk-encryption-set show | 获取具有客户管理的密钥的磁盘的目标磁盘加密集信息。 |
| az disk create (创建磁盘) | 从源快照创建托管磁盘。 |
| az disk show | 获取高级 SSD v2 和超级磁盘的后台复制完成百分比。 |
| az group delete 删除 Azure 资源组 | 删除资源组及其资源。 |
后续步骤
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可以在 Azure Linux VM 文档中找到更多虚拟机和托管磁盘 CLI 脚本示例。