summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/vm/QemuConfig.java
blob: ceb328516d6d004f3d4b474c24ae3da494feaef3 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package org.openslx.util.vm;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import java.util.Map;
import java.util.TreeMap;
import org.apache.log4j.Logger;
import static org.openslx.util.vm.QemuConfig.Header.*;
import org.openslx.util.vm.VmMetaData.DriveBusType;

public final class QemuConfig {

    private static final Logger LOGGER = Logger.getLogger(QemuConfig.class);

    private Map<LinkedHashMap<String, String>, TreeMap<String, String>> entries = new LinkedHashMap<>();

    private String osName = new String();

    private final static String QUOTE = "\"";

    private ArrayList<VmMetaData.HardDisk> hddsArray = new ArrayList<>();

    public static enum Header {
        BOOT("boot-opts", null), DEVICE("device", null), DRIVE("drive", ""),
        MACHINE("machine", null), MEMORY("memory", null), NAME("name", null),
        NETDEV("netdev", ""), SMP("smp-opts", null);

        private final String header;

        private String id;

        private Header(String header, String id) {
            this.header = header;
            this.id = id;
        }

        public String getHeader() {
            return this.header;
        }

        public String getID() {
            return id;
        }

        public void setID(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            String result;
            if (id == null) {
                result = "[" + header + "]";
            } else {
                result = "[" + header + " " + QUOTE + id + QUOTE + "]";
            }
            return result;
        }
    }

