From 7d05893c91f0de2a23aee8f027c049e78cd08f4b Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Wed, 21 Oct 2026 13:47:42 +0200 Subject: [PATCH] kvm: collect the disk stats of volumes on local storage The disk stats of a volume are matched on the last part of the path of its disk. For local storage, whose disks are in /var/lib/libvirt/images, the agent reported 'libvirt' instead of the path of the volume. The stats of the volume were never found, so its read and write counters stayed at 0. Use the last element of the path of the disk, which is the same as before for the other storage types. Also skip, instead of stopping at, a disk which does not match a volume, so the stats of the other disks of the Instance are still collected. Likewise, keep checking the other volumes of a storage pool after one with a format for which the volume stats are not implemented, instead of stopping at the first one. --- .../hypervisor/kvm/resource/LibvirtComputingResource.java | 4 ++-- .../kvm/resource/LibvirtComputingResourceTest.java | 8 ++++++++ server/src/main/java/com/cloud/server/StatsCollector.java | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index a9ed0c62cab8..315e7bd70426 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -5041,8 +5041,8 @@ protected String getDiskPathFromDiskDef(DiskDef disk) { } } - // for example, path = /mnt/pool_uuid/disk_path/ - return token[3]; + // for example, path = /mnt/pool_uuid/disk_path/ or, for local storage, /var/lib/libvirt/images/disk_path + return token[token.length - 1]; } } return null; diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index fa204f806261..a361858e54ac 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -5979,6 +5979,14 @@ public void testGetDiskPathFromDiskDefForNFS() { Assert.assertEquals("filepath", diskPath); } + @Test + public void testGetDiskPathFromDiskDefForLocalStorage() { + DiskDef diskDef = new DiskDef(); + diskDef.defFileBasedDisk("/var/lib/libvirt/images/filepath", 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2); + String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef); + Assert.assertEquals("filepath", diskPath); + } + @Test public void testGetDiskPathFromDiskDefForNFSWithNullPath() { DiskDef diskDef = new DiskDef(); diff --git a/server/src/main/java/com/cloud/server/StatsCollector.java b/server/src/main/java/com/cloud/server/StatsCollector.java index 3eec27d4f54d..e67a3994cbb0 100644 --- a/server/src/main/java/com/cloud/server/StatsCollector.java +++ b/server/src/main/java/com/cloud/server/StatsCollector.java @@ -1497,7 +1497,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) { List volumes = _volsDao.search(sc_volume, null); if (CollectionUtils.isEmpty(volumes)) - break; + continue; VolumeVO volume = volumes.get(0); VmDiskStatisticsVO previousVmDiskStats = _vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vmId, volume.getId()); @@ -1682,7 +1682,7 @@ protected void runInContext() { if (!List.of(ImageFormat.QCOW2, ImageFormat.VHD, ImageFormat.OVA, ImageFormat.RAW).contains(volume.getFormat()) && !List.of(Storage.StoragePoolType.PowerFlex, Storage.StoragePoolType.FiberChannel).contains(pool.getPoolType())) { logger.warn("Volume stats not implemented for this format type " + volume.getFormat()); - break; + continue; } } try {