bwsw edited a comment on issue #3839: FEATURE-3823: kvm agent hooks
URL: https://github.com/apache/cloudstack/pull/3839#issuecomment-588851139
 
 
   @andrijapanicsb 
   @svenvogel 
   
   ```groovy
   package groovy
   
   import com.sun.deploy.xml.XMLNode
   import groovy.util.XmlParser
   import groovy.util.Node
   import groovy.xml.XmlUtil
   
   class CacheDeviceAdder {
       def highSpeedDir = "/mnt/nvme0/"
       final def ramGBDivider = 1024
   
       Node diskXmlSpec(String swapFile) {
           return new XmlParser().parseText(
                   "    <disk type='file' device='disk'>\n" +
                   "      <driver name='qemu' type='qcow2' cache='none'/>\n" +
                   "      <source file='$swapFile'/>\n" +
                   "      <target dev='vdb' bus='ide'/>\n" +
                   "      <alias name='ide0-0-1'/>\n" +
                   "      <address type='drive' controller='0' bus='0' 
target='0' unit='1'/>\n" +
                   "    </disk>")
       }
   
       String transform(Object logger, String xml) {
           def vmDef = new XmlParser().parseText(xml)
           // get VM ram amount
           def memory = Integer.parseInt(vmDef.memory.text()) / ramGBDivider
           // get VM ram name
           def name = vmDef.name.text()
           def swapFile = highSpeedDir + name + ".qcow2"
           // remove swap device if exists
           "rm -f ${swapFile}".execute()
           def volCmd = "qemu-img create -f qcow2 ${swapFile} ${memory}G"
           // create new swap device
           volCmd.execute()
           def diskSpec = diskXmlSpec(swapFile)
           // update XML definition
           vmDef.devices[0].append(diskSpec)
           // return new XML definition
           return groovy.xml.XmlUtil.serialize(vmDef)
       }
   
       Object stop(Object logger, String vmName) {
           def swapFile = highSpeedDir + vmName.toString() + ".qcow2"
           // remove unused swap device
           "rm -f ${swapFile}".execute()
       }
   
       Object start(Object logger, String vmName) {
       }
   }
   
   new CacheDeviceAdder()
   ```
   
   This is a very simple hook which utilizes stop and transform cases:
   - When transform is used, it adds a highspeed volume which size is eq to VM 
RAM size, which can be used as a swap.
   - When stop, it removes an unused swap device from FS.
   - Start is not used, no idea how to use it in the current case.
   
   It's very primitive and avoids many checks, but allows getting a general 
idea.
   
   Again, I'll add the documentation, when somebody wants to approve the 
idea... Because, right now there is very simple documentation:
   
   - Configure hook paths and methods in the agent.properties
   - Implement hooks in the form: 
   
   ```groovy
   package groovy
   
   class AnyNameNoMatter {
          String method(Object logger, String xml) {
               return null // for onStart, onStop
               return xml // for xml transformation
         }
   }
   
   new AnyNameNoMatter()
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to