Github user wido commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/977#discussion_r42975057 --- Diff: plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java --- @@ -3388,4 +3419,83 @@ public String mapRbdDevice(final KVMPhysicalDisk disk){ } return device; } + + public List<Ternary<String, Boolean, String>> cleanVMSnapshotMetadata(Domain dm) throws LibvirtException { + s_logger.debug("Cleaning the metadata of vm snapshots of vm " + dm.getName()); + List<Ternary<String, Boolean, String>> vmsnapshots = new ArrayList<Ternary<String, Boolean, String>>(); + String currentSnapshotName = null; + try { + DomainSnapshot snapshotCurrent = dm.snapshotCurrent(); + String snapshotXML = snapshotCurrent.getXMLDesc(); + snapshotCurrent.free(); + DocumentBuilder builder; + try { + builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + + InputSource is = new InputSource(); + is.setCharacterStream(new StringReader(snapshotXML)); + Document doc = builder.parse(is); + Element rootElement = doc.getDocumentElement(); + + currentSnapshotName = getTagValue("name", rootElement); + } catch (ParserConfigurationException e) { + s_logger.debug(e.toString()); + } catch (SAXException e) { + s_logger.debug(e.toString()); + } catch (IOException e) { + s_logger.debug(e.toString()); + } + } catch (LibvirtException e) { + s_logger.debug("Fail to get the current vm snapshot for vm: " + dm.getName() + ", continue"); + } + for (String snapshotName: dm.snapshotListNames()) { + DomainSnapshot snapshot = dm.snapshotLookupByName(snapshotName); + Boolean isCurrent = (currentSnapshotName != null && currentSnapshotName.equals(snapshotName)) ? true: false; + vmsnapshots.add(new Ternary<String, Boolean, String>(snapshotName, isCurrent, snapshot.getXMLDesc())); + snapshot.free(); + } + for (String snapshotName: dm.snapshotListNames()) { + String cmdvirsh = "virsh snapshot-delete --metadata " + dm.getName() + " " + snapshotName; + int cmdout = Script.runSimpleBashScriptForExitValue(cmdvirsh); + if (cmdout != 0) { + s_logger.debug("Fail to delete the metadata of vm snapshot: " + snapshotName); + } + } + return vmsnapshots; + } + + private static String getTagValue(String tag, Element eElement) { + NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes(); + Node nValue = nlList.item(0); + + return nValue.getNodeValue(); + } + + public void restoreVMSnapshotMetadata(String vmName, List<Ternary<String, Boolean, String>> vmsnapshots) { + s_logger.debug("Restoring the metadata of vm snapshots of vm " + vmName); + for (Ternary<String, Boolean, String> vmsnapshot: vmsnapshots) { + s_logger.debug("Restoring vm snapshot " + vmsnapshot.first() + " on " + vmName + " with XML:\n " + vmsnapshot.third()); + File tmpCfgFile = null; + try { + tmpCfgFile = File.createTempFile(vmName + "-", "cfg"); + final PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tmpCfgFile))); + out.println(vmsnapshot.third()); + out.close(); + String cfgFilePath = tmpCfgFile.getAbsolutePath(); + String cmdvirsh = "virsh snapshot-create --redefine " + vmName + (vmsnapshot.second()? " --current ":" ") + cfgFilePath; + int cmdout = Script.runSimpleBashScriptForExitValue(cmdvirsh); --- End diff -- Same goes for this, see my comment above about libvirt-java.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---