summaryrefslogtreecommitdiffstats
path: root/src/main/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
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')
-rw-r--r--src/main/java/org/openslx/util/vm/QemuConfig.java77
-rw-r--r--src/main/java/org/openslx/util/vm/QemuMetaData.java241
-rw-r--r--src/main/java/org/openslx/util/vm/VmMetaData.java2
3 files changed, 211 insertions, 109 deletions
diff --git a/src/main/java/org/openslx/util/vm/QemuConfig.java b/src/main/java/org/openslx/util/vm/QemuConfig.java
index 8348790..3bf1742 100644
--- a/src/main/java/org/openslx/util/vm/QemuConfig.java
+++ b/src/main/java/org/openslx/util/vm/QemuConfig.java
@@ -137,6 +137,7 @@ public final class QemuConfig {
int index = -1;
int nbDev = 0;
int nbDrive = 0;
+ int nbNetDev = 0;
ArrayList<String> listID = new ArrayList<>();
LinkedHashMap<String, String> headers = null;
@@ -163,12 +164,32 @@ public final class QemuConfig {
}
headers.put(DRIVE.getHeader(), DRIVE.getID());
nbDrive++;
+ exist = false;
} else if (option.equals(DRIVE.getHeader())) {//Check drive without id
DRIVE.setID("id-disk-" + nbDrive);
listID.add(DRIVE.getID());
headers.put(option, DRIVE.getID());
nbDrive++;
- // } else if (option.equals(NETDEV.toString())){//Check drive without id TODO
+ } else if (option.contains(NETDEV.getHeader()) && option.contains(quote)){//Check netdev with id
+ String[] netdev = option.split(quote);
+ for (String id : listID) {
+ if (netdev[1].equals(id)) {
+ NETDEV.setID("id-netdev-"+nbNetDev);
+ listID.add(NETDEV.getID());
+ exist = true;
+ }
+ }
+ if (!exist) {
+ NETDEV.setID(netdev[1]);
+ listID.add(NETDEV.getID());
+ }
+ headers.put(NETDEV.getHeader(), NETDEV.getID());
+ nbNetDev++;
+ exist = false;
+ } else if (option.equals(NETDEV.toString())){//Check drive without id
+ NETDEV.setID("id-netdev-"+nbNetDev);
+ listID.add(NETDEV.getID());
+ headers.put(option, NETDEV.getID());
} else if (option.equals(DEVICE.getHeader())) {//This will alwas come as [device]
DRIVE.setID("dev" + nbDev);
headers.put(option, DRIVE.getID());
@@ -258,9 +279,7 @@ public final class QemuConfig {
}
//Get Bus Type
String busType = entry.getValue().get("if");
- if (busType == null) {
- bus = DriveBusType.IDE;
- } else {
+ if (busType != null) {
switch (busType) {
case "ide":
bus = DriveBusType.IDE;
@@ -274,13 +293,16 @@ public final class QemuConfig {
default:
break;
}
+ } else {
+ bus = DriveBusType.NONE;
}
}
}
- } //TODO set default device if no device found
+ }
if (entry.getKey().containsKey(DEVICE.getHeader())) {
String drive = entry.getValue().get("drive");
if (drive != null && drive.equals(DRIVE.getID())) {
+ DEVICE.setID(drive);
controllerDevice = entry.getValue().get("driver");
}
}
@@ -291,10 +313,25 @@ public final class QemuConfig {
}
//No device
if (dev == 0) {
- if (bus.equals(DriveBusType.IDE)) {
- controllerDevice = "ide-hd";
- }else if (bus.equals(DriveBusType.SCSI)) {
- controllerDevice = "scsi-hd";
+ if (bus != null) {
+ switch (bus) {
+ case IDE:
+ controllerDevice = "ide-hd";
+ break;
+ case SCSI:
+ controllerDevice = "scsi-hd";
+ break;
+ case NONE:
+ controllerDevice = "";
+ break;
+ default:
+ break;
+ }
+ {
+
+ }
+ } else {
+ controllerDevice = "";
}
}
if ((bus != null) && (filename != null) && (controllerDevice != null)) {
@@ -323,7 +360,7 @@ public final class QemuConfig {
for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : sortedArray == null ? entries.entrySet() : sortedArray.entrySet()) {
- String header = entry.getKey().keySet().toString();;
+ String header = entry.getKey().keySet().toString();
if (entry.getKey().containsKey(DRIVE.getHeader())) {
if ((entry.getValue().get("index") != null)) {
@@ -332,6 +369,14 @@ public final class QemuConfig {
} else {
continue;
}
+ } else if (entry.getValue().get("media") != null) {
+ DRIVE.setID(entry.getKey().get(DRIVE.getHeader()));
+ header = DRIVE.toString();
+ } else if (entry.getValue().get("if") != null) {
+ if (entry.getValue().get("if").equals("floppy")) {
+ DRIVE.setID(entry.getKey().get(DRIVE.getHeader()));
+ header = DRIVE.toString();
+ }
} else {
continue;
}
@@ -346,10 +391,18 @@ public final class QemuConfig {
for (String key : values.keySet()) {
String value = values.get(key);
- sb.append(" " + key + " = " + value + "\n");
+ if (value == null) {
+ sb.append(" " + key + " = " + value + "\n");
+ } else {
+ if (key.equals("driver")) {
+ if (value.isEmpty()) {
+ continue;
+ }
+ }
+ sb.append(" " + key + " = " + quote + value + quote + "\n");
+ }
}
}
- LOGGER.debug(sb);
return sb.toString();
}
}
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
diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java
index b260bc1..1d9fb1b 100644
--- a/src/main/java/org/openslx/util/vm/VmMetaData.java
+++ b/src/main/java/org/openslx/util/vm/VmMetaData.java
@@ -88,7 +88,7 @@ public abstract class VmMetaData<T, U, V, W> {
}
public static enum DriveBusType {
- SCSI, IDE, SATA;
+ SCSI, IDE, SATA, NONE;
}
public static class HardDisk {