summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/QemuMetaData.java
diff options
context:
space:
mode:
authorChristopher Lucas2018-12-07 15:34:11 +0100
committerChristopher Lucas2018-12-07 15:34:11 +0100
commit6a893099c4229020f4f439fe6388dbc41243f423 (patch)
tree84b7e7610a76ad2ddf28de69e5ebf9bda6309d65 /src/main/java/org/openslx/util/vm/QemuMetaData.java
parentOptimized Upload with new Map and Enum system (diff)
downloadmaster-sync-shared-6a893099c4229020f4f439fe6388dbc41243f423.tar.gz
master-sync-shared-6a893099c4229020f4f439fe6388dbc41243f423.tar.xz
master-sync-shared-6a893099c4229020f4f439fe6388dbc41243f423.zip
Upload and Download working
Diffstat (limited to 'src/main/java/org/openslx/util/vm/QemuMetaData.java')
-rw-r--r--src/main/java/org/openslx/util/vm/QemuMetaData.java241
1 files changed, 145 insertions, 96 deletions
diff --git a/src/main/java/org/openslx/util/vm/QemuMetaData.java b/src/main/java/org/openslx/util/vm/QemuMetaData.java
index c1a062b..8dca91e 100644
--- a/src/main/java/org/openslx/util/vm/QemuMetaData.java
+++ b/src/main/java/org/openslx/util/vm/QemuMetaData.java
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -57,20 +58,22 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
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 final QemuConfig config;
private String setup;
+ private final static String QUOTE = "\"";
+
private TreeMap<String, String> option;
+ private LinkedHashMap<String, String> header;
+
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 {
@@ -81,6 +84,10 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
private EthernetType(String vnet) {
this.vmnet = vnet;
}
+
+ public String getVMnet() {
+ return this.vmnet;
+ }
}
public QemuMetaData(List<OperatingSystem> osList, File config) {
@@ -100,13 +107,12 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
config.setHdds();
displayName = config.getDisplayName();
setOs(config.getOsName());
- isMachineSnapshot = config.isMachineSnapshot();
+ isMachineSnapshot = config.isMachineSnapshot();
for (HardDisk hardDisk : config.getHdds()) {
hdds.add(hardDisk);
}
}
-
@Override
public byte[] getFilteredDefinitionArray() {
return config.toString(false).getBytes(StandardCharsets.UTF_8);
@@ -124,51 +130,64 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
@Override
public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) {
- option = new TreeMap<>();
- String bus = "anychipset";
- DriveBusType busType = DriveBusType.IDE;
- option = config.get(DRIVE.getHeader(), DRIVE.getID());
- 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;
+ return addHddTemplate(diskImage.getName(), hddMode, redoDir);
}
@Override
public boolean addHddTemplate(String diskImagePath, String hddMode, String redoDir) {
+ DriveBusType bus = null;
option = new TreeMap<>();
+ option = config.get(DRIVE.getHeader(), DRIVE.getID());
- //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);
+ // Drive Filename && Bus
+ if (option != null) {
+ option.replace("file", diskImagePath);
+ if (option.get("if") == null) {
+ option.put("if", "none");
+ } else {
+ switch (option.get("if")) {
+ case "ide":
+ bus = DriveBusType.IDE;
+ break;
+ case "scsi":
+ bus = DriveBusType.SCSI;
+ break;
+ case "sata":
+ //not available for Qemu. Others : sd, mtd, floppy, pflash, virtio
+ break;
+ default:
+ break;
+ }
+ }
+ } else {
+ LOGGER.error("Missing disk");
+ }
+ //Setting settings
+ header = new LinkedHashMap<>();
+ header.put(DRIVE.getHeader(), DRIVE.getID());
+ config.set(header, option);
- hdds.add(new HardDisk("anychipset", DriveBusType.IDE, diskImagePath));
- driveCounter++;
+ // Device
+ option = new TreeMap<>();
+ option = config.get(DEVICE.getHeader(), DEVICE.getID());
+
+ if (option == null) {
+ option = new TreeMap<>();
+ header = new LinkedHashMap<>();
+ header.put(DEVICE.getHeader(), "");
+
+ option.put("drive",DRIVE.getID());
+ if (bus != null) {
+ if (bus.equals(DriveBusType.IDE)) {
+ option.put("driver", "ide-hd");
+ } else if (bus.equals(DriveBusType.SCSI)) {
+ option.put("driver", "scsi-hd");
+ }
+ }else{
+ //Shouldn't come to this but to check TODO
+ }
+ config.set(header, option);
+ }
return true;
}
@@ -186,16 +205,24 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
@Override
public boolean addDisplayName(String name) {
option = new TreeMap<>();
- option.put("guest", "\"" + name + "\"");
-// config.set("[name]", option);
+ option.put("guest", name);
+
+ header = new LinkedHashMap<>();
+ header.put(NAME.getHeader(), null);
+
+ config.set(header, option);
return true;
}
@Override
public boolean addRam(int mem) {
option = new TreeMap<>();
- option.put("size", "\"" + mem + "\"");
-// config.set("[memory]", option);
+ option.put("size", ""+ mem);
+
+ header = new LinkedHashMap<>();
+ header.put(MEMORY.getHeader(), null);
+
+ config.set(header, option);
return true;
}
@@ -203,22 +230,27 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
public void addFloppy(int index, String image, boolean readOnly) {
option = new TreeMap<>();
if (readOnly) {
- option.put("readonly", "\"on\"");
+ option.put("readonly", "on");
} else {
- option.put("readonly", "\"off\"");
+ option.put("readonly", "off");
}
- option.put("if", "\"floppy\"");
+ option.put("if", "floppy");
option.put("file", image);
-// config.set("[drive \"floppy" + floppyCounter + "\"]", option);
+
+ header = new LinkedHashMap<>();
+ header.put(DRIVE.getHeader(),"floppy" + floppyCounter);
+ config.set(header, option);
floppyCounter++;
}
@Override
public boolean addCdrom(String image) {
option = new TreeMap<>();
- option.put("media", "\"cdrom\"");
+ option.put("media", "cdrom");
option.put("file", image);
-// config.set("[drive \"cdrom" + cdromCounter + "\"]", option);
+ header = new LinkedHashMap<>();
+ header.put(DRIVE.getHeader(), "cdrom" + cdromCounter);
+ config.set(header, option);
cdromCounter++;
return true;
}
@@ -226,8 +258,11 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
@Override
public boolean addCpuCoreCount(int nrOfCores) {
option = new TreeMap<>();
- option.put("cpus", "\"" + nrOfCores + "\"");
-// config.set("[smp-opts]", option);
+ option.put("cpus", nrOfCores+"");
+
+ header = new LinkedHashMap<>();
+ header.put(SMP.getHeader(), null);
+ config.set(header, option);
return true;
}
@@ -239,13 +274,12 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
@Override
public VmMetaData.SoundCardType getSoundCard() {
//not possible to set just write comment
- VmMetaData.SoundCardType soundcard = null;
- return soundcard;
+ return null;
}
@Override
public void setDDAcceleration(VmMetaData.DDAcceleration type) {
- //Not really used by qemu. TODO:
+ //TODO: Not really used by qemu.
}
@Override
@@ -298,43 +332,55 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
@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;
-// }
+ int index = 0;
+ for (;; ++index) {
+ DEVICE.setID("dev"+index);
+ TreeMap<String, String> dev = config.get(DEVICE.getHeader(), DEVICE.getID());
+
+ if (dev == null) {
+ break;
+ } else {
+ if (dev.get("netdev") == 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){
+ NETDEV.setID("net"+index);
+ header = new LinkedHashMap<>();
+ header.put(NETDEV.getHeader(), NETDEV.getID());
+ //netdev
+ option = new TreeMap<>();
+ option.put("ifname", type.vmnet);
+ option.put("type", "tap");
+ config.set(header, option);
+ //device
+ header = new LinkedHashMap<>();
+ header.put(DEVICE.getHeader(), "");
+ option = new TreeMap<>();
+ option.put("driver", "e1000");
+ option.put("netdev", NETDEV.getID());
+ config.set(header, option);
+ return true;
+ }
@Override
public Virtualizer getVirtualizer() {
@@ -345,11 +391,14 @@ public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAcce
public void enableUsb(boolean enabled) {
option = new TreeMap<>();
if (enabled) {
- option.put("usb", "\"on\"");
+ option.put("usb", "on");
} else {
- option.put("usb", "\"off\"");
+ option.put("usb", "off");
}
-// config.set(MACHINE.value(), option);
+
+ header = new LinkedHashMap<>();
+ header.put(MACHINE.getHeader(), MACHINE.getID());
+ config.set(header, option);
}
@Override