private void deleteVol(LibvirtStoragePool pool, StorageVol vol) throws LibvirtException { vol.delete(0); }
private void deleteDirVol(LibvirtStoragePool pool, StorageVol vol) throws LibvirtException { Script.runSimpleBashScript("rm -r --interactive=never " + vol.getPath()); }
public StorageVol copyVolume(StoragePool destPool, LibvirtStorageVolumeDef destVol, StorageVol srcVol, int timeout) throws LibvirtException { StorageVol vol = destPool.storageVolCreateXML(destVol.toString(), 0); String srcPath = srcVol.getKey(); String destPath = vol.getKey(); Script.runSimpleBashScript("cp " + srcPath + " " + destPath, timeout); return vol; }
private KVMPhysicalDisk createPhysicalDiskByLibVirt(String name, KVMStoragePool pool, PhysicalDiskFormat format, Storage.ProvisioningType provisioningType, long size) { LibvirtStoragePool libvirtPool = (LibvirtStoragePool) pool; StoragePool virtPool = libvirtPool.getPool(); LibvirtStorageVolumeDef.VolumeFormat libvirtformat = LibvirtStorageVolumeDef.VolumeFormat.getFormat(format); String volPath = null; String volName = null; long volAllocation = 0; long volCapacity = 0; LibvirtStorageVolumeDef volDef = new LibvirtStorageVolumeDef(name, size, libvirtformat, null, null); s_logger.debug(volDef.toString()); try { StorageVol vol = virtPool.storageVolCreateXML(volDef.toString(), 0); volPath = vol.getPath(); volName = vol.getName(); volAllocation = vol.getInfo().allocation; volCapacity = vol.getInfo().capacity; } catch (LibvirtException e) { throw new CloudRuntimeException(e.toString()); } KVMPhysicalDisk disk = new KVMPhysicalDisk(volPath, volName, pool); disk.setFormat(format); disk.setSize(volAllocation); disk.setVirtualSize(volCapacity); return disk; }
private static StorageVol findVolumeInPool(StoragePool sp, String image) throws LibvirtException { for (String v : sp.listVolumes()) { StorageVol vol = sp.storageVolLookupByName(v); if (vol.getPath().equals(image)) { log.debug("Found volume '{}' for image '{}'", vol.getName(), image); return vol; } } return null; }
deleteVol(libvirtPool, vol); vol.free(); return true; } catch (LibvirtException e) {
public StorageVolInfo getInfo() { try { return volume.getInfo(); } catch (LibvirtException e) { throw new LibvirtRuntimeException(e); } }
public String getName() { try { return volume.getName(); } catch (LibvirtException e) { throw new LibvirtRuntimeException(e); } }
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn, StorageVol vol) throws LibvirtException { String volDefXML = vol.getXMLDesc(0); LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser(); return parser.parseStorageVolumeXML(volDefXML); }
v.resize(newSize, flags); } catch (final LibvirtException e) { return new ResizeVolumeAnswer(command, false, e.toString());
public StoragePool getStoragePool() { try { return volume.storagePoolLookupByVolume(); } catch (LibvirtException e) { throw new LibvirtRuntimeException(e); } }
/** Look up a disk image's {@link StorageVol} in the {@link StoragePool}s attached to connection. */ public static StorageVol findVolume(Connect connection, String path) throws LibvirtException { log.debug("Looking up StorageVolume for path '{}'", path); for (String s : connection.listStoragePools()) { StoragePool sp = connection.storagePoolLookupByName(s); for (String v : sp.listVolumes()) { StorageVol vol = sp.storageVolLookupByName(v); if (vol.getPath().equals(path)) { log.debug("Found volume '{}' for path '{}'", vol.getName(), path); return vol; } } } throw new LibvirtRuntimeException("no volume found for path " + path); }
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn, StorageVol vol) throws LibvirtException { String volDefXML = vol.getXMLDesc(0); LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser(); return parser.parseStorageVolumeXML(volDefXML); }
/** * Deletes the local volume from the storage pool. */ protected void deleteLocalVolume(String localPath) { try { Connect conn = LibvirtConnection.getConnection(); StorageVol storageVolLookupByPath = conn.storageVolLookupByPath(localPath); storageVolLookupByPath.delete(0); } catch (LibvirtException e) { s_logger.error(String.format("Cannot delete local volume [%s] due to: %s", localPath, e)); } }
/** * update the disks in the domain XML. It is assumed that the the size of the volumes is the same as the number of * disk elements and that the order is the same. */ public static void updateDisks(Document domainXml, List<StorageVol> volumes) throws LibvirtException { XPathFactory xpf = XPathFactory.instance(); XPathExpression<Element> diskExpr = xpf.compile(XPATH_DISK, Filters.element()); XPathExpression<Attribute> fileExpr = xpf.compile(XPATH_DISK_FILE, Filters.attribute()); List<Element> disks = diskExpr.evaluate(domainXml); Iterator<StorageVol> cloneDiskIter = volumes.iterator(); for (Element disk : disks) { Attribute file = fileExpr.evaluateFirst(disk); StorageVol cloneDisk = cloneDiskIter.next(); file.setValue(cloneDisk.getPath()); } }
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); } }