diff options
author | Manuel Bentele | 2021-11-10 09:41:56 +0100 |
---|---|---|
committer | Manuel Bentele | 2021-11-10 09:41:56 +0100 |
commit | 9bd2d82999eac1d27c6fc14ebe6e0e298435b2b6 (patch) | |
tree | 3fbfa2e63bd34015542bc4ec94b30ec842a19245 | |
parent | Remove specified Libvirt OS Nvram file during QEMU VM upload (diff) | |
download | master-sync-shared-9bd2d82999eac1d27c6fc14ebe6e0e298435b2b6.tar.gz master-sync-shared-9bd2d82999eac1d27c6fc14ebe6e0e298435b2b6.tar.xz master-sync-shared-9bd2d82999eac1d27c6fc14ebe6e0e298435b2b6.zip |
Enumerate target names of disk devices correctly
2 files changed, 51 insertions, 7 deletions
diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java index 8c3a95b..706a083 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java @@ -245,10 +245,12 @@ public class VirtualizationConfigurationQemu extends VirtualizationConfiguration if ( storageDiskDevice == null ) { // HDD does not exist, so create new storage (HDD) device + final BusType devBusType = BusType.VIRTIO; + final String targetDevName = VirtualizationConfigurationQemuUtils.createDeviceName( this.vmConfig, + devBusType ); storageDiskDevice = this.vmConfig.addDiskStorageDevice(); storageDiskDevice.setReadOnly( false ); - storageDiskDevice.setBusType( BusType.VIRTIO ); - String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "vd", index ); + storageDiskDevice.setBusType( devBusType ); storageDiskDevice.setTargetDevice( targetDevName ); if ( diskImagePath == null || diskImagePath.isEmpty() ) { @@ -319,9 +321,11 @@ public class VirtualizationConfigurationQemu extends VirtualizationConfiguration if ( floppyDiskDevice == null ) { // floppy device does not exist, so create new floppy device + final BusType devBusType = BusType.FDC; + final String targetDevName = VirtualizationConfigurationQemuUtils.createDeviceName( this.vmConfig, + devBusType ); floppyDiskDevice = this.vmConfig.addDiskFloppyDevice(); - floppyDiskDevice.setBusType( BusType.FDC ); - String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "fd", index ); + floppyDiskDevice.setBusType( devBusType ); floppyDiskDevice.setTargetDevice( targetDevName ); floppyDiskDevice.setReadOnly( readOnly ); @@ -364,9 +368,11 @@ public class VirtualizationConfigurationQemu extends VirtualizationConfiguration if ( cdromDiskDevice == null ) { // CDROM device does not exist, so create new CDROM device + final BusType devBusType = BusType.SATA; + final String targetDevName = VirtualizationConfigurationQemuUtils.createDeviceName( this.vmConfig, + devBusType ); cdromDiskDevice = this.vmConfig.addDiskCdromDevice(); - cdromDiskDevice.setBusType( BusType.SATA ); - String targetDevName = VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( "sd", index ); + cdromDiskDevice.setBusType( devBusType ); cdromDiskDevice.setTargetDevice( targetDevName ); cdromDiskDevice.setReadOnly( true ); diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuUtils.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuUtils.java index 06091c0..1befdc4 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuUtils.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuUtils.java @@ -1,9 +1,11 @@ package org.openslx.virtualization.configuration; import java.util.ArrayList; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.openslx.libvirt.domain.Domain; import org.openslx.libvirt.domain.device.Disk; import org.openslx.libvirt.domain.device.Disk.BusType; import org.openslx.virtualization.Version; @@ -109,7 +111,7 @@ public class VirtualizationConfigurationQemuUtils * @param deviceNumber number of the device. * @return alphabetical device name. */ - public static String createAlphabeticalDeviceName( String devicePrefix, int deviceNumber ) + private static String createAlphabeticalDeviceName( String devicePrefix, int deviceNumber ) { if ( deviceNumber < 0 || deviceNumber >= ( 'z' - 'a' ) ) { String errorMsg = new String( "Device number is out of range to be able to create a valid device name." ); @@ -120,6 +122,42 @@ public class VirtualizationConfigurationQemuUtils } /** + * Creates an alphabetical device name for a disk device with a bus <i>type</i> that is unique in + * a Libvirt domain XML configuration. + * + * @param config Libvirt domain XML configuration. + * @param type device type for device name. + * @return alphabetical device name. + */ + public static String createDeviceName( final Domain config, final BusType type ) throws IllegalArgumentException + { + final String devicePrefix; + final int deviceNumber; + + switch ( type ) { + case FDC: + devicePrefix = "fd"; + break; + case IDE: + devicePrefix = "hd"; + break; + case SATA: + devicePrefix = "sd"; + break; + case VIRTIO: + devicePrefix = "vd"; + break; + default: + return null; + } + + final Predicate<Disk> bySpecifiedBusType = d -> d.getBusType() == type; + deviceNumber = Long.valueOf( config.getDiskDevices().stream().filter( bySpecifiedBusType ).count() ).intValue(); + + return VirtualizationConfigurationQemuUtils.createAlphabeticalDeviceName( devicePrefix, deviceNumber ); + } + + /** * Data container to store a Libvirt/QEMU machine name with version information. * * @author Manuel Bentele |