Guest_Domains.xml 47.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?xml version='1.0'?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
	  ]>

<chapter id="Application_Development_Guide-Guest_Domains">
  <title>Guest Domains</title>
  <section id="Application_Development_Guide-Guest_Domains-Overview">
    <title>Domain overview</title>

    <para>
11
      A domain is an instance of an operating system running on a virtualized machine.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      A guest domain can refer to either a running virtual machine or a configuration
      which can be used to launch a virtual machine. The connection object provides APIs
      to enumerate the guest domains, create new guest domains and manage existing domains.
      A guest domain is represented with the <literal>virDomainPtr</literal> object and
      has a number of unique identifiers:
    </para>

    <itemizedlist>
      <title>Unique identifiers</title>
      <listitem>
        <para>
          <application>ID</application>: positive integer, unique amongst running guest
	  domains on a single host. An inactive domain does not have an ID. If the host
	  OS is a virtual domain, it is given a ID of zero by default. For example, with
	  the Xen hypervisor, <literal>Dom0</literal> indicates a guest domain. Other
	  domain IDs will be allocated starting at 1, and incrementing each time a new
	  domain starts. Typically domain IDs will not be re-used until the entire ID
	  space wraps around. The domain ID space is at least 16 bits in size, but often
	  extends to 32 bits.
        </para>
      </listitem>
      <listitem>
        <para>
          <application>name</application>: short string, unique amongst all guest domains on a single host,
          both running and inactive. For maximum portability between hypervisors
          applications should only rely on being able to use the characters
          <literal>a-Z,0-9,-,_</literal> in names. Many hypervisors will store
          inactive domain configurations as files on disk, based on the domain
          name.
        </para>
      </listitem>
      <listitem>
        <para>
          <application>UUID</application>: 16 unsigned bytes, guaranteed to be unique amongst all guest
          domains on any host. RFC 4122 defines the format for UUIDs and provides
          a recommended algorithm for generating UUIDs with guaranteed uniqueness.
          If the host OS is itself a virtual domain, then by convention it
          will be given a UUID of all zeros. This is the case with the Xen
          hypervisor, where <literal>Dom0</literal> is a guest domain itself.
        </para>
      </listitem>
    </itemizedlist>

    <para>
      A guest domain may be transient, or persistent. A transient guest domain
      can only be managed while it is running on the host and, when powered off,
58
      all traces of it will disappear. A persistent guest domain has its configuration
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      maintained in a data store on the host by the hypervisor, in an implementation
      defined format. Thus when a persistent guest is powered off, it is still
      possible to manage its inactive config. A transient guest can be turned into
      a persistent guest on the fly by defining a configuration for it.
    </para>

    <para>
      Once an application has a unique identifier for a domain, it will
      often want to obtain the corresponding <literal>virDomainPtr</literal>
      object. There are three, imaginatively named, methods to do lookup
      existing domains, <literal>virDomainLookupByID</literal>,
      <literal>virDomainLookupByName</literal> and
      <literal>virDomainLookupByUUID</literal>. Each of these takes
      a connection object as first parameter, and the domain identifier
      as the other. They will return NULL if no matching domain exists.
      The connection's error object can be queried to find specific
      details of the error if required.
    </para>

    <example>
      <title>Fetching a domain object from an ID</title>
      <programlisting>
81 82
int domainID = 6;
virDomainPtr dom;
83

84
dom = virDomainLookupByID(conn, domainID);
85 86 87 88 89 90
      </programlisting>
    </example>

    <example>
      <title>Fetching a domain object from an name</title>
      <programlisting>
91
char *domainName = "someguest";
92
virDomainPtr dom;
93

94
dom = virDomainLookupByName(conn, domainName);
95 96 97 98 99 100
      </programlisting>
    </example>

    <example>
      <title>Fetching a domain object from an UUID</title>
      <programlisting>
101 102
char *domainUUID = "00311636-7767-71d2-e94a-26e7b8bad250";
virDomainPtr dom;
103

104
dom = virDomainLookupByUUIDString(conn, domainUUID);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      </programlisting>
    </example>

    <para>
      For convenience of this document, the UUID example used the
      printable format of UUID. There is an equivalent method
      which accepts the raw bytes <literal>unsigned char[]</literal>
    </para>
  </section>

  <section id="Application_Development_Guide-Guest_Domains-Listing">
    <title>Listing domains</title>

    <para>
      The libvirt API exposes two lists of domains, the first
      contains running domains, while the second contains
      inactive, persistent domains. The lists are intended to
      be non-overlapping, exclusive sets, though there is always
      a small possibility that a domain can stop or start in
      between the querying of each set. The events API described
      later in this section provides a way to track all lifecycle
      changes avoiding this potential race condition.
    </para>

    <para>
      The API for listing active domains, returns a list of domain
      IDs. Every running domain has a positive integer ID, uniquely
      identifying it amongst all running domains on the host. The
      API for listing active domains, <literal>virConnectListDomains</literal>,
      requires the caller to pass in a pre-allocated <literal>int</literal>
      array which will be filled in domain IDs. The return value will
      be -1 upon error, or the total number of array elements filled.
      To determine how large to make the ID array, the application can
      use the API call <literal>virConnectNumOfDomains</literal>.
      Putting these two calls together, a fragment of code which
      prints a list running domain IDs would be
    </para>

    <example>
      <title>Listing active domains</title>
      <programlisting>
