ok, got it !
just add clone parameter for add function.
vm = vms_service.add(
types.Vm(
name=vm_name,
cluster=types.Cluster(
name=vm_cluster_name
),
stateless=False,
type=types.VmType('server'),
comment='cloned from template '+template_name+' version
'+str(template_version),
template=types.Template(
id=template_id
),
disk_attachments=template_disk_attachments,
),
clone=True,
)
-------- Message initial --------
Date: Thu, 22 Mar 2018 07:09:33 +0000
Objet: [ovirt-users] create a cloned virtual machine based on a template with SDK API
python
À: users(a)ovirt.org
<users@ovirt.org<mailto:%22users@ovirt.org%22%20%3cusers@ovirt.org%3e>>
Reply-to: Nicolas Vaye <nicolas.vaye(a)province-sud.nc>
De: Nicolas Vaye
<nicolas.vaye@province-sud.nc<mailto:Nicolas%20Vaye%20%3cnicolas.vaye@province-sud.nc%3e>>
Hi,
I want to create a cloned virtual machine based on a template with SDK API python and i
don't find the parameter to indicate the clone action for the disk
here is my code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pprint import pprint
import logging
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
template_name='test_debian_9.4'
template_version=1
cluster_name='nico-cluster'
data_domain_name='OVIRT-TEST2'
logging.basicConfig(level=logging.DEBUG, filename='example.log')
# This example will connect to the server and start a virtual machine
# with cloud-init, in order to automatically configure the network and
# the password of the `root` user.
# Create the connection to the server:
connection = sdk.Connection(
url='https://ocenter.province-sud.prod/ovirt-engine/api',
username='admin@internal<mailto:username='admin@internal>',
password='admin',
ca_file='CA_ocenter.pem',
debug=True,
log=logging.getLogger(),
)
##################################
############ TEMPLATE ############
##################################
# Get the reference to the root of the tree of services:
system_service = connection.system_service()
# Get the reference to the service that manages the storage domains:
storage_domains_service = system_service.storage_domains_service()
# Find the storage domain we want to be used for virtual machine disks:
storage_domain =
storage_domains_service.list(search='name='+data_domain_name)[0]
# Get the reference to the service that manages the templates:
templates_service = system_service.templates_service()
# When a template has multiple versions they all have the same name, so
# we need to explicitly find the one that has the version name or
# version number that we want to use. In this case we want to use
# version 1 of the template.
templates = templates_service.list(search='name='+template_name)
template_id = None
for template in templates:
if template.version.version_number == template_version:
template_id = template.id
break
if template_id == None:
print "ERREUR le template "+template_name+"en version
"+str(template_version)+" n'a pas été trouvé!!"
# Find the template disk we want be created on specific storage domain
# for our virtual machine:
template_service = templates_service.template_service(template_id)
disk_attachments = connection.follow_link(template_service.get().disk_attachments)
print "disk_attachments=" + str(len(disk_attachments))
template_disk_attachments = []
for disk in disk_attachments:
template_disk_attachments.append(types.DiskAttachment(
disk=types.Disk(
id=disk.id,
format=types.DiskFormat.COW,
storage_domains=[
types.StorageDomain(
id=storage_domain.id,
),
],
),
)
)
# Get the reference to the service that manages the virtual machines:
vms_service = system_service.vms_service()
# Add a new virtual machine explicitly indicating the identifier of the
# template version that we want to use and indicating that template disk
# should be created on specific storage domain for the virtual machine:
vm = vms_service.add(
types.Vm(
name='myvm',
cluster=types.Cluster(
name=cluster_name
),
stateless=False,
type=types.VmType('server'),
comment='based on template '+template_name+'en version
'+str(template_version),
template=types.Template(
id=template_id
),
disk_attachments=template_disk_attachments,
)
)
# Get a reference to the service that manages the virtual machine that
# was created in the previous step:
vm_service = vms_service.vm_service(vm.id)
# Wait till the virtual machine is down, which indicats that all the
# disks have been created:
while True:
time.sleep(1)
vm = vm_service.get()
if vm.status == types.VmStatus.DOWN:
break
# Close the connection to the server:
connection.close()
If the data_domain_name is the same as the template data domain, then this script seem to
create a vm but not with cloned disk.
If the data_domain_name is NOT the same as the template data domain, then this script
produce an error
ovirtsdk4.Error: Fault reason is "Operation Failed". Fault detail is
"[Cannot add VM. The selected Storage Domain does not contain the VM
Template.]". HTTP response code is 400.
So how to create a cloned virtual machine based on a template ?
I think i'm looking for the same parameter on the web ui, "Storage
Allocation" => Thin/Clone
[cid:1521702568.14216.1.camel@province-sud.nc<mailto:1521702568.14216.1.camel@province-sud.nc>]
Thanks.
Nicolas VAYE
_______________________________________________
Users mailing list
Users@ovirt.org<mailto:Users@ovirt.org>
http://lists.ovirt.org/mailman/listinfo/users