This section provides examples demonstrating the steps to create a virtual machine within a basic Red Hat Virtualization environment, using the Python SDK.
These examples use the ovirtsdk Python library provided by the ovirt-engine-sdk-python package. This package is available to systems attached to a Red Hat Virtualization subscription pool in Red Hat Subscription Manager. See Installing the Software Development Kit for more information on subscribing your system(s) to download the software.
You will also need:
A networked installation of Red Hat Virtualization Manager.
A networked and configured Red Hat Virtualization Host.
An ISO image file containing an operating system for installation on a virtual machine.
A working understanding of both the logical and physical objects that make up a Red Hat Virtualization environment.
A working understanding of the Python programming language.
The examples include placeholders for authentication details (admin@internal for user name, and password for password). Replace the placeholders with the authentication requirements of your environment.
Red Hat Virtualization Manager generates a globally unique identifier (GUID) for the id attribute for each resource. Identifier codes in these examples differ from the identifier codes in your Red Hat Virtualization environment.
The examples contain only basic exception and error handling logic. For more information on the exception handling specific to the SDK, see the pydoc for the ovirtsdk.infrastructure.errors module:
pydoc ovirtsdk.infrastructure.errors
$pydoc ovirtsdk.infrastructure.errors
Copy to ClipboardCopied!Toggle word wrapToggle overflow
To connect to the Red Hat Virtualization Manager, you must create an instance of the Connection class from the ovirtsdk4.sdk module by importing the class at the start of the script:
import ovirtsdk4 as sdk
import ovirtsdk4 as sdk
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The constructor of the Connection class takes a number of arguments. Supported arguments are:
url
A string containing the base URL of the Manager, such as https://server.example.com/ovirt-engine/api.
username
Specifies the user name to connect, such as admin@internal. This parameter is mandatory.
password
Specifies the password for the user name provided by the username parameter. This parameter is mandatory.
token
An optional token to access the API, instead of a user name and password. If the token parameter is not specified, the SDK will create one automatically.
insecure
A Boolean flag that indicates whether the server’s TLS certificate and host name should be checked.
ca_file
A PEM file containing the trusted CA certificates. The certificate presented by the server will be verified using these CA certificates. If ca_file parameter is not set, the system-wide CA certificate store is used.
debug
A Boolean flag indicating whether debug output should be generated. If the value is True and the log parameter is not None, the data sent to and received from the server will be written to the log.
Note
User names and passwords are written to the debug log, so handle it with care.
Compression is disabled in debug mode, which means that debug messages are sent as plain text.
log
The logger where the log messages will be written.
kerberos
A Boolean flag indicating whether Kerberos authentication should be used instead of the default basic authentication.
timeout
The maximum total time to wait for the response, in seconds. A value of 0 (default) means to wait forever. If the timeout expires before the response is received, an exception is raised.
compress
A Boolean flag indicating whether the SDK should ask the server to send compressed responses. The default is True. This is a hint for the server, which may return uncompressed data even when this parameter is set to True. Compression is disabled in debug mode, which means that debug messages are sent as plain text.
sso_url
A string containing the base SSO URL of the server. The default SSO URL is computed from the url if no sso_url is provided.
sso_revoke_url
A string containing the base URL of the SSO revoke service. This needs to be specified only when using an external authentication service. By default, this URL is automatically calculated from the value of the url parameter, so that SSO token revoke will be performed using the SSO service, which is part of the Manager.
sso_token_name
The token name in the JSON SSO response returned from the SSO server. Default value is access_token.
headers
A dictionary with headers, which should be sent with every request.
connections
The maximum number of connections to open to the host. If the value is 0 (default), the number of connections is unlimited.
pipeline
The maximum number of requests to put in an HTTP pipeline without waiting for the response. If the value is 0 (default), pipelining is disabled.
import ovirtsdk4 as sdk
# Create a connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
connection.test()
print("Connected successfully!")
connection.close()
import ovirtsdk4 as sdk
# Create a connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
connection.test()
print("Connected successfully!")
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
For a full list of supported methods, you can generate the documentation for the ovirtsdk.api module on the Manager machine:
pydoc ovirtsdk.api
$pydoc ovirtsdk.api
Copy to ClipboardCopied!Toggle word wrapToggle overflow
When a Red Hat Virtualization environment is first created, it is necessary to define at least a data storage domain and an ISO storage domain. The data storage domain stores virtual disks while the ISO storage domain stores the installation media for guest operating systems.
The storagedomains collection contains all the storage domains in the environment and can be used to add and remove storage domains.
Note
The code provided in this example assumes that the remote NFS share has been pre-configured for use with Red Hat Virtualization. See the Administration Guide for more information on preparing NFS shares.
Example 3.6. Creating NFS data storage
This example adds an NFS data domain to the storagedomains collection.
V4
For V4, the add method is used to add the new storage domain and the types class is used to pass the following parameters:
A name for the storage domain.
The data center object that was retrieved from the datacenters collection.
The host object that was retrieved from the hosts collection.
The type of storage domain being added (data, iso, or export).
The storage format to use (v1, v2, or v3).
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
# Create the connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the storage domains service:
sds_service = connection.system_service().storage_domains_service()
# Create a new NFS storage domain:
sd = sds_service.add(
types.StorageDomain(
name='mydata',
description='My data',
type=types.StorageDomainType.DATA,
host=types.Host(
name='myhost',
),
storage=types.HostStorage(
type=types.StorageType.NFS,
address='_FQDN_',
path='/nfs/ovirt/path/to/mydata',
),
),
)
# Wait until the storage domain is unattached:
sd_service = sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = sd_service.get()
if sd.status == types.StorageDomainStatus.UNATTACHED:
break
print("Storage Domain '%s' added (%s)." % (sd.name(), sd.id()))
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
# Create the connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the storage domains service:
sds_service = connection.system_service().storage_domains_service()
# Create a new NFS storage domain:
sd = sds_service.add(
types.StorageDomain(
name='mydata',
description='My data',
type=types.StorageDomainType.DATA,
host=types.Host(
name='myhost',
),
storage=types.HostStorage(
type=types.StorageType.NFS,
address='_FQDN_',
path='/nfs/ovirt/path/to/mydata',
),
),
)
# Wait until the storage domain is unattached:
sd_service = sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = sd_service.get()
if sd.status == types.StorageDomainStatus.UNATTACHED:
break
print("Storage Domain '%s' added (%s)." % (sd.name(), sd.id()))
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add method call is successful, the examples output the text:
To create a virtual machine, you need installation media for the guest operating system. The installation media are stored in an ISO storage domain.
Note
The code provided in this example assumes that the remote NFS share has been pre-configured for use with Red Hat Virtualization. See the Administration Guide for more information on preparing NFS shares.
Example 3.7. Creating NFS ISO storage
This example adds an NFS ISO domain to the storagedomains collection.
V4
For V4, the add method is used to add the new storage domain and the types class is used to pass the following parameters:
A name for the storage domain.
The data center object that was retrieved from the datacenters collection.
The host object that was retrieved from the hosts collection.
The type of storage domain being added (data, iso, or export).
The storage format to use (v1, v2, or v3).
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the storage domains service:
sds_service = connection.system_service().storage_domains_service()
# Use the "add" method to create a new NFS storage domain:
sd = sds_service.add(
types.StorageDomain(
name='myiso',
description='My ISO',
type=types.StorageDomainType.ISO,
host=types.Host(
name='myhost',
),
storage=types.HostStorage(
type=types.StorageType.NFS,
address='FQDN',
path='/nfs/ovirt/path/to/myiso',
),
),
)
# Wait until the storage domain is unattached:
sd_service = sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = sd_service.get()
if sd.status == types.StorageDomainStatus.UNATTACHED:
break
print("Storage Domain '%s' added (%s)." % (sd.name(), sd.id()))
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the storage domains service:
sds_service = connection.system_service().storage_domains_service()
# Use the "add" method to create a new NFS storage domain:
sd = sds_service.add(
types.StorageDomain(
name='myiso',
description='My ISO',
type=types.StorageDomainType.ISO,
host=types.Host(
name='myhost',
),
storage=types.HostStorage(
type=types.StorageType.NFS,
address='FQDN',
path='/nfs/ovirt/path/to/myiso',
),
),
)
# Wait until the storage domain is unattached:
sd_service = sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = sd_service.get()
if sd.status == types.StorageDomainStatus.UNATTACHED:
break
print("Storage Domain '%s' added (%s)." % (sd.name(), sd.id()))
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add method call is successful, the examples output the text:
Once you have added a storage domain to Red Hat Virtualization, you must attach it to a data center and activate it before it will be ready for use.
Example 3.8. Attaching a storage domain to a data center
This example attaches an existing NFS storage domain, mydata, to the an existing data center, Default. The attach action is facilitated by the add method of the data center’s storagedomains collection. These examples may be used to attach both data and ISO storage domains.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
# Create the connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the service that manages the storage domains and use it to
# search for the storage domain:
sds_service = connection.system_service().storage_domains_service()
sd = sds_service.list(search='name=mydata')[0]
# Locate the service that manages the data centers and use it to
# search for the data center:
dcs_service = connection.system_service().data_centers_service()
dc = dcs_service.list(search='name=Default')[0]
# Locate the service that manages the data center where we want to
# attach the storage domain:
dc_service = dcs_service.data_center_service(dc.id)
# Locate the service that manages the storage domains that are attached
# to the data centers:
attached_sds_service = dc_service.storage_domains_service()
# Use the "add" method of service that manages the attached storage
# domains to attach it:
attached_sds_service.add(
types.StorageDomain(
id=sd.id,
),
)
# Wait until the storage domain is active:
attached_sd_service = attached_sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = attached_sd_service.get()
if sd.status == types.StorageDomainStatus.ACTIVE:
break
print("Attached data storage domain '%s' to data center '%s' (Status: %s)." %
(sd.name(), dc.name(), sd.status.state()))
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
# Create the connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the service that manages the storage domains and use it to
# search for the storage domain:
sds_service = connection.system_service().storage_domains_service()
sd = sds_service.list(search='name=mydata')[0]
# Locate the service that manages the data centers and use it to
# search for the data center:
dcs_service = connection.system_service().data_centers_service()
dc = dcs_service.list(search='name=Default')[0]
# Locate the service that manages the data center where we want to
# attach the storage domain:
dc_service = dcs_service.data_center_service(dc.id)
# Locate the service that manages the storage domains that are attached
# to the data centers:
attached_sds_service = dc_service.storage_domains_service()
# Use the "add" method of service that manages the attached storage
# domains to attach it:
attached_sds_service.add(
types.StorageDomain(
id=sd.id,
),
)
# Wait until the storage domain is active:
attached_sd_service = attached_sds_service.storage_domain_service(sd.id)
while True:
time.sleep(5)
sd = attached_sd_service.get()
if sd.status == types.StorageDomainStatus.ACTIVE:
break
print("Attached data storage domain '%s' to data center '%s' (Status: %s)." %
(sd.name(), dc.name(), sd.status.state()))
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the calls to the add methods are successful, the examples output the following text:
Attached data storage domain 'data1' to data center 'Default' (Status: maintenance).
Attached data storage domain 'data1' to data center 'Default' (Status: maintenance).
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Status: maintenance indicates that the storage domains still need to be activated.
Once you have added a storage domain to Red Hat Virtualization and attached it to a data center, you must activate it before it will be ready for use.
Example 3.9. Activating a storage domain
This example activates an NFS storage domain, mydata, attached to the data center, Default. The activate action is facilitated by the activate method of the storage domain.
V4
import ovirtsdk4 as sdk
connection = sdk.Connection
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the service that manages the storage domains and use it to
# search for the storage domain:
sds_service = connection.system_service().storage_domains_service()
sd = sds_service.list(search='name=mydata')[0]
# Locate the service that manages the data centers and use it to
# search for the data center:
dcs_service = connection.system_service().data_centers_service()
dc = dcs_service.list(search='name=Default')[0]
# Locate the service that manages the data center where we want to
# attach the storage domain:
dc_service = dcs_service.data_center_service(dc.id)
# Locate the service that manages the storage domains that are attached
# to the data centers:
attached_sds_service = dc_service.storage_domains_service()
# Activate storage domain:
attached_sd_service = attached_sds_service.storage_domain_service(sd.id)
attached_sd_service.activate()
# Wait until the storage domain is active:
while True:
time.sleep(5)
sd = attached_sd_service.get()
if sd.status == types.StorageDomainStatus.ACTIVE:
break
print("Attached data storage domain '%s' to data center '%s' (Status: %s)." %
(sd.name(), dc.name(), sd.status.state()))
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
connection = sdk.Connection
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the service that manages the storage domains and use it to
# search for the storage domain:
sds_service = connection.system_service().storage_domains_service()
sd = sds_service.list(search='name=mydata')[0]
# Locate the service that manages the data centers and use it to
# search for the data center:
dcs_service = connection.system_service().data_centers_service()
dc = dcs_service.list(search='name=Default')[0]
# Locate the service that manages the data center where we want to
# attach the storage domain:
dc_service = dcs_service.data_center_service(dc.id)
# Locate the service that manages the storage domains that are attached
# to the data centers:
attached_sds_service = dc_service.storage_domains_service()
# Activate storage domain:
attached_sd_service = attached_sds_service.storage_domain_service(sd.id)
attached_sd_service.activate()
# Wait until the storage domain is active:
while True:
time.sleep(5)
sd = attached_sd_service.get()
if sd.status == types.StorageDomainStatus.ACTIVE:
break
print("Attached data storage domain '%s' to data center '%s' (Status: %s)." %
(sd.name(), dc.name(), sd.status.state()))
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the activate requests are successful, the examples output the text:
Activated storage domain 'mydata' in data center 'Default' (Status: active).
Activated storage domain 'mydata' in data center 'Default' (Status: active).
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Status: active indicates that the storage domains have been activated.
Virtual machine creation is performed in several steps. The first step, covered here, is to create the virtual machine object itself.
Example 3.11. Creating a virtual machine
This example creates a virtual machine, vm1, with the following requirements:
512 MB of memory, expressed in bytes.
Attached to the Default cluster, and therefore the Default data center.
Based on the default Blank template.
Boots from the virtual hard disk drive.
V4
In V4, the options are added as types, using the add method.
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Use the "add" method to create a new virtual machine:
vms_service.add(
types.Vm(
name='vm1',
memory = 512*1024*1024
cluster=types.Cluster(
name='Default',
),
template=types.Template(
name='Blank',
),
os=types.OperatingSystem(boot=types.Boot(devices=[types.BootDevice.HD)]
),
)
print("Virtual machine '%s' added." % vm.name)
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Use the "add" method to create a new virtual machine:
vms_service.add(
types.Vm(
name='vm1',
memory = 512*1024*1024
cluster=types.Cluster(
name='Default',
),
template=types.Template(
name='Blank',
),
os=types.OperatingSystem(boot=types.Boot(devices=[types.BootDevice.HD)]
),
)
print("Virtual machine '%s' added." % vm.name)
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add request is successful, the examples output the text:
Virtual machine 'vm1' added.
Virtual machine 'vm1' added.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
To ensure that a newly created virtual machine has network access, you must create and attach a virtual NIC.
Example 3.12. Creating a virtual NIC
This example creates a NIC, nic1, and attach it to a virtual machine, vm1. The NIC in this example is a virtio network device and attached to the ovirtmgmt management network.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the virtual machines service and use it to find the virtual
# machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the network interface cards of the
# virtual machine:
nics_service = vms_service.vm_service(vm.id).nics_service()
# Locate the vnic profiles service and use it to find the ovirmgmt
# network's profile id:
profiles_service = connection.system_service().vnic_profiles_service()
profile_id = None
for profile in profiles_service.list():
if profile.name == 'ovirtmgmt':
profile_id = profile.id
break
# Use the "add" method of the network interface cards service to add the
# new network interface card:
nic = nics_service.add(
types.Nic(
name='nic1',
interface=types.NicInterface.VIRTIO,
vnic_profile=types.VnicProfile(id=profile_id),
),
)
print("Network interface '%s' added to '%s'." % (nic.name, vm.name))
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the virtual machines service and use it to find the virtual
# machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the network interface cards of the
# virtual machine:
nics_service = vms_service.vm_service(vm.id).nics_service()
# Locate the vnic profiles service and use it to find the ovirmgmt
# network's profile id:
profiles_service = connection.system_service().vnic_profiles_service()
profile_id = None
for profile in profiles_service.list():
if profile.name == 'ovirtmgmt':
profile_id = profile.id
break
# Use the "add" method of the network interface cards service to add the
# new network interface card:
nic = nics_service.add(
types.Nic(
name='nic1',
interface=types.NicInterface.VIRTIO,
vnic_profile=types.VnicProfile(id=profile_id),
),
)
print("Network interface '%s' added to '%s'." % (nic.name, vm.name))
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add request is successful, the examples output the text:
Network interface 'nic1' added to 'vm1'.
Network interface 'nic1' added to 'vm1'.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
To ensure that a newly created virtual machine has access to persistent storage, you must create and attach a disk.
Example 3.13. Creating a virtual machine disk
This example creates an 8 GB virtio disk and attach it to a virtual machine, vm1. The disk has the following requirements:
Stored on the storage domain named data1.
8 GB in size.
system type disk (as opposed to data).
virtio storage device.
COW format.
Marked as a usable boot device.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the virtual machines service and use it to find the virtual
# machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the disk attachments of the virtual
# machine:
disk_attachments_service = vms_service.vm_service(vm.id).disk_attachments_service()
# Use the "add" method of the disk attachments service to add the disk.
# Note that the size of the disk, the `provisioned_size` attribute, is
# specified in bytes, so to create a disk of 10 GiB the value should
# be 10 * 2^30.
disk_attachment = disk_attachments_service.add(
types.DiskAttachment(
disk=types.Disk(
format=types.DiskFormat.COW,
provisioned_size=8*1024*1024,
storage_domains=[
types.StorageDomain(
name='data1',
),
],
),
interface=types.DiskInterface.VIRTIO,
bootable=True,
active=True,
),
)
# Wait until the disk status is OK:
disks_service = connection.system_service().disks_service()
disk_service = disks_service.disk_service(disk_attachment.disk.id)
while True:
time.sleep(5)
disk = disk_service.get()
if disk.status == types.DiskStatus.OK:
break
print("Disk '%s' added to '%s'." % (disk.name(), vm.name()))
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Locate the virtual machines service and use it to find the virtual
# machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the disk attachments of the virtual
# machine:
disk_attachments_service = vms_service.vm_service(vm.id).disk_attachments_service()
# Use the "add" method of the disk attachments service to add the disk.
# Note that the size of the disk, the `provisioned_size` attribute, is
# specified in bytes, so to create a disk of 10 GiB the value should
# be 10 * 2^30.
disk_attachment = disk_attachments_service.add(
types.DiskAttachment(
disk=types.Disk(
format=types.DiskFormat.COW,
provisioned_size=8*1024*1024,
storage_domains=[
types.StorageDomain(
name='data1',
),
],
),
interface=types.DiskInterface.VIRTIO,
bootable=True,
active=True,
),
)
# Wait until the disk status is OK:
disks_service = connection.system_service().disks_service()
disk_service = disks_service.disk_service(disk_attachment.disk.id)
while True:
time.sleep(5)
disk = disk_service.get()
if disk.status == types.DiskStatus.OK:
break
print("Disk '%s' added to '%s'." % (disk.name(), vm.name()))
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add request is successful, the examples output the text:
Disk 'vm1_Disk1' added to 'vm1'.
Disk 'vm1_Disk1' added to 'vm1'.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
To install a guest operating system on a newly created virtual machine, you must attach an ISO file containing the operating system installation media. To locate the ISO file, see Listing Files in an ISO Storage Domain.
Example 3.14. Attaching an ISO image to a virtual machine
This example attaches my_iso_file.iso to the vm1 virtual machine, using the add method of the virtual machine’s cdroms collection.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()
# Get the first CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
# Change the CD of the VM to 'my_iso_file.iso'. By default the
# operation permanently changes the disk that is visible to the
# virtual machine after the next boot, but has no effect
# on the currently running virtual machine. If you want to change the
# disk that is visible to the current running virtual machine, change
# the `current` parameter's value to `True`.
cdrom_service.update(
cdrom=types.Cdrom(
file=types.File(
id='my_iso_file.iso'
),
),
current=False,
)
print("Attached CD to '%s'." % vm.name())
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()
# Get the first CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
# Change the CD of the VM to 'my_iso_file.iso'. By default the
# operation permanently changes the disk that is visible to the
# virtual machine after the next boot, but has no effect
# on the currently running virtual machine. If you want to change the
# disk that is visible to the current running virtual machine, change
# the `current` parameter's value to `True`.
cdrom_service.update(
cdrom=types.Cdrom(
file=types.File(
id='my_iso_file.iso'
),
),
current=False,
)
print("Attached CD to '%s'." % vm.name())
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the add request is successful, the examples output the text:
Attached CD to 'vm1'.
Attached CD to 'vm1'.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Example 3.15. Ejecting a cdrom from a virtual machine
This example ejects an ISO image from a virtual machine’s cdrom collection.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the VM:
cdroms_service = vm_service.cdroms_service()
# Get the first found CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step
# of the VM:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
cdrom_service.remove()
print("Removed CD from '%s'." % vm.name())
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the VM:
cdroms_service = vm_service.cdroms_service()
# Get the first found CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step
# of the VM:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
cdrom_service.remove()
print("Removed CD from '%s'." % vm.name())
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the delete or remove request is successful, the examples output the text:
Removed CD from 'vm1'.
Removed CD from 'vm1'.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
attachments_service = vm_service.disk_attachments_service()
attachment = next(
(a for a in disk_attachments if a.disk.id == disk.id), None
)
# Remove the attachment. The default behavior is that the disk is detached
# from the virtual machine, but not deleted from the system. If you wish to
# delete the disk, change the detach_only parameter to "False".
attachment.remove(detach_only=True)
print("Detached disk %s successfully!" % attachment)
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
attachments_service = vm_service.disk_attachments_service()
attachment = next(
(a for a in disk_attachments if a.disk.id == disk.id), None
)
# Remove the attachment. The default behavior is that the disk is detached
# from the virtual machine, but not deleted from the system. If you wish to
# delete the disk, change the detach_only parameter to "False".
attachment.remove(detach_only=True)
print("Detached disk %s successfully!" % attachment)
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the delete or remove request is successful, the examples output the text:
Detached disk vm1_disk1 successfully!
Detached disk vm1_disk1 successfully!
Copy to ClipboardCopied!Toggle word wrapToggle overflow
This example starts the virtual machine using the start method.
V4
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine, as that is where
# the action methods are defined:
vm_service = vms_service.vm_service(vm.id)
# Call the "start" method of the service to start it:
vm_service.start()
# Wait until the virtual machine is up:
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.UP:
break
print("Started '%s'." % vm.name())
# Close the connection to the server:
connection.close()
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine, as that is where
# the action methods are defined:
vm_service = vms_service.vm_service(vm.id)
# Call the "start" method of the service to start it:
vm_service.start()
# Wait until the virtual machine is up:
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.UP:
break
print("Started '%s'." % vm.name())
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If the start request is successful, the examples output the text:
Started 'vm1'.
Started 'vm1'.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The UP status indicates that the virtual machine is running.
You can start a virtual machine, overriding its default parameters.
Example 3.17. Starting a virtual machine with overridden parameters
This example boots a virtual machine with a Windows ISO and attach the virtio-win_x86.vfd floppy disk, which contains Windows drivers. This action is equivalent to using the Run Once window in the Administration Portal to start a virtual machine.
V4
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()
# Get the first CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
# Change the CD of the VM to 'windows_example.iso':
cdrom_service.update(
cdrom=types.Cdrom(
file=types.File(
id='windows_example.iso'
),
),
current=False,
)
# Call the "start" method of the service to start it:
vm_service.start(
vm=types.Vm(
os=types.OperatingSystem(
boot=types.Boot(
devices=[
types.BootDevice.CDROM,
]
)
),
)
)
# Wait until the virtual machine's status is "UP":
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.UP:
break
print("Started '%s'." % vm.name())
# Close the connection to the server:
connection.close()
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Get the reference to the "vms" service:
vms_service = connection.system_service().vms_service()
# Find the virtual machine:
vm = vms_service.list(search='name=vm1')[0]
# Locate the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Locate the service that manages the CDROM devices of the virtual machine:
cdroms_service = vm_service.cdroms_service()
# Get the first CDROM:
cdrom = cdroms_service.list()[0]
# Locate the service that manages the CDROM device found in previous step:
cdrom_service = cdroms_service.cdrom_service(cdrom.id)
# Change the CD of the VM to 'windows_example.iso':
cdrom_service.update(
cdrom=types.Cdrom(
file=types.File(
id='windows_example.iso'
),
),
current=False,
)
# Call the "start" method of the service to start it:
vm_service.start(
vm=types.Vm(
os=types.OperatingSystem(
boot=types.Boot(
devices=[
types.BootDevice.CDROM,
]
)
),
)
)
# Wait until the virtual machine's status is "UP":
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.UP:
break
print("Started '%s'." % vm.name())
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
You can start a virtual machine with a specific configuration, using the Cloud-Init tool.
Example 3.18. Starting a virtual machine with Cloud-Init
This example shows you how to start a virtual machine using the Cloud-Init tool to set a host name and a static IP for the eth0 interface.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Find the virtual machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search = 'name=vm1')[0]
# Find the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Start the virtual machine enabling cloud-init and providing the
# password for the `root` user and the network configuration:
vm_service.start(
use_cloud_init=True,
vm=types.Vm(
initialization=types.Initialization(
user_name='root',
root_password='password',
host_name='MyHost.example.com',
nic_configurations=[
types.NicConfiguration(
name='eth0',
on_boot=True,
boot_protocol=types.BootProtocol.STATIC,
ip=types.Ip(
version=types.IpVersion.V4,
address='10.10.10.1',
netmask='255.255.255.0',
gateway='10.10.10.1'
)
)
)
)
)
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Find the virtual machine:
vms_service = connection.system_service().vms_service()
vm = vms_service.list(search = 'name=vm1')[0]
# Find the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Start the virtual machine enabling cloud-init and providing the
# password for the `root` user and the network configuration:
vm_service.start(
use_cloud_init=True,
vm=types.Vm(
initialization=types.Initialization(
user_name='root',
root_password='password',
host_name='MyHost.example.com',
nic_configurations=[
types.NicConfiguration(
name='eth0',
on_boot=True,
boot_protocol=types.BootProtocol.STATIC,
ip=types.Ip(
version=types.IpVersion.V4,
address='10.10.10.1',
netmask='255.255.255.0',
gateway='10.10.10.1'
)
)
)
)
)
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Red Hat Virtualization Manager records and logs many system events. These event logs are accessible through the user interface, the system log files, and using the API. The ovirtsdk library exposes events using the events collection.
Example 3.19. Checking system events
In this example the events collection is listed.
The query parameter of the list method is used to ensure that all available pages of results are returned. By default the list method returns only the first page of results, which is 100 records in length.
The returned list is sorted in reverse chronological order, to display the events in the order in which they occurred.
V4
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Find the service that manages the collection of events:
events_service = connection.system_service().events_service()
page_number = 1
events = events_service.list(search='page %s' % page_number)
while events:
for event in events:
print(
"%s %s CODE %s - %s" % (
event.time,
event.severity,
event.code,
event.description,
)
)
page_number = page_number + 1
events = events_service.list(search='page %s' % page_number)
# Close the connection to the server:
connection.close()
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin@internal',
password='password',
ca_file='ca.pem',
)
# Find the service that manages the collection of events:
events_service = connection.system_service().events_service()
page_number = 1
events = events_service.list(search='page %s' % page_number)
while events:
for event in events:
print(
"%s %s CODE %s - %s" % (
event.time,
event.severity,
event.code,
event.description,
)
)
page_number = page_number + 1
events = events_service.list(search='page %s' % page_number)
# Close the connection to the server:
connection.close()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
These examples output events in the following format:
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 30 - User admin@internal logged in.
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 153 - VM vm1 was started by admin@internal (Host: MyHost).
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 30 - User admin@internal logged in.
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 30 - User admin@internal logged in.
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 153 - VM vm1 was started by admin@internal (Host: MyHost).
YYYY-MM-DD_T_HH:MM:SS NORMAL CODE 30 - User admin@internal logged in.
Copy to ClipboardCopied!Toggle word wrapToggle overflow
We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.
Making open source more inclusive
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.
About Red Hat
We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.