Libvirt API 1.4

The OpenNebula libvirt implementation lets you use any libvirt application at a distributed level. In a nutshell, you'll be able to use your libvirt XML description files and any libvirt tool, like virsh or virt-manager to connect to OpenNebula. In this way, you can manage and monitor your VMs in a distributed environment using the current libvirt tools. This is, a whole cluster can be managed as any other libvirt node.

For example, you can create your domain with virsh create, then OpenNebula will look for a suitable resource, transfer the VM images and boot your VM using any of the supported hypervisors. The distributed management is completely transparent to the libvirt application.

Requirements

The OpenNebula driver for libvirt has been incorporated in libvirt main distribution, so in order to use it you will need in the OpenNebula front-end:

  • libvirt >= 0.6.5
  • OpenNebula >= 1.2

Configuration

Libvirt needs to be configured (look here for configuration options of libvirt). Afterwards, the libvirtd deaemon has to be started. Also, the oned daemon has to be up and running.

Checking the installation

To verify the installation of “ONE driver” for libvirt, execute “virsh” with the following URI: “one:///” to connect to the local ONE instance. With the installation done and both daemons running (libvirt's and OpenNebula's), you should be able to connect to OpenNebula:

<xterm> $ virsh -c one:/// Welcome to lt-virsh, the virtualization interactive terminal.

Type: 'help' for help with commands

     'quit' to quit

virsh # </xterm>

Usage Examples

The driver allows libvirt to interact with OpenNebula as a virtualization hypervisor, so you can use libvirt applications to manage a distributed infrastructure.

Using virsh

The virsh program is the main interface for managing libvirt guest domains.

Connect to the hypervisor

<xterm> $ virsh -c one:/// … Welcome to lt-virsh, the virtualization interactive terminal. </xterm>

Creating and Monitoring a new VM

<xterm> virsh # create /templ/sgehost2.xml | »> Check the OpenNebula domains «< Domain vm02 created from /templ/sgehost2.xml | virsh # list –all | $>onevm list Id Name State | ID NAME STAT CPU MEM HOSTNAME TIME ———————————- |

1 vm01                 paused              |  32 vm01 susp  0  65636  ursa03    00:20:10
2 vm02                 running             |  33 vm02 runn  2  65636  ursa06    00:01:13

</xterm>

Resume a suspended Domain

<xterm> virsh # resume vm01 | »> Check the OpenNebula domains «< Domain vm01 resumed | virsh # list | $>onevm list Id Name State | ID NAME STAT CPU MEM HOSTNAME TIME ———————————- |

1 vm01                 running             |  32 vm01 boot  0  65636  ursa03    00:22:18
2 vm02                 running             |  33 vm02 runn  2  65636  ursa06    00:03:21

</xterm>

Shutdown Domains

<xterm> virsh # shutdown 2 Domain 2 is being shutdown | »> Check the OpenNebula domains «<

                                           |

virsh # list | $>onevm list Id Name State | ID NAME STAT CPU MEM HOSTNAME TIME ———————————- |

1 vm01                 running             | 32 vm01 runn  6  65636  ursa03    00:23:16
2 vm02                 in shutdown         | 33 vm02 epil  0  65636  ursa06    00:04:19

virsh # list –all | $>onevm list Id Name State | ID NAME STAT CPU MEM HOSTNAME TIME ———————————- |

1 vm01                 running             | 32 vm01 runn  6  65636  ursa03    00:23:20
- vm02                 shut off            |

virsh # </xterm>

Programming Examples

The following examples illustrate how to use the C API >= libvirt-0.6.5 with OpenNebula. This will allow you to develop applications that interacts with OpenNebula as it was a single hypervisor.

Set connection to OpenNebula

virConnectPtr conn;
conn = virConnectOpen("one:%%///%%");

Creating a Domain and getting info

 virDomainPtr dom = NULL;   
 virDomainInfo info;       
 virDomainCreateLinux(conn,XML_template,NULL);
 ...  
/* Find the domain of the given id */
 dom = virDomainLookupByID(conn, id);

/* Get information: */
 ret = virDomainGetInfo(dom, &info); 
 printf("Domain %d CPUs\n",info.nrVirtCpu);
 printf("Domains %d Memory\n", id, info.memory);
 printf("Domains %s Status\n", id, info.status);

