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.
Azure Managed Disks are high-performance, durable block storage designed for use with Azure Virtual Machines and Azure VMware Solution. They simplify disk management, offer greater scalability, enhance security, and eliminate the need to manage storage accounts directly. For more information, see Azure Managed Disks.
For operations on Managed Disks associated with an existing VM, use the azure-mgmt-compute
library.
The code examples in this article demonstrate common operations with Managed Disks using the azure-mgmt-compute
library. These examples are not meant to be run as standalone scripts, but rather to be integrated into your own code. To learn how to create a ComputeManagementClient
instance from azure.mgmt.compute
in your script, see Example - Create a virtual machine.
For more complete examples of how to use the azure-mgmt-compute
library, see Azure SDK for Python samples for compute in GitHub.
Standalone Managed Disks
The following examples show different ways to provision standalone Managed Disks.
Create an empty Managed Disk
This example shows how to create a new empty Managed Disk. You can use it as a blank disk to attach to a virtual machine or as a starting point for creating snapshots or images.
from azure.mgmt.compute.models import DiskCreateOption
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'disk_size_gb': 20,
'creation_data': {
'create_option': DiskCreateOption.empty
}
}
)
disk_resource = poller.result()
Create a Managed Disk from blob storage
This example shows how to create a Managed Disk from a VHD file stored in Azure Blob Storage. This is helpful when you want to reuse or move an existing virtual hard disk into Azure.
from azure.mgmt.compute.models import DiskCreateOption
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'creation_data': {
'create_option': DiskCreateOption.IMPORT,
'storage_account_id': '/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>',
'source_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd'
}
}
)
disk_resource = poller.result()
Create a Managed Disk image from blob storage
This example shows how to create a Managed Disk image from a VHD file stored in Azure Blob Storage. This is useful when you want to make a reusable image that can be used to create new virtual machines.
from azure.mgmt.compute.models import OperatingSystemStateTypes, HyperVGeneration
poller = compute_client.images.begin_create_or_update(
'my_resource_group',
'my_image_name',
{
'___location': 'eastus',
'storage_profile': {
'os_disk': {
'os_type': 'Linux',
'os_state': OperatingSystemStateTypes.GENERALIZED,
'blob_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd',
'caching': "ReadWrite",
},
},
'hyper_v_generation': HyperVGeneration.V2,
}
)
image_resource = poller.result()
Create a Managed Disk from your own image
This example shows how to create a new Managed Disk by copying an existing one. This is helpful when you want to make a backup or use the same disk setup on another virtual machine.
from azure.mgmt.compute.models import DiskCreateOption
# If you don't know the id, do a 'get' like this to obtain it
managed_disk = compute_client.disks.get(self.group_name, 'myImageDisk')
poller = compute_client.disks.begin_create_or_update(
'my_resource_group',
'my_disk_name',
{
'___location': 'eastus',
'creation_data': {
'create_option': DiskCreateOption.COPY,
'source_resource_id': managed_disk.id
}
}
)
disk_resource = poller.result()
Virtual machine with Managed Disks
You can create a virtual machine with an implicitly created Managed Disk based on a specific disk image, eliminating the need to manually define all disk details.
A Managed Disk is created implicitly when creating a VM from an OS image in Azure. Azure automatically handles the storage account, so you don't need to specify storage_profile.os_disk
or create a storage account manually.
storage_profile = azure.mgmt.compute.models.StorageProfile(
image_reference = azure.mgmt.compute.models.ImageReference(
publisher='Canonical',
offer='UbuntuServer',
sku='16.04-LTS',
version='latest'
)
)
For a complete example showing how to create a virtual machine using the Azure management libraries for Python, see Example - Create a virtual machine. This example demonstrates how to use the storage_profile
parameter.
You can also create a storage_profile
from your own image:
# If you don't know the id, do a 'get' like this to obtain it
image = compute_client.images.get(self.group_name, 'myImageDisk')
storage_profile = azure.mgmt.compute.models.StorageProfile(
image_reference = azure.mgmt.compute.models.ImageReference(
id = image.id
)
)
You can easily attach a previously provisioned Managed Disk:
vm = compute_client.virtual_machines.get(
'my_resource_group',
'my_vm'
)
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
vm.storage_profile.data_disks.append({
'lun': 12, # You choose the value, depending of what is available for you
'name': managed_disk.name,
'create_option': DiskCreateOptionTypes.attach,
'managed_disk': {
'id': managed_disk.id
}
})
async_update = compute_client.virtual_machines.begin_create_or_update(
'my_resource_group',
vm.name,
vm,
)
async_update.wait()
Virtual Machine Scale Sets with Managed Disks
Before Azure Managed Disks, you had to manually create a storage account for each VM in your Virtual Machine Scale Set and use the vhd_containers
parameter to specify those storage accounts in the Scale Set REST API.
With Azure Managed Disks, storage account management is no longer required. As a result, the storage_profile
for Virtual Machine Scale Sets used for Virtual Machine Scale Sets can now match the one used for individual VM creation:
'storage_profile': {
'image_reference': {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "16.04-LTS",
"version": "latest"
}
},
The full sample is as follows:
naming_infix = "PyTestInfix"
vmss_parameters = {
'___location': self.region,
"overprovision": True,
"upgrade_policy": {
"mode": "Manual"
},
'sku': {
'name': 'Standard_A1',
'tier': 'Standard',
'capacity': 5
},
'virtual_machine_profile': {
'storage_profile': {
'image_reference': {
"publisher": "Canonical",
"offer": "UbuntuServer",
"sku": "16.04-LTS",
"version": "latest"
}
},
'os_profile': {
'computer_name_prefix': naming_infix,
'admin_username': 'Foo12',
'admin_password': 'BaR@123!!!!',
},
'network_profile': {
'network_interface_configurations' : [{
'name': naming_infix + 'nic',
"primary": True,
'ip_configurations': [{
'name': naming_infix + 'ipconfig',
'subnet': {
'id': subnet.id
}
}]
}]
}
}
}
# Create VMSS test
result_create = compute_client.virtual_machine_scale_sets.begin_create_or_update(
'my_resource_group',
'my_scale_set',
vmss_parameters,
)
vmss_result = result_create.result()
Other operations with Managed Disks
Resizing a Managed Disk
This example shows how to make an existing Managed Disk larger. This is useful when you need more space for your data or applications.
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
managed_disk.disk_size_gb = 25
async_update = self.compute_client.disks.begin_create_or_update(
'my_resource_group',
'myDisk',
managed_disk
)
async_update.wait()
Update the storage account type of the Managed Disks
This example shows how to change the storage type of a Managed Disk and make it larger. This is helpful when you need more space or better performance for your data or applications.
from azure.mgmt.compute.models import StorageAccountTypes
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
managed_disk.account_type = StorageAccountTypes.STANDARD_LRS
async_update = self.compute_client.disks.begin_create_or_update(
'my_resource_group',
'myDisk',
managed_disk
)
async_update.wait()
Create an image from blob storage
This example shows how to create a Managed Disk image from a VHD file stored in Azure Blob Storage. This is useful when you want to make a reusable image that you can use to create new virtual machines.
async_create_image = compute_client.images.create_or_update(
'my_resource_group',
'myImage',
{
'___location': 'eastus',
'storage_profile': {
'os_disk': {
'os_type': 'Linux',
'os_state': "Generalized",
'blob_uri': 'https://<storage-account-name>.blob.core.windows.net/vm-images/test.vhd',
'caching': "ReadWrite",
}
}
}
)
image = async_create_image.result()
Create a snapshot of a Managed Disk that is currently attached to a virtual machine
This example shows how to take a snapshot of a Managed Disk that's attached to a virtual machine. You can use the snapshot to back up the disk or restore it later if needed.
managed_disk = compute_client.disks.get('my_resource_group', 'myDisk')
async_snapshot_creation = self.compute_client.snapshots.begin_create_or_update(
'my_resource_group',
'mySnapshot',
{
'___location': 'eastus',
'creation_data': {
'create_option': 'Copy',
'source_uri': managed_disk.id
}
}
)
snapshot = async_snapshot_creation.result()