This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging 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, seeAzure Managed Disks.
For operations on Managed Disks associated with an existing VM, use theazure-mgmt-compute library.
The code examples in this article demonstrate common operations with Managed Disks using theazure-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 aComputeManagementClient instance fromazure.mgmt.compute in your script, seeExample - Create a virtual machine.
For more complete examples of how to use theazure-mgmt-compute library, seeAzure SDK for Python samples for compute in GitHub.
The following examples show different ways to provision standalone Managed Disks.
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 DiskCreateOptionpoller = 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()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 DiskCreateOptionpoller = 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()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, HyperVGenerationpoller = 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()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 itmanaged_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()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 specifystorage_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, seeExample - Create a virtual machine. This example demonstrates how to use thestorage_profile parameter.
You can also create astorage_profile from your own image:
# If you don't know the id, do a 'get' like this to obtain itimage = 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()Before Azure Managed Disks, you had to manually create a storage account for each VM in your Virtual Machine Scale Set and use thevhd_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, thestorage_profile forVirtual 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 testresult_create = compute_client.virtual_machine_scale_sets.begin_create_or_update( 'my_resource_group', 'my_scale_set', vmss_parameters,)vmss_result = result_create.result()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 = 25async_update = self.compute_client.disks.begin_create_or_update( 'my_resource_group', 'myDisk', managed_disk)async_update.wait()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 StorageAccountTypesmanaged_disk = compute_client.disks.get('my_resource_group', 'myDisk')managed_disk.account_type = StorageAccountTypes.STANDARD_LRSasync_update = self.compute_client.disks.begin_create_or_update( 'my_resource_group', 'myDisk', managed_disk)async_update.wait()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()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()Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?