Skip to content

Commit 99ca627

Browse files
committed
kvm: honour data disk controller for vm during attach volume
Fixes #12090 Some years back #4569 allowed honouring dataDiskController for KVM VMs during start but the same behviour is not seen while attaching a volume to a running VM. This PR first checks in the AttachCommand if a valid controller is present. If a valid controller/bus type is not present than existing logic is used. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent f1f779a commit 99ca627

File tree

3 files changed

+84
-59
lines changed

3 files changed

+84
-59
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,12 +4371,11 @@ public DiskDef.DiskBus getDataDiskModelFromVMDetail(final VirtualMachineTO vmTO)
43714371

43724372
String dataDiskController = details.get(VmDetailConstants.DATA_DISK_CONTROLLER);
43734373
if (StringUtils.isNotBlank(dataDiskController)) {
4374-
LOGGER.debug("Passed custom disk controller for DATA disk " + dataDiskController);
4375-
for (DiskDef.DiskBus bus : DiskDef.DiskBus.values()) {
4376-
if (bus.toString().equalsIgnoreCase(dataDiskController)) {
4377-
LOGGER.debug("Found matching enum for disk controller for DATA disk " + dataDiskController);
4378-
return bus;
4379-
}
4374+
LOGGER.debug("Passed custom disk controller for DATA disk {}", dataDiskController);
4375+
DiskDef.DiskBus bus = DiskDef.DiskBus.fromValue(dataDiskController);
4376+
if (bus != null) {
4377+
LOGGER.debug("Found matching enum for disk controller for DATA disk {}", dataDiskController);
4378+
return bus;
43804379
}
43814380
}
43824381
return null;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,15 @@ public enum DiskBus {
686686
_bus = bus;
687687
}
688688

689+
public static DiskBus fromValue(String bus) {
690+
for (DiskBus b : DiskBus.values()) {
691+
if (b.toString().equalsIgnoreCase(bus)) {
692+
return b;
693+
}
694+
}
695+
return null;
696+
}
697+
689698
@Override
690699
public String toString() {
691700
return _bus;

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

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,64 +1320,68 @@ protected boolean checkDetachSuccess(String diskPath, Domain dm) throws LibvirtE
13201320

13211321
/**
13221322
* Attaches or detaches a disk to an instance.
1323-
* @param conn libvirt connection
1324-
* @param attach boolean that determines whether the device will be attached or detached
1325-
* @param vmName instance name
1326-
* @param attachingDisk kvm physical disk
1327-
* @param devId device id in instance
1323+
*
1324+
* @param conn libvirt connection
1325+
* @param attach boolean that determines whether the device will be attached or detached
1326+
* @param vmName instance name
1327+
* @param attachingDisk kvm physical disk
1328+
* @param devId device id in instance
13281329
* @param serial
1329-
* @param bytesReadRate bytes read rate
1330-
* @param bytesReadRateMax bytes read rate max
1331-
* @param bytesReadRateMaxLength bytes read rate max length
1332-
* @param bytesWriteRate bytes write rate
1333-
* @param bytesWriteRateMax bytes write rate amx
1330+
* @param bytesReadRate bytes read rate
1331+
* @param bytesReadRateMax bytes read rate max
1332+
* @param bytesReadRateMaxLength bytes read rate max length
1333+
* @param bytesWriteRate bytes write rate
1334+
* @param bytesWriteRateMax bytes write rate amx
13341335
* @param bytesWriteRateMaxLength bytes write rate max length
1335-
* @param iopsReadRate iops read rate
1336-
* @param iopsReadRateMax iops read rate max
1337-
* @param iopsReadRateMaxLength iops read rate max length
1338-
* @param iopsWriteRate iops write rate
1339-
* @param iopsWriteRateMax iops write rate max
1340-
* @param iopsWriteRateMaxLength iops write rate max length
1341-
* @param cacheMode cache mode
1342-
* @param encryptDetails encrypt details
1336+
* @param iopsReadRate iops read rate
1337+
* @param iopsReadRateMax iops read rate max
1338+
* @param iopsReadRateMaxLength iops read rate max length
1339+
* @param iopsWriteRate iops write rate
1340+
* @param iopsWriteRateMax iops write rate max
1341+
* @param iopsWriteRateMaxLength iops write rate max length
1342+
* @param cacheMode cache mode
1343+
* @param encryptDetails encrypt details
1344+
* @param controllerInfo
13431345
* @throws LibvirtException
13441346
* @throws InternalErrorException
13451347
*/
13461348
protected synchronized void attachOrDetachDisk(final Connect conn, final boolean attach, final String vmName, final KVMPhysicalDisk attachingDisk, final int devId,
13471349
final String serial, final Long bytesReadRate, final Long bytesReadRateMax, final Long bytesReadRateMaxLength,
13481350
final Long bytesWriteRate, final Long bytesWriteRateMax, final Long bytesWriteRateMaxLength, final Long iopsReadRate,
13491351
final Long iopsReadRateMax, final Long iopsReadRateMaxLength, final Long iopsWriteRate, final Long iopsWriteRateMax,
1350-
final Long iopsWriteRateMaxLength, final String cacheMode, final DiskDef.LibvirtDiskEncryptDetails encryptDetails, Map<String, String> details)
1352+
final Long iopsWriteRateMaxLength, final String cacheMode, final DiskDef.LibvirtDiskEncryptDetails encryptDetails, Map<String, String> details, Map<String, String> controllerInfo)
13511353
throws LibvirtException, InternalErrorException {
13521354
attachOrDetachDisk(conn, attach, vmName, attachingDisk, devId, serial, bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength,
13531355
bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength, iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate,
1354-
iopsWriteRateMax, iopsWriteRateMaxLength, cacheMode, encryptDetails, 0l, details);
1356+
iopsWriteRateMax, iopsWriteRateMaxLength, cacheMode, encryptDetails, 0l, details, controllerInfo);
13551357
}
13561358

