Skip to content

Commit 89532a4

Browse files
author
toni.zamparetti
committed
Fix in the calculation of a volume's physical size
1 parent a289bb0 commit 89532a4

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtStorageVolumeDef.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public VolumeFormat getFormat() {
8383
return this._volFormat;
8484
}
8585

86+
public Long getVolSize() {
87+
return _volSize;
88+
}
89+
8690
@Override
8791
public String toString() {
8892
StringBuilder storageVolBuilder = new StringBuilder();

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtStorageVolumeXMLParser.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
public class LibvirtStorageVolumeXMLParser {
3636
protected Logger logger = LogManager.getLogger(getClass());
3737

38+
public String getBackingFileNameIfExists(String volXML) {
39+
try {
40+
DocumentBuilder builder = ParserUtils.getSaferDocumentBuilderFactory().newDocumentBuilder();
41+
42+
InputSource is = new InputSource();
43+
is.setCharacterStream(new StringReader(volXML));
44+
Document doc = builder.parse(is);
45+
46+
Element rootElement = doc.getDocumentElement();
47+
Element backingStore = (Element)rootElement.getElementsByTagName("backingStore").item(0);
48+
if (backingStore != null) {
49+
String[] paths = getTagValue("path", backingStore).split("/");
50+
return paths[paths.length-1];
51+
}
52+
} catch (ParserConfigurationException | SAXException | IOException e) {
53+
logger.error(e.toString(), e);
54+
}
55+
return null;
56+
}
57+
3858
public LibvirtStorageVolumeDef parseStorageVolumeXML(String volXML) {
3959
DocumentBuilder builder;
4060
try {
@@ -50,6 +70,7 @@ public LibvirtStorageVolumeDef parseStorageVolumeXML(String volXML) {
5070
Element target = (Element)rootElement.getElementsByTagName("target").item(0);
5171
String format = getAttrValue("type", "format", target);
5272
Long capacity = Long.parseLong(getTagValue("capacity", rootElement));
73+
5374
return new LibvirtStorageVolumeDef(VolName, capacity, LibvirtStorageVolumeDef.VolumeFormat.getFormat(format), null, null);
5475
} catch (ParserConfigurationException e) {
5576
logger.debug(e.toString());

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,12 @@ public LibvirtStoragePoolDef getStoragePoolDef(Connect conn, StoragePool pool) t
507507
return parser.parseStoragePoolXML(poolDefXML);
508508
}
509509

510+
public String getBackingFileOfVolumeIfExists(StorageVol vol) throws LibvirtException {
511+
String volDefXML = vol.getXMLDesc(0);
512+
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
513+
return parser.getBackingFileNameIfExists(volDefXML);
514+
}
515+
510516
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn, StorageVol vol) throws LibvirtException {
511517
String volDefXML = vol.getXMLDesc(0);
512518
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
@@ -657,6 +663,16 @@ public KVMStoragePool getStoragePool(String uuid, boolean refreshInfo) {
657663
}
658664
}
659665

666+
public Long getBackingFileSizes(StoragePool pool, StorageVol vol) throws LibvirtException {
667+
long size = vol.getInfo().allocation;
668+
String backingFileOfVolumeIfExists = getBackingFileOfVolumeIfExists(vol);
669+
if (backingFileOfVolumeIfExists != null) {
670+
StorageVol backingFile = getVolume(pool, backingFileOfVolumeIfExists);
671+
size += getBackingFileSizes(pool, backingFile);
672+
}
673+
return size;
674+
}
675+
660676
@Override
661677
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
662678
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
@@ -665,8 +681,9 @@ public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
665681
StorageVol vol = getVolume(libvirtPool.getPool(), volumeUuid);
666682
KVMPhysicalDisk disk;
667683
LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool.getPool().getConnect(), vol);
684+
Long allSizes = getBackingFileSizes(libvirtPool.getPool(), vol);
668685
disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
669-
disk.setSize(vol.getInfo().allocation);
686+
disk.setSize(allSizes);
670687
disk.setVirtualSize(vol.getInfo().capacity);
671688

672689
/**

0 commit comments

Comments
 (0)