From eb32fccde3765481fd89e2041ec2c0f6ab9c6327 Mon Sep 17 00:00:00 2001 From: Christopher Lucas Date: Wed, 19 Dec 2018 10:42:00 +0100 Subject: Clean up project --- src/main/java/org/openslx/util/vm/QemuConfig.java | 75 ++++++++++++----------- 1 file changed, 40 insertions(+), 35 deletions(-) (limited to 'src/main/java/org/openslx/util/vm/QemuConfig.java') diff --git a/src/main/java/org/openslx/util/vm/QemuConfig.java b/src/main/java/org/openslx/util/vm/QemuConfig.java index e6154d8..ceb3285 100644 --- a/src/main/java/org/openslx/util/vm/QemuConfig.java +++ b/src/main/java/org/openslx/util/vm/QemuConfig.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Set; import java.util.TreeMap; import org.apache.log4j.Logger; import static org.openslx.util.vm.QemuConfig.Header.*; @@ -25,11 +24,10 @@ public final class QemuConfig { private String osName = new String(); - private final String quote = "\""; + private final static String QUOTE = "\""; private ArrayList hddsArray = new ArrayList<>(); - // BOOT("netdev", "net0") -> toString -> "[netdev "net0"]" public static enum Header { BOOT("boot-opts", null), DEVICE("device", null), DRIVE("drive", ""), MACHINE("machine", null), MEMORY("memory", null), NAME("name", null), @@ -62,14 +60,13 @@ public final class QemuConfig { if (id == null) { result = "[" + header + "]"; } else { - result = "[" + header + " \"" + id + "\"]"; + result = "[" + header + " " + QUOTE + id + QUOTE + "]"; } return result; } } public QemuConfig(byte[] vmContent, int length) { - //transform byte[] to arralist sorted ArrayList result; BufferedReader stream = null; try { @@ -82,11 +79,7 @@ public final class QemuConfig { while ((line = stream.readLine()) != null) { if (line.startsWith("#") == false) { if (line.isEmpty() == false) { -// if (line.contains(DEVICE.getHeader())) { -// result.add("[" + DEVICE.getHeader() + "]"); -// } else { result.add(line.trim()); -// } } } } @@ -107,7 +100,6 @@ public final class QemuConfig { } public QemuConfig(File configFile) { - //Transform file into byte[]/arraylist ArrayList result; BufferedReader stream = null; try { @@ -150,14 +142,14 @@ public final class QemuConfig { LinkedHashMap headers = null; TreeMap options = null; boolean exist = false; + boolean save = true; if (lines != null) { - //Adding all the lines to a Map - for (String option : lines) { - if (option.startsWith("[") && option.endsWith("]")) { + for (String option : lines) { + if (option.startsWith("[") && option.endsWith("]")) { option = option.replaceAll("\\[|\\]", ""); headers = new LinkedHashMap<>(); - if (option.contains(DRIVE.getHeader()) && option.contains(quote)) { //Check drive with id - String[] drive = option.split(quote); + if (option.contains(DRIVE.getHeader()) && option.contains(QUOTE)) { //Check drive with id + String[] drive = option.split(QUOTE); for (String id : listID) { if (drive[1].equals(id)) { DRIVE.setID("id-disk-" + nbDrive); @@ -177,8 +169,8 @@ public final class QemuConfig { listID.add(DRIVE.getID()); headers.put(option, DRIVE.getID()); nbDrive++; - } else if (option.contains(NETDEV.getHeader()) && option.contains(quote)) {//Check netdev with id - String[] netdev = option.split(quote); + } 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); @@ -193,16 +185,21 @@ public final class QemuConfig { headers.put(NETDEV.getHeader(), NETDEV.getID()); nbNetDev++; exist = false; - } else if (option.equals(NETDEV.toString())) {//Check drive without id + } else if (option.equals(NETDEV.toString())) {//Check netdev 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] + } else if (option.equals(DEVICE.getHeader())) {//This will always come as [device] DEVICE.setID("dev" + nbDev); headers.put(option, DEVICE.getID()); nbDev++; } else { - headers.put(option, null); + if (option.equals(MEMORY.getHeader()) || option.equals(SMP.getHeader()) || option.equals(MACHINE.getHeader())) { + save = false; + continue; + } else { + headers.put(option, null); + } } options = new TreeMap<>(); index++; @@ -210,9 +207,13 @@ public final class QemuConfig { //In case file doesn't begin with a header LOGGER.error("This config file is invalid. Chech syntax. Must begin with [..]"); } else { - String[] parameter = option.split("="); - options.put(parameter[0].trim(), parameter[1].trim().replace("\"", "")); - entries.put(headers, options); + if (save) { + String[] parameter = option.split("="); + options.put(parameter[0].trim(), parameter[1].trim().replace(QUOTE, "")); + entries.put(headers, options); + }else{ + save = true; + } } } } @@ -225,7 +226,7 @@ public final class QemuConfig { if (entries.containsKey(keys)) { value = entries.get(keys); } - return value; //value of key + return value; } public void set(LinkedHashMap key, TreeMap value) { @@ -271,22 +272,22 @@ public final class QemuConfig { public void setHdds() { int dev = 0; String filename = null; - String busType = null; DriveBusType bus = null; String controllerDevice = null; LinkedHashMap keys; TreeMap options; for (Map.Entry, TreeMap> entry : entries.entrySet()) { + String busType; if (entry.getKey().containsKey(DRIVE.getHeader())) { if (entry.getValue().containsKey("index")) { if (entry.getValue().get("index").equals("0")) { DRIVE.setID(entry.getKey().get(DRIVE.getHeader())); - //Get Path + filename = entry.getValue().get("file"); if (filename == null) { LOGGER.error("Please make sure your harddrive has a path"); } - //Get Bus Type + busType = entry.getValue().get("if"); if (busType != null) { switch (busType) { @@ -296,10 +297,13 @@ public final class QemuConfig { case "scsi": bus = DriveBusType.SCSI; break; + case "virtio": + bus = DriveBusType.VIRTIO; case "sata": - //not available for Qemu. Others : sd, mtd, floppy, pflash, virtio + //not available for Qemu. Others : sd, mtd, floppy, pflash break; default: + bus = DriveBusType.SCSI; break; } } else { @@ -321,14 +325,12 @@ public final class QemuConfig { dev++; } } else { -// for (String key : entry.getKey().keySet()) { for (LinkedHashMap key : entries.keySet()) { if (key.containsKey(DEVICE.getHeader())) { dev++; } } } - //No device if (dev == 0) { if (bus != null) { switch (bus) { @@ -336,10 +338,13 @@ public final class QemuConfig { controllerDevice = "ide-hd"; break; case SCSI: - controllerDevice = "scsi-hd"; + controllerDevice = "scsi-generic"; + break; + case VIRTIO: + controllerDevice = "virtio-9p-device"; break; default: - controllerDevice = "scsi-hd"; + controllerDevice = "scsi-generic"; break; } } @@ -377,7 +382,6 @@ public final class QemuConfig { sortedArray.put(entry.getKey(), entry.getValue()); } if (entry.getKey().containsKey(NAME.getHeader()) || entry.getKey().containsKey(DRIVE.getHeader())) { - sortedArray.put(entry.getKey(), entry.getValue()); } } @@ -409,7 +413,8 @@ public final class QemuConfig { if (entry.getKey().containsKey(DEVICE.getHeader())) { DEVICE.setID(null); - entry.getKey().replace(DEVICE.getHeader(), null); + entry.getKey().remove(DEVICE.getHeader()); + entry.getKey().put(DEVICE.getHeader(), null); header = DEVICE.toString(); } @@ -430,7 +435,7 @@ public final class QemuConfig { continue; } } - sb.append(" " + key + " = " + quote + value + quote + "\n"); + sb.append(" " + key + " = " + QUOTE + value + QUOTE + "\n"); } } } -- cgit v1.2.3-55-g7522