Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article provides two Azure CLI scripts for creating a managed disk from a snapshot. The first script creates a managed disk with platform-managed keys, and the second script creates a managed disk with customer-managed keys. After you create OS and data disks from their respective snapshots, you can use the disks to create a VM or restore data on an existing VM.
If you don't have an Azure account, create a free account before you begin.
Prerequisites
Use the Bash environment in Azure Cloud Shell. For more information, see Get started with Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Authenticate to Azure using Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use and manage extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Create managed disks from snapshots
Launch Azure Cloud Shell
The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.
To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.
When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
Sign in to Azure
Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing subscriptionId with your Azure subscription ID.
If you don't have an Azure account, create a free account before you begin.
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
For more information, see set active subscription or log in interactively.
Disks with platform-managed keys
Use this script to create a managed disk with platform-managed keys. Provide the subscription, resource group, source snapshot, new disk name, disk size, and storage type. For Premium SSD v2 and Ultra Disk, also provide an availability zone. The script sets the subscription, gets the snapshot resource ID, and creates the managed disk from the snapshot in the same location.
#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
Disks with customer-managed keys
Use this script to create a managed disk with customer-managed keys. In addition to the subscription, resource group, snapshot, and new disk values, provide the target disk encryption set and its resource group. The script gets the snapshot and disk encryption set information, and then creates the managed disk from the snapshot with the specified disk encryption set.
#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
Performance impact - background copy process
When you create a managed disk from a snapshot, it starts a background copy process. You can attach a disk to a VM while this process is running but you'll experience performance impact (4k disks experience read impact, 512e experience both read and write impact) with higher latency, lower IOPS and throughput until background copy completes. For Ultra Disks and Premium SSD v2, use the following command to check the completionPercent property. The background copy is complete when the value reaches 100.
Important
You can't use the following sections to get the status of the background copy process for disk types other than Ultra Disk or Premium SSD v2. Other disk types will always report 100%.
subscriptionId=yourSubscriptionID
resourceGroupName=yourResourceGroupName
diskName=yourDiskName
az account set --subscription $subscriptionId
az disk show -n $diskName -g $resourceGroupName --query [completionPercent] -o tsv
Clean up resources
Run the following command to delete the resource group and all resources it contains.
az group delete --name myResourceGroupName
Azure CLI command reference
The scripts use the following Azure CLI commands to create managed disks, monitor the background copy process, and clean up resources.
| Command | Notes |
|---|---|
| az account set | Sets the subscription where the managed disk is created. |
| az snapshot show | Gets the source snapshot resource ID. |
| az disk-encryption-set show | Gets the target disk encryption set information for a disk with customer-managed keys. |
| az disk create | Creates a managed disk from the source snapshot. |
| az disk show | Gets the background copy completion percentage for Premium SSD v2 and Ultra Disk. |
| az group delete | Deletes the resource group and its resources. |
Next steps
Create a virtual machine by attaching a managed disk as OS disk
For more information on the Azure CLI, see Azure CLI documentation.
More virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.