0

I have been working with Microsoft Azure to build virtual machines using the Azure SDK for Python, and now I want to create a managed image from an existing virtual machine.

I saw that there is a way to do it in power shell here

But after a long research i didn't find how to do it in python sdk. My goal is to be able to save a virtual machine into an image and load it afterwards (I'm using the ARM and not the ASM).

Turge
  • 1

2 Answers2

1

After a long time trying to figure this out I was finally able to capture an image from a vm. firstly, the vm needs to be dealocated and generalized:

# Deallocate
async_vm_deallocate = self.compute_client.virtual_machines.deallocate(resource_group.name, names.vm)
async_vm_deallocate.wait()

# Generalize (possible because deallocated)
self.compute_client.virtual_machines.generalize(resource_group.name, names.vm)

I found that there are 2 options to creating the image:

  1. compute_client.virtual_machines.capture(resource_group_name=resource_group.name, vm_name=vm.name, parameters=parameters)

this way requires creating a ComputeManagmentClient and the following import:

from azure.mgmt.compute.v2015_06_15.models import VirtualMachineCaptureParameters

the parameters have to be object type: ~azure.mgmt.compute.v2015_06_15.models.VirtualMachineCaptureParameters.

The object VirtualMachineCaptureParameters has 3 required params: vhd_name_prefix (str), destination container name (str), overwrite vhds (bool)

what these are, I have no idea and there is no explanation as to what they are. so I didnt use this way

  1. (the way I choose to use) compute_client.images.create_or_update(resource_group_name=resource_group, image_name=unique_name, parameters=params)

this way requires creating a ComputeManagmentClient and the following import:

from azure.mgmt.compute.v2020_06_01.models import Image, SubResource

and it is pretty straight forward

sub_resource = SubResource(id=vm.id)
params = Image(location=LOCATION, source_virtual_machine=sub_resource)
i = compute_client.images.create_or_update(resource_group_name=resource_group, image_name=image_name, parameters=params)
i.wait()

creating the SubResource() and the Image() objects is mandatory as that is the object type expected

Dharman
  • 30,962
  • 25
  • 85
  • 135
ShaiB
  • 111
  • 8
  • Hey there, I follow the same steps and was able to save the VM into an image. But when I try to create new VM's using the image it fails with the below error. ```msrestazure.azure_exceptions.CloudError: Azure Error: OSProvisioningClientError Message: OS Provisioning for VM 'xx' did not finish in the allotted time. However, the VM guest agent was detected running. This suggests the guest OS has not been properly prepared to be used as a VM image (with CreateOption=FromImage). To resolve this issue, either use the VHD as is with CreateOption=Attach or prepare it properly for use as an image``` – Aravind Reddy Jul 23 '21 at 15:25
0

Create a compute client:

Deallocate and generalize:

    # Deallocate
    async_vm_deallocate = self.compute_client.virtual_machines.deallocate(resource_group.name, names.vm)
    async_vm_deallocate.wait()

    # Generalize (possible because deallocated)
    self.compute_client.virtual_machines.generalize(resource_group.name, names.vm)

Create an image, there an operation group compute_client.images. I have no exact example like yours, but see this one to create an image from a blob (can be adapted to your scenario):

Laurent Mazuel
  • 3,422
  • 13
  • 27
  • Hi Laurent, thanks for your help. At this point I have created several virtual machines under different resource groups. The question now is where do I get the "blob_uri" from? As quoted in [link](https://learn.microsoft.com/en-us/python/azure/python-sdk-azure-samples-managed-disks?view=azure-python#virtual-machine-with-managed-disks), I need not worry about storage accounts anymore, therefore how will I be able to obtain the blob or .vhd file? Thanks in advance – Turge Oct 16 '17 at 14:34
  • If you want to manage yourself blob and vhd, you can't use managed disk. https://stackoverflow.com/questions/42388855/how-to-get-the-vhd-of-an-azure-managed-disk – Laurent Mazuel Oct 16 '17 at 21:26