13571359
/**
13581360
*
13591361
* Attaches or detaches a disk to an instance.
1360-
* @param conn libvirt connection
1361-
* @param attach boolean that determines whether the device will be attached or detached
1362-
* @param vmName instance name
1363-
* @param attachingDisk kvm physical disk
1364-
* @param devId device id in instance
1362+
*
1363+
* @param conn libvirt connection
1364+
* @param attach boolean that determines whether the device will be attached or detached
1365+
* @param vmName instance name
1366+
* @param attachingDisk kvm physical disk
1367+
* @param devId device id in instance
13651368
* @param serial
1366-
* @param bytesReadRate bytes read rate
1367-
* @param bytesReadRateMax bytes read rate max
1368-
* @param bytesReadRateMaxLength bytes read rate max length
1369-
* @param bytesWriteRate bytes write rate
1370-
* @param bytesWriteRateMax bytes write rate amx
1369+
* @param bytesReadRate bytes read rate
1370+
* @param bytesReadRateMax bytes read rate max
1371+
* @param bytesReadRateMaxLength bytes read rate max length
1372+
* @param bytesWriteRate bytes write rate
1373+
* @param bytesWriteRateMax bytes write rate amx
13711374
* @param bytesWriteRateMaxLength bytes write rate max length
1372-
* @param iopsReadRate iops read rate
1373-
* @param iopsReadRateMax iops read rate max
1374-
* @param iopsReadRateMaxLength iops read rate max length
1375-
* @param iopsWriteRate iops write rate
1376-
* @param iopsWriteRateMax iops write rate max
1377-
* @param iopsWriteRateMaxLength iops write rate max length
1378-
* @param cacheMode cache mode
1379-
* @param encryptDetails encrypt details
1380-
* @param waitDetachDevice value set in milliseconds to wait before assuming device removal failed
1375+
* @param iopsReadRate iops read rate
1376+
* @param iopsReadRateMax iops read rate max
1377+
* @param iopsReadRateMaxLength iops read rate max length
1378+
* @param iopsWriteRate iops write rate
1379+
* @param iopsWriteRateMax iops write rate max
1380+
* @param iopsWriteRateMaxLength iops write rate max length
1381+
* @param cacheMode cache mode
1382+
* @param encryptDetails encrypt details
1383+
* @param waitDetachDevice value set in milliseconds to wait before assuming device removal failed
1384+
* @param controllerInfo
13811385
* @throws LibvirtException
13821386
* @throws InternalErrorException
13831387
*/
@@ -1386,7 +1390,7 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
13861390
final Long bytesWriteRate, final Long bytesWriteRateMax, final Long bytesWriteRateMaxLength, final Long iopsReadRate,
13871391
final Long iopsReadRateMax, final Long iopsReadRateMaxLength, final Long iopsWriteRate, final Long iopsWriteRateMax,
13881392
final Long iopsWriteRateMaxLength, final String cacheMode, final DiskDef.LibvirtDiskEncryptDetails encryptDetails,
1389-
long waitDetachDevice, Map<String, String> details)
1393+
long waitDetachDevice, Map<String, String> details, Map<String, String> controllerInfo)
13901394
throws LibvirtException, InternalErrorException {
13911395

13921396
List<DiskDef> disks = null;
@@ -1423,17 +1427,7 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
14231427
return;
14241428
}
14251429
} else {
1426-
DiskDef.DiskBus busT = DiskDef.DiskBus.VIRTIO;
1427-
for (final DiskDef disk : disks) {
1428-
if (disk.getDeviceType() == DeviceType.DISK) {
1429-
if (disk.getBusType() == DiskDef.DiskBus.SCSI) {
1430-
busT = DiskDef.DiskBus.SCSI;
1431-
} else if (disk.getBusType() == DiskDef.DiskBus.VIRTIOBLK) {
1432-
busT = DiskDef.DiskBus.VIRTIOBLK;
1433-
}
1434-
break;
1435-
}
1436-
}
1430+
DiskDef.DiskBus busT = getAttachDiskBusType(devId, disks, controllerInfo);
14371431
diskdef = new DiskDef();
14381432
if (busT == DiskDef.DiskBus.SCSI || busT == DiskDef.DiskBus.VIRTIOBLK) {
14391433
diskdef.setQemuDriver(true);
@@ -1538,6 +1532,28 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
15381532
}
15391533
}
15401534

