Changing XML of virtual machine

From ISPWiki
Jump to: navigation, search

If you need to delete an xml document that is passed to libvirt while creating or editing a virtual machine, you can use our handler that is processed on the last step before the xml is sent to libvirt. You can add a plug-in and edit the xml description according to your needs.

The handler name is editxml

Input parameters:

  • vmi - OS template name (for version 5.22 and later)
  • hostnode - ID of the cluster node where the virtual machine is located

XML for libvirt is located in the /doc/outxml tag

VMmanager also read the xml from the /doc/outxml tag

Handler example

Add the pm (Power management) tags into the XML

   <suspend-to-disk enabled='yes'/>
   <suspend-to-mem enabled='yes'/>  

For OS template "SlitazOs" find all virtual disks with the "block" blcok type and change caching into "writeback"


Create a description file for your handler and put it into the /usr/local/mgr5/etc/xml directory

The /usr/local/mgr5/etc/xml/vmmgr_mod_pm.xml file:

    <handler name="" type="xml">
      <event name="editxml" after="yes"/>

We will also need the script, which should locate in /usr/local/mgr5/addon

The /usr/local/mgr5/addon/ file

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import xml.etree.ElementTree as ET

# Contents of  VMmanager session in the form of an XML-filr
sesXmlContent =
ses = ET.fromstring(sesXmlContent)
# Get an OS template name (starting from version 5.22)
vmi = ses.find("./vmi").text
# Get a custer node's ID (starting from version 5.22)
hostNodeId = ses.find("./hostnode").text
# Original xml of a virtual machine
domainXmlString = ses.find("./outxml").text
domainXml = ET.fromstring(domainXmlString)
# Get a VM name
domainName = domainXml.find("./name").text
# Edit the xml only for SlitazOs
if vmi == "SlitazOs":
    # Add the <pm> (Power Management) tag
    pm = ET.SubElement(domainXml, "pm")
    suspendToDisk = ET.SubElement(pm, "suspend-to-disk", {"enabled": "yes"})
    suspendToMem = ET.SubElement(pm, "suspend-to-mem", {"enabled": "yes"})
    # Search for all connected disks
    disks = domainXml.findall("./devices/disk")
    for diskNode in disks:
        # Get a type of the virtual disk  (file, network, block, volume)
        diskType = diskNode.get("type")
        # Edit only for  "block"
        if diskType == "block":
            # Set the caching type  "writeback"
            driver = diskNode.find("driver")
            driver.set("cache", "writeback")

# Generate an output XML
newDomContent = ET.tostring(domainXml, "UTF-8")
ses.find("./outxml").text = newDomContent
# XML output 
sys.stdout.write(ET.tostring(ses, "UTF-8"))

For the /usr/local/mgr5/addon/ file you need to set start permissions:

chmod +x /usr/local/mgr5/addon/

Restart VMmanager:

killall core

You will see the following record in the vmmgr.log log

Dec  9 06:48:45 [15118:1] action EXTINFO Register event '' for action 'editxml'

This add-on is ready to use. Now, when you edit a virtual machine, our add-on will process and modify its XML.

In order to regenerate XML of the virtual machine without editing it in the control panel, execute the following request:

/usr/local/mgr5/sbin/mgrctl -m vmmgr vm.redefine elid=<VM_ID>