146 147 148
int i;
int numDomains;
int *activeDomains;
149

150
numDomains = virConnectNumOfDomains(conn);
151

152 153
activeDomains = malloc(sizeof(int) * numDomains);
numDomains = virConnectListDomains(conn, activeDomains, numDomains);
154

155 156 157 158 159
printf("Active domain IDs:\n");
for (i = 0 ; i &lt; numDomains ; i++) {
    printf("  %d\n", activeDomains[i]);
}
free(activeDomains);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      </programlisting>
      <!-- XXX we should include cross-ref to equivalent
           code snippet in appendix for python, perl, java -->
    </example>


    <para>
      In addition to the running domains, there may be some persistent
      inactive domain configurations stored on the host. Since an inactive
      domain does not have any ID identifier, the listing of inactive
      domains is exposed as a list of name strings. In a similar style
      to the API just discussed, the <literal>virConnectListDefinedDomains</literal>
      API requires the caller to provide a pre-allocated
      <literal>char *</literal>  array which will be filled with domain
      name strings. The return value will be -1 upon error, or the total
      number of array elements filled with names. It is the caller's
      responsibility to free the memory associated with each returned
      name. As you might expect, there is also a <literal>virConnectNumOfDefinedDomains</literal>
      API to determine how many names are known. Putting these calls
      together, a fragment of code which prints a list of inactive
      persistent domain names would be:
    </para>

    <example>
      <title>Listing inactive domains</title>
      <programlisting>
186 187 188
int i;
int numDomains;
char **inactiveDomains;
189

190
numDomains = virConnectNumOfDefinedDomains(conn);
191

192
inactiveDomains = malloc(sizeof(char *) * numDomains);
193
numDomains = virConnectListDefinedDomains(conn, inactiveDomains, numDomains);
194

195 196 197 198 199 200
printf("Inactive domain names:\n");
for (i = 0 ; i &lt; numDomains ; i++) {
    printf("  %s\n", inactiveDomains[i]);
    free(inactiveDomains[i]);
}
free(inactiveDomains);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      </programlisting>
      <!-- XXX we should include cross-ref to equivalent
           code snippet in appendix for python, perl, java -->
    </example>

    <para>
      The APIs for listing domains do not directly return the full
      <literal>virDomainPtr</literal> objects, since this may
      incur undue performance penalty for applications which wish
      to query the list of domains on a frequent basis. Given a
      domain ID or name, obtaining a full <literal>virDomainPtr</literal>
      object is a straightforward matter of calling one of the
      <literal>virDomainLookupBy{Name,ID}</literal> methods. So
      an example which obtained a <literal>virDomainPtr</literal>
      object for every domain, both active and inactive, would
      be:
    </para>

    <example>
      <title>Fetching all domain objects</title>
      <programlisting>
222
virDomainPtr *allDomains;
223
int numDomains = 0;
224
int numActiveDomains, numInactiveDomains;
225
char **inactiveDomains;
226
int *activeDomains;
227
int i;
228 229 230 231 232

numActiveDomains = virConnectNumOfDomains(conn);
numInactiveDomains = virConnectNumOfDefinedDomains(conn);

allDomains = malloc(sizeof(virDomainPtr) *
233 234 235
    (numActiveDomains + numInactiveDomains));
inactiveDomains = malloc(sizeof(char *) * numInactiveDomains);
activeDomains = malloc(sizeof(int) * numActiveDomains);
236 237

numActiveDomains = virConnectListDomains(conn,
238 239
    activeDomains,
    numActiveDomains);
240
numInactiveDomains = virConnectListDefinedDomains(conn,
241 242
    inactiveDomains,
    numInactiveDomains);
243 244

for (i = 0 ; i &lt; numActiveDomains ; i++) {
245 246
    allDomains[numDomains] = virDomainLookupByID(conn, activeDomains[i]);
    numDomains++;
247 248 249
}

for (i = 0 ; i &lt; numInactiveDomains ; i++) {
250
    allDomains[numDomains] = virDomainLookupByName(conn, inactiveDomains[i]);
251
    free(inactiveDomains[i]);
252
    numDomains++;
253 254 255
}
free(activeDomains);
free(inactiveDomains);
256 257 258 259 260 261 262 263
      </programlisting>
    </example>
  </section>

  <section id="Application_Development_Guide-Guest_Domains-Lifecycle">
    <title>Lifecycle control</title>

    <para>
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	libvirt can control the entire lifecycle of guest domains. Guest domains can transition through several states throughout their lifecycle:
    </para>
    <orderedlist>
      <listitem>
        <para>
          <literal>Undefined</literal>. This is the baseline state. An undefined guest domain has not been defined or created in any way.
        </para>
      </listitem>
      <listitem>
        <para>
          <literal>Defined</literal>. A defined guest domain has been defined but is not running. This state could also be described as <literal>Stopped</literal>.
        </para>
      </listitem>
      <listitem>
        <para>
          <literal>Running</literal>. A running guest domain is defined and being executed on a hypervisor.
        </para>
      </listitem>
      <listitem>
        <para>
          <literal>Paused</literal>. A paused guest domain is in a suspended state from the <literal>Running</literal> state. Its memory image has been temporarily stored, and it can be resumed to the <literal>Running</literal> state without the guest domain operating system being aware it was ever suspended.
        </para>
      </listitem>
      <listitem>
        <para>
          <literal>Saved</literal>. A saved domain has had its memory image, as captured in the <literal>Paused</literal> state, saved to persistent storage. It can be restored to the <literal>Running</literal> state without the guest domain operating system being aware it was ever suspended.
        </para>
      </listitem>
    </orderedlist>
    <para>
      The transitions between these states fall into several categories: <xref linkend="Application_Development_Guide-Lifecycle-Provisioning" />, <xref linkend="Application_Development_Guide-Lifecycle-Save" />, <xref linkend="Application_Development_Guide-Lifecycle-Migration" /> and <xref linkend="Application_Development_Guide-Lifecycle-Autostart" />.
