VMmanager Cloud Documentation
en En
es Es
Your feedback is an opportunity for improvement!
Take part in the survey and contribute to the development of the ISPsystem ecosystem.
Take the survey

How to change the XML-description of a virtual machine

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

Task

Add the pm (Power management) tags into the XML

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


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

Solution

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:


<mgrdata>
  <handler name="editxml_pm.py" type="xml">
  <event name="editxml" after="yes"/>
</handler>
</mgrdata>

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

The /usr/local/mgr5/addon/editxml_pm.py 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-file
sesXmlContent = sys.stdin.read()
ses = ET.fromstring(sesXmlContent)
# Retrieve an OS template name 
vmi = ses.find("./vmi").text
# Retrieve the cluster node ID
hostNodeId = ses.find("./hostnode").text
# Original xml of the virtual machine
domainXmlString = ses.find("./outxml").text
domainXml = ET.fromstring(domainXmlString)
# Retrieve the virtual machine name
domainName = domainXml.find("./name").text
#Edit the xml only for SlitazOs
if vmi == "SlitazOs":
    # Add the tag <pm> (Power Management)
    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 virtual disk type  (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 the 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/editxml_pm.py file you need to set start permissions:

chmod +x /usr/local/mgr5/addon/editxml_pm.py

Restart VMmanager:

killall core

You will see the following record in vmmgr.log 

Dec  9 06:48:45 [15118:1] action EXTINFO Register event 'editxml_pm.py' 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>
The article was last updated on 11.27.2018. The article was prepared by technical writers of ISPsystem.