commit 6f781506e51ee29bb6ffa84b346be64296797b31 Author: W. David Ashley w.david.ashley@gmail.com Date: Tue Jul 28 14:30:16 2015 -0500
Domains chapter - Block device section - Added Domains-Example-42.py
en-US/Guest_Domains.xml | 101 ++---------------------------------- en-US/extras/Domains-Example-42.py | 75 ++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 97 deletions(-) --- diff --git a/en-US/Guest_Domains.xml b/en-US/Guest_Domains.xml index 6df3d48..04942d1 100644 --- a/en-US/Guest_Domains.xml +++ b/en-US/Guest_Domains.xml @@ -1272,103 +1272,10 @@ units of MB/sec. </para>
- <programlisting> - <![CDATA[/* example blockpull-example.c */ - /* compile with: gcc -g -Wall blockpull-example.c -o blockpull-example -lvirt */ - #include <stdio.h> - #include <stdlib.h> - #include <unistd.h> - #include <libvirt/libvirt.h> - - int do_cmd(const char *cmdline) - { - int status = system(cmdline); - if (status < 0) - return -1; - else - return WEXITSTATUS(status); - } - - virDomainPtr make_domain(virConnectPtr conn) - { - virDomainPtr dom; - char domxml[] = \ - "<domain type='kvm'> \ - <name>example</name> \ - <memory>131072</memory> \ - <vcpu>1</vcpu> \ - <os> \ - <type arch='x86_64' machine='pc-0.13'>hvm</type> \ - </os> \ - <devices> \ - <disk type='file' device='disk'> \ - <driver name='qemu' type='qed'/> \ - <source file='/var/lib/libvirt/images/example.qed' /> \ - <target dev='vda' bus='virtio'/> \ - </disk> \ - </devices> \ - </domain>"; - - do_cmd("qemu-img create -f raw /var/lib/libvirt/images/backing.qed 100M"); - do_cmd("qemu-img create -f qed -b /var/lib/libvirt/images/backing.qed \ - /var/lib/libvirt/images/example.qed"); - - dom = virDomainCreateXML(conn, domxml, 0); - return dom; - } - - int main(int argc, char *argv[]) - { - virConnectPtr conn; - virDomainPtr dom = NULL; - char disk[] = "/var/lib/libvirt/images/example.qed"; - - conn = virConnectOpen("qemu:///system"); - if (conn == NULL) { - fprintf(stderr, "Failed to open connection to qemu:///system\n"); - goto error; - } - - dom = make_domain(conn); - if (dom == NULL) { - fprintf(stderr, "Failed to create domain\n"); - goto error; - } - - if ((virDomainBlockPull(dom, disk, 0, 0)) < 0) { - fprintf(stderr, "Failed to start block pull"); - goto error; - } - - while (1) { - virDomainBlockJobInfo info; - int ret = virDomainGetBlockJobInfo(dom, disk, &info, 0); - - if (ret == 1) { - printf("BlockPull progress: %0.0f %%\n", - (float)(100 * info.cur / info.end)); - } else if (ret == 0) { - printf("BlockPull complete\n"); - break; - } else { - fprintf(stderr, "Failed to query block jobs\n"); - break; - } - usleep(100000); - } - - error: - unlink("/var/lib/libvirt/images/backing.qed"); - unlink("/var/lib/libvirt/images/example.qed"); - if (dom != NULL) { - virDomainDestroy(dom); - virDomainFree(dom); - } - if (conn != NULL) - virConnectClose(conn); - return 0; - }]]> - </programlisting> + <example> + <title>Get domain's input device information</title> + <programlisting language="Python"><xi:include href="extras/Domains-Example-42.py" parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" /></programlisting> + </example>
</section>
diff --git a/en-US/extras/Domains-Example-42.py b/en-US/extras/Domains-Example-42.py new file mode 100644 index 0000000..eca7866 --- /dev/null +++ b/en-US/extras/Domains-Example-42.py @@ -0,0 +1,75 @@ +# Example-40.py +from __future__ import print_function +import sys +import libvirt + + +domxml = + """<domain type='kvm'> + <name>example</name> + <memory>131072</memory> + <vcpu>1</vcpu> + <os> + <type arch='x86_64' machine='pc-0.13'>hvm</type> + </os> + <devices> + <disk type='file' device='disk'> + <driver name='qemu' type='qed'/> + <source file='/var/lib/libvirt/images/example.qed' /> + <target dev='vda' bus='virtio'/> + </disk> + </devices> + </domain>""" + +def do_cmd (cmdline): + status = os.system(cmdline) + if status < 0: + return -1 + return WEXITSTATUS(status) + +def make_domain (conn) + do_cmd("qemu-img create -f raw /var/lib/libvirt/images/backing.qed 100M") + do_cmd("qemu-img create -f qed -b /var/lib/libvirt/images/backing.qed"+ + "/var/lib/libvirt/images/example.qed") + dom = conn.createXML(domxml, 0) + return dom + + +virConnectPtr conn +dom = None +disk = "/var/lib/libvirt/images/example.qed" + +conn = libvirt.open('qemu:///system') +if conn == None: + print('Failed to open connection to qemu:///system', file=sys.stderr) + exit(1) + +dom = make_domain(conn) +if dom == None: + print("Failed to create domain", file=sys.stderr) + exit(1) + +if dom.blockPull(disk, 0, 0) < 0: + print("Failed to start block pull", file=sys.stderr) + exit(1) + +while (1): + info = dom.blockJobInfo(disk, 0); + if (info != None: + print("BlockPull progress: %0.0f %%", + float(100 * info.cur / info.end)) + elif info.cur == info.end): + printf("BlockPull complete") + break + else: + print("Failed to query block jobs", file=os.stderr) + break + time.sleep(1) + +os.unlink("/var/lib/libvirt/images/backing.qed") +os.unlink("/var/lib/libvirt/images/example.qed") +if dom != NULL: + conn.destroy(dom) + +conn.close() +exit(0)