2.10. Example: Creating NFS Data Storage using Python
When a Red Hat Enterprise Virtualization environment is first being created it is necessary to define at least a data storage domain, and an ISO storage domain. The data storage domain will be used to store virtual machine disk images while the ISO storage domain will be used to store installation media for guest operating systems.
The
API class provides access to a storage domains collection, named storagedomains. This collection contains all the storage domains in the environment. The storagedomains collection can also 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 Enterprise Virtualization. Refer to the Red Hat Enterprise Virtualization Administration Guide for more information on preparing NFS shares for use.
Example 2.9. Creating NFS data storage using Python
This Python example adds an NFS data domain to the
storagedomains collection. Adding an NFS storage domain in Python can be broken down into several steps:
- Identify the data center to which the storage must be attached, using the
getmethod of thedatacenterscollection.dc = api.datacenters.get(name="Default")
dc = api.datacenters.get(name="Default")Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Identify the host that must be used to attach the storage, using the
getmethod of thehostscollection.h = api.hosts.get(name="Atlantic")
h = api.hosts.get(name="Atlantic")Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Define the
Storageparameters for the NFS storage domain. In this example the NFS location192.0.43.10/storage/datais being used.s = params.Storage(address="192.0.43.10", path="/storage/data", type_="nfs")
s = params.Storage(address="192.0.43.10", path="/storage/data", type_="nfs")Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Request creation of the storage domain, using the
addmethod of thestoragedomainscollection. In addition to theStorageparameters it is necessary to pass:- A name for the storage domain.
- The data center object that was retrieved from the
datacenterscollection. - The host object that was retrieved from the
hostscollection. - The type of storage domain being added (
data,iso, orexport). - The storage format to use (
v1,v2, orv3).
Once these steps are combined, the completed script is:
If the
add method call is successful then the script will output:
Storage Domain 'data1' added (bd954c03-d180-4d16-878c-2aedbdede566).
Storage Domain 'data1' added (bd954c03-d180-4d16-878c-2aedbdede566).