Show Table of Contents
2.16. Example: Creating a Virtual Machine Storage Disk using Python
To ensure a newly created virtual machine has access to persistent storage you must create and attach a disk.
Example 2.15. Creating a virtual machine storage disk using Python
This Python example creates an 8 GB
These options are combined into a disk parameter object, before using the
virtio disk drive and attaches it to the virtual machine named vm1. The disk in this example:
- must be stored on the storage domain named
data1,disk_storage_domain = params.StorageDomains(storage_domain=[api.storagedomains.get(name="data1")])
- must be 8 GB in size,
disk_size = 8*1024*1024
- must be a
systemtype disk (as opposed todata),disk_type = "system"
- must be
virtiostorage device,disk_interface = "virtio"
- must be stored in
cowformat, anddisk_format = "cow"
- must be marked as a usable boot device.
disk_bootable = True
add method of the virtual machine's disks collection to create the disk itself.
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")
vm = api.vms.get(name="vm1")
sd = params.StorageDomains(storage_domain=[api.storagedomains.get(name="data1")])
disk_size = 8*1024*1024
disk_type = "system"
disk_interface = "virtio"
disk_format = "cow"
disk_bootable = True
disk_params = params.Disk(storage_domains=sd,
size=disk_size,
type_=disk_type,
interface=disk_interface,
format=disk_format,
bootable=disk_bootable)
try:
d = vm.disks.add(disk_params)
print "Disk '%s' added to '%s'." % (d.get_name(), vm.get_name())
except Exception as ex:
print "Adding disk to '%s' failed: %s" % (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:
Disk 'vm1_Disk1' added to 'vm1'.

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.