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 ++++++++------- .../java/org/openslx/util/vm/QemuMetaData.java | 103 ++++++++------------- src/main/java/org/openslx/util/vm/VmMetaData.java | 2 +- 3 files changed, 80 insertions(+), 100 deletions(-) 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"); } } } diff --git a/src/main/java/org/openslx/util/vm/QemuMetaData.java b/src/main/java/org/openslx/util/vm/QemuMetaData.java index 73612ab..8855f9c 100644 --- a/src/main/java/org/openslx/util/vm/QemuMetaData.java +++ b/src/main/java/org/openslx/util/vm/QemuMetaData.java @@ -3,10 +3,8 @@ package org.openslx.util.vm; 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; import org.apache.log4j.Logger; @@ -56,26 +54,22 @@ class QemuSoundCardMeta { public final class QemuMetaData extends VmMetaData { - private final Map arguments = new HashMap<>(); //to remove at the end. easier - // the above map's elements will take the place of in the config string - private final QemuConfig config; + private static final Logger LOGGER = Logger.getLogger(QemuMetaData.class); - private String setup; + private static final Virtualizer virtualizer = new Virtualizer(TConst.VIRT_QEMU, "QEMU-KVM"); - private final static String QUOTE = "\""; + private final QemuConfig config; private TreeMap option; private LinkedHashMap 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 floppyCounter = 0; + private int netdevCounter = 0; + public static enum EthernetType { NAT("qnet1"), BRIDGED("qnet0"), HOST_ONLY("qnet2"); @@ -84,10 +78,6 @@ public final class QemuMetaData extends VmMetaData osList, File config) { @@ -126,7 +116,7 @@ public final class QemuMetaData extends VmMetaData(); option = config.get(DRIVE.getHeader(), DRIVE.getID()); - // Drive Filename && Bus if (option != null) { - option.replace("file", diskImagePath); + option.remove("file"); + option.put("file", diskImagePath); if (option.get("if") == null) { - + } else { switch (option.get("if")) { case "ide": @@ -163,12 +153,10 @@ public final class QemuMetaData extends VmMetaData(); header.put(DRIVE.getHeader(), DRIVE.getID()); config.set(header, option); - // Device option = new TreeMap<>(); option = config.get(DEVICE.getHeader(), DEVICE.getID()); @@ -177,14 +165,14 @@ public final class QemuMetaData extends VmMetaData(); header.put(DEVICE.getHeader(), null); - option.put("drive",DRIVE.getID()); + 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{ + } else { //Shouldn't come to this but to check TODO } config.set(header, option); @@ -218,7 +206,7 @@ public final class QemuMetaData extends VmMetaData(); - option.put("size", ""+ mem); + option.put("size", "" + mem); header = new LinkedHashMap<>(); header.put(MEMORY.getHeader(), null); @@ -239,7 +227,7 @@ public final class QemuMetaData extends VmMetaData(); - header.put(DRIVE.getHeader(),"floppy" + floppyCounter); + header.put(DRIVE.getHeader(), "floppy" + floppyCounter); config.set(header, option); floppyCounter++; } @@ -250,7 +238,7 @@ public final class QemuMetaData extends VmMetaData(); - header.put(DRIVE.getHeader(), "cdrom" + cdromCounter); + header.put(DRIVE.getHeader(), "cdrom" + cdromCounter); config.set(header, option); cdromCounter++; return true; @@ -259,7 +247,7 @@ public final class QemuMetaData extends VmMetaData(); - option.put("cpus", nrOfCores+""); + option.put("cpus", nrOfCores + ""); header = new LinkedHashMap<>(); header.put(SMP.getHeader(), null); @@ -269,13 +257,13 @@ public final class QemuMetaData extends VmMetaData(); + header.put(DEVICE.getHeader(), "net" + netdevCounter); option = new TreeMap<>(); - option.put("driver", "\"" + dev.value + "\""); - option.put("netdev", "\"net" + cardIndex + "\""); -// config.set("[device]", option); + option.put("driver", dev.value); + //option.put("netdev", "net" + cardIndex); + config.set(header, option); + netdevCounter++; } @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; -// } -// } -// } -// } -// } + //TO DO return EthernetDevType.AUTO; } @@ -334,21 +309,19 @@ public final class QemuMetaData extends VmMetaData dev = config.get(DEVICE.getHeader(), DEVICE.getID()); - if (dev == null) { + if (dev == null || dev.get("netdev") == null) { break; - } else { - if (dev.get("netdev") == null) { - break; - } } + } - + switch (type) { - case NAT: + case NAT: ret = addEthernet(index, EthernetType.NAT); break; case BRIDGED: @@ -363,23 +336,25 @@ public final class QemuMetaData extends VmMetaData(); header.put(NETDEV.getHeader(), NETDEV.getID()); //netdev option = new TreeMap<>(); + //device tap,ifname=nat1 option.put("ifname", type.vmnet); option.put("type", "tap"); config.set(header, option); //device header = new LinkedHashMap<>(); - header.put(DEVICE.getHeader(), ""); + header.put(DEVICE.getHeader(), "netType" + netdevCounter); option = new TreeMap<>(); option.put("driver", "e1000"); option.put("netdev", NETDEV.getID()); config.set(header, option); + netdevCounter++; return true; } @@ -396,7 +371,7 @@ public final class QemuMetaData extends VmMetaData(); header.put(MACHINE.getHeader(), MACHINE.getID()); config.set(header, option); diff --git a/src/main/java/org/openslx/util/vm/VmMetaData.java b/src/main/java/org/openslx/util/vm/VmMetaData.java index b260bc1..c17df89 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 { } public static enum DriveBusType { - SCSI, IDE, SATA; + SCSI, IDE, SATA, VIRTIO; } public static class HardDisk { -- cgit v1.2.3-55-g7522