Suspending a Virtual Machine

 virDomainPtr dom;
 virDomainInfo ret;
 dom =  virDomainLookupByName(conn, vm_name);
 ret = virDomainGetInfo(dom, &info);

/* Check if the current State allows to shutdown the VM */
 if(info.state == VIR_DOMAIN_RUNNING) 
   virDomainShutdown(dom);
 else
  fprintf(stderr, "Failed to Shutdown %s\n", vm_name);

XML Domain Definition

Libvirt make use of XML format to represent domains, there are some limitations on the attributes that may be specified when interfacing OpenNebula. The following list details the attributes and options supported by the libvirt driver:

General Metadata

  • Name

OS Booting

The booting method should be configured using a “Direct kernel boot” method, as:

<os>
  <type>hvm</type>
  <kernel>/boot/vmlinuz-2.6.24-17-xen</kernel>      
  <initrd>/boot/initrd.img-2.6.24-17-xen</initrd>   
  <cmdline></cmdline>                              
  <root>sda1</root>                                 
</os>

The following options can be define within an os section

  • type, should be set to hvm
  • kernel
  • initrd
  • root
  • cmdline

Basic Resources

  • memory
  • vcpu

Devices

Sample configuration of a VM disk

<disk type='file' device='disk'>
  <source file='/images/sgehosts/01/swap.img'/>
  <target dev='sda2'/>
</disk>

Attributes for a disk section:

  • disk: the type option should be set to file and device to disk
  • source: option file is supported
  • target: the option bus is no supported

Network

The network of the VMs should be configured following the semantics of Bridged networking, or Virtual Network for example:

...  
<interface type='bridge'>
  <source bridge=.../>
  <target dev=.../> 
  <mac address='00:16:3e:01:01:01'/>
</interface>
... 
...  
<interface type='network'>
  <source network='mynetwork'/>
</interface>
... 

The following options can be define within an interface section:

  • type: should be set to bridge or network
  • source : {bridge,network}
  • target: {optional at bridge configuration, not used at network}
  • mac {optional at bridge configuration, not used at network}

XML description example

Following, there is an example “libvirt XML Description” file defining a guest Domain in OpenNebula:

<domain type='one'>
  <name>vm01</name>
  <memory>32768</memory>
  <vcpu>1</vcpu>
  <os>
    <type>hvm</type>
    <cmdline></cmdline>
    <kernel>/boot/vmlinuz-2.6.24-17-xen</kernel>
    <initrd>/boot/initrd.img-2.6.24-17-xen</initrd>
    <root>sda1</root>
  </os>
  <devices>     
    <disk type='file' device='disk'>
      <source file='/images/sgehosts/01/disk.img'/>
      <target dev='sda1'/>
    </disk>
    <disk type='file' device='disk'>
      <source file='/images/sgehosts/01/swap.img'/>
      <target dev='sda2'/>
    </disk>
    <interface type='bridge'>
      <source bridge='eth0'/>
      <target dev='tap0'/> 
      <mac address='00:16:3e:01:01:01'/>
    </interface> 
  </devices>
</domain>

Driver Support

These are the main functions to manage VM supported by ONE driver:

  • virDomainCreate
  • virDomainCreateLinux
  • virDomainGetInfo
  • virDomainSuspend
  • virDomainResume
  • virDomainShutDown

Libvirt API details can be scouted here.

Libvirt calls supported on the OpenNebula driver are listed in the table below:

API Call API call
virClose virDomainGetConnect
virConnectGetVersion virDomainGetUUID
virConnectNumOfDefinedDomains virDomainLookupByName
virDomainCreate virDomainSuspend
virDomainDestroy virInitialize
virDomainGetAutostart virConnectGetURI
virDomainGetInfo virConnectListDomains
virDomainGetName virConnectOpen
virDomainGetVcpus virDomainDefineXML
virDomainLookupByID virDomainFree
virDomainLookupByUUIDString virDomainGetID
virDomainShutdown virDomainGetOSType
virGetVersion virDomainGetUUIDString
virConnectListDefinedDomains virDomainLookupByUUID
virConnectNumOfDomains virDomainResume
virDomainCreateLinux virDomainUndefine