0
votes

Does anyone have experience with the Python SDK (v: v2.0.0rc6) and cloning/duplicating running VM's into another resource group?

getting the OS disk to start. Will need the data disk as well

managed_disk = self.compute_client.disks.get(resource_group_name=source_rg_name, disk_name=vm.storage_profile.os_disk.name)

make a snapshot of the os disk.

self.compute_client.snapshots.create_or_update(
    self.config.resource_group_name,
    'SNAPSHOT-' + virtual_machine,
    {
        'location': managed_disk.location,
        'creation_data': {
            'create_option': 'Copy',
            'source_uri': managed_disk.id
        }
    }
)

create the VM. Throws exception below.

result = self.compute_client.virtual_machines.create_or_update(
    self.config.cybric_resource_group_name,
    virtual_machine,
    azure.mgmt.compute.models.VirtualMachine(
        location=vm.location,
        os_profile=vm.os_profile,
        hardware_profile=vm.hardware_profile,
        network_profile=azure.mgmt.compute.models.NetworkProfile(
            network_interfaces=[
                azure.mgmt.compute.models.NetworkInterfaceReference(
                    id=nic_obj['id'],
                    primary=True
                ),
            ],
        ),
        storage_profile=azure.mgmt.compute.models.StorageProfile(
        os_disk=azure.mgmt.compute.models.OSDisk(
            caching=azure.mgmt.compute.models.CachingTypes.none,
            create_option=azure.mgmt.compute.models.DiskCreateOptionTypes.attach,
            name=dup_virtual_machine,
            managed_disk=azure.mgmt.compute.models.ManagedDiskParameters(
                id=managed_disk.id
            ),
        ),
        image_reference = azure.mgmt.compute.models.ImageReference(
            publisher=vm.storage_profile.image_reference.publisher,
            offer=vm.storage_profile.image_reference.offer,
            sku=vm.storage_profile.image_reference.sku,
            version=vm.storage_profile.image_reference.version,
        ),
    ),
    ),
)

Exception:

Failed to create virtual machines: Azure Error: InvalidParameter Message: Cannot attach an existing OS disk if the VM is created from a platform or user image. Target: osDisk

1

1 Answers

1
votes

The error log reason is that create_option is wrong, you should use FromImage not attach. You could check Azure Python SDK in this link.