In this post, we introduce vmcreate, our Python script for automating the creation of oVirt and RHEV virtual machines. This script has three purposes:
- to automate the creation of VMs with an easy to use command line interface
- to implement constraints on the provisioning of VMs
- to demonstrate the use of the oVirt Python API (as the documentation is not always very helpful)
How to install and configure the script
The script is located at https://github.com/clevernet/RHEV-scripts. In order to download it, you need to clone the git repository with this command:
git clone https://github.com/clevernet/RHEV-scripts
The two interesting files are vmcreate and vmcreateconfig.py.
First, download the oVirt / RHEVM certificate with this command (assuming oVirt is installed on localhost):
Then, edit the file vmcreateconfig.py and set the correct values for username, password and url in the credentials array.
If you get the error message « The host name ‘xxx’ contained in the URL doesn’t match any of the names in the server certificate », this means that you’ve set a URL that does indeed connect to the oVirt manager but the manager is not configured to recognize that name. You need to modify /etc/hosts to make sure that it contains the correct association between the name and the IP address of the manager.
The vmcreate script allows you to specify provisioning constraints based on the hypervisors, the clusters and the storage domains. In vmcreateconfig.py the array domains_clusters allows you to specify that a VM must be created on a specific storage domain if it is to run in a specific cluster. For example, the following configuration for the domains_clusters setting specifies that a VM that is to run in cluster Cluster2 cannot use storage domain StorageDomain3:
'Cluster1': [ 'StorageDomain1', 'StorageDomain2', 'StorageDomain3' ],
'Cluster2': [ 'StorageDomain1', 'StorageDomain2' ]
This will allow you to make sure that any VM created does run on the storage that is closest to it. If you do not want to take advantage of this feature, specify all your storage domains for all your clusters and the script will let you create a VM on any cluster and use any storage domain.
The same goes for the setting domains_hosts, where you can specify constraints between the hypervisors and the storage domains.
Finally, make sure to specify some default values in the defaults setting. Click https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Virtualization/3.0/html-single/REST_API_Guide/#appe-REST_API_Guide-Timezones to find out your timezone settings.
How to use the script
To get help, execute the following command:
This will output the command line help. Please note that at this stage, the help command outputs the list of hosts and clusters. Therefore, if the connection is to the manager is not valid, the help option will not work either.
To create a VM, use the following command line:
This will create a VM named testvm1 in the cluster Default, with 1GB of memory and 1 VCPU. Additionally, a virtual NIC card named eth0 will be created with driver virtio on the virtual network named ovirtmgmt. Additionally, a virtual bootable disk on storage domain data1 of size 1GB allocated with thin provisioning will be created and attached to the VM.
A number of additional options can be used:
- –host allows you to specify the hypervisor on which the VM should run
- –affinity allows you to specify whether the VM is migratable automatically, or only manually or not at all
- –nic and –disk can be used multiple times to create multiple virtual NICs and multiple virtual disks
- -d can be used to specify a description
- -c can be used to specify a comment
- -b can be used to tell the script to immediately boot the VM after creation