Skip to content

Commit 4a6957d

Browse files
rp-dhslove
authored andcommitted
linstor/kvm: Workaround a qemu bug and IDE bus discard enabled. (apache#9859)
qemu has a bug versions prior 7.0 with discard enabled and using the IDE bus. It would crash the qemu process and kill the virtual machine, this is most noticeable on installing a windows guest from the Windows ISO installer.
1 parent ebd019d commit 4a6957d

File tree

4 files changed

+134
-3
lines changed

4 files changed

+134
-3
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
397397
protected static final String DEFAULT_TUNGSTEN_VIF_DRIVER_CLASS_NAME = "com.cloud.hypervisor.kvm.resource.VRouterVifDriver";
398398
private final static long HYPERVISOR_LIBVIRT_VERSION_SUPPORTS_IO_URING = 6003000;
399399
private final static long HYPERVISOR_QEMU_VERSION_SUPPORTS_IO_URING = 5000000;
400+
private final static long HYPERVISOR_QEMU_VERSION_IDE_DISCARD_FIXED = 7000000;
400401

401402
protected HypervisorType hypervisorType;
402403
protected String hypervisorURI;
@@ -3325,7 +3326,7 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
33253326
}
33263327
} else if (volume.getType() != Volume.Type.ISO) {
33273328
final PrimaryDataStoreTO store = (PrimaryDataStoreTO)data.getDataStore();
3328-
physicalDisk = storagePoolManager.getPhysicalDisk(store.getPoolType(), store.getUuid(), data.getPath());
3329+
physicalDisk = getStoragePoolMgr().getPhysicalDisk(store.getPoolType(), store.getUuid(), data.getPath());
33293330
pool = physicalDisk.getPool();
33303331
}
33313332

