[libvirt] Using Xen config files

Matthew Donovan matthew at atc-nycorp.com
Wed Aug 20 12:35:46 UTC 2008


I am trying to programmatically start Xen guest VMs (HVM) with libvirt (on
Fedora Core 8) and I'm having some trouble with it. I've included my code
below.  Essentially, the domain seems to get created correctly but it
doesn't seem to run.  I don't receive any error messages from the libvirt
functions but the VM does not get any CPU time (as shown with "xm list").  

[root at grape ~]$ xm list
Name                                        ID   Mem VCPUs      State
Time(s)
Domain-0                                     0  1462     2     r-----
1313.5
fc8.conf                                     7   128     1     ------
0.0

Can someone explain what I'm doing wrong here?  I've tried to get this
working with linux and windows guests with the same results.

I've seen references on this mailing list to libvirt's ability to handle Xen
configuration files but I haven't seen anything in the API documentation or
public header files that refer to it.  Is there a public interface to
specifying a configuration file?

Thanks 
-matthew


CODE:
#include <stdio.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>

      //"<!--<mac address='00:16:3e:5d:c7:9e'/>-->"
    //"<graphics type='vnc' port='5904'/>"

const char* fedora = 
"<domain type='xen'>"
"<name>fc8.conf</name>"
"<os>"
    "<type>hvm</type>"
    "<loader>/usr/lib/xen/boot/hvmloader</loader>"
    "<boot dev='hd'/>"
"</os>"
"<memory>1024</memory>"
"<vcpu>1</vcpu>"
"<on_poweroff>destroy</on_poweroff>"
"<on_reboot>restart</on_reboot>"
"<on_crash>restart</on_crash>"
"<features>"
    "<pae/>"
    "<acpi/>"
    "<apic/>"
"</features>"
"<clock sync=\"localtime\"/>"
"<devices>"
    "<emulator>/usr/lib/xen/bin/qemu-dm</emulator>"
    "<interface type='bridge'>"
     "<source bridge='xenbr0'/>"
      "<script path='vif-bridge'/>"
    "</interface>"
    "<disk type='block'>"
      "<source dev='/dev/vgvms/fc8'/>"
      "<target dev='hda'/>"
    "</disk>"
    "<disk type='block' device='cdrom'>"
      "<source dev='/dev/cdrom'/>"
      "<target dev='hdc'/>"
      "<readonly/>"
    "</disk>"
    "<graphics type='vnc'/>"
  "</devices>"
"</domain>";


int main (int argc, char** argv) 
{
    virConnectPtr con;      /* connection to the Xen hypervisor */
    virDomainPtr  domain;

    virInitialize();
    con = virConnectOpen ("xen:///");
    if (!con) {
        virErrorPtr err = virGetLastError ();
        printf ("virConnectOpen failed: %s\n",
                err->message);
        return -1;
    }

    domain = virDomainDefineXML (con, fedora);
    if (!domain) {
        virErrorPtr err = virGetLastError ();
        printf ("virDomainDefineXML failed: %s\n",
                err->message);

    } else {
        
        if (virDomainCreate (domain) < 0) {
            virErrorPtr err = virGetLastError ();
            printf ("virDomainCreate failed: %s\n",
                    err->message);
        } else {
            virDomainInfo info = {0};
            virDomainGetInfo (domain, &info);
            printf ("state = %d\n", info.state);
        }
    }

    virConnectClose (con);
    return 0;
}




More information about the libvir-list mailing list