summaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorChristopher Lucas2018-12-05 17:45:04 +0100
committerChristopher Lucas2018-12-05 17:45:04 +0100
commit888f9e490cc3f03cc9399f779344c25d3082eafa (patch)
tree041d8a5a6dc02c1647cf0d8ec764873f1c5e14b4 /src/main/java
parent[thrift] Sat interface for getting predefined shares/filters (diff)
downloadmaster-sync-shared-888f9e490cc3f03cc9399f779344c25d3082eafa.tar.gz
master-sync-shared-888f9e490cc3f03cc9399f779344c25d3082eafa.tar.xz
master-sync-shared-888f9e490cc3f03cc9399f779344c25d3082eafa.zip
Upload and Download image with config file working
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/openslx/util/vm/DiskImage.java42
-rw-r--r--src/main/java/org/openslx/util/vm/QemuMetaData.java577
-rw-r--r--src/main/java/org/openslx/util/vm/VboxConfig.java4
-rw-r--r--src/main/java/org/openslx/util/vm/VmMetaData.java640
-rw-r--r--src/main/java/org/openslx/util/vm/VmwareMetaData.java1097
5 files changed, 1236 insertions, 1124 deletions
diff --git a/src/main/java/org/openslx/util/vm/DiskImage.java b/src/main/java/org/openslx/util/vm/DiskImage.java
index fc57e79..ca016f2 100644
--- a/src/main/java/org/openslx/util/vm/DiskImage.java
+++ b/src/main/java/org/openslx/util/vm/DiskImage.java
@@ -19,7 +19,7 @@ public class DiskImage
*/
private static final int VMDK_MAGIC = 0x4b444d56;
private static final int VDI_MAGIC = 0x7f10dabe;
- private static final String QEMU = "QFI";
+ private static final int QEMU_MAGIC = 0x514649fb;
public enum ImageFormat
{
@@ -152,22 +152,30 @@ public class DiskImage
return;
}
- // TODO: qcow
- file.seek( 0 );
- byte[] qcowBuffer = new byte[ QEMU.length() ];
- file.readFully( qcowBuffer );
- String qcowString = new String( qcowBuffer );
- if ( QEMU.equals( qcowString ) ) {
- // dummy values
- this.isStandalone = true;
- this.isCompressed = false;
- this.isSnapshot = false;
- this.format = ImageFormat.QCOW2;
- this.subFormat = null;
- this.diskDescription = null;
- this.hwVersion = 0;
- return;
- }
+ file.seek(0);
+ //TODO: Standalone & Snapshot
+ if (file.readInt() == QEMU_MAGIC) {
+ //skip the next 14 ints as they don't interest us
+ // - QFI version (4 bytes)
+ // - backing file offset (8 bytes)
+ // - backing (8 bytes)
+ // - crypt method (4 bytes)
+ // - l1 size (4 bytes)
+ // - l1 table offset (8 bytes)
+ // - refcount table offset (8 bytes)
+ // - refcount cluster (4 bytes)
+ file.skipBytes( 14 * 4 );
+ this.isSnapshot = file.readInt() > 0;
+ //skip the next 14 ints as they don't interest us
+ file.seek(374);
+ this.isCompressed = file.read() == 1;
+ this.diskDescription = null;
+ this.format = ImageFormat.QCOW2;
+ this.isStandalone = true;
+ this.subFormat = null;
+ this.hwVersion = 0;
+ return;
+ }
}
throw new UnknownImageFormatException();
}
diff --git a/src/main/java/org/openslx/util/vm/QemuMetaData.java b/src/main/java/org/openslx/util/vm/QemuMetaData.java
index dcb3b68..cf3b550 100644
--- a/src/main/java/org/openslx/util/vm/QemuMetaData.java
+++ b/src/main/java/org/openslx/util/vm/QemuMetaData.java
@@ -1,225 +1,382 @@
package org.openslx.util.vm;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.TreeMap;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.thrifthelper.TConst;
-import org.openslx.util.vm.DiskImage.ImageFormat;
-import org.openslx.util.vm.DiskImage.UnknownImageFormatException;
-
-public class QemuMetaData extends VmMetaData<VBoxSoundCardMeta, VBoxDDAccelMeta, VBoxHWVersionMeta, VBoxEthernetDevTypeMeta>
-{
-
- private final Map<String, String> arguments = new HashMap<String, String>();
- // the above map's elements will take the place of <args> in the config string
- private String config;
- private static final Logger LOGGER = Logger.getLogger( QemuMetaData.class );
-
- private static final Virtualizer virtualizer = new Virtualizer( TConst.VIRT_QEMU, "QEMU-KVM" );
-
- public QemuMetaData( List<OperatingSystem> osList, File file ) throws FileNotFoundException, IOException, UnsupportedVirtualizerFormatException
- {
- super( osList );
- DiskImage di;
- try {
- di = new DiskImage( file );
- } catch ( UnknownImageFormatException e ) {
- di = null;
- }
- if ( di == null || di.format != ImageFormat.QCOW2 ) {
- throw new UnsupportedVirtualizerFormatException( "This is not a qcow2 disk image" );
- }
- config = "qemu-system-i386 <args> <image> -enable-kvm \n\r qemu-system-x86_64 <args> <image> -enable-kvm";
- displayName = file.getName().substring( 0, file.getName().indexOf( "." ) );
- setOs( "anyOs" );
- hdds.add( new HardDisk( "anychipset", DriveBusType.IDE, file.getAbsolutePath() ) );
- makeStartSequence();
- }
-
- // initiates the arguments map with a default working sequence that will later be used in the definition array
- public void makeStartSequence()
- {
- arguments.put( "cpu", "host" );
- arguments.put( "smp", "2" );
- arguments.put( "m", "1024" );
- arguments.put( "vga", "std" );
- }
-
- private String configWithArgs()
- {
- String tempString = "";
- for ( String key : arguments.keySet() ) {
- tempString += "-" + key + " " + arguments.get( key ) + " ";
- }
- return config.replaceAll( "<args>", tempString );
- }
-
- @Override
- public byte[] getFilteredDefinitionArray()
- {
- return configWithArgs().getBytes( StandardCharsets.UTF_8 );
- }
-
- @Override
- public void applySettingsForLocalEdit()
- {
- }
-
- @Override
- public boolean addHddTemplate( File diskImage, String hddMode, String redoDir )
- {
- String tempS = config.replaceAll( "<image>", diskImage.getAbsolutePath() );
- config = tempS;
- hdds.add( new HardDisk( "anychipset", DriveBusType.IDE, diskImage.getAbsolutePath() ) );
- return true;
- }
-
- @Override
- public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir )
- {
- String tempS = config.replaceAll( "<image>", diskImagePath );
- config = tempS;
- hdds.add( new HardDisk( "anychipset", DriveBusType.IDE, diskImagePath ) );
- return true;
- }
-
- @Override
- public boolean addDefaultNat()
- {
- return true;
- }
-
- @Override
- public void setOs( String vendorOsId )
- {
- setOs( TConst.VIRT_QEMU, vendorOsId );
- }
-
- @Override
- public boolean addDisplayName( String name )
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean addRam( int mem )
- {
- this.arguments.put( "m", Integer.toString( mem ) );
- return true;
- }
-
- @Override
- public void addFloppy( int index, String image, boolean readOnly )
- {
- // TODO Auto-generated method stub
-
- }
-
- @Override
- public boolean addCdrom( String image )
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean addCpuCoreCount( int nrOfCores )
- {
- this.arguments.put( "smp", Integer.toString( nrOfCores ) );
- return true;
- }
-
- @Override
- public void setSoundCard( VmMetaData.SoundCardType type )
- {
- }
-
- @Override
- public VmMetaData.SoundCardType getSoundCard()
- {
- return null;
- }
-
- @Override
- public void setDDAcceleration( VmMetaData.DDAcceleration type )
- {
- }
-
- @Override
- public VmMetaData.DDAcceleration getDDAcceleration()
- {
- return null;
- }
-
- @Override
- public void setHWVersion( VmMetaData.HWVersion type )
- {
- }
-
- @Override
- public VmMetaData.HWVersion getHWVersion()
- {
- return null;
- }
-
- @Override
- public void setEthernetDevType( int cardIndex, VmMetaData.EthernetDevType type )
- {
- }
-
- @Override
- public VmMetaData.EthernetDevType getEthernetDevType( int cardIndex )
- {
- return null;
- }
-
- @Override
- public byte[] getDefinitionArray()
- {
- return configWithArgs().getBytes( StandardCharsets.UTF_8 );
- }
-
- @Override
- public boolean addEthernet( VmMetaData.EtherType type )
- {
- return false;
- }
-
- @Override
- public Virtualizer getVirtualizer()
- {
- return virtualizer;
- }
-
- @Override
- public void enableUsb( boolean enabled )
- {
- // TODO test this properly
- if ( enabled ) {
- arguments.put( "usb", "" );
- } else {
- arguments.remove( "usb" );
- }
- }
-
- @Override
- public boolean disableSuspend()
- {
- return false;
- }
-
- @Override
- public void registerVirtualHW()
- {
- }
+import org.openslx.util.vm.QemuConfig.Header;
+import static org.openslx.util.vm.QemuConfig.Header.MACHINE;
+
+class QemuDDAccelMeta {
+
+ public final boolean isPresent;
+
+ public QemuDDAccelMeta(boolean present) {
+ isPresent = present;
+ }
+}
+
+class QemuHWVersionMeta {
+
+ public final int version;
+
+ public QemuHWVersionMeta(int vers) {
+ version = vers;
+ }
+}
+
+class QemuEthernetDevTypeMeta {
+
+ public final String value;
+
+ public QemuEthernetDevTypeMeta(String val) {
+ value = val;
+ }
+}
+
+class QemuSoundCardMeta {
+
+ public final boolean isPresent;
+ public final String value;
+
+ public QemuSoundCardMeta(boolean present, String val) {
+
+ isPresent = present;
+ value = val;
+ }
+}
+
+public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAccelMeta, QemuHWVersionMeta, QemuEthernetDevTypeMeta> {
+
+ private final Map<String, String> arguments = new HashMap<>(); //to remove at the end. easier
+ // the above map's elements will take the place of <args> in the config string
+ private QemuConfig config;
+
+ private String setup;
+
+ private TreeMap<String, String> option;
+
+ private static final Logger LOGGER = Logger.getLogger(QemuMetaData.class);
+
+ private static final Virtualizer virtualizer = new Virtualizer(TConst.VIRT_QEMU, "QEMU-KVM");
+
+ private int cdromCounter = 0;
+
+ private int driveCounter = 0;
+
+ private int floppyCounter = 0;
+
+ public static enum EthernetType {
+ NAT("qnet1"), BRIDGED("qnet0"), HOST_ONLY("qnet2");
+
+ public final String vmnet;
+
+ private EthernetType(String vnet) {
+ this.vmnet = vnet;
+ }
+ }
+
+ public QemuMetaData(List<OperatingSystem> osList, File config) {
+ super(osList);
+ this.config = new QemuConfig(config);
+ init();
+ }
+
+ public QemuMetaData(List<OperatingSystem> osList, byte[] vmContent, int length) throws IOException, UnsupportedVirtualizerFormatException {
+ super(osList);
+ this.config = new QemuConfig(vmContent, length);
+ init();
+ }
+
+ public void init() {
+ registerVirtualHW();
+ displayName = config.getDisplayName();
+ setOs(config.getOsName());
+ isMachineSnapshot = config.isMachineSnapshot();
+ config.setHdds();
+ for (HardDisk hardDisk : config.getHdds()) {
+ hdds.add(hardDisk);
+ }
+ }
+
+
+ @Override
+ public byte[] getFilteredDefinitionArray() {
+ return config.toString(false).getBytes(StandardCharsets.UTF_8);
+ }
+
+ @Override
+ public byte[] getDefinitionArray() {
+ return config.toString(true).getBytes(StandardCharsets.UTF_8);
+ }
+
+ @Override
+ public void applySettingsForLocalEdit() {
+ //nothing for VmWare
+ }
+
+ @Override
+ public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) {
+ option = new TreeMap<>();
+ String bus = "anychipset";
+ DriveBusType busType = DriveBusType.IDE;
+ //drive
+ option = config.get("[drive \"disk0\"]");
+ if (option != null) {
+ option.replace("file", "\"" + diskImage.getAbsolutePath() + "\"");
+ bus = option.get("if");
+ }
+ switch (bus) {
+ case "ide":
+ busType = DriveBusType.IDE;
+ break;
+ case "scsi":
+ busType = DriveBusType.SCSI;
+ break;
+ case "sata":
+ //not available for Qemu. Others : sd, mtd, floppy, pflash, virtio
+ break;
+ default:
+ break;
+ }
+ //device
+ option = config.get("[dev0]");
+ hdds.add(new HardDisk(option.get("driver"), busType, diskImage.getAbsolutePath()));
+ return true;
+ }
+
+ @Override
+ public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) {
+ option = new TreeMap<>();
+
+ //drive
+ option.put("file", "\"" + diskImagePath + "\"");
+ option.put("if", "\"scsi\"");
+ String status = (isMachineSnapshot == true ? "on" : "off");
+ option.put("snapshot", "\"" + status + "\"");
+ config.set("[drive \"drive" + driveCounter + "\"]", option);
+
+ //device
+ option.put("drive", "\"drive" + driveCounter + "\"");
+ option.put("driver", "scsi-hd");
+ config.set("[device]", option);
+
+ hdds.add(new HardDisk("anychipset", DriveBusType.IDE, diskImagePath));
+ driveCounter++;
+ return true;
+ }
+
+ @Override
+ public boolean addDefaultNat() {
+ //No arguments needed, default on nat. But cannot receive
+ return true;
+ }
+
+ @Override
+ public void setOs(String vendorOsId) {
+ setOs(TConst.VIRT_QEMU, vendorOsId);
+ }
+
+ @Override
+ public boolean addDisplayName(String name) {
+ option = new TreeMap<>();
+ option.put("guest", "\"" + name + "\"");
+ config.set("[name]", option);
+ return true;
+ }
+
+ @Override
+ public boolean addRam(int mem) {
+ option = new TreeMap<>();
+ option.put("size", "\"" + mem + "\"");
+ config.set("[memory]", option);
+ return true;
+ }
+
+ @Override
+ public void addFloppy(int index, String image, boolean readOnly) {
+ option = new TreeMap<>();
+ if (readOnly) {
+ option.put("readonly", "\"on\"");
+ } else {
+ option.put("readonly", "\"off\"");
+ }
+ option.put("if", "\"floppy\"");
+ option.put("file", image);
+ config.set("[drive \"floppy" + floppyCounter + "\"]", option);
+ floppyCounter++;
+ }
+
+ @Override
+ public boolean addCdrom(String image) {
+ option = new TreeMap<>();
+ option.put("media", "\"cdrom\"");
+ option.put("file", image);
+ config.set("[drive \"cdrom" + cdromCounter + "\"]", option);
+ cdromCounter++;
+ return true;
+ }
+
+ @Override
+ public boolean addCpuCoreCount(int nrOfCores) {
+ option = new TreeMap<>();
+ option.put("cpus", "\"" + nrOfCores + "\"");
+ config.set("[smp-opts]", option);
+ return true;
+ }
+
+ @Override
+ public void setSoundCard(VmMetaData.SoundCardType type) {
+ //Not possible will be set as comment in config file
+ }
+
+ @Override
+ public VmMetaData.SoundCardType getSoundCard() {
+ //not possible to set just write comment
+ VmMetaData.SoundCardType soundcard = null;
+ return soundcard;
+ }
+
+ @Override
+ public void setDDAcceleration(VmMetaData.DDAcceleration type) {
+ //Not really used by qemu. TODO:
+ }
+
+ @Override
+ public VmMetaData.DDAcceleration getDDAcceleration() {
+ return DDAcceleration.OFF;
+ }
+
+ @Override
+ public void setHWVersion(VmMetaData.HWVersion type) {
+ //nothing...
+ }
+
+ @Override
+ public VmMetaData.HWVersion getHWVersion() {
+ return VmMetaData.HWVersion.DEFAULT;
+ }
+
+ @Override
+ public void setEthernetDevType(int cardIndex, VmMetaData.EthernetDevType type) {
+ QemuEthernetDevTypeMeta dev = networkCards.get(type);
+ option = new TreeMap<>();
+ option.put("driver", "\"" + dev.value + "\"");
+ option.put("netdev", "\"net" + cardIndex + "\"");
+ config.set("[device]", option);
+ }
+
+ @Override
+ public VmMetaData.EthernetDevType getEthernetDevType(int cardIndex) {
+ QemuEthernetDevTypeMeta ethernetDevTypeMeta = null;
+ for (String key : config.get().keySet()) {
+ if (key.equals("[dev" + cardIndex + "]")) {//wont work dev0
+ if (config.get(key).get("netdev").equals("\"net" + cardIndex + "\"")) {
+ String devs = config.get(key).get("driver");
+
+ for (EthernetDevType type : VmMetaData.EthernetDevType.values()) {
+ ethernetDevTypeMeta = networkCards.get(type);
+ if (ethernetDevTypeMeta == null) {
+ continue;
+ }
+ if (devs.equals(ethernetDevTypeMeta.value)) {
+ return type;
+ }
+ }
+ }
+ }
+ }
+ return EthernetDevType.AUTO;
+ }
+
+ @Override
+ public boolean addEthernet(VmMetaData.EtherType type) {
+ boolean ret = false;
+ int index = 0;
+ for (;; ++index) {
+ TreeMap<String, String> dev = config.get("[dev" + index + "]");
+
+ if (dev == null) {
+ break;
+ } else {
+ if (dev.get("netdev") == null) {
+ break;
+ }
+ }
+ }
+ switch (type) {
+ case NAT:
+ //netdev
+ option = new TreeMap<>();
+ option.put("br", "nat1");
+ option.put("type", "bridge");
+ config.set("[netdev \"net" + index + "\"]", option);
+ //device
+ option = new TreeMap<>();
+ break;
+ case BRIDGED:
+ option = new TreeMap<>();
+ option.put("br", "'br0'");
+ option.put("type", "'bridge'");
+ config.set("[netdev \"net" + index + "\"]", option);
+ break;
+ case HOST_ONLY:
+ //Dont know how to do it...
+ break;
+ default:
+ // Should not come to this...
+ break;
+ }
+ return ret;
+ }
+
+ @Override
+ public Virtualizer getVirtualizer() {
+ return virtualizer;
+ }
+
+ @Override
+ public void enableUsb(boolean enabled) {
+ option = new TreeMap<>();
+ if (enabled) {
+ option.put("usb", "\"on\"");
+ } else {
+ option.put("usb", "\"off\"");
+ }
+ config.set(MACHINE.value(), option);
+ }
+
+ @Override
+ public boolean disableSuspend() {
+ return false;
+ }
+
+ @Override
+ public void registerVirtualHW() {
+ soundCards.put(VmMetaData.SoundCardType.NONE, new QemuSoundCardMeta(false, null));
+ soundCards.put(VmMetaData.SoundCardType.DEFAULT, new QemuSoundCardMeta(true, "ich6"));
+ soundCards.put(VmMetaData.SoundCardType.AC, new QemuSoundCardMeta(true, "ac97"));
+ soundCards.put(VmMetaData.SoundCardType.ES, new QemuSoundCardMeta(true, "es1370"));
+ soundCards.put(VmMetaData.SoundCardType.SOUND_BLASTER, new QemuSoundCardMeta(true, "sb16"));
+
+ ddacc.put(DDAcceleration.OFF, new QemuDDAccelMeta(false));
+ ddacc.put(DDAcceleration.ON, new QemuDDAccelMeta(true));
+
+ hwversion.put(HWVersion.DEFAULT, new QemuHWVersionMeta(0));
+
+ networkCards.put(EthernetDevType.VIRTIO, new QemuEthernetDevTypeMeta("virtio-net-pci"));
+ networkCards.put(EthernetDevType.E1000, new QemuEthernetDevTypeMeta("e1000"));
+ networkCards.put(EthernetDevType.PCNET32, new QemuEthernetDevTypeMeta("pcnet"));
+ networkCards.put(EthernetDevType.RTL8139, new QemuEthernetDevTypeMeta("rtl8139"));
+
+ }
}
diff --git a/src/main/java/org/openslx/util/vm/VboxConfig.java b/src/main/java/org/openslx/util/vm/VboxConfig.java
index fe85638..9d6e6f1 100644
--- a/src/main/java/org/openslx/util/vm/VboxConfig.java
+++ b/src/main/java/org/openslx/util/vm/VboxConfig.java
@@ -101,7 +101,9 @@ public class VboxConfig
}
} catch ( SAXException e ) {
LOGGER.error( "Selected vbox file was not validated against the XSD schema: " + e.getMessage() );
- }
+ } catch ( Exception e) {
+ LOGGER.error( "Some error occured while trying to parse select virtualbox machine files: " + e.getMessage() );
+ }
// valid xml, try to create the DOM
doc = XmlHelper.parseDocumentFromStream( new FileInputStream( file ) );
doc = XmlHelper.removeFormattingNodes( doc );
diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java
index aafb6a4..b260bc1 100644
--- a/src/main/java/org/openslx/util/vm/VmMetaData.java
+++ b/src/main/java/org/openslx/util/vm/VmMetaData.java
@@ -18,343 +18,329 @@ import org.openslx.bwlp.thrift.iface.Virtualizer;
* Describes a configured virtual machine. This class is parsed from a machine
* description, like a *.vmx for VMware machines.
*/
-public abstract class VmMetaData<T, U, V, W>
-{
- private static final Logger LOGGER = Logger.getLogger( VmMetaData.class );
+public abstract class VmMetaData<T, U, V, W> {
- /*
+ private static final Logger LOGGER = Logger.getLogger(VmMetaData.class);
+
+ /*
* Helper types
- */
- protected Map<SoundCardType, T> soundCards = new HashMap<>();
- protected Map<DDAcceleration, U> ddacc = new HashMap<>();
- protected Map<HWVersion, V> hwversion = new HashMap<>();
- protected Map<EthernetDevType, W> networkCards = new HashMap<>();
-
- /**
- * Virtual sound cards types
- */
- public static enum SoundCardType
- {
- NONE( "None" ), DEFAULT( "(default)" ), SOUND_BLASTER( "Sound Blaster 16" ), ES( "ES 1371" ), HD_AUDIO( "Intel Integrated HD Audio" ), AC( "Intel ICH Audio Codec 97" );
-
- public final String displayName;
-
- private SoundCardType( String dName )
- {
- this.displayName = dName;
- }
- }
-
- /**
- * 3D acceleration types
- */
- public static enum DDAcceleration
- {
- OFF( "Off" ), ON( "On" );
-
- public final String displayName;
-
- private DDAcceleration( String dName )
- {
- this.displayName = dName;
- }
- }
-
- /**
- * Virtual hardware version - currently only in use for VMPlayer
- */
- public static enum HWVersion
- {
- NONE( "(invalid)" ), THREE( " 3 (Workstation 4/5, Player 1)" ), FOUR( " 4 (Workstation 4/5, Player 1/2, Fusion 1)" ), SIX( " 6 (Workstation 6)" ), SEVEN(
- " 7 (Workstation 6.5/7, Player 3, Fusion 2/3)" ), EIGHT( " 8 (Workstation 8, Player/Fusion 4)" ), NINE( " 9 (Workstation 9, Player/Fusion 5)" ), TEN(
- "10 (Workstation 10, Player/Fusion 6)" ), ELEVEN(
- "11 (Workstation 11, Player/Fusion 7)" ), TWELVE( "12 (Workstation/Player 12, Fusion 8)" ), DEFAULT( "default" );
-
- public final String displayName;
-
- private HWVersion( String dName )
- {
- this.displayName = dName;
- }
- }
-
- /**
- * Virtual network cards
- */
- public static enum EthernetDevType
- {
- AUTO( "(default)" ), PCNET32( "AMD PCnet32" ), E1000( "Intel E1000 (PCI)" ), E1000E( "Intel E1000e (PCI-Express)" ), VMXNET( "VMXnet" ), VMXNET3( "VMXnet 3" ), PCNETPCI2(
- "PCnet-PCI II" ), PCNETFAST3( "PCnet-FAST III" ), PRO1000MTD( "Intel PRO/1000 MT Desktop" ), PRO1000TS(
- "Intel PRO/1000 T Server" ), PRO1000MTS( "Intel PRO/1000 MT Server" ), PARAVIRT( "Paravirtualized Network" ), NONE( "No Network Card" );
-
- public final String displayName;
-
- private EthernetDevType( String dName )
- {
- this.displayName = dName;
- }
- }
-
- public static enum DriveBusType
- {
- SCSI, IDE, SATA;
- }
-
- public static class HardDisk
- {
- public final String chipsetDriver;
- public final DriveBusType bus;
- public final String diskImage;
-
- public HardDisk( String chipsetDriver, DriveBusType bus, String diskImage )
- {
- this.chipsetDriver = chipsetDriver;
- this.bus = bus;
- this.diskImage = diskImage;
- }
- }
-
- public static enum EtherType
- {
- NAT, BRIDGED, HOST_ONLY;
- }
- /*
+ */
+ protected Map<SoundCardType, T> soundCards = new HashMap<>();
+ protected Map<DDAcceleration, U> ddacc = new HashMap<>();
+ protected Map<HWVersion, V> hwversion = new HashMap<>();
+ protected Map<EthernetDevType, W> networkCards = new HashMap<>();
+
+ /**
+ * Virtual sound cards types
+ */
+ public static enum SoundCardType {
+ NONE("None"), DEFAULT("(default)"), SOUND_BLASTER("Sound Blaster 16"), ES("ES 1371"), HD_AUDIO("Intel Integrated HD Audio"), AC("Intel ICH Audio Codec 97");
+
+ public final String displayName;
+
+ private SoundCardType(String dName) {
+ this.displayName = dName;
+ }
+ }
+
+ /**
+ * 3D acceleration types
+ */
+ public static enum DDAcceleration {
+ OFF("Off"), ON("On");
+
+ public final String displayName;
+
+ private DDAcceleration(String dName) {
+ this.displayName = dName;
+ }
+ }
+
+ /**
+ * Virtual hardware version - currently only in use for VMPlayer
+ */
+ public static enum HWVersion {
+ NONE("(invalid)"), THREE(" 3 (Workstation 4/5, Player 1)"), FOUR(" 4 (Workstation 4/5, Player 1/2, Fusion 1)"), SIX(" 6 (Workstation 6)"), SEVEN(
+ " 7 (Workstation 6.5/7, Player 3, Fusion 2/3)"), EIGHT(" 8 (Workstation 8, Player/Fusion 4)"), NINE(" 9 (Workstation 9, Player/Fusion 5)"), TEN(
+ "10 (Workstation 10, Player/Fusion 6)"), ELEVEN(
+ "11 (Workstation 11, Player/Fusion 7)"), TWELVE("12 (Workstation/Player 12, Fusion 8)"), DEFAULT("default");
+
+ public final String displayName;
+
+ private HWVersion(String dName) {
+ this.displayName = dName;
+ }
+ }
+
+ /**
+ * Virtual network cards
+ */
+ public static enum EthernetDevType {
+ AUTO("(default)"), PCNET32("AMD PCnet32"), E1000("Intel E1000 (PCI)"), E1000E("Intel E1000e (PCI-Express)"), VMXNET("VMXnet"), VMXNET3("VMXnet 3"), PCNETPCI2(
+ "PCnet-PCI II"), PCNETFAST3("PCnet-FAST III"), PRO1000MTD("Intel PRO/1000 MT Desktop"), PRO1000TS(
+ "Intel PRO/1000 T Server"), PRO1000MTS("Intel PRO/1000 MT Server"), PARAVIRT("Paravirtualized Network"), VIRTIO("Virtual High Performance Ethernet card"), RTL8139("Realtek Fast Ethernet "), NONE("No Network Card");
+
+ public final String displayName;
+
+ private EthernetDevType(String dName) {
+ this.displayName = dName;
+ }
+ }
+
+ public static enum DriveBusType {
+ SCSI, IDE, SATA;
+ }
+
+ public static class HardDisk {
+
+ public final String chipsetDriver;
+ public final DriveBusType bus;
+ public final String diskImage;
+
+ public HardDisk(String chipsetDriver, DriveBusType bus, String diskImage) {
+ this.chipsetDriver = chipsetDriver;
+ this.bus = bus;
+ this.diskImage = diskImage;
+ }
+ }
+
+ public static enum EtherType {
+ NAT, BRIDGED, HOST_ONLY;
+ }
+ /*
* Members
- */
+ */
- protected final List<HardDisk> hdds = new ArrayList<>();
+ protected final List<HardDisk> hdds = new ArrayList<>();
- private final List<OperatingSystem> osList;
+ private final List<OperatingSystem> osList;
- private OperatingSystem os = null;
+ private OperatingSystem os = null;
- protected String displayName = null;
+ protected String displayName = null;
- protected boolean isMachineSnapshot;
+ protected boolean isMachineSnapshot;
- /*
+ /*
* Getters for virtual hardware
- */
- public List<SoundCardType> getSupportedSoundCards()
- {
- ArrayList<SoundCardType> availables = new ArrayList<SoundCardType>( soundCards.keySet() );
- Collections.sort( availables );
- return availables;
- }
-
- public List<DDAcceleration> getSupportedDDAccs()
- {
- ArrayList<DDAcceleration> availables = new ArrayList<DDAcceleration>( ddacc.keySet() );
- Collections.sort( availables );
- return availables;
- }
-
- public List<HWVersion> getSupportedHWVersions()
- {
- ArrayList<HWVersion> availables = new ArrayList<HWVersion>( hwversion.keySet() );
- Collections.sort( availables );
- return availables;
- }
-
- public List<EthernetDevType> getSupportedEthernetDevices()
- {
- ArrayList<EthernetDevType> availables = new ArrayList<EthernetDevType>( networkCards.keySet() );
- Collections.sort( availables );
- return availables;
- }
-
- /**
- * Get operating system of this VM.
- */
- public OperatingSystem getOs()
- {
- return os;
- }
-
- /**
- * Get all hard disks of this VM.
- */
- public List<HardDisk> getHdds()
- {
- return Collections.unmodifiableList( hdds );
- }
-
- /**
- * Get display name of VM.
- */
- public String getDisplayName()
- {
- return displayName;
- }
-
- /*
+ */
+ public List<SoundCardType> getSupportedSoundCards() {
+ ArrayList<SoundCardType> availables = new ArrayList<SoundCardType>(soundCards.keySet());
+ Collections.sort(availables);
+ return availables;
+ }
+
+ public List<DDAcceleration> getSupportedDDAccs() {
+ ArrayList<DDAcceleration> availables = new ArrayList<DDAcceleration>(ddacc.keySet());
+ Collections.sort(availables);
+ return availables;
+ }
+
+ public List<HWVersion> getSupportedHWVersions() {
+ ArrayList<HWVersion> availables = new ArrayList<HWVersion>(hwversion.keySet());
+ Collections.sort(availables);
+ return availables;
+ }
+
+ public List<EthernetDevType> getSupportedEthernetDevices() {
+ ArrayList<EthernetDevType> availables = new ArrayList<EthernetDevType>(networkCards.keySet());
+ Collections.sort(availables);
+ return availables;
+ }
+
+ /**
+ * Get operating system of this VM.
+ */
+ public OperatingSystem getOs() {
+ return os;
+ }
+
+ /**
+ * Get all hard disks of this VM.
+ */
+ public List<HardDisk> getHdds() {
+ return Collections.unmodifiableList(hdds);
+ }
+
+ /**
+ * Get display name of VM.
+ */
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ /*
* Getter for isMachineSnapshot
- */
- public boolean isMachineSnapshot()
- {
- return isMachineSnapshot;
- }
-
- /**
- * This method should return a minimal representation of the input meta data.
- * The representation is platform dependent, and should be stripped of all
- * non-essential configuration, such as CD/DVD/FLoppy drives, serial or parallel
- * ports, shared folders, or anything else that could be considered sensible
- * information (absolute paths containing the local user's name).
- */
- public abstract byte[] getFilteredDefinitionArray();
-
- public final ByteBuffer getFilteredDefinition()
- {
- return ByteBuffer.wrap( getFilteredDefinitionArray() );
- }
-
- /*
+ */
+ public boolean isMachineSnapshot() {
+ return isMachineSnapshot;
+ }
+
+ /**
+ * This method should return a minimal representation of the input meta
+ * data. The representation is platform dependent, and should be stripped of
+ * all non-essential configuration, such as CD/DVD/FLoppy drives, serial or
+ * parallel ports, shared folders, or anything else that could be considered
+ * sensible information (absolute paths containing the local user's name).
+ */
+ public abstract byte[] getFilteredDefinitionArray();
+
+ public final ByteBuffer getFilteredDefinition() {
+ return ByteBuffer.wrap(getFilteredDefinitionArray());
+ }
+
+ /*
* Methods
- */
-
- public VmMetaData( List<OperatingSystem> osList )
- {
- this.osList = osList;
- }
-
- /**
- * Called from subclass to set the OS. If the OS cannot be determined from the
- * given parameters, it will not be set.
- *
- * @param virtId
- * virtualizer, eg "vmware" for VMware
- * @param virtOsId
- * the os identifier used by the virtualizer, eg. windows7-64 for
- * 64bit Windows 7 on VMware
- */
- protected final void setOs( String virtId, String virtOsId )
- {
- OperatingSystem lazyMatch = null;
- for ( OperatingSystem os : osList ) {
- if ( os.getVirtualizerOsId() == null )
- continue;
- for ( Entry<String, String> entry : os.getVirtualizerOsId().entrySet() ) {
- if ( !entry.getValue().equals( virtOsId ) )
- continue;
- if ( entry.getKey().equals( virtId ) ) {
- this.os = os;
- return;
- } else {
- lazyMatch = os;
- }
- }
- }
- this.os = lazyMatch;
- }
-
- /**
- * Apply config options that are desired when locally editing a VM. for vmware,
- * this disables automatic DPI scaling of the guest.
- */
- public abstract void applySettingsForLocalEdit();
-
- /**
- * Returns a VmMetaData instance of the given machine description given as file
- *
- * @param osList List of supported operating systems
- * @param file VM's machine description file to get the metadata instance from
- * @return VmMetaData object representing the relevant parts of the given machine description
- */
- public static VmMetaData<?, ?, ?, ?> getInstance( List<OperatingSystem> osList, File file )
- throws IOException
- {
- try {
- return new VmwareMetaData( osList, file );
- } catch ( UnsupportedVirtualizerFormatException e ) {
- LOGGER.info( "Not a VMware file", e );
- }
- try {
- return new VboxMetaData( osList, file );
- } catch ( UnsupportedVirtualizerFormatException e ) {
- LOGGER.info( "Not a VirtualBox file", e );
- }
- try {
- return new QemuMetaData( osList, file );
- } catch ( Exception e ) {
- LOGGER.info( "Not a QEmu file", e );
- }
- LOGGER.error( "Could not detect any known virtualizer format" );
- return null;
- }
-
- /**
- * Returns a VmMetaData instance of the given machine description given as a byte array
- *
- * @param osList List of supported operating systems
- * @param vmContent VM's machine description as byte array (e.g. stored in DB)
- * @param length length of the byte array given as vmContent
- * @return VmMetaData object representing the relevant parts of the given machine description
- * @throws IOException
- */
- public static VmMetaData<?, ?, ?, ?> getInstance( List<OperatingSystem> osList, byte[] vmContent, int length ) throws IOException
- {
- try {
- return new VmwareMetaData( osList, vmContent, length );
- } catch ( UnsupportedVirtualizerFormatException e ) {
- LOGGER.info( "Not a VMware file", e );
- }
- try {
- return new VboxMetaData( osList, vmContent, length );
- } catch ( UnsupportedVirtualizerFormatException e ) {
- LOGGER.info( "Not a VirtualBox file", e );
- }
- // TODO QEmu -- hack above expects qcow2 file, so we can't do anything here yet
- LOGGER.error( "Could not detect any known virtualizer format" );
- return null;
- }
-
- public abstract boolean addHddTemplate( File diskImage, String hddMode, String redoDir );
-
- public abstract boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir );
-
- public abstract boolean addDefaultNat();
-
- public abstract void setOs( String vendorOsId );
-
- public abstract boolean addDisplayName( String name );
-
- public abstract boolean addRam( int mem );
-
- public abstract void addFloppy( int index, String image, boolean readOnly );
-
- public abstract boolean addCdrom( String image );
-
- public abstract boolean addCpuCoreCount( int nrOfCores );
-
- public abstract void setSoundCard( SoundCardType type );
-
- public abstract SoundCardType getSoundCard();
-
- public abstract void setDDAcceleration( DDAcceleration type );
-
- public abstract DDAcceleration getDDAcceleration();
-
- public abstract void setHWVersion( HWVersion type );
-
- public abstract HWVersion getHWVersion();
-
- public abstract void setEthernetDevType( int cardIndex, EthernetDevType type );
-
- public abstract EthernetDevType getEthernetDevType( int cardIndex );
-
- public abstract byte[] getDefinitionArray();
-
- public abstract boolean addEthernet( EtherType type );
-
- public abstract Virtualizer getVirtualizer();
-
- public abstract void enableUsb( boolean enabled );
-
- public abstract boolean disableSuspend();
-
- /**
- * Function used to register virtual devices
- */
- public abstract void registerVirtualHW();
+ */
+ public VmMetaData(List<OperatingSystem> osList) {
+ this.osList = osList;
+ }
+
+ /**
+ * Called from subclass to set the OS. If the OS cannot be determined from
+ * the given parameters, it will not be set.
+ *
+ * @param virtId virtualizer, eg "vmware" for VMware
+ * @param virtOsId the os identifier used by the virtualizer, eg.
+ * windows7-64 for 64bit Windows 7 on VMware
+ */
+ protected final void setOs(String virtId, String virtOsId) {
+ OperatingSystem lazyMatch = null;
+ for (OperatingSystem os : osList) {
+ if (os.getVirtualizerOsId() == null) {
+ continue;
+ }
+ for (Entry<String, String> entry : os.getVirtualizerOsId().entrySet()) {
+ if (!entry.getValue().equals(virtOsId)) {
+ continue;
+ }
+ if (entry.getKey().equals(virtId)) {
+ this.os = os;
+ return;
+ } else {
+ lazyMatch = os;
+ }
+ }
+ }
+ this.os = lazyMatch;
+ }
+
+ /**
+ * Apply config options that are desired when locally editing a VM. for
+ * vmware, this disables automatic DPI scaling of the guest.
+ */
+ public abstract void applySettingsForLocalEdit();
+
+ /**
+ * Returns a VmMetaData instance of the given machine description given as
+ * file
+ *
+ * @param osList List of supported operating systems
+ * @param file VM's machine description file to get the metadata instance
+ * from
+ * @return VmMetaData object representing the relevant parts of the given
+ * machine description
+ */
+ public static VmMetaData<?, ?, ?, ?> getInstance(List<OperatingSystem> osList, File file)
+ throws IOException {
+ try {
+ return new VmwareMetaData(osList, file);
+ } catch (UnsupportedVirtualizerFormatException e) {
+ LOGGER.info("Not a VMware file", e);
+ }
+ try {
+ return new VboxMetaData(osList, file);
+ } catch (UnsupportedVirtualizerFormatException e) {
+ LOGGER.info("Not a VirtualBox file", e);
+ }
+ try {
+ return new QemuMetaData(osList, file);
+ } catch (Exception e) {
+ LOGGER.info("Not a QEmu file", e);
+ }
+ LOGGER.error("Could not detect any known virtualizer format");
+ return null;
+ }
+
+ /**
+ * Returns a VmMetaData instance of the given machine description given as a
+ * byte array
+ *
+ * @param osList List of supported operating systems
+ * @param vmContent VM's machine description as byte array (e.g. stored in
+ * DB)
+ * @param length length of the byte array given as vmContent
+ * @return VmMetaData object representing the relevant parts of the given
+ * machine description
+ * @throws IOException
+ */
+ public static VmMetaData<?, ?, ?, ?> getInstance(List<OperatingSystem> osList, byte[] vmContent, int length) throws IOException {
+ try {
+ return new VmwareMetaData(osList, vmContent, length);
+ } catch (UnsupportedVirtualizerFormatException e) {
+ LOGGER.info("Not a VMware file", e);
+ }
+ try {
+ return new VboxMetaData(osList, vmContent, length);
+ } catch (UnsupportedVirtualizerFormatException e) {
+ LOGGER.info("Not a VirtualBox file", e);
+ }
+ try {
+ return new QemuMetaData(osList, vmContent, length);
+ } catch (UnsupportedVirtualizerFormatException e) {
+ LOGGER.info("Not a VirtualBox file", e);
+ }
+ // TODO QEmu -- hack above expects qcow2 file, so we can't do anything here yet
+ LOGGER.error("Could not detect any known virtualizer format");
+ return null;
+ }
+
+ public abstract boolean addHddTemplate(File diskImage, String hddMode, String redoDir);
+
+ public abstract boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir);
+
+ public abstract boolean addDefaultNat();
+
+ public abstract void setOs(String vendorOsId);
+
+ public abstract boolean addDisplayName(String name);
+
+ public abstract boolean addRam(int mem);
+
+ public abstract void addFloppy(int index, String image, boolean readOnly);
+
+ public abstract boolean addCdrom(String image);
+
+ public abstract boolean addCpuCoreCount(int nrOfCores);
+
+ public abstract void setSoundCard(SoundCardType type);
+
+ public abstract SoundCardType getSoundCard();
+
+ public abstract void setDDAcceleration(DDAcceleration type);
+
+ public abstract DDAcceleration getDDAcceleration();
+
+ public abstract void setHWVersion(HWVersion type);
+
+ public abstract HWVersion getHWVersion();
+
+ public abstract void setEthernetDevType(int cardIndex, EthernetDevType type);
+
+ public abstract EthernetDevType getEthernetDevType(int cardIndex);
+
+ public abstract byte[] getDefinitionArray();
+
+ public abstract boolean addEthernet(EtherType type);
+
+ public abstract Virtualizer getVirtualizer();
+
+ public abstract void enableUsb(boolean enabled);
+
+ public abstract boolean disableSuspend();
+
+ /**
+ * Function used to register virtual devices
+ */
+ public abstract void registerVirtualHW();
}
diff --git a/src/main/java/org/openslx/util/vm/VmwareMetaData.java b/src/main/java/org/openslx/util/vm/VmwareMetaData.java
index cf190d4..c54ded8 100644
--- a/src/main/java/org/openslx/util/vm/VmwareMetaData.java
+++ b/src/main/java/org/openslx/util/vm/VmwareMetaData.java
@@ -17,584 +17,543 @@ import org.openslx.thrifthelper.TConst;
import org.openslx.util.Util;
import org.openslx.util.vm.VmwareConfig.ConfigEntry;
-class VmWareSoundCardMeta
-{
- public final boolean isPresent;
- public final String value;
-
- public VmWareSoundCardMeta( boolean present, String val )
- {
- isPresent = present;
- value = val;
- }
+class VmWareSoundCardMeta {
+
+ public final boolean isPresent;
+ public final String value;
+
+ public VmWareSoundCardMeta(boolean present, String val) {
+ isPresent = present;
+ value = val;
+ }
}
-class VmWareDDAccelMeta
-{
- public final boolean isPresent;
+class VmWareDDAccelMeta {
+
+ public final boolean isPresent;
- public VmWareDDAccelMeta( boolean present )
- {
- isPresent = present;
- }
+ public VmWareDDAccelMeta(boolean present) {
+ isPresent = present;
+ }
}
-class VmWareHWVersionMeta
-{
- public final int version;
+class VmWareHWVersionMeta {
- public VmWareHWVersionMeta( int vers )
- {
- version = vers;
- }
+ public final int version;
+
+ public VmWareHWVersionMeta(int vers) {
+ version = vers;
+ }
}
-class VmWareEthernetDevTypeMeta
-{
- public final String value;
+class VmWareEthernetDevTypeMeta {
+
+ public final String value;
- public VmWareEthernetDevTypeMeta( String val )
- {
- value = val;
- }
+ public VmWareEthernetDevTypeMeta(String val) {
+ value = val;
+ }
}
-public class VmwareMetaData extends VmMetaData<VmWareSoundCardMeta, VmWareDDAccelMeta, VmWareHWVersionMeta, VmWareEthernetDevTypeMeta>
-{
-
- private static final Logger LOGGER = Logger.getLogger( VmwareMetaData.class );
-
- private static final Virtualizer virtualizer = new Virtualizer( TConst.VIRT_VMWARE, "VMware" );
-
- private static final Pattern hddKey = Pattern.compile( "^(ide\\d|scsi\\d|sata\\d):?(\\d)?\\.(.*)", Pattern.CASE_INSENSITIVE );
-
- // Lowercase list of allowed settings for upload (as regex)
- private static final Pattern[] whitelist;
-
- private final VmwareConfig config;
-
- // Init static members
- static {
- String[] list = { "^guestos", "^uuid\\.bios", "^config\\.version", "^ehci\\.", "^mks\\.enable3d", "^virtualhw\\.", "^sound\\.", "\\.pcislotnumber$", "^pcibridge",
- "\\.virtualdev$", "^tools\\.syncTime$", "^time\\.synchronize", "^bios\\.bootDelay", "^rtc\\.", "^xhci\\." };
- whitelist = new Pattern[ list.length ];
- for ( int i = 0; i < list.length; ++i ) {
- whitelist[i] = Pattern.compile( list[i].toLowerCase() );
- }
- }
-
- public static enum EthernetType
- {
- NAT( "vmnet1" ), BRIDGED( "vmnet0" ), HOST_ONLY( "vmnet2" );
-
- public final String vmnet;
-
- private EthernetType( String vnet )
- {
- this.vmnet = vnet;
- }
- }
-
- private final Map<String, Controller> disks = new HashMap<>();
-
- public VmwareMetaData( List<OperatingSystem> osList, File file ) throws IOException, UnsupportedVirtualizerFormatException
- {
- super( osList );
- this.config = new VmwareConfig( file );
- init();
- }
-
- public VmwareMetaData( List<OperatingSystem> osList, byte[] vmxContent, int length ) throws UnsupportedVirtualizerFormatException
- {
- super( osList );
- this.config = new VmwareConfig( vmxContent, length ); // still unfiltered
- init(); // now filtered
- }
-
- private void init()
- {
- registerVirtualHW();
-
- for ( Entry<String, ConfigEntry> entry : config.entrySet() ) {
- handleLoadEntry( entry );
- }
- // if we find this tag, we already went through the hdd's - so we're done.
- if ( config.get( "#SLX_HDD_BUS" ) != null ) {
- return;
- }
- // Now find the HDDs and add to list
- for ( Entry<String, Controller> cEntry : disks.entrySet() ) {
- Controller controller = cEntry.getValue();
- String controllerType = cEntry.getKey();
- if ( !controller.present )
- continue;
- for ( Entry<String, Device> dEntry : controller.devices.entrySet() ) {
- Device device = dEntry.getValue();
- if ( !device.present )
- continue; // Not present
- if ( device.deviceType != null && !device.deviceType.toLowerCase().endsWith( "disk" ) )
- continue; // Not a HDD
- DriveBusType bus = null;
- if ( controllerType.startsWith( "ide" ) ) {
- bus = DriveBusType.IDE;
- } else if ( controllerType.startsWith( "scsi" ) ) {
- bus = DriveBusType.SCSI;
- } else if ( controllerType.startsWith( "sata" ) ) {
- bus = DriveBusType.SATA;
- }
- hdds.add( new HardDisk( controller.virtualDev, bus, device.filename ) );
- }
- }
- // TODO check if this machine is in a paused/suspended state
- this.isMachineSnapshot = false;
-
- // Add HDD to cleaned vmx
- if ( !hdds.isEmpty() ) {
- HardDisk hdd = hdds.get( 0 );
- addFiltered( "#SLX_HDD_BUS", hdd.bus.toString() );
- if ( hdd.chipsetDriver != null ) {
- addFiltered( "#SLX_HDD_CHIP", hdd.chipsetDriver );
- }
- }
- }
-
- private void addFiltered( String key, String value )
- {
- config.set( key, value ).filtered( true );
- }
-
- private boolean isSetAndTrue( String key )
- {
- String value = config.get( key );
- return value != null && value.equalsIgnoreCase( "true" );
- }
-
- private void handleLoadEntry( Entry<String, ConfigEntry> entry )
- {
- String lowerKey = entry.getKey().toLowerCase();
- // Cleaned vmx construction
- for ( Pattern exp : whitelist ) {
- if ( exp.matcher( lowerKey ).find() ) {
- entry.getValue().filtered( true );
- break;
- }
- }
- //
- // Dig Usable meta data
- String value = entry.getValue().getValue();
- if ( lowerKey.equals( "guestos" ) ) {
- setOs( value );
- return;
- }
- if ( lowerKey.equals( "displayname" ) ) {
- displayName = value;
- return;
- }
- Matcher hdd = hddKey.matcher( entry.getKey() );
- if ( hdd.find() ) {
- handleHddEntry( hdd.group( 1 ).toLowerCase(), hdd.group( 2 ), hdd.group( 3 ), value );
- }
- }
-
- private void handleHddEntry( String controllerStr, String deviceStr, String property, String value )
- {
- Controller controller = disks.get( controllerStr );
- if ( controller == null ) {
- controller = new Controller();
- disks.put( controllerStr, controller );
- }
- if ( deviceStr == null || deviceStr.isEmpty() ) {
- // Controller property
- if ( property.equalsIgnoreCase( "present" ) ) {
- controller.present = Boolean.parseBoolean( value );
- } else if ( property.equalsIgnoreCase( "virtualDev" ) ) {
- controller.virtualDev = value;
- }
- return;
- }
- // Device property
- Device device = controller.devices.get( deviceStr );
- if ( device == null ) {
- device = new Device();
- controller.devices.put( deviceStr, device );
- }
- if ( property.equalsIgnoreCase( "deviceType" ) ) {
- device.deviceType = value;
- } else if ( property.equalsIgnoreCase( "filename" ) ) {
- device.filename = value;
- } else if ( property.equalsIgnoreCase( "present" ) ) {
- device.present = Boolean.parseBoolean( value );
- }
- }
-
- @Override
- public boolean addHddTemplate( File diskImage, String hddMode, String redoDir )
- {
- return addHddTemplate( diskImage.getName(), hddMode, redoDir );
- }
-
- @Override
- public boolean addHddTemplate( String diskImagePath, String hddMode, String redoDir )
- {
- if ( diskImagePath.isEmpty() ) {
- LOGGER.error( "Empty disk image path given!" );
- return false;
- }
- DriveBusType bus;
- try {
- bus = DriveBusType.valueOf( config.get( "#SLX_HDD_BUS" ) );
- } catch ( Exception e ) {
- LOGGER.warn( "Unknown bus type: " + config.get( "#SLX_HDD_BUS" ) + ". Cannot add hdd config." );
- return false;
- }
- String chipset = config.get( "#SLX_HDD_CHIP" );
- String prefix;
- switch ( bus ) {
- case IDE:
- prefix = "ide0:0";
- addFiltered( "ide0.present", "TRUE" );
- break;
- case SATA:
- // Cannot happen?... use lsisas1068
- case SCSI:
- prefix = "scsi0:0";
- addFiltered( "scsi0.present", "TRUE" );
- if ( chipset != null ) {
- addFiltered( "scsi0.virtualDev", chipset );
- }
- break;
- default:
- LOGGER.warn( "Unknown HDD bus type: " + bus.toString() );
- return false;
- }
- // Gen
- addFiltered( prefix + ".present", "TRUE" );
- addFiltered( prefix + ".deviceType", "disk" );
- addFiltered( prefix + ".fileName", diskImagePath );
- if ( hddMode != null ) {
- addFiltered( prefix + ".mode", hddMode );
- addFiltered( prefix + ".redo", "" );
- addFiltered( prefix + ".redoLogDir", redoDir );
- }
- config.remove( "#SLX_HDD_BUS" );
- config.remove( "#SLX_HDD_CHIP" );
- return true;
- }
-
- public boolean addDefaultNat()
- {
- addFiltered( "ethernet0.present", "TRUE" );
- addFiltered( "ethernet0.connectionType", "nat" );
- return true;
- }
-
- public boolean addEthernet( VmMetaData.EtherType type )
- {
- boolean ret = false;
- int index = 0;
- for ( ;; ++index ) {
- if ( config.get( "ethernet" + index + ".present" ) == null )
- break;
- }
- switch ( type ) {
- case NAT:
- ret = addEthernet( index, EthernetType.NAT );
- break;
- case BRIDGED:
- ret = addEthernet( index, EthernetType.BRIDGED );
- break;
- case HOST_ONLY:
- ret = addEthernet( index, EthernetType.HOST_ONLY );
- break;
- default:
- // Should not come to this...
- break;
- }
- return ret;
- }
-
- public boolean addEthernet( int index, EthernetType type )
- {
- String ether = "ethernet" + index;
- addFiltered( ether + ".present", "TRUE" );
- addFiltered( ether + ".connectionType", "custom" );
- addFiltered( ether + ".vnet", type.vmnet );
- if ( config.get( ether + ".virtualDev" ) == null ) {
- String dev = config.get( "ethernet0.virtualDev" );
- if ( dev != null ) {
- addFiltered( ether + ".virtualDev", dev );
- }
- }
- return true;
- }
-
- public void addFloppy( int index, String image, boolean readOnly )
- {
- String pre = "floppy" + index;
- addFiltered( pre + ".present", "TRUE" );
- if ( image == null ) {
- addFiltered( pre + ".startConnected", "FALSE" );
- addFiltered( pre + ".fileType", "device" );
- config.remove( pre + ".fileName" );
- config.remove( pre + ".readonly" );
- addFiltered( pre + ".autodetect", "TRUE" );
- } else {
- addFiltered( pre + ".startConnected", "TRUE" );
- addFiltered( pre + ".fileType", "file" );
- addFiltered( pre + ".fileName", image );
- addFiltered( pre + ".readonly", vmBoolean( readOnly ) );
- config.remove( pre + ".autodetect" );
- }
- }
-
- public boolean addCdrom( String image )
- {
- for ( String port : new String[] { "ide0:0", "ide0:1", "ide1:0", "ide1:1", "scsi0:1" } ) {
- if ( !isSetAndTrue( port + ".present" ) ) {
- addFiltered( port + ".present", "TRUE" );
- if ( image == null ) {
- addFiltered( port + ".autodetect", "TRUE" );
- addFiltered( port + ".deviceType", "cdrom-raw" );
- config.remove( port + ".fileName" );
- } else {
- config.remove( port + ".autodetect" );
- addFiltered( port + ".deviceType", "cdrom-image" );
- addFiltered( port + ".fileName", image );
- }
- return true;
- }
- }
- return false;
- }
-
- private static String vmBoolean( boolean var )
- {
- return Boolean.toString( var ).toUpperCase();
- }
-
- private static String vmInteger( int val )
- {
- return Integer.toString( val );
- }
-
- @Override
- public boolean disableSuspend()
- {
- addFiltered( "suspend.disabled", "TRUE" );
- return true;
- }
-
- @Override
- public boolean addDisplayName( String name )
- {
- addFiltered( "displayName", name );
- return true;
- }
-
- @Override
- public boolean addRam( int mem )
- {
- addFiltered( "memsize", Integer.toString( mem ) );
- return true;
- }
-
- public void setOs( String vendorOsId )
- {
- addFiltered( "guestOS", vendorOsId );
- setOs( TConst.VIRT_VMWARE, vendorOsId );
- }
-
- @Override
- public byte[] getFilteredDefinitionArray()
- {
- return config.toString( true, false ).getBytes( StandardCharsets.UTF_8 );
- }
-
- public byte[] getDefinitionArray()
- {
- return config.toString( false, false ).getBytes( StandardCharsets.UTF_8 );
- }
-
- @Override
- public Virtualizer getVirtualizer()
- {
- return virtualizer;
- }
-
- private static class Device
- {
- public boolean present = false;
- public String deviceType = null;
- public String filename = null;
-
- @Override
- public String toString()
- {
- return filename + " is " + deviceType + " (present: " + present + ")";
- }
- }
-
- private static class Controller
- {
- public boolean present = true; // Seems to be implicit, seen at least for IDE...
- public String virtualDev = null;
- Map<String, Device> devices = new HashMap<>();
-
- @Override
- public String toString()
- {
- return virtualDev + " is (present: " + present + "): " + devices.toString();
- }
- }
-
- @Override
- public void enableUsb( boolean enabled )
- {
- addFiltered( "usb.present", vmBoolean( enabled ) );
- addFiltered( "ehci.present", vmBoolean( enabled ) );
- }
-
- @Override
- public void applySettingsForLocalEdit()
- {
- addFiltered( "gui.applyHostDisplayScalingToGuest", "FALSE" );
- }
-
- public String getValue( String key )
- {
- return config.get( key );
- }
-
- public void setSoundCard( VmMetaData.SoundCardType type )
- {
- VmWareSoundCardMeta soundCardMeta = soundCards.get( type );
- addFiltered( "sound.present", vmBoolean( soundCardMeta.isPresent ) );
- if ( soundCardMeta.value != null ) {
- addFiltered( "sound.virtualDev", soundCardMeta.value );
- } else {
- config.remove( "sound.virtualDev" );
- }
- }
-
- public VmMetaData.SoundCardType getSoundCard()
- {
- if ( !isSetAndTrue( "sound.present" ) || !isSetAndTrue( "sound.autodetect" ) ) {
- return VmMetaData.SoundCardType.NONE;
- }
- String current = config.get( "sound.virtualDev" );
- if ( current != null ) {
- VmWareSoundCardMeta soundCardMeta = null;
- for ( VmMetaData.SoundCardType type : VmMetaData.SoundCardType.values() ) {
- soundCardMeta = soundCards.get( type );
- if ( soundCardMeta != null ) {
- if ( current.equals( soundCardMeta.value ) ) {
- return type;
- }
- }
- }
- }
- return VmMetaData.SoundCardType.DEFAULT;
- }
-
- public void setDDAcceleration( VmMetaData.DDAcceleration type )
- {
- VmWareDDAccelMeta ddaMeta = ddacc.get( type );
- addFiltered( "mks.enable3d", vmBoolean( ddaMeta.isPresent ) );
- }
-
- public VmMetaData.DDAcceleration getDDAcceleration()
- {
- if ( isSetAndTrue( "mks.enable3d" ) ) {
- return VmMetaData.DDAcceleration.ON;
- } else {
- return VmMetaData.DDAcceleration.OFF;
- }
- }
-
- public void setHWVersion( VmMetaData.HWVersion type )
- {
- VmWareHWVersionMeta hwVersionMeta = hwversion.get( type );
- addFiltered( "virtualHW.version", vmInteger( hwVersionMeta.version ) );
- }
-
- public VmMetaData.HWVersion getHWVersion()
- {
- int currentValue = Util.parseInt( config.get( "virtualHW.version" ), -1 );
- VmWareHWVersionMeta hwVersionMeta = null;
- for ( VmMetaData.HWVersion ver : VmMetaData.HWVersion.values() ) {
- hwVersionMeta = hwversion.get( ver );
- if ( hwVersionMeta == null ) {
- continue;
- }
- if ( currentValue == hwVersionMeta.version ) {
- return ver;
- }
- }
- return HWVersion.NONE;
- }
-
- public void setEthernetDevType( int cardIndex, VmMetaData.EthernetDevType type )
- {
- VmWareEthernetDevTypeMeta ethernetDevTypeMeta = networkCards.get( type );
- if ( ethernetDevTypeMeta.value != null ) {
- addFiltered( "ethernet" + cardIndex + ".virtualDev", ethernetDevTypeMeta.value );
- } else {
- config.remove( "ethernet" + cardIndex + ".virtualDev" );
- }
- }
-
- public EthernetDevType getEthernetDevType( int cardIndex )
- {
- String temp = config.get( "ethernet" + cardIndex + ".virtualDev" );
- if ( temp != null ) {
- VmWareEthernetDevTypeMeta ethernetDevTypeMeta = null;
- for ( EthernetDevType type : VmMetaData.EthernetDevType.values() ) {
- ethernetDevTypeMeta = networkCards.get( type );
- if ( ethernetDevTypeMeta == null ) {
- continue;
- }
- if ( temp.equals( ethernetDevTypeMeta.value ) ) {
- return type;
- }
- }
- }
- return EthernetDevType.AUTO;
- }
-
- @Override
- public boolean addCpuCoreCount( int numCores )
- {
- // TODO actually add the cpu core count to the machine description
- return false;
- }
-
- public void registerVirtualHW()
- {
- soundCards.put( VmMetaData.SoundCardType.NONE, new VmWareSoundCardMeta( false, null ) );
- soundCards.put( VmMetaData.SoundCardType.DEFAULT, new VmWareSoundCardMeta( true, null ) );
- soundCards.put( VmMetaData.SoundCardType.SOUND_BLASTER, new VmWareSoundCardMeta( true, "sb16" ) );
- soundCards.put( VmMetaData.SoundCardType.ES, new VmWareSoundCardMeta( true, "es1371" ) );
- soundCards.put( VmMetaData.SoundCardType.HD_AUDIO, new VmWareSoundCardMeta( true, "hdaudio" ) );
-
- ddacc.put( VmMetaData.DDAcceleration.OFF, new VmWareDDAccelMeta( false ) );
- ddacc.put( VmMetaData.DDAcceleration.ON, new VmWareDDAccelMeta( true ) );
-
- hwversion.put( VmMetaData.HWVersion.NONE, new VmWareHWVersionMeta( 0 ) );
- hwversion.put( VmMetaData.HWVersion.THREE, new VmWareHWVersionMeta( 3 ) );
- hwversion.put( VmMetaData.HWVersion.FOUR, new VmWareHWVersionMeta( 4 ) );
- hwversion.put( VmMetaData.HWVersion.SIX, new VmWareHWVersionMeta( 6 ) );
- hwversion.put( VmMetaData.HWVersion.SEVEN, new VmWareHWVersionMeta( 7 ) );
- hwversion.put( VmMetaData.HWVersion.EIGHT, new VmWareHWVersionMeta( 8 ) );
- hwversion.put( VmMetaData.HWVersion.NINE, new VmWareHWVersionMeta( 9 ) );
- hwversion.put( VmMetaData.HWVersion.TEN, new VmWareHWVersionMeta( 10 ) );
- hwversion.put( VmMetaData.HWVersion.ELEVEN, new VmWareHWVersionMeta( 11 ) );
- hwversion.put( VmMetaData.HWVersion.TWELVE, new VmWareHWVersionMeta( 12 ) );
-
- networkCards.put( VmMetaData.EthernetDevType.AUTO, new VmWareEthernetDevTypeMeta( null ) );
- networkCards.put( VmMetaData.EthernetDevType.PCNET32, new VmWareEthernetDevTypeMeta( "vlance" ) );
- networkCards.put( VmMetaData.EthernetDevType.E1000, new VmWareEthernetDevTypeMeta( "e1000" ) );
- networkCards.put( VmMetaData.EthernetDevType.E1000E, new VmWareEthernetDevTypeMeta( "e1000e" ) );
- networkCards.put( VmMetaData.EthernetDevType.VMXNET, new VmWareEthernetDevTypeMeta( "vmxnet" ) );
- networkCards.put( VmMetaData.EthernetDevType.VMXNET3, new VmWareEthernetDevTypeMeta( "vmxnet3" ) );
- }
+public class VmwareMetaData extends VmMetaData<VmWareSoundCardMeta, VmWareDDAccelMeta, VmWareHWVersionMeta, VmWareEthernetDevTypeMeta> {
+
+ private static final Logger LOGGER = Logger.getLogger(VmwareMetaData.class);
+
+ private static final Virtualizer virtualizer = new Virtualizer(TConst.VIRT_VMWARE, "VMware");
+
+ private static final Pattern hddKey = Pattern.compile("^(ide\\d|scsi\\d|sata\\d):?(\\d)?\\.(.*)", Pattern.CASE_INSENSITIVE);
+
+ // Lowercase list of allowed settings for upload (as regex)
+ private static final Pattern[] whitelist;
+
+ private final VmwareConfig config;
+
+ // Init static members
+ static {
+ String[] list = {"^guestos", "^uuid\\.bios", "^config\\.version", "^ehci\\.", "^mks\\.enable3d", "^virtualhw\\.", "^sound\\.", "\\.pcislotnumber$", "^pcibridge",
+ "\\.virtualdev$", "^tools\\.syncTime$", "^time\\.synchronize", "^bios\\.bootDelay", "^rtc\\.", "^xhci\\."};
+ whitelist = new Pattern[list.length];
+ for (int i = 0; i < list.length; ++i) {
+ whitelist[i] = Pattern.compile(list[i].toLowerCase());
+ }
+ }
+
+ public static enum EthernetType {
+ NAT("vmnet1"), BRIDGED("vmnet0"), HOST_ONLY("vmnet2");
+
+ public final String vmnet;
+
+ private EthernetType(String vnet) {
+ this.vmnet = vnet;
+ }
+ }
+
+ private final Map<String, Controller> disks = new HashMap<>();
+
+ public VmwareMetaData(List<OperatingSystem> osList, File file) throws IOException, UnsupportedVirtualizerFormatException {
+ super(osList);
+ this.config = new VmwareConfig(file);
+ init();
+ }
+
+ public VmwareMetaData(List<OperatingSystem> osList, byte[] vmxContent, int length) throws UnsupportedVirtualizerFormatException {
+ super(osList);
+ this.config = new VmwareConfig(vmxContent, length); // still unfiltered
+ init(); // now filtered
+ }
+
+ private void init() {
+ registerVirtualHW();
+
+ for (Entry<String, ConfigEntry> entry : config.entrySet()) {
+ handleLoadEntry(entry);
+ }
+ // if we find this tag, we already went through the hdd's - so we're done.
+ if (config.get("#SLX_HDD_BUS") != null) {
+ return;
+ }
+ // Now find the HDDs and add to list
+ for (Entry<String, Controller> cEntry : disks.entrySet()) {
+ Controller controller = cEntry.getValue();
+ String controllerType = cEntry.getKey();
+ if (!controller.present) {
+ continue;
+ }
+ for (Entry<String, Device> dEntry : controller.devices.entrySet()) {
+ Device device = dEntry.getValue();
+ if (!device.present) {
+ continue; // Not present
+ }
+ if (device.deviceType != null && !device.deviceType.toLowerCase().endsWith("disk")) {
+ continue; // Not a HDD
+ }
+ DriveBusType bus = null;
+ if (controllerType.startsWith("ide")) {
+ bus = DriveBusType.IDE;
+ } else if (controllerType.startsWith("scsi")) {
+ bus = DriveBusType.SCSI;
+ } else if (controllerType.startsWith("sata")) {
+ bus = DriveBusType.SATA;
+ }
+ hdds.add(new HardDisk(controller.virtualDev, bus, device.filename));
+ }
+ }
+ // TODO check if this machine is in a paused/suspended state
+ this.isMachineSnapshot = false;
+
+ // Add HDD to cleaned vmx
+ if (!hdds.isEmpty()) {
+ HardDisk hdd = hdds.get(0);
+ addFiltered("#SLX_HDD_BUS", hdd.bus.toString());
+ if (hdd.chipsetDriver != null) {
+ addFiltered("#SLX_HDD_CHIP", hdd.chipsetDriver);
+ }
+ }
+ }
+
+ private void addFiltered(String key, String value) {
+ config.set(key, value).filtered(true);
+ }
+
+ private boolean isSetAndTrue(String key) {
+ String value = config.get(key);
+ return value != null && value.equalsIgnoreCase("true");
+ }
+
+ private void handleLoadEntry(Entry<String, ConfigEntry> entry) {
+ String lowerKey = entry.getKey().toLowerCase();
+ // Cleaned vmx construction
+ for (Pattern exp : whitelist) {
+ if (exp.matcher(lowerKey).find()) {
+ entry.getValue().filtered(true);
+ break;
+ }
+ }
+ //
+ // Dig Usable meta data
+ String value = entry.getValue().getValue();
+ if (lowerKey.equals("guestos")) {
+ setOs(value);
+ return;
+ }
+ if (lowerKey.equals("displayname")) {
+ displayName = value;
+ return;
+ }
+ Matcher hdd = hddKey.matcher(entry.getKey());
+ if (hdd.find()) {
+ handleHddEntry(hdd.group(1).toLowerCase(), hdd.group(2), hdd.group(3), value);
+ }
+ }
+
+ private void handleHddEntry(String controllerStr, String deviceStr, String property, String value) {
+ Controller controller = disks.get(controllerStr);
+ if (controller == null) {
+ controller = new Controller();
+ disks.put(controllerStr, controller);
+ }
+ if (deviceStr == null || deviceStr.isEmpty()) {
+ // Controller property
+ if (property.equalsIgnoreCase("present")) {
+ controller.present = Boolean.parseBoolean(value);
+ } else if (property.equalsIgnoreCase("virtualDev")) {
+ controller.virtualDev = value;
+ }
+ return;
+ }
+ // Device property
+ Device device = controller.devices.get(deviceStr);
+ if (device == null) {
+ device = new Device();
+ controller.devices.put(deviceStr, device);
+ }
+ if (property.equalsIgnoreCase("deviceType")) {
+ device.deviceType = value;
+ } else if (property.equalsIgnoreCase("filename")) {
+ device.filename = value;
+ } else if (property.equalsIgnoreCase("present")) {
+ device.present = Boolean.parseBoolean(value);
+ }
+ }
+
+ @Override
+ public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) {
+ return addHddTemplate(diskImage.getName(), hddMode, redoDir);
+ }
+
+ @Override
+ public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) {
+ if (diskImagePath.isEmpty()) {
+ LOGGER.error("Empty disk image path given!");
+ return false;
+ }
+ DriveBusType bus;
+ try {
+ bus = DriveBusType.valueOf(config.get("#SLX_HDD_BUS"));
+ } catch (Exception e) {
+ LOGGER.warn("Unknown bus type: " + config.get("#SLX_HDD_BUS") + ". Cannot add hdd config.");
+ return false;
+ }
+ String chipset = config.get("#SLX_HDD_CHIP");
+ String prefix;
+ switch (bus) {
+ case IDE:
+ prefix = "ide0:0";
+ addFiltered("ide0.present", "TRUE");
+ break;
+ case SATA:
+ // Cannot happen?... use lsisas1068
+ case SCSI:
+ prefix = "scsi0:0";
+ addFiltered("scsi0.present", "TRUE");
+ if (chipset != null) {
+ addFiltered("scsi0.virtualDev", chipset);
+ }
+ break;
+ default:
+ LOGGER.warn("Unknown HDD bus type: " + bus.toString());
+ return false;
+ }
+ // Gen
+ addFiltered(prefix + ".present", "TRUE");
+ addFiltered(prefix + ".deviceType", "disk");
+ addFiltered(prefix + ".fileName", diskImagePath);
+ if (hddMode != null) {
+ addFiltered(prefix + ".mode", hddMode);
+ addFiltered(prefix + ".redo", "");
+ addFiltered(prefix + ".redoLogDir", redoDir);
+ }
+ config.remove("#SLX_HDD_BUS");
+ config.remove("#SLX_HDD_CHIP");
+ return true;
+ }
+
+ public boolean addDefaultNat() {
+ addFiltered("ethernet0.present", "TRUE");
+ addFiltered("ethernet0.connectionType", "nat");
+ return true;
+ }
+
+ public boolean addEthernet(VmMetaData.EtherType type) {
+ boolean ret = false;
+ int index = 0;
+ for (;; ++index) {
+ if (config.get("ethernet" + index + ".present") == null) {
+ break;
+ }
+ }
+ switch (type) {
+ case NAT:
+ ret = addEthernet(index, EthernetType.NAT);
+ break;
+ case BRIDGED:
+ ret = addEthernet(index, EthernetType.BRIDGED);
+ break;
+ case HOST_ONLY:
+ ret = addEthernet(index, EthernetType.HOST_ONLY);
+ break;
+ default:
+ // Should not come to this...
+ break;
+ }
+ return ret;
+ }
+
+ public boolean addEthernet(int index, EthernetType type) {
+ String ether = "ethernet" + index;
+ addFiltered(ether + ".present", "TRUE");
+ addFiltered(ether + ".connectionType", "custom");
+ addFiltered(ether + ".vnet", type.vmnet);
+ if (config.get(ether + ".virtualDev") == null) {
+ String dev = config.get("ethernet0.virtualDev");
+ if (dev != null) {
+ addFiltered(ether + ".virtualDev", dev);
+ }
+ }
+ return true;
+ }
+
+ public void addFloppy(int index, String image, boolean readOnly) {
+ String pre = "floppy" + index;
+ addFiltered(pre + ".present", "TRUE");
+ if (image == null) {
+ addFiltered(pre + ".startConnected", "FALSE");
+ addFiltered(pre + ".fileType", "device");
+ config.remove(pre + ".fileName");
+ config.remove(pre + ".readonly");
+ addFiltered(pre + ".autodetect", "TRUE");
+ } else {
+ addFiltered(pre + ".startConnected", "TRUE");
+ addFiltered(pre + ".fileType", "file");
+ addFiltered(pre + ".fileName", image);
+ addFiltered(pre + ".readonly", vmBoolean(readOnly));
+ config.remove(pre + ".autodetect");
+ }
+ }
+
+ public boolean addCdrom(String image) {
+ for (String port : new String[]{"ide0:0", "ide0:1", "ide1:0", "ide1:1", "scsi0:1"}) {
+ if (!isSetAndTrue(port + ".present")) {
+ addFiltered(port + ".present", "TRUE");
+ if (image == null) {
+ addFiltered(port + ".autodetect", "TRUE");
+ addFiltered(port + ".deviceType", "cdrom-raw");
+ config.remove(port + ".fileName");
+ } else {
+ config.remove(port + ".autodetect");
+ addFiltered(port + ".deviceType", "cdrom-image");
+ addFiltered(port + ".fileName", image);
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static String vmBoolean(boolean var) {
+ return Boolean.toString(var).toUpperCase();
+ }
+
+ private static String vmInteger(int val) {
+ return Integer.toString(val);
+ }
+
+ @Override
+ public boolean disableSuspend() {
+ addFiltered("suspend.disabled", "TRUE");
+ return true;
+ }
+
+ @Override
+ public boolean addDisplayName(String name) {
+ addFiltered("displayName", name);
+ return true;
+ }
+
+ @Override
+ public boolean addRam(int mem) {
+ addFiltered("memsize", Integer.toString(mem));
+ return true;
+ }
+
+ public void setOs(String vendorOsId) {
+ addFiltered("guestOS", vendorOsId);
+ setOs(TConst.VIRT_VMWARE, vendorOsId);
+ }
+
+ @Override
+ public byte[] getFilteredDefinitionArray() {
+ return config.toString(true, false).getBytes(StandardCharsets.UTF_8);
+ }
+
+ public byte[] getDefinitionArray() {
+ return config.toString(false, false).getBytes(StandardCharsets.UTF_8);
+ }
+
+ @Override
+ public Virtualizer getVirtualizer() {
+ return virtualizer;
+ }
+
+ private static class Device {
+
+ public boolean present = false;
+ public String deviceType = null;
+ public String filename = null;
+
+ @Override
+ public String toString() {
+ return filename + " is " + deviceType + " (present: " + present + ")";
+ }
+ }
+
+ private static class Controller {
+
+ public boolean present = true; // Seems to be implicit, seen at least for IDE...
+ public String virtualDev = null;
+ Map<String, Device> devices = new HashMap<>();
+
+ @Override
+ public String toString() {
+ return virtualDev + " is (present: " + present + "): " + devices.toString();
+ }
+ }
+
+ @Override
+ public void enableUsb(boolean enabled) {
+ addFiltered("usb.present", vmBoolean(enabled));
+ addFiltered("ehci.present", vmBoolean(enabled));
+ }
+
+ @Override
+ public void applySettingsForLocalEdit() {
+ addFiltered("gui.applyHostDisplayScalingToGuest", "FALSE");
+ }
+
+ public String getValue(String key) {
+ return config.get(key);
+ }
+
+ public void setSoundCard(VmMetaData.SoundCardType type) {
+ VmWareSoundCardMeta soundCardMeta = soundCards.get(type);
+ addFiltered("sound.present", vmBoolean(soundCardMeta.isPresent));
+ if (soundCardMeta.value != null) {
+ addFiltered("sound.virtualDev", soundCardMeta.value);
+ } else {
+ config.remove("sound.virtualDev");
+ }
+ }
+
+ public VmMetaData.SoundCardType getSoundCard() {
+ if (!isSetAndTrue("sound.present") || !isSetAndTrue("sound.autodetect")) {
+ return VmMetaData.SoundCardType.NONE;
+ }
+ String current = config.get("sound.virtualDev");
+ if (current != null) {
+ VmWareSoundCardMeta soundCardMeta = null;
+ for (VmMetaData.SoundCardType type : VmMetaData.SoundCardType.values()) {
+ soundCardMeta = soundCards.get(type);
+ if (soundCardMeta != null) {
+ if (current.equals(soundCardMeta.value)) {
+ return type;
+ }
+ }
+ }
+ }
+ return VmMetaData.SoundCardType.DEFAULT;
+ }
+
+ public void setDDAcceleration(VmMetaData.DDAcceleration type) {
+ VmWareDDAccelMeta ddaMeta = ddacc.get(type);
+ addFiltered("mks.enable3d", vmBoolean(ddaMeta.isPresent));
+ }
+
+ public VmMetaData.DDAcceleration getDDAcceleration() {
+ if (isSetAndTrue("mks.enable3d")) {
+ return VmMetaData.DDAcceleration.ON;
+ } else {
+ return VmMetaData.DDAcceleration.OFF;
+ }
+ }
+
+ public void setHWVersion(VmMetaData.HWVersion type) {
+ VmWareHWVersionMeta hwVersionMeta = hwversion.get(type);
+ addFiltered("virtualHW.version", vmInteger(hwVersionMeta.version));
+ }
+
+ public VmMetaData.HWVersion getHWVersion() {
+ int currentValue = Util.parseInt(config.get("virtualHW.version"), -1);
+ VmWareHWVersionMeta hwVersionMeta = null;
+ for (VmMetaData.HWVersion ver : VmMetaData.HWVersion.values()) {
+ hwVersionMeta = hwversion.get(ver);
+ if (hwVersionMeta == null) {
+ continue;
+ }
+ if (currentValue == hwVersionMeta.version) {
+ return ver;
+ }
+ }
+ return HWVersion.NONE;
+ }
+
+ public void setEthernetDevType(int cardIndex, VmMetaData.EthernetDevType type) {
+ VmWareEthernetDevTypeMeta ethernetDevTypeMeta = networkCards.get(type);
+ if (ethernetDevTypeMeta.value != null) {
+ addFiltered("ethernet" + cardIndex + ".virtualDev", ethernetDevTypeMeta.value);
+ } else {
+ config.remove("ethernet" + cardIndex + ".virtualDev");
+ }
+ }
+
+ public EthernetDevType getEthernetDevType(int cardIndex) {
+ String temp = config.get("ethernet" + cardIndex + ".virtualDev");
+ if (temp != null) {
+ VmWareEthernetDevTypeMeta ethernetDevTypeMeta = null;
+ for (EthernetDevType type : VmMetaData.EthernetDevType.values()) {
+ ethernetDevTypeMeta = networkCards.get(type);
+ if (ethernetDevTypeMeta == null) {
+ continue;
+ }
+ if (temp.equals(ethernetDevTypeMeta.value)) {
+ return type;
+ }
+ }
+ }
+ return EthernetDevType.AUTO;
+ }
+
+ @Override
+ public boolean addCpuCoreCount(int numCores) {
+ // TODO actually add the cpu core count to the machine description
+ return false;
+ }
+
+ public void registerVirtualHW() {
+ soundCards.put(VmMetaData.SoundCardType.NONE, new VmWareSoundCardMeta(false, null));
+ soundCards.put(VmMetaData.SoundCardType.DEFAULT, new VmWareSoundCardMeta(true, null));
+ soundCards.put(VmMetaData.SoundCardType.SOUND_BLASTER, new VmWareSoundCardMeta(true, "sb16"));
+ soundCards.put(VmMetaData.SoundCardType.ES, new VmWareSoundCardMeta(true, "es1371"));
+ soundCards.put(VmMetaData.SoundCardType.HD_AUDIO, new VmWareSoundCardMeta(true, "hdaudio"));
+
+ ddacc.put(VmMetaData.DDAcceleration.OFF, new VmWareDDAccelMeta(false));
+ ddacc.put(VmMetaData.DDAcceleration.ON, new VmWareDDAccelMeta(true));
+
+ hwversion.put(VmMetaData.HWVersion.NONE, new VmWareHWVersionMeta(0));
+ hwversion.put(VmMetaData.HWVersion.THREE, new VmWareHWVersionMeta(3));
+ hwversion.put(VmMetaData.HWVersion.FOUR, new VmWareHWVersionMeta(4));
+ hwversion.put(VmMetaData.HWVersion.SIX, new VmWareHWVersionMeta(6));
+ hwversion.put(VmMetaData.HWVersion.SEVEN, new VmWareHWVersionMeta(7));
+ hwversion.put(VmMetaData.HWVersion.EIGHT, new VmWareHWVersionMeta(8));
+ hwversion.put(VmMetaData.HWVersion.NINE, new VmWareHWVersionMeta(9));
+ hwversion.put(VmMetaData.HWVersion.TEN, new VmWareHWVersionMeta(10));
+ hwversion.put(VmMetaData.HWVersion.ELEVEN, new VmWareHWVersionMeta(11));
+ hwversion.put(VmMetaData.HWVersion.TWELVE, new VmWareHWVersionMeta(12));
+
+ networkCards.put(VmMetaData.EthernetDevType.AUTO, new VmWareEthernetDevTypeMeta(null));
+ networkCards.put(VmMetaData.EthernetDevType.PCNET32, new VmWareEthernetDevTypeMeta("vlance"));
+ networkCards.put(VmMetaData.EthernetDevType.E1000, new VmWareEthernetDevTypeMeta("e1000"));
+ networkCards.put(VmMetaData.EthernetDevType.E1000E, new VmWareEthernetDevTypeMeta("e1000e"));
+ networkCards.put(VmMetaData.EthernetDevType.VMXNET, new VmWareEthernetDevTypeMeta("vmxnet"));
+ networkCards.put(VmMetaData.EthernetDevType.VMXNET3, new VmWareEthernetDevTypeMeta("vmxnet3"));
+ }
}