แก้ไข

แชร์ผ่าน


Use Azure Managed Disks with the Azure libraries (SDK) for Python

Azure Managed Disks are high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution. Azure Managed Disks provide simplified disk management, enhanced scalability, improved security, and better scaling without having to work directly with storage accounts. For more information, see Azure Managed Disks.

You use the azure-mgmt-compute library to administer Managed Disks for an existing virtual machine.

For an example of how to create a virtual machine with the azure-mgmt-compute library, see Example - Create a virtual machine.

The code examples in this article demonstrate how to perform some common tasks with managed disks using the azure-mgmt-compute library. They aren't runnable as-is, but are designed for you to incorporate into your own code. You can consult Example - Create a virtual machine to learn how to create an instance of azure.mgmt.compute ComputeManagementClient in your code to run the examples.

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

You can create standalone Managed Disks in many ways as illustrated in the following sections.

Create an empty Managed Disk

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

The managed disk is created from a virtual hard disk (VHD) stored as a blob.

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

The managed disk image is created from a virtual hard disk (VHD) stored as a blob.

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

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 implicit Managed Disk for a specific disk image, which relieves you from specifying all the details.

A Managed Disk is created implicitly when creating a VM from an OS image in Azure. In the storage_profile parameter, the os_disk is optional and you don't have to create a storage account as required precondition to create a Virtual Machine.

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 on how to create a virtual machine using the Azure management libraries, for Python, see Example - Create a virtual machine. In the create example, you 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 Managed Disks, you needed to create a storage account manually for all the VMs you wanted inside your Scale Set, and then use the list parameter vhd_containers to provide all the storage account name to the Scale Set RestAPI.

Because you don't have to manage storage accounts with Azure Managed Disks, your storage_profile for Virtual Machine Scale Sets can now be exactly the same as the one used in 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

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

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

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

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()

See also