295
    </para>
296 297 298 299 300 301 302 303
    <figure id="guest_domain_state_transition">
      <title>Guest domain lifecycle</title>
      <mediaobject>
        <imageobject>
          <imagedata fileref="images/guest-state-transition.png" format="PNG" />
        </imageobject>
      </mediaobject>
    </figure>
304 305

    <section id="Application_Development_Guide-Lifecycle-Provisioning">
306
      <title>Provisioning and starting</title>
307 308 309 310 311 312 313 314

      <para>
	Provisioning refers to the task of creating new guest domains,
	typically using some form of operating system installation
	media. There are a wide variety of ways in which a guest can
	be provisioned, but the choices available will vary according
	to the hypervisor and type of guest domain being provisioned.
	It is not uncommon for an application to support several
315
	different provisioning methods. Starting refers to executing a provisioned guest domain on a hypervisor.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
      </para>

      <section id="Application_Development_Guide-Lifecycle-Provisioning-apis">
        <title>APIs for provisioning</title>

	<para>
	  There are up to three APIs involved in provisioning guests.
	  The <literal>virDomainCreateXML</literal> command will create
	  and immediately boot a new transient guest domain. When this
	  guest domain shuts down, all trace of it will disappear. The
	  <literal>virDomainDefineXML</literal> command will store the
	  configuration for a persistent guest domain. The <literal>virDomainCreate</literal>
	  command will boot a previously defined guest domain
	  from its persistent configuration. One important thing to
	  note, is that the <literal>virDomainDefineXML</literal> command
	  can be used to turn a previously booted transient guest domain,
	  into a persistent domain. This can be useful for some provisioning
	  scenarios that will be illustrated later.
	</para>

	<section id="Application_Development_Guide-Lifecycle-Provisioning-apis-transient">
	  <title>Booting a transient guest domain</title>

	  <para>
	    To boot a transient guest domain, simply requires a connection to
	    libvirt and a string containing the XML document describing the
	    required guest configuration. The following example assumes that
	    <literal>conn</literal> is an instance of the <literal>virConnectPtr</literal>
	    object.
	  </para>

	  <programlisting>
	    <![CDATA[
virDomainPtr dom;
const char *xmlconfig = "<domain>........</domain>";

dom = virConnectCreateXML(conn, xmlconfig, 0);

if (!dom) {
    fprintf(stderr, "Domain creation failed");
    return;
}

fprintf(stderr, "Guest %s has booted", virDomainName(dom));
virDomainFree(dom);
]]>
	  </programlisting>

	  <para>
	    If the domain creation attempt succeeded, then the returned
	    <literal>virDomainPtr</literal> will be a handle to the guest
	    domain. This must be released later when no longer needed by
	    using the <literal>virDomainFree</literal> method. Although
	    the domain was booted successfully, this does not guarantee
	    that the domain is still running. It is entirely possible for
	    the guest domain to crash, in which case attempts to use the
	    returned <literal>virDomainPtr</literal> object will generate
	    an error, since transient guests cease to exist when they
	    shutdown (whether a planned shutdown, or a crash). To cope
	    with this scenario requires use of a persistent guest.
	  </para>

	</section>

	<section id="Application_Development_Guide-Lifecycle-Provisioning-apis-persistent">
	  <title>Defining and booting a persistent guest domain</title>

	  <para>
	    Before a persistent domain can be booted, it must have its configuration
	    defined. This again requires a connection to libvirt and a string containing
	    the XML document describing the required guest configuration. The
	    <literal>virDomainPtr</literal> object obtained from defining the guest,
	    can then be used to boot it. The following example assumes that
	    <literal>conn</literal> is an instance of the <literal>virConnectPtr</literal>
	    object.
	  </para>

	  <programlisting>
	    <![CDATA[
virDomainPtr dom;
const char *xmlconfig = "<domain>........</domain>";

dom = virConnectDefineXML(conn, xmlconfig, 0);

if (!dom) {
    fprintf(stderr, "Domain definition failed");
    return;
}

if (virDomainCreate(dom) < 0) {
    virDomainFree(dom);
    fprintf(stderr, "Cannot boot guest");
    return;
}

fprintf(stderr, "Guest %s has booted", virDomainName(dom));
virDomainFree(dom);
]]>
	  </programlisting>

	</section>

      </section>

      <section id="Application_Development_Guide-Lifecycle-Provisioning-config">
        <title>New guest provisioning techniques</title>

	<para>
	  This section will first illustrate two configurations that
	  allow for a provisioning approach that is comparable to those
	  used for physical machines. It then outlines a third option
	  which is specific to virtualized hardware, but has some
	  interesting benefits. For the purposes of illustration, the
	  examples that follow will use a XML configuration that sets
	  up a KVM fully virtualized guest, with a single disk and
	  network interface and a video card using VNC for display.
	</para>

	<programlisting>
	  <![CDATA[
<domain type='kvm'>
  <name>demo</name>
  <uuid>c7a5fdbd-cdaf-9455-926a-d65c16db1809</uuid>
  <memory>500000</memory>
  <vcpu>1</vcpu>
  .... the <os> block will vary per approach ...
    <clock offset='utc'/>
    <on_poweroff>destroy</on_poweroff>
    <on_reboot>restart</on_reboot>
    <on_crash>destroy</on_crash>
    <devices>
      <emulator>/usr/bin/qemu-kvm</emulator>
      <disk type='file' device='disk'>
	<source file='/var/lib/libvirt/images/demo.img'/>
	<driver name='qemu' type='raw'/>
	<target dev='hda'/>
      </disk>
      <interface type='bridge'>
	<mac address='52:54:00:d8:65:c9'/>
	<source bridge='br0'/>
      </interface>
      <input type='mouse' bus='ps2'/>
      <graphics type='vnc' port='-1' listen='127.0.0.1'/>
    </devices>
  </domain>
  ]]>
	</programlisting>
