使用 CLI 基于快照创建托管磁盘 (Linux)Create a managed disk from a snapshot with CLI (Linux)

此脚本从快照创建托管磁盘。This script creates a managed disk from a snapshot. 可以使用它基于 OS 和数据磁盘的快照还原虚拟机。Use it to restore a virtual machine from snapshots of OS and data disks. 基于相应的快照创建 OS 和数据托管磁盘,然后通过附加托管磁盘创建新的虚拟机。Create OS and data managed disks from respective snapshots and then create a new virtual machine by attaching managed disks. 还可以通过附加基于快照创建的数据磁盘来还原现有 VM 的数据磁盘。You can also restore data disks of an existing VM by attaching data disks created from snapshots.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

#Provide the subscription Id of the subscription where you want to create Managed Disks
subscriptionId=dd80b94e-0463-4a65-8d04-c94f403879dc

#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. Premium_LRS or Standard_LRS.
storageType=Premium_LRS

#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
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --size-gb $diskSize --source $snapshotId

脚本说明Script explanation

此脚本使用以下命令基于快照创建托管磁盘。This script uses following commands to create a managed disk from a snapshot. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az snapshot showaz snapshot show 使用快照的名称和资源组属性获取该快照的所有属性。Gets all the properties of a snapshot using the name and resource group properties of the snapshot. 使用 ID 属性创建托管磁盘。Id property is used to create managed disk.
az disk createaz disk create 使用托管快照的快照 ID 创建托管磁盘Creates a managed disk using snapshot Id of a managed snapshot

后续步骤Next steps

通过将托管磁盘附加为 OS 磁盘来创建虚拟机Create a virtual machine by attaching a managed disk as OS disk

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.