Product SiteDocumentation Site

4.8. Live Configuration Change

4.8.1. Block Device Jobs

Libvirt provides a generic Block Job methods that can be used to initiate and manage operations on disks that belong to a domain. Jobs are started by calling the function associated with the desired operation (eg. blockPull). Once started, all block jobs are managed in the same manner. They can be aborted, throttled, and queried. Upon completion, an asynchronous event is issued to indicate the final status.
The following block jobs can be started:
  1. blockPull() starts a block pull operation for the specified disk. This operation is valid only for specially configured disks. blockPull will populate a disk image with data from its backing image. Once all data from its backing image has been pulled, the disk no longer depends on a backing image.
A disk can be queried for active block jobs by using blockJobInfo. If found, job information is reported in a structure that contains: the job type, bandwidth throttling setting, and progress information.
virDomainBlockJobAbort() can be used to cancel the active block job on the specified disk.
Use blockJobSetSpeed() to limit the amount of bandwidth that a block job may consume. Bandwidth is specified in units of MB/sec.

Example 4.53. Get domain's input device information

# Example-40.py
#!/usr/bin/env python3
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 = None
try:
    conn = libvirt.open("qemu:///system")
except libvirt.libvirtError as e:
    print(repr(e), 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)