From 6a893099c4229020f4f439fe6388dbc41243f423 Mon Sep 17 00:00:00 2001 From: Christopher Lucas Date: Fri, 7 Dec 2018 15:34:11 +0100 Subject: Upload and Download working --- .../java/org/openslx/util/vm/QemuMetaData.java | 241 +++++++++++++-------- 1 file changed, 145 insertions(+), 96 deletions(-) (limited to 'src/main/java/org/openslx/util/vm/QemuMetaData.java') 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 arguments = new HashMap<>(); //to remove at the end. easier // the above map's elements will take the place of in the config string - private QemuConfig config; + private final QemuConfig config; private String setup; + private final static String QUOTE = "\""; + 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 driveCounter = 0; - private int floppyCounter = 0; public static enum EthernetType { @@ -81,6 +84,10 @@ public final class QemuMetaData extends VmMetaData osList, File config) { @@ -100,13 +107,12 @@ public final class QemuMetaData extends VmMetaData(); - 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(); - 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(); 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(); - 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 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 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(); 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 -- cgit v1.2.3-55-g7522