463 464 465
	<important>
	 <para>
	  Be careful in the choice of initial memory allocation, since
466 467 468 469
	  too low a value may cause mysterious crashes and installation
	  failures. Some operating systems need as much as 600 MB of memory
	  for initial installation, though this can often be reduced
	  post-install.
470 471
	 </para>
	</important>
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

        <section id="Application_Development_Guide-Lifecycle-Provisioning-ISO">
	  <title>CDROM/ISO image provisioning</title>

	  <para>
	    All full virtualization technologies have support for emulating
	    a CDROM device in a guest domain, making this an obvious choice
	    for provisioning new guest domains. It is, however, fairly rare
	    to find a hypervisor which provides CDROM devices for paravirtualized
	    guests.
	  </para>

	  <para>
	    The first obvious change required to the XML configuration to
	    support CDROM installation, is to add a CDROM device. A guest
	    domains' CDROM device can be pointed to either a host CDROM
	    device, or to a ISO image file. The next change is to determine
	    what the BIOS boot order should be, with there being two
	    possible options. If the hard disk is listed ahead of the
	    CDROM device, then the CDROM media won't be booted unless
	    the first boot sector on the hard disk is blank. If the
	    CDROM device is listed ahead of the hard disk, then it will
	    be necessary to alter the guest config after install to
	    make it boot off the installed disk. While both can be made
	    to work, the first option is easiest to implement.
	  </para>

	  <para>
	    The guest configuration shown earlier would have the following
	    XML chunk inserted:
	  </para>

	  <programlisting>
	    <![CDATA[
<os>
  <type arch='x86_64' machine='pc'>hvm</type>
  <boot dev='hd'/>
  <boot dev='cdrom'/>
</os>
]]>
	  </programlisting>

	  <para>
	    NB, this assumes the hard disk boot sector is blank initially,
	    so that the first boot attempt falls through to the CD-ROM drive.
	    It will also need a CD-ROM drive device added
	  </para>

	  <programlisting>
	    <![CDATA[
<disk type='file' device='cdrom'>
  <source file='/var/lib/libvirt/images/rhel5-x86_64-dvd.iso'/>
  <target dev='hdc' bus='ide'/>
</disk>
]]>
	  </programlisting>

	  <para>
	    With the configuration determined, it is now possible
	    to provision the guest. This is an easy process, simply
	    requiring a persistent guest to be defined, and then
	    booted.
	  </para>

	  <programlisting>
	    <![CDATA[
const char *xml = "<domain>....</domain>";
virDomainPtr dom;

dom = virDomainDefineXML(conn, xml);
if (!dom) {
    fprintf(stderr, "Unable to define persistent guest configuration");
    return;
}

if (virDomainCreate(dom) < 0) {
    fprintf(stderr, "Unable to boot guest configuration");
}
]]>
	  </programlisting>

	  <para>
	    If it was not possible to guarantee that the boot
	    sector of the hard disk is blank, then provisioning
	    would have been a two step process. First a transient
	    guest would have been booted using CD-ROM drive as the
	    primary boot device. Once that completed, then
	    a persistent configuration for the guest would be
	    defined to boot off the hard disk.
	  </para>

	</section>

        <section id="Application_Development_Guide-Lifecycle-Provisioning-PXE">
	  <title>PXE boot provisioning</title>

	  <para>
	    Some newer full virtualization technologies provide a BIOS that
	    is able to use the PXE boot protocol to boot of the network. If
	    an environment already has a PXE boot provisioning server deployed,
	    this is a desirable method to use for guest domains.
	  </para>

	  <para>
	    PXE booting a guest obviously requires that the guest has a
	    network device configured. The LAN that this network card is
	    attached to, also needs a PXE / TFTP server available.
	    The next change is to determine
	    what the BIOS boot order should be, with there being two
	    possible options. If the hard disk is listed ahead of the
	    network device, then the network card won't PXE boot unless
	    the first boot sector on the hard disk is blank. If the
	    network device is listed ahead of the hard disk, then it will
	    be necessary to alter the guest config after install to
	    make it boot off the installed disk. While both can be made
	    to work, the first option is easiest to implement.
	  </para>

	  <para>
	    The guest configuration shown earlier would have the following
	    XML chunk inserted:
	  </para>

	  <programlisting>
	    <![CDATA[
<os>
  <type arch='x86_64' machine='pc'>hvm</type>
  <boot dev='hd'/>
  <boot dev='network'/>
</os>
]]>
	  </programlisting>

	  <para>
	    NB, this assumes the hard disk boot sector is blank initially,
	    so that the first boot attempt falls through to the NIC.
	    With the configuration determined, it is now possible
	    to provision the guest. This is an easy process, simply
	    requiring a persistent guest to be defined, and then
	    booted.
	  </para>

	  <programlisting>
	    <![CDATA[
const char *xml = "<domain>....</domain>";
virDomainPtr dom;

dom = virDomainDefineXML(conn, xml);
if (!dom) {
    fprintf(stderr, "Unable to define persistent guest configuration");
    return;
}

if (virDomainCreate(dom) < 0) {
    fprintf(stderr, "Unable to boot guest configuration");
}
]]>
	  </programlisting>

	  <para>
	    If it was not possible to guarantee that the boot
	    sector of the hard disk is blank, then provisioning
	    would have been a two step process. First a transient
	    guest would have been booted using network as the
	    primary boot device. Once that completed, then
	    a persistent configuration for the guest would be
	    defined to boot off the hard disk.
	  </para>
	</section>

        <section id="Application_Development_Guide-Lifecycle-Provisioning-Kernel">
	  <title>Direct kernel boot provisioning</title>

	  <para>
	    Paravirtualization technologies emulate a fairly restrictive
	    set of hardware, often making it impossible to use the provisioning
	    options just outlined. For such scenarios it is often possible to
	    boot a new guest domain directly from an kernel and initrd image
	    stored on the host file system. This has one interesting advantage,
	    which is that it is possible to directly set kernel command line
	    boot arguments, making it very easy to do fully automated
	    installation. This advantage can be compelling enough that this
	    technique is used even for fully virtualized guest domains with
	    CD-ROM drive/PXE support.
	  </para>

	  <para>
	    The one complication with direct kernel booting is that provisioning
	    becomes a two step process. For the first step, it is necessary to
	    configure the guest XML configuration to point to a kernel/initrd.
	  </para>

	  <programlisting>
	    <![CDATA[
<os>
  <type arch='x86_64' machine='pc'>hvm</type>
  <kernel>/var/lib/libvirt/boot/f11-x86_64-vmlinuz</kernel>
  <initrd>/var/lib/libvirt/boot/f11-x86_64-initrd.img</initrd>
  <cmdline>method=http://download.fedoraproject.org/pub/fedora/linux/releases/11/x86_64/os console=ttyS0 console=tty</cmdline>
</os>
]]>
	  </programlisting>

	  <para>
	    Notice how the kernel command line provides the URL of download
	    site containing the distro install tree matching the kernel/initrd.
	    This allows the installer to automatically download all its resources
	    without prompting the user for install URL. It could also be used to
	    provide a kickstart file for completely unattended installation.
	    Finally, this command line also tells the kernel to activate both
	    the first serial port and the VGA card as consoles, with the latter
	    being the default. Having kernel messages duplicated on the serial
	    port in this manner can be a useful debugging avenue. Of course
	    valid command line arguments vary according to the particular kernel
	    being booted. Consult the kernel vendor/distributor's documentation
	    for valid options.
	  </para>

	  <para>
	    The last XML configuration detail before starting the guest, is to
	    change the 'on_reboot' element action to be 'destroy'. This ensures
	    that when the guest installer finishes and requests a reboot, the
	    guest is instead powered off. This allows the management application
	    to change the configuration to make it boot off, just installed, the
	    hard disk again. The provisioning process can be started now by
	    creating a transient guest with the first XML configuration
	  </para>

	  <programlisting>
	    <![CDATA[
const char *xml = "<domain>....</domain>";
virDomainPtr dom;

dom = virDomainCreateXML(conn, xml);
if (!dom) {
    fprintf(stderr, "Unable to boot transient guest configuration");
    return;
}
]]>
	  </programlisting>

	  <para>
	    Once this guest shuts down, the second phase of the provisioning
	    process can be started. For this phase, the 'OS' element will
	    have the kernel/initrd/cmdline elements removed, and replaced
	    by either a reference to a host side bootloader, or a BIOS
	    boot setup. The former is used for Xen paravirtualized guests,
	    while the latter is used for fully virtualized guests.
	  </para>

	  <para>
	    The phase 2 configuration for a Xen paravirtualized guest
	    would thus look like:
	  </para>

	  <programlisting>
	    <![CDATA[
<bootloader>/usr/bin/pygrub</bootloader>
<os>
  <type arch='x86_64' machine='pc'>xen</type>
</os>
]]>
	  </programlisting>

	  <para>
	    while a fully-virtualized guest would use:
	  </para>

	  <programlisting>
	    <![CDATA[
<bootloader>/usr/bin/pygrub</bootloader>
<os>
  <type arch='x86_64' machine='pc'>hvm</type>
  <boot dev='hd'/>
</os>
]]>
	  </programlisting>

	  <para>
	    With the second phase configuration determined, the guest can
	    be recreated, this time using a persistent configuration
	  </para>

	  <programlisting>
	    <![CDATA[
const char *xml = "<domain>....</domain>";
virDomainPtr dom;

dom = virDomainCreateXML(conn, xml);
if (!dom) {
    fprintf(stderr, "Unable to define persistent guest configuration\n");
    return;
}

if (virDomainCreate(dom) < 0) {
    fprintf(stderr, "Unable to boot persistent guest\n");
    return;
}

fprintf(stderr, "Guest provisoning complete, OS is running\n");
]]>
	  </programlisting>
	</section>
      </section>
    </section>

