[ovirt-users] Ovirt python-sdk cloning VM

Juan Hernández jhernand at redhat.com
Mon Jan 18 10:34:07 EST 2016


On 01/18/2016 02:49 PM, Algirdas Žemaitis wrote:
> Hello,
> 
> 
> I was playing with Ovirt API/python-sdk for a week or so and stuck in
> one from first sight simple yet unsolvable spot…
> 
> Ovirt web GUI has option to clone VM from snapshot, to new(other)
> storage domain. Is it possible to do so with API ?
> 
> The point is, I’m using backup script (already modified a lot of it to
> suit our needs), once it is cloning from snapshot into new VM, it will
> use same storage, and causing a lot of load on already stressed domain,
> idea is to create VM (allocate disk before exporting it as backup) on
> other storage domain, in this way significantly reducing impact on live
> storage domain. What do you think ?
> 
> Original : https://github.com/wefixit-AT/oVirtBackup
> 
>  
> 
> Thank you for your time, and sorry for reaching directly, Ovirt lacks
> for an forum or something similar… or I just couldn’t find it…
> 

I'm adding the users at ovirt.org list, there is where you can send your
questions.

In order to clone a VM from an snapshot you have to use the "add"
operation of the "vms" service, passing as a parameter a "vm" object
that contains the identifier of the snapshot and the explicit mapping of
disk to storage domain. In XML it should look like this:

---8<---
<vm>
  <name>myclone</name>
  <cluster>
    <name>mycluster</name>
  </cluster>

  <!-- This is how you indicate what snapshot to clone: -->
  <snapshots>
   <snapshot id="266742a5-6a65-483c-816d-d2ce49746680"/>
  </snapshots>

  <!-- This is how you indicate how to map disks to storage
       domains. -->
  <disks>
    <disk>
      <image_id>8d4bd566-6c86-4592-a4a7-912dbf93c298</image_id>
      <storage_domains>
        <storage_domain id="9cb6cb0a-cf1d-41c2-92ca-5a6d665649c9"/>
      </storage_domains>
    </disk>
  </disks>

</vm>
--->8---

With the Python SDK it should look like this:

---8<---
# Find the VM:
vm = api.vms.get(name="myvm")

# Find the snapshot:
snapshot = None
for current in vm.snapshots.list():
  if current.get_description() == 'mysnap':
    snapshot = current
    break # Should probably end here, with an error.

# Find the image identifiers of the disks of the snapshot, as
# we need them in order to explicitly indicate that we want
# them created in a different storage domain:
disk_ids = []
for current in snapshot.disks.list():
  disk_ids.append(current.get_id())

# Find the storage domain where the disks should be created:
sd = api.storagedomains.get(name="yourdata")

# Prepare the list of disks for the operation to create the
# snapshot,explicitly indicating for each of them the storage
# domain where it should be created:
disk_list = []
for disk_id in disk_ids:
  disk = params.Disk(
    image_id=disk_id,
    storage_domains=params.StorageDomains(
      storage_domain=[
        params.StorageDomain(
          id=sd.get_id(),
        ),
      ],
    ),
  )
  disk_list.append(disk)

# Create the VM from the snapshot:
api.vms.add(
  params.VM(
    name="myclone",
    cluster=params.Cluster(name="mycluster"),
    snapshots=params.Snapshots(
      snapshot=[
        params.Snapshot(
          id=snapshot.get_id(),
        ),
      ],
    ),
    disks=params.Disks(
      disk=disk_list,
    ),
  )
)
--->8---

This should work for you.

However, I have to say that I find a bit strange that we use the
"image_id" attribute as the key of the disk in this case, instead of
just the "id". Allon, do you know if this is on purpose? I mean, when we
clone a VM from a template we use do like this:

  <disk id="the_key">
    ...
  </disk>

But in this case we do like this:

  <disk>
    <image_id>the_key</image_id>
    ...
  </disk>

Is there any reason for these two different ways to specify the
identifier of the disk?

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.


More information about the Users mailing list