@@ -3458,7 +3459,7 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
34583459
else {
34593460
disk.defBlockBasedDisk(physicalDisk.getPath(), devId, diskBusType);
34603461
}
3461-
if (pool.getType() == StoragePoolType.Linstor) {
3462+
if (pool.getType() == StoragePoolType.Linstor && isQemuDiscardBugFree(diskBusType)) {
34623463
disk.setDiscard(DiscardType.UNMAP);
34633464
}
34643465
} else {
@@ -3609,6 +3610,16 @@ private boolean isIoUringEnabled() {
36093610
return isUbuntuHost() || isIoUringSupportedByQemu();
36103611
}
36113612

3613+
/**
3614+
* Qemu has a bug with discard enabled on IDE bus devices if qemu version < 7.0.
3615+
* <a href="https://bugzilla.redhat.com/show_bug.cgi?id=2029980">redhat bug entry</a>
3616+
* @param diskBus used for the disk
3617+
* @return true if it is safe to enable discard, otherwise false.
3618+
*/
3619+
public boolean isQemuDiscardBugFree(DiskDef.DiskBus diskBus) {
3620+
return diskBus != DiskDef.DiskBus.IDE || getHypervisorQemuVersion() >= HYPERVISOR_QEMU_VERSION_IDE_DISCARD_FIXED;
3621+
}
3622+
36123623
public boolean isUbuntuHost() {
36133624
Map<String, String> versionString = getVersionStrings();
36143625
String hostKey = "Host.OS";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ protected synchronized void attachOrDetachDisk(final Connect conn, final boolean
14931493
diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, busT, DiskDef.DiskFmtType.QCOW2);
14941494
} else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
14951495
diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, busT);
1496-
if (attachingPool.getType() == StoragePoolType.Linstor) {
1496+
if (attachingPool.getType() == StoragePoolType.Linstor && resource.isQemuDiscardBugFree(busT)) {
14971497
diskdef.setDiscard(DiscardType.UNMAP);
14981498
}
14991499
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@
5858

5959
import com.cloud.utils.net.NetUtils;
6060

61+
import com.cloud.vm.VmDetailConstants;
6162
import org.apache.cloudstack.api.ApiConstants.IoDriverPolicy;
6263
import org.apache.cloudstack.storage.command.AttachAnswer;
6364
import org.apache.cloudstack.storage.command.AttachCommand;
65+
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
66+
import org.apache.cloudstack.storage.to.VolumeObjectTO;
6467
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
6568
import org.apache.cloudstack.utils.linux.CPUStat;
6669
import org.apache.cloudstack.utils.linux.MemStat;
@@ -6415,4 +6418,114 @@ public void calculateVmMetricsTestOldStatsIsNotNullCalculatesUtilization() throw
64156418
Assert.assertEquals(newStats.getDiskReadKBs() - oldStats.getDiskReadKBs(), metrics.getDiskReadKBs(), 0);
64166419
Assert.assertEquals(newStats.getDiskWriteKBs() - oldStats.getDiskWriteKBs(), metrics.getDiskWriteKBs(), 0);
64176420
}
6421+
6422+
@Test
6423+
public void createLinstorVdb() throws LibvirtException, InternalErrorException, URISyntaxException {
6424+
final Connect connect = Mockito.mock(Connect.class);
6425+
6426+
final int id = random.nextInt(65534);
6427+
final String name = "test-instance-1";
6428+
6429+
final int cpus = 2;
6430+
final int speed = 1024;
6431+
final int minRam = 256 * 1024;
6432+
final int maxRam = 512 * 1024;
6433+
final String os = "Ubuntu";
6434+
final String vncPassword = "mySuperSecretPassword";
6435+
6436+
final VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, speed, minRam,
6437+
maxRam, BootloaderType.HVM, os, false, false, vncPassword);
6438+
to.setVncAddr("");
6439+
to.setArch("x86_64");
6440+
to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
6441+
to.setVcpuMaxLimit(cpus + 1);
6442+
final HashMap<String, String> vmToDetails = new HashMap<>();
6443+
to.setDetails(vmToDetails);
6444+
6445+
String diskLinPath = "9ebe53c1-3d35-46e5-b7aa-6fc223ba0fcf";
6446+
final DiskTO diskTO = new DiskTO();
6447+
diskTO.setDiskSeq(1L);
6448+
diskTO.setType(Volume.Type.ROOT);
6449+
diskTO.setDetails(new HashMap<>());
6450+
diskTO.setPath(diskLinPath);
6451+
6452+
final PrimaryDataStoreTO primaryDataStoreTO = Mockito.mock(PrimaryDataStoreTO.class);
6453+
String pDSTOUUID = "9ebe53c1-3d35-46e5-b7aa-6fc223ac4fcf";
6454+
when(primaryDataStoreTO.getPoolType()).thenReturn(StoragePoolType.Linstor);
6455+
when(primaryDataStoreTO.getUuid()).thenReturn(pDSTOUUID);
6456+
6457+
VolumeObjectTO dataTO = new VolumeObjectTO();
6458+
6459+
dataTO.setUuid("12be53c1-3d35-46e5-b7aa-6fc223ba0f34");
6460+
dataTO.setPath(diskTO.getPath());
6461+
dataTO.setDataStore(primaryDataStoreTO);
6462+
diskTO.setData(dataTO);
6463+
to.setDisks(new DiskTO[]{diskTO});
6464+
6465+
String path = "/dev/drbd1020";
6466+
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
6467+
final KVMStoragePool storagePool = Mockito.mock(KVMStoragePool.class);
6468+
final KVMPhysicalDisk vol = Mockito.mock(KVMPhysicalDisk.class);
6469+
6470+
when(libvirtComputingResourceSpy.getStoragePoolMgr()).thenReturn(storagePoolMgr);
6471+
when(storagePool.getType()).thenReturn(StoragePoolType.Linstor);
6472+
when(storagePoolMgr.getPhysicalDisk(StoragePoolType.Linstor, pDSTOUUID, diskLinPath)).thenReturn(vol);
6473+
when(vol.getPath()).thenReturn(path);
6474+
when(vol.getPool()).thenReturn(storagePool);
6475+
when(vol.getFormat()).thenReturn(PhysicalDiskFormat.RAW);
6476+
6477+
// 1. test Bus: IDE and broken qemu version -> NO discard
6478+
when(libvirtComputingResourceSpy.getHypervisorQemuVersion()).thenReturn(6000000L);
6479+
vmToDetails.put(VmDetailConstants.ROOT_DISK_CONTROLLER, DiskDef.DiskBus.IDE.name());
6480+
{
6481+
LibvirtVMDef vm = new LibvirtVMDef();
6482+
vm.addComp(new DevicesDef());
6483+
libvirtComputingResourceSpy.createVbd(connect, to, name, vm);
6484+
6485+
DiskDef rootDisk = vm.getDevices().getDisks().get(0);
6486+
assertEquals(DiskDef.DiskType.BLOCK, rootDisk.getDiskType());
6487+
assertEquals(DiskDef.DiskBus.IDE, rootDisk.getBusType());
6488+
assertEquals(DiskDef.DiscardType.IGNORE, rootDisk.getDiscard());
6489+
}
6490+
6491+
// 2. test Bus: VIRTIO and broken qemu version -> discard unmap
6492+
vmToDetails.put(VmDetailConstants.ROOT_DISK_CONTROLLER, DiskDef.DiskBus.VIRTIO.name());
6493+
{
6494+
LibvirtVMDef vm = new LibvirtVMDef();
6495+
vm.addComp(new DevicesDef());
6496+
libvirtComputingResourceSpy.createVbd(connect, to, name, vm);
6497+
6498+
DiskDef rootDisk = vm.getDevices().getDisks().get(0);
6499+
assertEquals(DiskDef.DiskType.BLOCK, rootDisk.getDiskType());
6500+
assertEquals(DiskDef.DiskBus.VIRTIO, rootDisk.getBusType());
6501+
assertEquals(DiskDef.DiscardType.UNMAP, rootDisk.getDiscard());
6502+
}
6503+
6504+
// 3. test Bus; IDE and "good" qemu version -> discard unmap
6505+
vmToDetails.put(VmDetailConstants.ROOT_DISK_CONTROLLER, DiskDef.DiskBus.IDE.name());
6506+
when(libvirtComputingResourceSpy.getHypervisorQemuVersion()).thenReturn(7000000L);
6507+
{
6508+
LibvirtVMDef vm = new LibvirtVMDef();
6509+
vm.addComp(new DevicesDef());
6510+
libvirtComputingResourceSpy.createVbd(connect, to, name, vm);
6511+
6512+
DiskDef rootDisk = vm.getDevices().getDisks().get(0);
6513+
assertEquals(DiskDef.DiskType.BLOCK, rootDisk.getDiskType());
6514+
assertEquals(DiskDef.DiskBus.IDE, rootDisk.getBusType());
6515+
assertEquals(DiskDef.DiscardType.UNMAP, rootDisk.getDiscard());
6516+
}
6517+
6518+
// 4. test Bus: VIRTIO and "good" qemu version -> discard unmap
6519+
vmToDetails.put(VmDetailConstants.ROOT_DISK_CONTROLLER, DiskDef.DiskBus.VIRTIO.name());
6520+
{
6521+
LibvirtVMDef vm = new LibvirtVMDef();
6522+
vm.addComp(new DevicesDef());
6523+
libvirtComputingResourceSpy.createVbd(connect, to, name, vm);
6524+
6525+
DiskDef rootDisk = vm.getDevices().getDisks().get(0);
6526+
assertEquals(DiskDef.DiskType.BLOCK, rootDisk.getDiskType());
6527+
assertEquals(DiskDef.DiskBus.VIRTIO, rootDisk.getBusType());
6528+
assertEquals(DiskDef.DiscardType.UNMAP, rootDisk.getDiscard());
6529+
}
6530+
}
64186531
}

plugins/storage/volume/linstor/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to Linstor CloudStack plugin will be documented in this file
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2024-10-28]
9+
10+
### Fixed
11+
12+
- Disable discard="unmap" for ide devices and qemu < 7.0
13+
https://bugzilla.redhat.com/show_bug.cgi?id=2029980
14+
815
## [2024-10-04]
916

1017
### Added

0 commit comments

Comments
 (0)