Show Table of Contents
2.16. Example: Attaching an ISO Image to a Virtual Machine using Python
To begin installing a guest operating system on a newly created virtual machine you must attach an ISO file containing the operating system installation media.
Example 2.15. Identifying ISO images
ISO images are found in the
files collection attached to the ISO storage domain. This example lists the contents of the files collection on an ISO storage domain.
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API(url="https://HOST",
username="USER@DOMAIN",
password="PASS",
ca_file="ca.crt")
sd = api.storagedomains.get(name="iso1")
iso = sd.files.list()
for i in iso:
print "%s" % i.get_name()
except Exception as ex:
print "Unexpected error: %s" % ex
If successful the script will output an entry like this for each file found in the
files collection:
RHEL6.3-Server-x86_64-DVD1.iso
Note that because files on the ISO domain must be uniquely named the
id and name attributes of the file are shared.
Example 2.16. Attaching an ISO image to a virtual machine using Python
This Python example attaches the
RHEL6.3-Server-x86_64-DVD1.iso ISO image file to the vm1 virtual machine. Once identified the image file is attached using the add method of the virtual machine's cdroms collection.
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API(url="https://HOST",
username="USER@DOMAIN",
password="PASS,
ca_file="ca.crt")
sd = api.storagedomains.get(name="iso1")
cd_iso = sd.files.get(name="RHEL6.3-Server-x86_64-DVD1.iso")
cd_vm = api.vms.get(name="vm1")
cd_params = params.CdRom(file=cd_iso)
try:
cd_vm.cdroms.add(cd_params)
print "Attached CD to '%s'." % cd_vm.get_name()
except Exception as ex:
print "Failed to attach CD to '%s': %s" % (cd_vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex
If the
add request is successful then the script will output:
Attached CD to 'vm1'.
Note
This procedure is for attaching an ISO image to virtual machines with a status of
Down. To attach an ISO to a virtual machine with an Up status, amend the second try statement to the following:
try: cdrom=cd_vm.cdroms.get(id="00000000-0000-0000-0000-000000000000") cdrom.set_file(cd_iso) cdrom.update(current=True) print "Attached CD to '%s'." % cd_vm.get_name() except: print "Failed to attach CD to '%s': %s" % (cd_vm.get_name(), ex)
Example 2.17. Ejecting a cdrom from a Virtual Machine using Python
Eject an ISO from a virtual machine's
cdrom collection.
from ovirtsdk.api import API
from ovirtsdk.xml import params
try:
api = API(url="https://HOST",
username="USER@DOMAIN",
password="PASS,
ca_file="ca.crt")
sd = api.storagedomains.get(name="iso1")
vm = api.vms.get(name="vm1")
try:
vm.cdroms.get(id="00000000-0000-0000-0000-000000000000").delete()
print "Removed CD from '%s'." % vm.get_name()
except Exception as ex:
print "Failed to remove CD from '%s': %s" % (vm.get_name(), ex)
api.disconnect()
except Exception as ex:
print "Unexpected error: %s" % ex

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.