    public QemuConfig(byte[] vmContent, int length) {
        ArrayList<String> result;
        BufferedReader stream = null;
        try {
            stream = new BufferedReader(
                    new InputStreamReader(
                            new ByteArrayInputStream(vmContent, 0, length), StandardCharsets.ISO_8859_1));
            result = new ArrayList<>();
            String line;

            while ((line = stream.readLine()) != null) {
                if (line.startsWith("#") == false) {
                    if (line.isEmpty() == false) {
                        result.add(line.trim());
                    }
                }
            }
            stream.close();
            stream = null;
        } catch (IOException e) {
            result = null;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                    stream = null;
                } catch (IOException ioe2) {
                }
            }
        }
        init(result);
    }

    public QemuConfig(File configFile) {
        ArrayList<String> result;
        BufferedReader stream = null;
        try {
            stream = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(configFile), StandardCharsets.ISO_8859_1));
            result = new ArrayList<>();
            String line;

            while ((line = stream.readLine()) != null) {
                if (line.startsWith("#") == false) {
                    if (line.isEmpty() == false) {
                        result.add(line.trim());
                    }
                }
            }
            stream.close();
            stream = null;
        } catch (IOException e) {
            result = null;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                    stream = null;
                } catch (IOException ioe2) {
                }
            }
        }
        init(result);
    }

    public void init(ArrayList<String> lines) {
        int index = -1;
        int nbDev = 0;
        int nbDrive = 0;
        int nbNetDev = 0;

        ArrayList<String> listID = new ArrayList<>();
        LinkedHashMap<String, String> headers = null;
        TreeMap<String, String> options = null;
        boolean exist = false;
        boolean save = true;
        if (lines != null) {
            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);
                        for (String id : listID) {
                            if (drive[1].equals(id)) {
                                DRIVE.setID("id-disk-" + nbDrive);
                                listID.add(DRIVE.getID());
                                exist = true;
                            }
                        }
                        if (!exist) {
                            DRIVE.setID(drive[1]);
                            listID.add(DRIVE.getID());
                        }
                        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.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 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 always come as [device]
                        DEVICE.setID("dev" + nbDev);
                        headers.put(option, DEVICE.getID());
                        nbDev++;
                    } else {
                        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++;
                } else if (index == -1) {
                    //In case file doesn't begin with a header
                    LOGGER.error("This config file is invalid. Chech syntax. Must begin with [..]");
                } else {
                    if (save) {
                        String[] parameter = option.split("=");
                        options.put(parameter[0].trim(), parameter[1].trim().replace(QUOTE, ""));
                        entries.put(headers, options);
                    }else{
                        save = true;
                    }
                }
            }
        }
    }

    public TreeMap<String, String> get(String key, String id) {
        TreeMap<String, String> value = null;
        LinkedHashMap keys = new LinkedHashMap();
        keys.put(key, id);
        if (entries.containsKey(keys)) {
            value = entries.get(keys);
        }
        return value;
    }

    public void set(LinkedHashMap<String, String> key, TreeMap value) {
        entries.put(key, value);
    }

    public TreeMap<LinkedHashMap<String, String>, TreeMap<String, String>> get() {
        return (TreeMap<LinkedHashMap<String, String>, TreeMap<String, String>>) entries;
    }

    public String getDisplayName() {
        String result = "";
        LinkedHashMap key = new LinkedHashMap();
        key.put(NAME.getHeader(), null);

        if (entries.containsKey(key)) {
            result = entries.get(key).get("guest");
        }
        return result;
    }

    public boolean isMachineSnapshot() {
        boolean isSnapshot = false;
        LinkedHashMap key = new LinkedHashMap();
        key.put(DRIVE.getHeader(), DRIVE.getID());
        String active = entries.get(key).get("snapshot");

        if (active != null && active.equals("on")) {
            isSnapshot = true;
        }
        return isSnapshot;
    }

    public void setOsName() {
        //It is not defined in config file. Using dummy name. Will be set in combo box later
        osName = "QemuOS";
    }

    public String getOsName() {
        return osName;
    }

    public void setHdds() {
        int dev = 0;
        String filename = null;
        DriveBusType bus = null;
        String controllerDevice = null;
        LinkedHashMap<String, String> keys;
        TreeMap<String, String> options;
        for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> 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()));

                        filename = entry.getValue().get("file");
                        if (filename == null) {
                            LOGGER.error("Please make sure your harddrive has a path");
                        }

                        busType = entry.getValue().get("if");
                        if (busType != null) {
                            switch (busType) {
                                case "ide":
                                    bus = DriveBusType.IDE;
                                    break;
                                case "scsi":
                                    bus = DriveBusType.SCSI;
                                    break;
                                case "virtio":
                                    bus = DriveBusType.VIRTIO;
                                case "sata":
                                    //not available for Qemu. Others : sd, mtd, floppy, pflash
                                    break;
                                default:
                                    bus = DriveBusType.SCSI;
                                    break;
                            }
                        } else {
                            bus = DriveBusType.SCSI;
                            busType = "scsi";
                            keys = new LinkedHashMap<>();
                            keys.put(DRIVE.getHeader(), DRIVE.getID());
                            options = entries.get(entry.getKey());
                            options.put("if", busType);
                            entries.put(keys, options);
                        }
                    }
                }
            }
            if (entry.getKey().containsKey(DEVICE.getHeader())) {
                String drive = entry.getValue().get("drive");
                if (drive != null && drive.equals(DRIVE.getID())) {
                    controllerDevice = entry.getValue().get("driver");
                    dev++;
                }
            } else {
                for (LinkedHashMap<String, String> key : entries.keySet()) {
                    if (key.containsKey(DEVICE.getHeader())) {
                        dev++;
                    }
                }
            }
            if (dev == 0) {
                if (bus != null) {
                    switch (bus) {
                        case IDE:
                            controllerDevice = "ide-hd";
                            break;
                        case SCSI:
                            controllerDevice = "scsi-generic";
                            break;
                        case VIRTIO:
                            controllerDevice = "virtio-9p-device";
                            break;
                        default:
                            controllerDevice = "scsi-generic";
                            break;
                    }
                }
            }
            if ((bus != null) && (filename != null) && (controllerDevice != null)) {
                hddsArray.add(new VmMetaData.HardDisk(controllerDevice, bus, filename));;

                if (dev == 0) {
                    keys = new LinkedHashMap<>();
                    options = new TreeMap<>();
                    DEVICE.setID(DRIVE.getID());
                    keys.put(DEVICE.getHeader(), DEVICE.getID());
                    options.put("drive", DRIVE.getID());
                    options.put("driver", controllerDevice);
                    entries.put(keys, options);
                }
                break;
            }
        }
    }

    public ArrayList<VmMetaData.HardDisk> getHdds() {
        return hddsArray;
    }

    public String toString(boolean filtered) {
        StringBuilder sb = new StringBuilder(300);
        LinkedHashMap<LinkedHashMap<String, String>, TreeMap<String, String>> sortedArray = null;
        if (filtered) {
            sortedArray = new LinkedHashMap<>();
            for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : entries.entrySet()) {
                if (entry.getKey().containsKey(DEVICE.getHeader())) {
                    DEVICE.setID(null);
                    entry.getKey().replace(DEVICE.getHeader(), null);
                    sortedArray.put(entry.getKey(), entry.getValue());
                }
                if (entry.getKey().containsKey(NAME.getHeader()) || entry.getKey().containsKey(DRIVE.getHeader())) {
                    sortedArray.put(entry.getKey(), entry.getValue());
                }
            }
        }

        for (Map.Entry<LinkedHashMap<String, String>, TreeMap<String, String>> entry : sortedArray == null ? entries.entrySet() : sortedArray.entrySet()) {

            String header = entry.getKey().keySet().toString();

            if (entry.getKey().containsKey(DRIVE.getHeader())) {
                if ((entry.getValue().get("index") != null)) {
                    if ((entry.getValue().get("index").equals("0"))) {
                        header = DRIVE.toString();
                    } 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;
                }
            }

            if (entry.getKey().containsKey(DEVICE.getHeader())) {
                DEVICE.setID(null);
                entry.getKey().remove(DEVICE.getHeader());
                entry.getKey().put(DEVICE.getHeader(), null);
                header = DEVICE.toString();
            }

            if (entry.getKey().containsKey(NETDEV.getHeader())) {
                header = NETDEV.toString();
            }

            sb.append(header + "\n");
            TreeMap<String, String> values = entry.getValue();

            for (String key : values.keySet()) {
                String value = values.get(key);
                if (value == null) {
                    sb.append("  " + key + " = " + value + "\n");
                } else {
                    if (key.equals("driver")) {
                        if (value.isEmpty()) {
                            continue;
                        }
                    }
                    sb.append("  " + key + " = " + QUOTE + value + QUOTE + "\n");
                }
            }
        }
        return sb.toString();
    }
}