qapi: add qapi.py helper libraries
[qemu/aliguori.git] / qemu-config.c
blobc63741c6b1c2983156815690e6b8520197f42520
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 },{
27 .name = "cyls",
28 .type = QEMU_OPT_NUMBER,
29 .help = "number of cylinders (ide disk geometry)",
30 },{
31 .name = "heads",
32 .type = QEMU_OPT_NUMBER,
33 .help = "number of heads (ide disk geometry)",
34 },{
35 .name = "secs",
36 .type = QEMU_OPT_NUMBER,
37 .help = "number of sectors (ide disk geometry)",
38 },{
39 .name = "trans",
40 .type = QEMU_OPT_STRING,
41 .help = "chs translation (auto, lba. none)",
42 },{
43 .name = "media",
44 .type = QEMU_OPT_STRING,
45 .help = "media type (disk, cdrom)",
46 },{
47 .name = "snapshot",
48 .type = QEMU_OPT_BOOL,
49 },{
50 .name = "file",
51 .type = QEMU_OPT_STRING,
52 .help = "disk image",
53 },{
54 .name = "cache",
55 .type = QEMU_OPT_STRING,
56 .help = "host cache usage (none, writeback, writethrough, unsafe)",
57 },{
58 .name = "aio",
59 .type = QEMU_OPT_STRING,
60 .help = "host AIO implementation (threads, native)",
61 },{
62 .name = "format",
63 .type = QEMU_OPT_STRING,
64 .help = "disk format (raw, qcow2, ...)",
65 },{
66 .name = "serial",
67 .type = QEMU_OPT_STRING,
68 },{
69 .name = "rerror",
70 .type = QEMU_OPT_STRING,
71 },{
72 .name = "werror",
73 .type = QEMU_OPT_STRING,
74 },{
75 .name = "addr",
76 .type = QEMU_OPT_STRING,
77 .help = "pci address (virtio only)",
78 },{
79 .name = "readonly",
80 .type = QEMU_OPT_BOOL,
82 { /* end of list */ }
86 static QemuOptsList qemu_chardev_opts = {
87 .name = "chardev",
88 .implied_opt_name = "backend",
89 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
90 .desc = {
92 .name = "backend",
93 .type = QEMU_OPT_STRING,
94 },{
95 .name = "path",
96 .type = QEMU_OPT_STRING,
97 },{
98 .name = "host",
99 .type = QEMU_OPT_STRING,
101 .name = "port",
102 .type = QEMU_OPT_STRING,
104 .name = "localaddr",
105 .type = QEMU_OPT_STRING,
107 .name = "localport",
108 .type = QEMU_OPT_STRING,
110 .name = "to",
111 .type = QEMU_OPT_NUMBER,
113 .name = "ipv4",
114 .type = QEMU_OPT_BOOL,
116 .name = "ipv6",
117 .type = QEMU_OPT_BOOL,
119 .name = "wait",
120 .type = QEMU_OPT_BOOL,
122 .name = "server",
123 .type = QEMU_OPT_BOOL,
125 .name = "delay",
126 .type = QEMU_OPT_BOOL,
128 .name = "telnet",
129 .type = QEMU_OPT_BOOL,
131 .name = "width",
132 .type = QEMU_OPT_NUMBER,
134 .name = "height",
135 .type = QEMU_OPT_NUMBER,
137 .name = "cols",
138 .type = QEMU_OPT_NUMBER,
140 .name = "rows",
141 .type = QEMU_OPT_NUMBER,
143 .name = "mux",
144 .type = QEMU_OPT_BOOL,
146 .name = "signal",
147 .type = QEMU_OPT_BOOL,
149 .name = "name",
150 .type = QEMU_OPT_STRING,
152 .name = "debug",
153 .type = QEMU_OPT_NUMBER,
155 { /* end of list */ }
159 QemuOptsList qemu_fsdev_opts = {
160 .name = "fsdev",
161 .implied_opt_name = "fstype",
162 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
163 .desc = {
165 .name = "fstype",
166 .type = QEMU_OPT_STRING,
167 }, {
168 .name = "path",
169 .type = QEMU_OPT_STRING,
170 }, {
171 .name = "security_model",
172 .type = QEMU_OPT_STRING,
174 { /*End of list */ }
178 QemuOptsList qemu_virtfs_opts = {
179 .name = "virtfs",
180 .implied_opt_name = "fstype",
181 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
182 .desc = {
184 .name = "fstype",
185 .type = QEMU_OPT_STRING,
186 }, {
187 .name = "path",
188 .type = QEMU_OPT_STRING,
189 }, {
190 .name = "mount_tag",
191 .type = QEMU_OPT_STRING,
192 }, {
193 .name = "security_model",
194 .type = QEMU_OPT_STRING,
197 { /*End of list */ }
201 static QemuOptsList qemu_device_opts = {
202 .name = "device",
203 .implied_opt_name = "driver",
204 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
205 .desc = {
207 * no elements => accept any
208 * sanity checking will happen later
209 * when setting device properties
211 { /* end of list */ }
215 static QemuOptsList qemu_netdev_opts = {
216 .name = "netdev",
217 .implied_opt_name = "type",
218 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
219 .desc = {
221 * no elements => accept any params
222 * validation will happen later
224 { /* end of list */ }
228 static QemuOptsList qemu_net_opts = {
229 .name = "net",
230 .implied_opt_name = "type",
231 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
232 .desc = {
234 * no elements => accept any params
235 * validation will happen later
237 { /* end of list */ }
241 static QemuOptsList qemu_rtc_opts = {
242 .name = "rtc",
243 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
244 .desc = {
246 .name = "base",
247 .type = QEMU_OPT_STRING,
249 .name = "clock",
250 .type = QEMU_OPT_STRING,
252 .name = "driftfix",
253 .type = QEMU_OPT_STRING,
255 { /* end of list */ }
259 static QemuOptsList qemu_global_opts = {
260 .name = "global",
261 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
262 .desc = {
264 .name = "driver",
265 .type = QEMU_OPT_STRING,
267 .name = "property",
268 .type = QEMU_OPT_STRING,
270 .name = "value",
271 .type = QEMU_OPT_STRING,
273 { /* end of list */ }
277 static QemuOptsList qemu_mon_opts = {
278 .name = "mon",
279 .implied_opt_name = "chardev",
280 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
281 .desc = {
283 .name = "mode",
284 .type = QEMU_OPT_STRING,
286 .name = "chardev",
287 .type = QEMU_OPT_STRING,
289 .name = "default",
290 .type = QEMU_OPT_BOOL,
292 .name = "pretty",
293 .type = QEMU_OPT_BOOL,
295 { /* end of list */ }
299 #ifdef CONFIG_SIMPLE_TRACE
300 static QemuOptsList qemu_trace_opts = {
301 .name = "trace",
302 .implied_opt_name = "trace",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
304 .desc = {
306 .name = "file",
307 .type = QEMU_OPT_STRING,
309 { /* end of list */ }
312 #endif
314 static QemuOptsList qemu_cpudef_opts = {
315 .name = "cpudef",
316 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
317 .desc = {
319 .name = "name",
320 .type = QEMU_OPT_STRING,
322 .name = "level",
323 .type = QEMU_OPT_NUMBER,
325 .name = "vendor",
326 .type = QEMU_OPT_STRING,
328 .name = "family",
329 .type = QEMU_OPT_NUMBER,
331 .name = "model",
332 .type = QEMU_OPT_NUMBER,
334 .name = "stepping",
335 .type = QEMU_OPT_NUMBER,
337 .name = "feature_edx", /* cpuid 0000_0001.edx */
338 .type = QEMU_OPT_STRING,
340 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
341 .type = QEMU_OPT_STRING,
343 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
344 .type = QEMU_OPT_STRING,
346 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
347 .type = QEMU_OPT_STRING,
349 .name = "xlevel",
350 .type = QEMU_OPT_NUMBER,
352 .name = "model_id",
353 .type = QEMU_OPT_STRING,
355 .name = "vendor_override",
356 .type = QEMU_OPT_NUMBER,
358 { /* end of list */ }
362 QemuOptsList qemu_spice_opts = {
363 .name = "spice",
364 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
365 .desc = {
367 .name = "port",
368 .type = QEMU_OPT_NUMBER,
370 .name = "tls-port",
371 .type = QEMU_OPT_NUMBER,
373 .name = "addr",
374 .type = QEMU_OPT_STRING,
376 .name = "ipv4",
377 .type = QEMU_OPT_BOOL,
379 .name = "ipv6",
380 .type = QEMU_OPT_BOOL,
382 .name = "password",
383 .type = QEMU_OPT_STRING,
385 .name = "disable-ticketing",
386 .type = QEMU_OPT_BOOL,
388 .name = "disable-copy-paste",
389 .type = QEMU_OPT_BOOL,
391 .name = "sasl",
392 .type = QEMU_OPT_BOOL,
394 .name = "x509-dir",
395 .type = QEMU_OPT_STRING,
397 .name = "x509-key-file",
398 .type = QEMU_OPT_STRING,
400 .name = "x509-key-password",
401 .type = QEMU_OPT_STRING,
403 .name = "x509-cert-file",
404 .type = QEMU_OPT_STRING,
406 .name = "x509-cacert-file",
407 .type = QEMU_OPT_STRING,
409 .name = "x509-dh-key-file",
410 .type = QEMU_OPT_STRING,
412 .name = "tls-ciphers",
413 .type = QEMU_OPT_STRING,
415 .name = "tls-channel",
416 .type = QEMU_OPT_STRING,
418 .name = "plaintext-channel",
419 .type = QEMU_OPT_STRING,
421 .name = "image-compression",
422 .type = QEMU_OPT_STRING,
424 .name = "jpeg-wan-compression",
425 .type = QEMU_OPT_STRING,
427 .name = "zlib-glz-wan-compression",
428 .type = QEMU_OPT_STRING,
430 .name = "streaming-video",
431 .type = QEMU_OPT_STRING,
433 .name = "agent-mouse",
434 .type = QEMU_OPT_BOOL,
436 .name = "playback-compression",
437 .type = QEMU_OPT_BOOL,
439 { /* end of list */ }
443 QemuOptsList qemu_option_rom_opts = {
444 .name = "option-rom",
445 .implied_opt_name = "romfile",
446 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
447 .desc = {
449 .name = "bootindex",
450 .type = QEMU_OPT_NUMBER,
451 }, {
452 .name = "romfile",
453 .type = QEMU_OPT_STRING,
455 { /* end of list */ }
459 static QemuOptsList qemu_machine_opts = {
460 .name = "machine",
461 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
462 .desc = {
464 .name = "accel",
465 .type = QEMU_OPT_STRING,
466 .help = "accelerator list",
468 { /* End of list */ }
472 static QemuOptsList *vm_config_groups[32] = {
473 &qemu_drive_opts,
474 &qemu_chardev_opts,
475 &qemu_device_opts,
476 &qemu_netdev_opts,
477 &qemu_net_opts,
478 &qemu_rtc_opts,
479 &qemu_global_opts,
480 &qemu_mon_opts,
481 &qemu_cpudef_opts,
482 #ifdef CONFIG_SIMPLE_TRACE
483 &qemu_trace_opts,
484 #endif
485 &qemu_option_rom_opts,
486 &qemu_machine_opts,
487 NULL,
490 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
492 int i;
494 for (i = 0; lists[i] != NULL; i++) {
495 if (strcmp(lists[i]->name, group) == 0)
496 break;
498 if (lists[i] == NULL) {
499 error_report("there is no option group \"%s\"", group);
501 return lists[i];
504 QemuOptsList *qemu_find_opts(const char *group)
506 return find_list(vm_config_groups, group);
509 void qemu_add_opts(QemuOptsList *list)
511 int entries, i;
513 entries = ARRAY_SIZE(vm_config_groups);
514 entries--; /* keep list NULL terminated */
515 for (i = 0; i < entries; i++) {
516 if (vm_config_groups[i] == NULL) {
517 vm_config_groups[i] = list;
518 return;
521 fprintf(stderr, "ran out of space in vm_config_groups");
522 abort();
525 int qemu_set_option(const char *str)
527 char group[64], id[64], arg[64];
528 QemuOptsList *list;
529 QemuOpts *opts;
530 int rc, offset;
532 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
533 if (rc < 3 || str[offset] != '=') {
534 error_report("can't parse: \"%s\"", str);
535 return -1;
538 list = qemu_find_opts(group);
539 if (list == NULL) {
540 return -1;
543 opts = qemu_opts_find(list, id);
544 if (!opts) {
545 error_report("there is no %s \"%s\" defined",
546 list->name, id);
547 return -1;
550 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
551 return -1;
553 return 0;
556 int qemu_global_option(const char *str)
558 char driver[64], property[64];
559 QemuOpts *opts;
560 int rc, offset;
562 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
563 if (rc < 2 || str[offset] != '=') {
564 error_report("can't parse: \"%s\"", str);
565 return -1;
568 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
569 qemu_opt_set(opts, "driver", driver);
570 qemu_opt_set(opts, "property", property);
571 qemu_opt_set(opts, "value", str+offset+1);
572 return 0;
575 struct ConfigWriteData {
576 QemuOptsList *list;
577 FILE *fp;
580 static int config_write_opt(const char *name, const char *value, void *opaque)
582 struct ConfigWriteData *data = opaque;
584 fprintf(data->fp, " %s = \"%s\"\n", name, value);
585 return 0;
588 static int config_write_opts(QemuOpts *opts, void *opaque)
590 struct ConfigWriteData *data = opaque;
591 const char *id = qemu_opts_id(opts);
593 if (id) {
594 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
595 } else {
596 fprintf(data->fp, "[%s]\n", data->list->name);
598 qemu_opt_foreach(opts, config_write_opt, data, 0);
599 fprintf(data->fp, "\n");
600 return 0;
603 void qemu_config_write(FILE *fp)
605 struct ConfigWriteData data = { .fp = fp };
606 QemuOptsList **lists = vm_config_groups;
607 int i;
609 fprintf(fp, "# qemu config file\n\n");
610 for (i = 0; lists[i] != NULL; i++) {
611 data.list = lists[i];
612 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
616 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
618 char line[1024], group[64], id[64], arg[64], value[1024];
619 Location loc;
620 QemuOptsList *list = NULL;
621 QemuOpts *opts = NULL;
622 int res = -1, lno = 0;
624 loc_push_none(&loc);
625 while (fgets(line, sizeof(line), fp) != NULL) {
626 loc_set_file(fname, ++lno);
627 if (line[0] == '\n') {
628 /* skip empty lines */
629 continue;
631 if (line[0] == '#') {
632 /* comment */
633 continue;
635 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
636 /* group with id */
637 list = find_list(lists, group);
638 if (list == NULL)
639 goto out;
640 opts = qemu_opts_create(list, id, 1);
641 continue;
643 if (sscanf(line, "[%63[^]]]", group) == 1) {
644 /* group without id */
645 list = find_list(lists, group);
646 if (list == NULL)
647 goto out;
648 opts = qemu_opts_create(list, NULL, 0);
649 continue;
651 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
652 /* arg = value */
653 if (opts == NULL) {
654 error_report("no group defined");
655 goto out;
657 if (qemu_opt_set(opts, arg, value) != 0) {
658 goto out;
660 continue;
662 error_report("parse error");
663 goto out;
665 if (ferror(fp)) {
666 error_report("error reading file");
667 goto out;
669 res = 0;
670 out:
671 loc_pop(&loc);
672 return res;
675 int qemu_read_config_file(const char *filename)
677 FILE *f = fopen(filename, "r");
678 int ret;
680 if (f == NULL) {
681 return -errno;
684 ret = qemu_config_parse(f, vm_config_groups, filename);
685 fclose(f);
687 if (ret == 0) {
688 return 0;
689 } else {
690 return -EINVAL;