使用 CLI 基于快照创建托管磁盘 (Linux)

本文提供了两个用于从快照创建托管磁盘的Azure CLI脚本。 第一个脚本使用平台管理的密钥创建托管磁盘,第二个脚本使用客户管理的密钥创建托管磁盘。 从各自的快照创建 OS 和数据磁盘后,可以使用磁盘在现有 VM 上创建 VM 或还原数据。

如果您没有 Azure 试用订阅,请在开始之前创建 试用订阅

先决条件

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用的是本地安装,请使用 az login 命令登录 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

从快照创建托管磁盘

登录 Azure

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有Azure帐户,请在开始前创建一个试用帐户

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

有关详细信息,请参阅设置有效的订阅登录

具有平台管理的密钥的磁盘

使用此脚本创建包含平台管理的密钥的托管磁盘。 提供订阅、资源组、源快照、新磁盘名称、磁盘大小和存储类型。 对于高级 SSD v2 和超级磁盘,还需指定可用区。 该脚本设置订阅、获取快照资源 ID,并从同一位置的快照创建托管磁盘。

# Verified per Raman Kumar as of 2/23/2022

# <FullScript>
#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
# </FullScript>

具有客户管理的密钥的磁盘

使用此脚本创建包含客户管理的密钥的托管磁盘。 除了订阅、资源组、快照和新磁盘的相关值外,还请提供目标磁盘加密集及其所属的资源组。 该脚本获取快照和磁盘加密集信息,然后使用指定的磁盘加密集从该快照创建托管磁盘。

#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(显示磁盘信息) 获取 Premium SSD v2 和超级磁盘的后台复制完成百分比。
az group delete 删除 Azure 资源组 删除资源组及其资源。

后续步骤

通过将托管磁盘附加为 OS 磁盘来创建虚拟机

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可以在 Azure Linux VM 文档中找到更多虚拟机和托管磁盘 CLI 脚本示例。