commit 9bc02e266c3e74f9af8a84cea689d694eb273765 Author: W. David Ashley w.david.ashley@gmail.com Date: Mon Aug 3 12:05:49 2015 -0500
Storage chapter - added example 10 - added support text for example 10
en-US/Storage_Pools.xml | 13 +++++- en-US/extras/StoragePools-Example-10.py | 74 +++++++++++++++++++++++++++++++ en-US/extras/StoragePools-Example-9.py | 3 +- 3 files changed, 87 insertions(+), 3 deletions(-) --- diff --git a/en-US/Storage_Pools.xml b/en-US/Storage_Pools.xml index 98f17ee..b9e5e90 100644 --- a/en-US/Storage_Pools.xml +++ b/en-US/Storage_Pools.xml @@ -225,8 +225,19 @@ <section id="libvirt_application_development_guide_using_python-Storage_Pools-Cloning"> <title>Cloning volumes</title> <para> - TBD + Cloning a storage volume is similar to creating a new storage volume, except that an existing + storage volume is used for most of the attributes. Only the name and permissions in the XML + parameter are used for the new volume, everything else is inherited from the existing volume. + </para> + <para> + It should be noted that cloning can take a very long time to accomplish, depending on the size + of the storage volume being cloned. This is because the clone process copies the data from the + source vloume to the new target volume. </para> + <example> + <title>Clone an existing storage volume</title> + <programlisting language="Python"><xi:include href="extras/StoragePools-Example-10.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting> + </example> </section>
<section id="libvirt_application_development_guide_using_python-Storage_Pools-Config_Vol"> diff --git a/en-US/extras/StoragePools-Example-10.py b/en-US/extras/StoragePools-Example-10.py new file mode 100644 index 0000000..5570c9c --- /dev/null +++ b/en-US/extras/StoragePools-Example-10.py @@ -0,0 +1,74 @@ +# Example-.py +from __future__ import print_function +import sys +import libvirt + +stgvol_xml = """ +<volume> + <name>sparse.img</name> + <allocation>0</allocation> + <capacity unit="G">2</capacity> + <target> + <path>/var/lib/virt/images/sparse.img</path> + <permissions> + <owner>107</owner> + <group>107</group> + <mode>0744</mode> + <label>virt_image_t</label> + </permissions> + </target> +</volume>""" +stgvol_xml2 = """ +<volume> + <name>sparse2.img</name> + <allocation>0</allocation> + <capacity unit="G">2</capacity> + <target> + <path>/var/lib/virt/images/sparse.img</path> + <permissions> + <owner>107</owner> + <group>107</group> + <mode>0744</mode> + <label>virt_image_t</label> + </permissions> + </target> +</volume>""" +pool = 'default' + +conn = libvirt.open('qemu:///system') +if conn == None: + print('Failed to open connection to qemu:///system', file=sys.stderr) + exit(1) + +pool = conn.storagePoolLookupByName(pool) +if pool == None: + print('Failed to locate any StoragePool objects.', file=sys.stderr) + exit(1) + +# create a new storage volume +stgvol = pool.createXML(stgvol_xml, 0) +if stgvol == None: + print('Failed to create a StorageVol object.', file=sys.stderr) + exit(1) + +# now clone the existing storage volume +print('This could take some time...') +stgvol2 = pool.createXMLFrom(stgvol_xml2, stgvol, 0) +if stgvol2 == None: + print('Failed to clone a StorageVol object.', file=sys.stderr) + exit(1) + +# remove the cloned storage volume +# physically remove the storage volume from the underlying disk media +stgvol2.wipe(0) +# logically remove the storage volume from the storage pool +stgvol2.delete(0) + +# remove the storage volume +# physically remove the storage volume from the underlying disk media +stgvol.wipe(0) +# logically remove the storage volume from the storage pool +stgvol.delete(0) + +conn.close() +exit(0) diff --git a/en-US/extras/StoragePools-Example-9.py b/en-US/extras/StoragePools-Example-9.py index 790c748..c57cd21 100644 --- a/en-US/extras/StoragePools-Example-9.py +++ b/en-US/extras/StoragePools-Example-9.py @@ -7,7 +7,7 @@ stgvol_xml = """ <volume> <name>sparse.img</name> <allocation>0</allocation> - <capacity unit="T">1</capacity> + <capacity unit="G">2</capacity> <target> <path>/var/lib/virt/images/sparse.img</path> <permissions> @@ -30,7 +30,6 @@ if pool == None: print('Failed to locate any StoragePool objects.', file=sys.stderr) exit(1)
-# create the storage volume stgvol = pool.createXML(stgvol_xml, 0) if stgvol == None: print('Failed to create a StorageVol objects.', file=sys.stderr)