昨天自己调用API实现了一个xm list功能的函数,今天准备动手写一个函数实现xm create功能,结果只写了一个用XML启动的程序,在xml写法上折腾了一天,下面贴代码:

/* example createvirt.c */
/* compile with: gcc -g -Wall createvirt.c -o createvirt -lvirt */
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>

void closeConn(virConnectPtr conn)
{
    if(conn!=NULL)
        virConnectClose(conn);
}

int main()
{
    virConnectPtr conn=NULL;
    virDomainPtr dom=NULL;
    conn=virConnectOpen("");
    if(conn==NULL)
    {
        fprintf(stderr,"Fail To Connect Hypervisor/n");
        return 1;
    }
    const char *xmlconfig="<domain type='xen' id='3'><name>xp11</name><uuid>4dea22b3-1d52-d8f3-2516-782e98ab3f57</uuid><os><type>hvm</type><loader>/usr/lib/xen/boot/hvmloader</loader><boot dev='hd'/></os><memory>524288</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/lib64/xen/bin/qemu-dm</emulator><interface type='bridge'><source bridge='xenbr0'/><mac address='00:16:3e:5d:c7:9e'/><script path='vif-bridge'/></interface><disk type='file'><source file='/var/lib/xen/images/xp.img' /><target dev='hda'/></disk><disk type='file' device='cdrom'><source file=''/><target dev='hdc'/><readonly/></disk><graphics type='vnc' port='5904'/><serial type='pty'><source path='/dev/pts/3'/><target port='0'/></serial></devices></domain>";

    dom=virDomainDefineXML(conn,xmlconfig);
    if (!dom)
    {
        fprintf(stderr, "Domain definition failed");
        return 0;
    }
    if (virDomainCreate(dom) < 0)
    {
        virDomainFree(dom);
        fprintf(stderr, "Cannot boot guest");
        return 0;
    }
    fprintf(stderr, "Guest %s has booted/n", virDomainGetName(dom));
    virDomainFree(dom);
    return 0;
}

 

 

XML文件内容:

<domain type='xen' id='3'>
  <name>xp</name>
  <uuid>4dea22b3-1d52-d8f3-2516-782e98ab3f12</uuid>
  <os>
    <type>hvm</type>
    <loader>/usr/lib/xen/boot/hvmloader</loader>
    <boot dev='hd'/>
  </os>
  <memory>524288</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/lib64/xen/bin/qemu-dm</emulator>
    <interface type='bridge'>
      <source bridge='xenbr0'/>
      <mac address='00:16:3e:5d:c7:9e'/>
      <script path='vif-bridge'/>
    </interface>
    <disk type='file'>
      <source file='/var/lib/xen/images/xp.img'/>
      <target dev='hda' />
    </disk>
    <disk type='file' device='cdrom'>
      <source file=''/>
      <target dev='hdc'/>
      <readonly/>
    </disk>
    <graphics type='vnc' port='5904'/>
    <serial type='pty'>
      <source path='/dev/pts/3'/>
      <target port='0'/>
    </serial>
  </devices>
</domain>

 

上面文件具体解释见:http://libvirt.org/formatdomain.html#elementsConsole

文件运行后会在/ect/xen下生成一个XM的配置文件。

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