778 779 780 781 782 783 784 785 786 787 788 789 790
    <section id="Application_Development_Guide-Lifecycle-Stopping">
      <title>Stopping</title>
      <para>
        Stopping refers to the process of halting a running guest. A guest can be stopped by two methods: shutdown and destroy.
      </para>
      <para>
        Shutdown is a clean stop process, which sends a signal to the guest domain operating system asking it to shut down immediately. The guest will only be stopped once the operating system has successfuly shut down. The shutdown process is analagous to running a shutdown command on a physical machine.
      </para>
      <para>
        Destroy immediately terminates the guest domain. The destroy process is analogous to pulling the plug on a physical machine.
      </para>
    </section>

791
    <section id="Application_Development_Guide-Lifecycle-Save">
792
      <title>Suspend / Resume and Save / Restore</title>
793

794 795 796
      <para>
        Suspend and resume refers to the process of taking a running guest and temporarily saving its memory state. At a later time, it is possible to resume the guest to its original running state, contiuining execution where it left off. Suspend does not save a persistent image of the guest's memory. For this, save is used.
      </para>
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      <para>
        Save and restore refers to the process of taking a running guest
	and saving its memory state to a file. At some time later, it
	is possible to restore the guest to its original running state,
	continuing execution where it left off.
      </para>

      <para>
	It is important to note that the save/restore APIs only save the
	memory state, no storage state is preserved. Thus when the guest
	is restored, the underlying guest storage must be in exactly the
	same state as it was when the guest was initially saved. For
        basic usage this implies that a guest can only be restored once
	from any given saved state image. To allow a guest to be restored
	from the same saved state multiple times, the application must
	also have taken a snapshot of the guest storage at time of saving,
	and explicitly revert to this storage snapshot when restoring.
	A future API enhancement in libvirt will allow for an automated
	snapshot capability which saves memory and storage state in
	one operation.
      </para>

      <para>
	The save operation requires the fully qualified path to a file
	in which the guest memory state will be saved. This filename
	is in the hypervisor's file system, not the libvirt client
	application's. There's no difference between the two if managing
	a local hypervisor, but it is critically important if connecting
	remotely to a hypervisor across the network. The example that
	follows demonstrates saving a guest called 'demo-guest' to a
	file. It checks to verify that the guest is running before
	saving, though this is technically redundant since the
	hypervisor driver will do such a check itself.
      </para>

      <programlisting>
	<![CDATA[
virDomainPtr dom;
virDomainInfoPtr info;
const char *filename = "/var/lib/libvirt/save/demo-guest.img";

dom = virDomainLookupByName(conn, "demo-guest");
if (!dom) {
    fprintf(stderr, "Cannot find guest to be saved");
    return;
}

if (virDomainGetInfo(dom, &amp;info) < 0) {
    fprintf(stderr, "Cannot check guest state");
    return;
}

if (info.state == VIR_DOMAIN_SHUTOFF) {
    fprintf(stderr, "Not saving guest that isn't running");
    return;
}

if (virDomainSave(dom, filename) < 0) {
    fprintf(stderr, "Unable to save guest to %s", filename);
}

fprintf(stderr, "Guest state saved to %s", filename);
]]>
      </programlisting>

      <para>
	Some period of time later, the saved state file can then be
	used to restart the guest where it left of, using the
	virDomainRestore API. The hypervisor driver will return an
	error if the guest is already running, however, it won't
	prevent attempts to restore from the same state file multiple
	times. As noted earlier, it is the applications' responsibility
	to ensure the guest storage is in exactly the same state as it
	was when the save image was created
      </para>

      <programlisting>
	<![CDATA[
virDomainPtr dom;
int id;
const char *filename = "/var/lib/libvirt/save/demo-guest.img";

if ((id = virDomainRestore(conn, filename)) < 0) {
    fprintf(stderr, "Unable to restore guest from %s", filename);
}

dom = virDomainLookupByID(conn, id);
if (!dom) {
    fprintf(stderr, "Cannot find guest that was restored");
    return;
}

fprintf(stderr, "Guest state restored from %s", filename);
]]>
      </programlisting>

    </section>

    <section id="Application_Development_Guide-Lifecycle-Migration">
      <title>Migration</title>

      <para>
