Merge remote-tracking branch 'origin/master' into staging
[qemu/mdroth.git] / qemu-config.c
blob93d20c6cf82d8e9bb8b77e28cc902d685d17398d
1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
5 #include "hw/qdev.h"
7 static QemuOptsList qemu_drive_opts = {
8 .name = "drive",
9 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
10 .desc = {
12 .name = "bus",
13 .type = QEMU_OPT_NUMBER,
14 .help = "bus number",
15 },{
16 .name = "unit",
17 .type = QEMU_OPT_NUMBER,
18 .help = "unit number (i.e. lun for scsi)",
19 },{
20 .name = "if",
21 .type = QEMU_OPT_STRING,
22 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
23 },{
24 .name = "index",
25 .type = QEMU_OPT_NUMBER,
26 .help = "index number",
27 },{
28 .name = "cyls",
29 .type = QEMU_OPT_NUMBER,
30 .help = "number of cylinders (ide disk geometry)",
31 },{
32 .name = "heads",
33 .type = QEMU_OPT_NUMBER,
34 .help = "number of heads (ide disk geometry)",
35 },{
36 .name = "secs",
37 .type = QEMU_OPT_NUMBER,
38 .help = "number of sectors (ide disk geometry)",
39 },{
40 .name = "trans",
41 .type = QEMU_OPT_STRING,
42 .help = "chs translation (auto, lba. none)",
43 },{
44 .name = "media",
45 .type = QEMU_OPT_STRING,
46 .help = "media type (disk, cdrom)",
47 },{
48 .name = "snapshot",
49 .type = QEMU_OPT_BOOL,
50 .help = "enable/disable snapshot mode",
51 },{
52 .name = "file",
53 .type = QEMU_OPT_STRING,
54 .help = "disk image",
55 },{
56 .name = "cache",
57 .type = QEMU_OPT_STRING,
58 .help = "host cache usage (none, writeback, writethrough, unsafe)",
59 },{
60 .name = "aio",
61 .type = QEMU_OPT_STRING,
62 .help = "host AIO implementation (threads, native)",
63 },{
64 .name = "format",
65 .type = QEMU_OPT_STRING,
66 .help = "disk format (raw, qcow2, ...)",
67 },{
68 .name = "serial",
69 .type = QEMU_OPT_STRING,
70 .help = "disk serial number",
71 },{
72 .name = "rerror",
73 .type = QEMU_OPT_STRING,
74 .help = "read error action",
75 },{
76 .name = "werror",
77 .type = QEMU_OPT_STRING,
78 .help = "write error action",
79 },{
80 .name = "addr",
81 .type = QEMU_OPT_STRING,
82 .help = "pci address (virtio only)",
83 },{
84 .name = "readonly",
85 .type = QEMU_OPT_BOOL,
86 .help = "open drive file as read-only",
88 { /* end of list */ }
92 static QemuOptsList qemu_chardev_opts = {
93 .name = "chardev",
94 .implied_opt_name = "backend",
95 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
96 .desc = {
98 .name = "backend",
99 .type = QEMU_OPT_STRING,
101 .name = "path",
102 .type = QEMU_OPT_STRING,
104 .name = "host",
105 .type = QEMU_OPT_STRING,
107 .name = "port",
108 .type = QEMU_OPT_STRING,
110 .name = "localaddr",
111 .type = QEMU_OPT_STRING,
113 .name = "localport",
114 .type = QEMU_OPT_STRING,
116 .name = "to",
117 .type = QEMU_OPT_NUMBER,
119 .name = "ipv4",
120 .type = QEMU_OPT_BOOL,
122 .name = "ipv6",
123 .type = QEMU_OPT_BOOL,
125 .name = "wait",
126 .type = QEMU_OPT_BOOL,
128 .name = "server",
129 .type = QEMU_OPT_BOOL,
131 .name = "delay",
132 .type = QEMU_OPT_BOOL,
134 .name = "telnet",
135 .type = QEMU_OPT_BOOL,
137 .name = "width",
138 .type = QEMU_OPT_NUMBER,
140 .name = "height",
141 .type = QEMU_OPT_NUMBER,
143 .name = "cols",
144 .type = QEMU_OPT_NUMBER,
146 .name = "rows",
147 .type = QEMU_OPT_NUMBER,
149 .name = "mux",
150 .type = QEMU_OPT_BOOL,
152 .name = "signal",
153 .type = QEMU_OPT_BOOL,
155 .name = "name",
156 .type = QEMU_OPT_STRING,
158 .name = "debug",
159 .type = QEMU_OPT_NUMBER,
161 { /* end of list */ }
165 QemuOptsList qemu_fsdev_opts = {
166 .name = "fsdev",
167 .implied_opt_name = "fstype",
168 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
169 .desc = {
171 .name = "fstype",
172 .type = QEMU_OPT_STRING,
173 }, {
174 .name = "path",
175 .type = QEMU_OPT_STRING,
176 }, {
177 .name = "security_model",
178 .type = QEMU_OPT_STRING,
180 { /*End of list */ }
184 QemuOptsList qemu_virtfs_opts = {
185 .name = "virtfs",
186 .implied_opt_name = "fstype",
187 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
188 .desc = {
190 .name = "fstype",
191 .type = QEMU_OPT_STRING,
192 }, {
193 .name = "path",
194 .type = QEMU_OPT_STRING,
195 }, {
196 .name = "mount_tag",
197 .type = QEMU_OPT_STRING,
198 }, {
199 .name = "security_model",
200 .type = QEMU_OPT_STRING,
203 { /*End of list */ }
207 static QemuOptsList qemu_device_opts = {
208 .name = "device",
209 .implied_opt_name = "driver",
210 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
211 .desc = {
213 * no elements => accept any
214 * sanity checking will happen later
215 * when setting device properties
217 { /* end of list */ }
221 static QemuOptsList qemu_netdev_opts = {
222 .name = "netdev",
223 .implied_opt_name = "type",
224 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
225 .desc = {
227 * no elements => accept any params
228 * validation will happen later
230 { /* end of list */ }
234 static QemuOptsList qemu_net_opts = {
235 .name = "net",
236 .implied_opt_name = "type",
237 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
238 .desc = {
240 * no elements => accept any params
241 * validation will happen later
243 { /* end of list */ }
247 static QemuOptsList qemu_rtc_opts = {
248 .name = "rtc",
249 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
250 .desc = {
252 .name = "base",
253 .type = QEMU_OPT_STRING,
255 .name = "clock",
256 .type = QEMU_OPT_STRING,
258 .name = "driftfix",
259 .type = QEMU_OPT_STRING,
261 { /* end of list */ }
265 static QemuOptsList qemu_global_opts = {
266 .name = "global",
267 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
268 .desc = {
270 .name = "driver",
271 .type = QEMU_OPT_STRING,
273 .name = "property",
274 .type = QEMU_OPT_STRING,
276 .name = "value",
277 .type = QEMU_OPT_STRING,
279 { /* end of list */ }
283 static QemuOptsList qemu_mon_opts = {
284 .name = "mon",
285 .implied_opt_name = "chardev",
286 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
287 .desc = {
289 .name = "mode",
290 .type = QEMU_OPT_STRING,
292 .name = "chardev",
293 .type = QEMU_OPT_STRING,
295 .name = "default",
296 .type = QEMU_OPT_BOOL,
298 .name = "pretty",
299 .type = QEMU_OPT_BOOL,
301 { /* end of list */ }
305 #ifdef CONFIG_SIMPLE_TRACE
306 static QemuOptsList qemu_trace_opts = {
307 .name = "trace",
308 .implied_opt_name = "trace",
309 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
310 .desc = {
312 .name = "file",
313 .type = QEMU_OPT_STRING,
315 { /* end of list */ }
318 #endif
320 static QemuOptsList qemu_cpudef_opts = {
321 .name = "cpudef",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
323 .desc = {
325 .name = "name",
326 .type = QEMU_OPT_STRING,
328 .name = "level",
329 .type = QEMU_OPT_NUMBER,
331 .name = "vendor",
332 .type = QEMU_OPT_STRING,
334 .name = "family",
335 .type = QEMU_OPT_NUMBER,
337 .name = "model",
338 .type = QEMU_OPT_NUMBER,
340 .name = "stepping",
341 .type = QEMU_OPT_NUMBER,
343 .name = "feature_edx", /* cpuid 0000_0001.edx */
344 .type = QEMU_OPT_STRING,
346 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
347 .type = QEMU_OPT_STRING,
349 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
350 .type = QEMU_OPT_STRING,
352 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
353 .type = QEMU_OPT_STRING,
355 .name = "xlevel",
356 .type = QEMU_OPT_NUMBER,
358 .name = "model_id",
359 .type = QEMU_OPT_STRING,
361 .name = "vendor_override",
362 .type = QEMU_OPT_NUMBER,
364 { /* end of list */ }
368 QemuOptsList qemu_spice_opts = {
369 .name = "spice",
370 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
371 .desc = {
373 .name = "port",
374 .type = QEMU_OPT_NUMBER,
376 .name = "tls-port",
377 .type = QEMU_OPT_NUMBER,
379 .name = "addr",
380 .type = QEMU_OPT_STRING,
382 .name = "ipv4",
383 .type = QEMU_OPT_BOOL,
385 .name = "ipv6",
386 .type = QEMU_OPT_BOOL,
388 .name = "password",
389 .type = QEMU_OPT_STRING,
391 .name = "disable-ticketing",
392 .type = QEMU_OPT_BOOL,
394 .name = "disable-copy-paste",
395 .type = QEMU_OPT_BOOL,
397 .name = "sasl",
398 .type = QEMU_OPT_BOOL,
400 .name = "x509-dir",
401 .type = QEMU_OPT_STRING,
403 .name = "x509-key-file",
404 .type = QEMU_OPT_STRING,
406 .name = "x509-key-password",
407 .type = QEMU_OPT_STRING,
409 .name = "x509-cert-file",
410 .type = QEMU_OPT_STRING,
412 .name = "x509-cacert-file",
413 .type = QEMU_OPT_STRING,
415 .name = "x509-dh-key-file",
416 .type = QEMU_OPT_STRING,
418 .name = "tls-ciphers",
419 .type = QEMU_OPT_STRING,
421 .name = "tls-channel",
422 .type = QEMU_OPT_STRING,
424 .name = "plaintext-channel",
425 .type = QEMU_OPT_STRING,
427 .name = "image-compression",
428 .type = QEMU_OPT_STRING,
430 .name = "jpeg-wan-compression",
431 .type = QEMU_OPT_STRING,
433 .name = "zlib-glz-wan-compression",
434 .type = QEMU_OPT_STRING,
436 .name = "streaming-video",
437 .type = QEMU_OPT_STRING,
439 .name = "agent-mouse",
440 .type = QEMU_OPT_BOOL,
442 .name = "playback-compression",
443 .type = QEMU_OPT_BOOL,
445 { /* end of list */ }
449 QemuOptsList qemu_option_rom_opts = {
450 .name = "option-rom",
451 .implied_opt_name = "romfile",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
453 .desc = {
455 .name = "bootindex",
456 .type = QEMU_OPT_NUMBER,
457 }, {
458 .name = "romfile",
459 .type = QEMU_OPT_STRING,
461 { /* end of list */ }
465 static QemuOptsList qemu_machine_opts = {
466 .name = "machine",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
468 .desc = {
470 .name = "accel",
471 .type = QEMU_OPT_STRING,
472 .help = "accelerator list",
474 { /* End of list */ }
478 static QemuOptsList *vm_config_groups[32] = {
479 &qemu_drive_opts,
480 &qemu_chardev_opts,
481 &qemu_device_opts,
482 &qemu_netdev_opts,
483 &qemu_net_opts,
484 &qemu_rtc_opts,
485 &qemu_global_opts,
486 &qemu_mon_opts,
487 &qemu_cpudef_opts,
488 #ifdef CONFIG_SIMPLE_TRACE
489 &qemu_trace_opts,
490 #endif
491 &qemu_option_rom_opts,
492 &qemu_machine_opts,
493 NULL,
496 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
498 int i;
500 for (i = 0; lists[i] != NULL; i++) {
501 if (strcmp(lists[i]->name, group) == 0)
502 break;
504 if (lists[i] == NULL) {
505 error_report("there is no option group \"%s\"", group);
507 return lists[i];
510 QemuOptsList *qemu_find_opts(const char *group)
512 return find_list(vm_config_groups, group);
515 void qemu_add_opts(QemuOptsList *list)
517 int entries, i;
519 entries = ARRAY_SIZE(vm_config_groups);
520 entries--; /* keep list NULL terminated */
521 for (i = 0; i < entries; i++) {
522 if (vm_config_groups[i] == NULL) {
523 vm_config_groups[i] = list;
524 return;
527 fprintf(stderr, "ran out of space in vm_config_groups");
528 abort();
531 int qemu_set_option(const char *str)
533 char group[64], id[64], arg[64];
534 QemuOptsList *list;
535 QemuOpts *opts;
536 int rc, offset;
538 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
539 if (rc < 3 || str[offset] != '=') {
540 error_report("can't parse: \"%s\"", str);
541 return -1;
544 list = qemu_find_opts(group);
545 if (list == NULL) {
546 return -1;
549 opts = qemu_opts_find(list, id);
550 if (!opts) {
551 error_report("there is no %s \"%s\" defined",
552 list->name, id);
553 return -1;
556 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
557 return -1;
559 return 0;
562 int qemu_global_option(const char *str)
564 char driver[64], property[64];
565 QemuOpts *opts;
566 int rc, offset;
568 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
569 if (rc < 2 || str[offset] != '=') {
570 error_report("can't parse: \"%s\"", str);
571 return -1;
574 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
575 qemu_opt_set(opts, "driver", driver);
576 qemu_opt_set(opts, "property", property);
577 qemu_opt_set(opts, "value", str+offset+1);
578 return 0;
581 struct ConfigWriteData {
582 QemuOptsList *list;
583 FILE *fp;
586 static int config_write_opt(const char *name, const char *value, void *opaque)
588 struct ConfigWriteData *data = opaque;
590 fprintf(data->fp, " %s = \"%s\"\n", name, value);
591 return 0;
594 static int config_write_opts(QemuOpts *opts, void *opaque)
596 struct ConfigWriteData *data = opaque;
597 const char *id = qemu_opts_id(opts);
599 if (id) {
600 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
601 } else {
602 fprintf(data->fp, "[%s]\n", data->list->name);
604 qemu_opt_foreach(opts, config_write_opt, data, 0);
605 fprintf(data->fp, "\n");
606 return 0;
609 void qemu_config_write(FILE *fp)
611 struct ConfigWriteData data = { .fp = fp };
612 QemuOptsList **lists = vm_config_groups;
613 int i;
615 fprintf(fp, "# qemu config file\n\n");
616 for (i = 0; lists[i] != NULL; i++) {
617 data.list = lists[i];
618 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
622 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
624 char line[1024], group[64], id[64], arg[64], value[1024];
625 Location loc;
626 QemuOptsList *list = NULL;
627 QemuOpts *opts = NULL;
628 int res = -1, lno = 0;
630 loc_push_none(&loc);
631 while (fgets(line, sizeof(line), fp) != NULL) {
632 loc_set_file(fname, ++lno);
633 if (line[0] == '\n') {
634 /* skip empty lines */
635 continue;
637 if (line[0] == '#') {
638 /* comment */
639 continue;
641 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
642 /* group with id */
643 list = find_list(lists, group);
644 if (list == NULL)
645 goto out;
646 opts = qemu_opts_create(list, id, 1);
647 continue;
649 if (sscanf(line, "[%63[^]]]", group) == 1) {
650 /* group without id */
651 list = find_list(lists, group);
652 if (list == NULL)
653 goto out;
654 opts = qemu_opts_create(list, NULL, 0);
655 continue;
657 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
658 /* arg = value */
659 if (opts == NULL) {
660 error_report("no group defined");
661 goto out;
663 if (qemu_opt_set(opts, arg, value) != 0) {
664 goto out;
666 continue;
668 error_report("parse error");
669 goto out;
671 if (ferror(fp)) {
672 error_report("error reading file");
673 goto out;
675 res = 0;
676 out:
677 loc_pop(&loc);
678 return res;
681 int qemu_read_config_file(const char *filename)
683 FILE *f = fopen(filename, "r");
684 int ret;
686 if (f == NULL) {
687 return -errno;
690 ret = qemu_config_parse(f, vm_config_groups, filename);
691 fclose(f);
693 if (ret == 0) {
694 return 0;
695 } else {
696 return -EINVAL;