summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/QemuMetaData.java
blob: 73612abc0bd9984ea28230e7ef77df452f14b5f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
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;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.thrifthelper.TConst;
import static org.openslx.util.vm.QemuConfig.Header.*;

class QemuDDAccelMeta {

    public final boolean isPresent;

    public QemuDDAccelMeta(boolean present) {
        isPresent = present;
    }
}

class QemuHWVersionMeta {

    public final int version;

    public QemuHWVersionMeta(int vers) {
        version = vers;
    }
}

class QemuEthernetDevTypeMeta {

    public final String value;

    public QemuEthernetDevTypeMeta(String val) {
        value = val;
    }
}

class QemuSoundCardMeta {

    public final boolean isPresent;
    public final String value;

    public QemuSoundCardMeta(boolean present, String val) {

        isPresent = present;
        value = val;
    }
}

public final class QemuMetaData extends VmMetaData<QemuSoundCardMeta, QemuDDAccelMeta, QemuHWVersionMeta, QemuEthernetDevTypeMeta> {

    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 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 floppyCounter = 0;

    public static enum EthernetType {
        NAT("qnet1"), BRIDGED("qnet0"), HOST_ONLY("qnet2");

        public final String vmnet;

        private EthernetType(String vnet) {
            this.vmnet = vnet;
        }
        
        public String getVMnet() {
            return this.vmnet;
        }
    }

    public QemuMetaData(List<OperatingSystem> osList, File config) {
        super(osList);
        this.config = new QemuConfig(config);
        init();
    }

    public QemuMetaData(List<OperatingSystem> osList, byte[] vmContent, int length) throws IOException, UnsupportedVirtualizerFormatException {
        super(osList);
        this.config = new QemuConfig(vmContent, length);
        init();
    }

    public void init() {
        registerVirtualHW();
        config.setHdds();
        displayName = config.getDisplayName();
        setOs(config.getOsName());
        isMachineSnapshot = config.isMachineSnapshot();
        for (HardDisk hardDisk : config.getHdds()) {
            hdds.add(hardDisk);
        }
    }

    @Override
    public byte[] getFilteredDefinitionArray() {
        //TODO: Filter is done fix server side
        return config.toString(false).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public byte[] getDefinitionArray() {
        return config.toString(true).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public void applySettingsForLocalEdit() {
        //nothing for VmWare
    }

    @Override
    public boolean addHddTemplate(File diskImage, String hddMode, String redoDir) {
        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 Filename && Bus
        if (option != null) {
            option.replace("file", diskImagePath);
            if (option.get("if") == null) {
           
            } 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);

        // Device
        option = new TreeMap<>();
        option = config.get(DEVICE.getHeader(), DEVICE.getID());

        if (option == null) {
            option = new TreeMap<>();
            header = new LinkedHashMap<>();
            header.put(DEVICE.getHeader(), null);

            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;
    }

    @Override
    public boolean addDefaultNat() {
        //No arguments needed, default on nat. But cannot receive
        return true;
    }

    @Override
    public void setOs(String vendorOsId) {
        setOs(TConst.VIRT_QEMU, vendorOsId);
    }

    @Override
    public boolean addDisplayName(String name) {
        option = new TreeMap<>();
        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);

        header = new LinkedHashMap<>();
        header.put(MEMORY.getHeader(), null);

        config.set(header, option);
        return true;
    }

    @Override
    public void addFloppy(int index, String image, boolean readOnly) {
        option = new TreeMap<>();
        if (readOnly) {
            option.put("readonly", "on");
        } else {
            option.put("readonly", "off");
        }
        option.put("if", "floppy");
        option.put("file", image);

        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("file", image);
        header = new LinkedHashMap<>();
        header.put(DRIVE.getHeader(), "cdrom" + cdromCounter);       
        config.set(header, option);
        cdromCounter++;
        return true;
    }

    @Override
    public boolean addCpuCoreCount(int nrOfCores) {
        option = new TreeMap<>();
        option.put("cpus", nrOfCores+"");

        header = new LinkedHashMap<>();
        header.put(SMP.getHeader(), null);
        config.set(header, option);
        return true;
    }

    @Override
    public void setSoundCard(VmMetaData.SoundCardType type) {
        //Not possible will be set as comment in config file
    }

    @Override
    public VmMetaData.SoundCardType getSoundCard() {
        //not possible to set just write comment
        return null;
    }

    @Override
    public void setDDAcceleration(VmMetaData.DDAcceleration type) {
        //TODO: Not really used by qemu. 
    }

    @Override
    public VmMetaData.DDAcceleration getDDAcceleration() {
        return DDAcceleration.OFF;
    }

    @Override
    public void setHWVersion(VmMetaData.HWVersion type) {
        //nothing...
    }

    @Override
    public VmMetaData.HWVersion getHWVersion() {
        return VmMetaData.HWVersion.DEFAULT;
    }

    @Override
    public void setEthernetDevType(int cardIndex, VmMetaData.EthernetDevType type) {
        QemuEthernetDevTypeMeta dev = networkCards.get(type);
        option = new TreeMap<>();
        option.put("driver", "\"" + dev.value + "\"");
        option.put("netdev", "\"net" + cardIndex + "\"");
//        config.set("[device]", option);
    }

    @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;
//                        }
//                    }
//                }
//            }
//        }
        return EthernetDevType.AUTO;
    }

    @Override
    public boolean addEthernet(VmMetaData.EtherType type) {
        boolean ret = false;
        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() {
        return virtualizer;
    }

    @Override
    public void enableUsb(boolean enabled) {
        option = new TreeMap<>();
        if (enabled) {
            option.put("usb", "on");
        } else {
            option.put("usb", "off");
        }
        
        header = new LinkedHashMap<>();
        header.put(MACHINE.getHeader(), MACHINE.getID());
        config.set(header, option);
    }

    @Override
    public boolean disableSuspend() {
        return false;
    }

    @Override
    public void registerVirtualHW() {
        soundCards.put(VmMetaData.SoundCardType.NONE, new QemuSoundCardMeta(false, null));
        soundCards.put(VmMetaData.SoundCardType.DEFAULT, new QemuSoundCardMeta(true, "ich6"));
        soundCards.put(VmMetaData.SoundCardType.AC, new QemuSoundCardMeta(true, "ac97"));
        soundCards.put(VmMetaData.SoundCardType.ES, new QemuSoundCardMeta(true, "es1370"));
        soundCards.put(VmMetaData.SoundCardType.SOUND_BLASTER, new QemuSoundCardMeta(true, "sb16"));

        ddacc.put(DDAcceleration.OFF, new QemuDDAccelMeta(false));
        ddacc.put(DDAcceleration.ON, new QemuDDAccelMeta(true));

        hwversion.put(HWVersion.DEFAULT, new QemuHWVersionMeta(0));

        networkCards.put(EthernetDevType.VIRTIO, new QemuEthernetDevTypeMeta("virtio-net-pci"));
        networkCards.put(EthernetDevType.E1000, new QemuEthernetDevTypeMeta("e1000"));
        networkCards.put(EthernetDevType.PCNET32, new QemuEthernetDevTypeMeta("pcnet"));
        networkCards.put(EthernetDevType.RTL8139, new QemuEthernetDevTypeMeta("rtl8139"));

    }

}