899 900 901 902
        Migration is the process of taking the image of a guest domain and moving it somewhere, typically from a hypervisor on one node to a hypervisor on another node. There are two APIs for migration. The <literal>virDomainMigrate</literal> command takes an established hypervisor connection, and instructs the domain to migrate to this connection. The <literal>virMigrateToUri</literal> command takes a URI specifying a hypervisor connection, opens the connection, then instructions the domain to migrate to this connection. Both these commands can be passed a parameter to specify live migration. For migration to complete successfully, storage needs to be shared between the source and target hypervisors.
      </para>
      <para>
        TODO: Add 2 cold examples, 1 live example.
903 904 905 906 907 908 909 910
      </para>

    </section>

    <section id="Application_Development_Guide-Lifecycle-Autostart">
      <title>Autostart</title>

      <para>
911 912 913 914
        A guest domain can be configured to autostart on a particular hypervisor, either by the hypervisor itself or libvirt. In combination with managed save, this allows the operating system on a guest domain to withstand host reboots without ever considering itself to have rebooted. When libvirt restarts, the guest domain will be automatically restored. This is handled by an API separate to regular save and restore, because paths must be known to libvirt without user input.
      </para>
      <para>
        TODO: code example.
915 916 917 918 919
      </para>
    </section>

  </section>