1535+
protected DiskDef.DiskBus getAttachDiskBusType(int deviceId, List<DiskDef> disks, Map<String, String> controllerInfo) {
1536+
String controllerKey = deviceId == 0 ? VmDetailConstants.ROOT_DISK_CONTROLLER : VmDetailConstants.DATA_DISK_CONTROLLER;
1537+
String diskController = MapUtils.getString(controllerInfo, controllerKey);
1538+
DiskDef.DiskBus busType = DiskDef.DiskBus.fromValue(diskController);
1539+
if (diskController != null) {
1540+
logger.debug("Using controller '{}' from command specified as {} while attaching disk (deviceId={})",
1541+
diskController, controllerKey, deviceId);
1542+
return busType;
1543+
}
1544+
for (final DiskDef disk : disks) {
1545+
if (disk.getDeviceType() != DeviceType.DISK) {
1546+
continue;
1547+
}
1548+
if (disk.getBusType() == DiskDef.DiskBus.SCSI) {
1549+
return DiskDef.DiskBus.SCSI;
1550+
} else if (disk.getBusType() == DiskDef.DiskBus.VIRTIOBLK) {
1551+
return DiskDef.DiskBus.VIRTIOBLK;
1552+
}
1553+
}
1554+
return DiskDef.DiskBus.VIRTIO;
1555+
}
1556+
15411557
@Override
15421558
public Answer attachVolume(final AttachCommand cmd) {
15431559
final DiskTO disk = cmd.getDisk();
@@ -1565,7 +1581,8 @@ public Answer attachVolume(final AttachCommand cmd) {
15651581
vol.getBytesReadRate(), vol.getBytesReadRateMax(), vol.getBytesReadRateMaxLength(),
15661582
vol.getBytesWriteRate(), vol.getBytesWriteRateMax(), vol.getBytesWriteRateMaxLength(),
15671583
vol.getIopsReadRate(), vol.getIopsReadRateMax(), vol.getIopsReadRateMaxLength(),
1568-
vol.getIopsWriteRate(), vol.getIopsWriteRateMax(), vol.getIopsWriteRateMaxLength(), volCacheMode, encryptDetails, disk.getDetails());
1584+
vol.getIopsWriteRate(), vol.getIopsWriteRateMax(), vol.getIopsWriteRateMaxLength(), volCacheMode,
1585+
encryptDetails, disk.getDetails(), cmd.getControllerInfo());
15691586

15701587
return new AttachAnswer(disk);
15711588
} catch (final LibvirtException e) {
@@ -1602,7 +1619,7 @@ public Answer dettachVolume(final DettachCommand cmd) {
16021619
vol.getBytesReadRate(), vol.getBytesReadRateMax(), vol.getBytesReadRateMaxLength(),
16031620
vol.getBytesWriteRate(), vol.getBytesWriteRateMax(), vol.getBytesWriteRateMaxLength(),
16041621
vol.getIopsReadRate(), vol.getIopsReadRateMax(), vol.getIopsReadRateMaxLength(),
1605-
vol.getIopsWriteRate(), vol.getIopsWriteRateMax(), vol.getIopsWriteRateMaxLength(), volCacheMode, null, waitDetachDevice, null);
1622+
vol.getIopsWriteRate(), vol.getIopsWriteRateMax(), vol.getIopsWriteRateMaxLength(), volCacheMode, null, waitDetachDevice, null, null);
16061623

16071624
storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
16081625

0 commit comments

Comments
 (0)