diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java
index 1fba9f3e96f0..58a9d373fc55 100644
--- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java
+++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java
@@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermissions;
@@ -1478,13 +1479,7 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
}
}
- for (final DiskDef disk : disks) {
- final String file = disk.getDiskPath();
- if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
- diskdef = disk;
- break;
- }
- }
+ diskdef = findDiskDefByPath(disks, attachingDisk.getPath());
if (diskdef == null) {
logger.warn(String.format("Could not find disk [%s] attached to VM instance with UUID [%s]. We will set it as detached in the database to ensure consistency.",
attachingDisk.getPath(), dm.getUUIDString()));
@@ -1596,6 +1591,53 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
}
}
+ /**
+ * Searches the disk of an instance that is backed by the given path.
+ * The path is first compared as is and, if that finds nothing, by the file both paths resolve to. The latter is
+ * needed as the path of a volume might be a different link to the same device than the one in the domain XML of a
+ * running instance; e.g. Linstor volumes attached as /dev/drbd1000 before the plugin switched to
+ * /dev/drbd/by-res/<resource>/0 keep the old path until the instance is restarted.
+ * @param disks disks of the instance
+ * @param path path of the disk to search for
+ * @return the disk definition using the given path or null if the instance doesn't have such a disk.
+ */
+ protected DiskDef findDiskDefByPath(List disks, String path) {
+ for (final DiskDef disk : disks) {
+ final String file = disk.getDiskPath();
+ if (file != null && file.equalsIgnoreCase(path)) {
+ return disk;
+ }
+ }
+
+ final Path realPath = resolveRealPath(path);
+ if (realPath == null) {
+ return null;
+ }
+ for (final DiskDef disk : disks) {
+ if (realPath.equals(resolveRealPath(disk.getDiskPath()))) {
+ logger.info("Disk [{}] is attached with path [{}], both resolve to [{}].", path, disk.getDiskPath(), realPath);
+ return disk;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Resolves all symbolic links of a local path.
+ * @return the real path or null if the path isn't an absolute local path or doesn't exist.
+ */
+ protected Path resolveRealPath(String path) {
+ if (path == null || !path.startsWith(File.separator)) {
+ return null;
+ }
+ try {
+ return Paths.get(path).toRealPath();
+ } catch (IOException | InvalidPathException e) {
+ logger.trace("Unable to resolve path [{}]: {}", path, e.getMessage());
+ return null;
+ }
+ }
+
protected DiskDef.DiskBus getAttachDiskBusType(int deviceId, List disks, Map controllerInfo) {
String controllerKey = deviceId == 0 ? VmDetailConstants.ROOT_DISK_CONTROLLER : VmDetailConstants.DATA_DISK_CONTROLLER;
String diskController = MapUtils.getString(controllerInfo, controllerKey);
diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java
index 11d508d16468..cd11afb45d83 100644
--- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java
+++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java
@@ -37,6 +37,7 @@
import org.apache.cloudstack.utils.qemu.QemuImg;
import org.apache.cloudstack.utils.qemu.QemuImgException;
import org.apache.cloudstack.utils.qemu.QemuImgFile;
+import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Assert;
@@ -576,4 +577,60 @@ public void takeRbdVolumeSnapshotOfStoppedVmTestReleasesHandlesWhenSnapshotFails
Mockito.verify(radosMock).ioCtxDestroy(ioCtxMock);
}
}
+
+ private LibvirtVMDef.DiskDef createBlockDiskDef(String path, int devId) {
+ LibvirtVMDef.DiskDef diskDef = new LibvirtVMDef.DiskDef();
+ diskDef.defBlockBasedDisk(path, devId, LibvirtVMDef.DiskDef.DiskBus.VIRTIO);
+ return diskDef;
+ }
+
+ @Test
+ public void findDiskDefByPathTestReturnsDiskWithSamePath() {
+ LibvirtVMDef.DiskDef rootDisk = createBlockDiskDef("/dev/drbd/by-res/cs-root/0", 0);
+ LibvirtVMDef.DiskDef dataDisk = createBlockDiskDef("/dev/drbd/by-res/cs-data/0", 1);
+
+ LibvirtVMDef.DiskDef result = storageProcessorSpy.findDiskDefByPath(List.of(rootDisk, dataDisk), "/dev/drbd/by-res/cs-data/0");
+
+ Assert.assertSame(dataDisk, result);
+ }
+
+ /**
+ * An instance started before the Linstor plugin provided /dev/drbd/by-res/ paths still has the /dev/drbdX device
+ * in its domain XML, the disk has to be found anyway as it would otherwise stay attached to the instance.
+ */
+ @Test
+ public void findDiskDefByPathTestReturnsDiskAttachedWithTargetOfLink() throws IOException {
+ Path dir = Files.createTempDirectory("findDiskDefByPath");
+ try {
+ Path rootDevice = Files.createFile(dir.resolve("drbd1000"));
+ Path dataDevice = Files.createFile(dir.resolve("drbd1001"));
+ Path dataLink = Files.createSymbolicLink(dir.resolve("by-res-cs-data"), dataDevice);
+ LibvirtVMDef.DiskDef cdrom = new LibvirtVMDef.DiskDef();
+ LibvirtVMDef.DiskDef rootDisk = createBlockDiskDef(rootDevice.toString(), 0);
+ LibvirtVMDef.DiskDef dataDisk = createBlockDiskDef(dataDevice.toString(), 1);
+
+ LibvirtVMDef.DiskDef result = storageProcessorSpy.findDiskDefByPath(List.of(cdrom, rootDisk, dataDisk), dataLink.toString());
+
+ Assert.assertSame(dataDisk, result);
+ } finally {
+ FileUtils.deleteQuietly(dir.toFile());
+ }
+ }
+
+ @Test
+ public void findDiskDefByPathTestReturnsNullWhenNoDiskUsesThePath() throws IOException {
+ Path dir = Files.createTempDirectory("findDiskDefByPath");
+ try {
+ Path rootDevice = Files.createFile(dir.resolve("drbd1000"));
+ Path dataDevice = Files.createFile(dir.resolve("drbd1001"));
+ LibvirtVMDef.DiskDef rootDisk = createBlockDiskDef(rootDevice.toString(), 0);
+ LibvirtVMDef.DiskDef networkDisk = createBlockDiskDef("pool/image", 1);
+
+ Assert.assertNull(storageProcessorSpy.findDiskDefByPath(List.of(rootDisk, networkDisk), dataDevice.toString()));
+ Assert.assertNull(storageProcessorSpy.findDiskDefByPath(List.of(rootDisk, networkDisk), dir.resolve("missing").toString()));
+ Assert.assertNull(storageProcessorSpy.findDiskDefByPath(List.of(rootDisk, networkDisk), "pool/other"));
+ } finally {
+ FileUtils.deleteQuietly(dir.toFile());
+ }
+ }
}