920 921
  <section id="Application_Development_Guide-Guest_Domains-Domain_Config">
    <title>Domain configuration</title>
922 923

    <para>
924
      Domains are defined in libvirt using XML. Everything related only to the domain, such as memory and CPU, is defined in the domain XML. The domain XML format is specified at <ulink url="http://libvirt.org/formatdomain.html">http://libvirt.org/formatdomain.html</ulink>. This can be accessed locally in <filename>/usr/share/doc/libvirt-devel-version/</filename> if your system has the <package>libvirt-devel</package> package installed.
925 926
    </para>

927 928
    <section id="Application_Development_Guide-Domain_Config-Boot">
      <title>Boot modes</title>
929 930 931 932 933 934 935 936

      <para>
        TBD

      </para>

    </section>

937 938
    <section id="Application_Development_Guide-Domain_Config-Memory_CPU">
      <title>Memory / CPU resources</title>
939 940

      <para>
941
        TBD. Maps to the basic resources section.
942 943 944 945 946

      </para>

    </section>

947 948
    <section id="Application_Development_Guide-Domain_Config-Lifecycle">
      <title>Lifecycle controls</title>
949 950 951 952 953 954 955 956

      <para>
        TBD

      </para>

    </section>

957 958
    <section id="Application_Development_Guide-Domain_Config-Clock">
      <title>Clock sync</title>
959 960 961 962 963 964 965 966

      <para>
        TBD

      </para>

    </section>

967 968
    <section id="Application_Development_Guide-Domain_Config-Features">
      <title>Features</title>
969 970 971 972 973 974 975 976

      <para>
        TBD

      </para>

    </section>

977 978 979 980 981 982 983 984 985 986 987
  </section>

  <section id="Application_Development_Guide-Guest_Domains-Monitoring">
    <title>Monitoring performance</title>

    <para>
      Statistical metrics are available for monitoring the utilization rates of domains, vCPUs, memory, block devices, and network interfaces.
    </para>

    <section id="Application_Development_Guide-Monitoring-Domain">
      <title>Domain performance</title>
988 989 990 991 992 993 994 995

      <para>
        TBD

      </para>

    </section>

996 997
    <section id="Application_Development_Guide-Monitoring-vCPU">
      <title>vCPU performance</title>
998 999 1000 1001 1002 1003 1004 1005

      <para>
        TBD

      </para>

    </section>

