Skip to content

Commit df4977f

Browse files
author
SadiJr
committed
Merge branch 'fix-physical-size-calculation-in-kvm' into '4.20.0.0-scclouds'
Correção no cálculo do tamanho físico de um volume Closes #3098 See merge request scclouds/scclouds!1354
2 parents 8092ad2 + 361455a commit df4977f

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

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
@@ -505,6 +505,12 @@ public LibvirtStoragePoolDef getStoragePoolDef(Connect conn, StoragePool pool) t
505505
return parser.parseStoragePoolXML(poolDefXML);
506506
}
507507

508+
public String getBackingFileOfVolumeIfExists(StorageVol vol) throws LibvirtException {
509+
String volDefXML = vol.getXMLDesc(0);
510+
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
511+
return parser.getBackingFileNameIfExists(volDefXML);
512+
}
513+
508514
public LibvirtStorageVolumeDef getStorageVolumeDef(Connect conn, StorageVol vol) throws LibvirtException {
509515
String volDefXML = vol.getXMLDesc(0);
510516
LibvirtStorageVolumeXMLParser parser = new LibvirtStorageVolumeXMLParser();
@@ -606,6 +612,16 @@ public KVMStoragePool getStoragePool(String uuid, boolean refreshInfo) {
606612
}
607613
}
608614

615+
public Long getBackingFileSizes(StoragePool pool, StorageVol vol) throws LibvirtException {
616+
long size = vol.getInfo().allocation;
617+
String backingFileOfVolumeIfExists = getBackingFileOfVolumeIfExists(vol);
618+
if (backingFileOfVolumeIfExists != null) {
619+
StorageVol backingFile = getVolume(pool, backingFileOfVolumeIfExists);
620+
size += getBackingFileSizes(pool, backingFile);
621+
}
622+
return size;
623+
}
624+
609625
@Override
610626
public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
611627
LibvirtStoragePool libvirtPool = (LibvirtStoragePool)pool;
@@ -614,8 +630,9 @@ public KVMPhysicalDisk getPhysicalDisk(String volumeUuid, KVMStoragePool pool) {
614630
StorageVol vol = getVolume(libvirtPool.getPool(), volumeUuid);
615631
KVMPhysicalDisk disk;
616632
LibvirtStorageVolumeDef voldef = getStorageVolumeDef(libvirtPool.getPool().getConnect(), vol);
633+
Long allSizes = getBackingFileSizes(libvirtPool.getPool(), vol);
617634
disk = new KVMPhysicalDisk(vol.getPath(), vol.getName(), pool);
618-
disk.setSize(vol.getInfo().allocation);
635+
disk.setSize(allSizes);
619636
disk.setVirtualSize(vol.getInfo().capacity);
620637

621638
/**

0 commit comments

Comments
 (0)