private void cloneDisks(Document cloneXmlDocument, String cloneName) throws LibvirtException { List<StorageVol> cloneDisks = new ArrayList<>(); int idx = 0; for (Disk d : getDisks(domain.getConnect(), domainXml)) { idx++; String clonedDisk = String.format("%s-%02d.qcow2", cloneName, idx); StorageVol vol = d.createCloneWithBackingStore(clonedDisk); logger.debug("Disk {} cloned to {}", d.getName(), clonedDisk); cloneDisks.add(vol); } updateDisks(cloneXmlDocument, cloneDisks); }
public void updateMetadata(String baseDomainName, String provisionCmd, String expirationTag, Date date) { try { if (domain.isActive() == 1) { throw new IllegalStateException("Domain must be shut down before updating metdata"); } // need a really fresh copy of the domain xml or the update may fail... reloadDomainXml(); updateProvisioningMetadata(domainXml, baseDomainName, provisionCmd, expirationTag, date); String xml = documentToString(domainXml); logger.debug("Updating domain '{}' XML with {}", getName(), xml); domain.getConnect().domainDefineXML(xml); } catch (IOException | LibvirtException e) { throw new LibvirtRuntimeException(String.format("Unable to update metadata for domain '%s'", getName()), e); } }
public void destroyWithDisks() { try { // look up state before undefining the domain... boolean isRunning = (domain.getInfo().state == DomainState.VIR_DOMAIN_RUNNING); List<Disk> disks = getDisks(domain.getConnect(), domainXml); logger.info("Undefining domain '{}'", domain.getName()); domain.undefine(3); // also remove snapshot data and managed save data if (isRunning) { logger.info("Shutting down domain '{}'", domain.getName()); domain.destroy(); } // this will not destroy the backing store disks. for (Disk disk : disks) { logger.info("Removing disk {}", disk.getName()); disk.getVolume().delete(0); } } catch (LibvirtException e) { throw new LibvirtRuntimeException("Unable to destroy domain", e); } }
/** * Clone the domain. All disks are cloned using the original disk as backing store. The names of the disks are * created by suffixing the original disk name with a number. */ public DomainWrapper cloneWithBackingStore(String cloneName, List<Filesystem> mappings) { logger.info("Creating clone from {}", getName()); try { // duplicate definition of base Document cloneXmlDocument = domainXml.clone(); setDomainName(cloneXmlDocument, cloneName); prepareForCloning(cloneXmlDocument); // keep track of who we are a clone from... updateCloneMetadata(cloneXmlDocument, getName(), new Date()); cloneDisks(cloneXmlDocument, cloneName); updateFilesystemMappings(cloneXmlDocument, mappings); String cloneXml = documentToString(cloneXmlDocument); logger.debug("Clone xml={}", cloneXml); // Domain cloneDomain = domain.getConnect().domainCreateXML(cloneXml, 0); Domain cloneDomain = domain.getConnect().domainDefineXML(cloneXml); String createdCloneXml = cloneDomain.getXMLDesc(0); logger.debug("Created clone xml: {}", createdCloneXml); cloneDomain.create(); logger.debug("Starting clone: '{}'", cloneDomain.getName()); return newWrapper(cloneDomain); } catch (IOException | LibvirtException e) { throw new LibvirtRuntimeException("Unable to clone domain", e); } }