1006 1007
    <section id="Application_Development_Guide-Monitoring-IO_stats">
      <title>I/O statistics</title>
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

      <para>
        TBD

      </para>

    </section>

  </section>

  <section id="Application_Development_Guide-Guest_Domains-Device_Config">
    <title>Device configuration</title>

    <para>
      TBD

    </para>

    <section id="Application_Development_Guide-Device_Config-Emulator">
      <title>Emulator</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-Disks">
      <title>Disks</title>

      <para>
        TBD

      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-Networking">
      <title>Networking</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-Filesystems">
      <title>Filesystems</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-Mice">
      <title>Mice &amp; tablets</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-USB_Pass">
      <title>USB device passthrough</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Device_Config-PCI_Pass">
      <title>PCI device passthrough</title>

      <para>
        The PCI device passthrough capability allows a physical PCI device from
	the host machine to be assigned directly to a guest machine.The guest
	OS drivers can use the device hardware directly without relying on any
	driver capabilities from the host OS.
      </para>

      <para>
	Some caveats apply when using PCI device passthrough. When a PCI device is
	directly assigned to a guest, migration will not be possible, without
	first hot-unplugging the device from the guest. In addition
	libvirt does not guarantee that direct device assignment is secure, leaving
	security policy decisions to the underlying virtualization technology. Secure
	PCI device passthrough typically requires special hardware capabilities, such
	the VT-d feature for Intel chipset, or IOMMU for AMD chipsets.
      </para>

      <para>
	There are two modes in which a PCI device can be attached, "managed" or
	"unmanaged" mode, although at time of writing only KVM supports "managed"
	mode attachment. In managed mode, the configured device will be automatically
	detached from the host OS drivers when the guest is started, and then
	re-attached when the guest shuts down. In unmanaged mode, the device
	must be explicit detached ahead of booting the guest. The guest will
	refuse to start if the device is still attached to the host OS. The
	libvirt 'Node Device' APIs provide a means to detach/reattach PCI devices
	from/to host drivers. Alternatively the host OS may be configured to
	blacklist the PCI devices used for guest, so that they never get attached
	to host OS drivers.
      </para>

      <para>
	In both modes, the virtualization technology will always perform a reset
	on the device before starting a guest, and after the guest shuts down.
	This is critical to ensure isolation between host and guest OS. There
	are a variety of ways in which a PCI device can be reset. Some reset
	techniques are limited in scope to a single device/function, while
	others may affect multiple devices at once. In the latter case, it will
	be necessary to co-assign all affect devices to the same guest,
	otherwise a reset will be impossible to do safely. The node device
	APIs can be used to determine whether a device needs to be co-assigned,
	by manually detaching the device and then attempting to perform the
	reset operation. If this succeeds, then it will be possible to assign
	the device to a guest on its own. If it fails, then it will be necessary
	to co-assign the device will others on the same PCI bus. The section
	documenting node device APIs covers this topic in detail, but as a
	quick demonstration the following code checks whether a PCI device
	(represented by a virNodeDevicePtr object instance) can be reset and
	is thus assignable to a guest
      </para>
      <programlisting>
	<![CDATA[
virNodeDevicePtr dev = ....get virNodeDevicePtr for the PCI device...

if (virNodeDeviceDettach(dev) < 0) {
    fprintf(stderr, "Device cannot be dettached from the host OS drivers\n");
    return;
}

if (virNodeDeviceReset(dev) < 0) {
    fprintf(stderr, "Device cannot be safely reset without affecting other devices\n");
    return;
}

fprintf(stderr, "Device is suitable for passthrough to a guest\n");
]]>
      </programlisting>

      <para>
	A PCI device is attached to a guest using the 'hostdevice' element.
	The 'mode' attribute should always be set to 'subsystem', and the
	'type' attribute to 'pci'. The 'managed' attribute can be either
	'yes' or 'no' as required by the application. Within the 'hostdevice'
	element there is a 'source' element and within that a further 'address'
	element is used to specify the PCI device to be attached. The address
	element expects attributes for 'domain', 'bus', 'slot' and 'function'.
	This is easiest to see with a short example
      </para>

      <programlisting>
	<![CDATA[
<hostdev mode='subsystem' type='pci' managed='yes'>
  <source>
    <address domain='0x0000'
             bus='0x06'
             slot='0x12'
             function='0x5'/>
  </source>
</hostdev>
]]>
      </programlisting>

    </section>

  </section>

  <section id="Application_Development_Guide-Guest_Domains-Live_Config">
    <title>Live configuration change</title>

    <para>
      TBD
    </para>

    <section id="Application_Development_Guide-Live_Config-Memory">
      <title>Memory ballooning</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Live_Config-CPU">
      <title>CPU hotplug</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Live_Config-Device_Plug">
      <title>Device hotplug / unplug</title>

      <para>
        TBD
      </para>

    </section>

    <section id="Application_Development_Guide-Live_Config-Device_Media">
      <title>Device media change</title>

      <para>
        TBD
      </para>

    </section>

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    <section id="Application_Development_Guide-Live_Config-Block_Jobs">
      <title>Block Device Jobs</title>

      <para>
        Libvirt provides a generic Block Job API 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.  <literal>virDomainBlockPull</literal>).  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.
      </para>

      <para>
        The following block jobs can be started:
      </para>
      <orderedlist>
        <listitem>
          <para>
	    <literal>virDomainBlockPull()</literal> 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.
          </para>
        </listitem>
      </orderedlist>

      <para>
        A disk can be queried for active block jobs by using
        <literal>virDomainGetBlockJobInfo()</literal>.  If found, job
        information is reported in a structure that contains: the job type,
        bandwidth throttling setting, and progress information.
      </para>

      <para>
        <literal>virDomainBlockJobAbort()</literal> can be used to cancel the
        active block job on the specified disk.
      </para>

      <para>
        Use <literal>virDomainBlockJobSetSpeed()</literal> to limit the amount
        of bandwidth that a block job may consume.  Bandwidth is specified in
        units of MB/sec.
      </para>

      <para>
        When a block job operation completes, the final status is reported using
        an asynchronous event.  To receive this event, register a
        <literal>virConnectDomainEventBlockJobCallback</literal> function which
        will receive the disk, event type, and status as parameters.
      </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>

    </section>

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
  </section>

  <section id="Application_Development_Guide-Guest_Domains-Security">
    <title>Security model</title>

    <para>
      TBD
    </para>

  </section>

  <section id="Application_Development_Guide-Guest_Domains-Event_Not">
    <title>Event notifications</title>

    <para>
      TBD

    </para>

  </section>

  <section id="Application_Development_Guide-Guest_Domains-Tuning">
    <title>Tuning</title>

    <para>
      TBD

    </para>

    <section id="Application_Development_Guide-Tuning-Schedular">
      <title>Scheduler parameters</title>

      <para>
        TBD

      </para>

    </section>

    <section id="Application_Development_Guide-Tuning-NUMA">
      <title>NUMA placement</title>

      <para>
        TBD

      </para>

    </section>

  </section>

</chapter>