Merge remote-tracking branch 'qemu-kvm-tmp/memory/core' into staging
[qemu.git] / qemu-config.c
blob139e07768b72d5484bc05ff31f76e25d5c50a5a8
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, "
59 "directsync, unsafe)",
60 },{
61 .name = "aio",
62 .type = QEMU_OPT_STRING,
63 .help = "host AIO implementation (threads, native)",
64 },{
65 .name = "format",
66 .type = QEMU_OPT_STRING,
67 .help = "disk format (raw, qcow2, ...)",
68 },{
69 .name = "serial",
70 .type = QEMU_OPT_STRING,
71 .help = "disk serial number",
72 },{
73 .name = "rerror",
74 .type = QEMU_OPT_STRING,
75 .help = "read error action",
76 },{
77 .name = "werror",
78 .type = QEMU_OPT_STRING,
79 .help = "write error action",
80 },{
81 .name = "addr",
82 .type = QEMU_OPT_STRING,
83 .help = "pci address (virtio only)",
84 },{
85 .name = "readonly",
86 .type = QEMU_OPT_BOOL,
87 .help = "open drive file as read-only",
89 { /* end of list */ }
93 static QemuOptsList qemu_chardev_opts = {
94 .name = "chardev",
95 .implied_opt_name = "backend",
96 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
97 .desc = {
99 .name = "backend",
100 .type = QEMU_OPT_STRING,
102 .name = "path",
103 .type = QEMU_OPT_STRING,
105 .name = "host",
106 .type = QEMU_OPT_STRING,
108 .name = "port",
109 .type = QEMU_OPT_STRING,
111 .name = "localaddr",
112 .type = QEMU_OPT_STRING,
114 .name = "localport",
115 .type = QEMU_OPT_STRING,
117 .name = "to",
118 .type = QEMU_OPT_NUMBER,
120 .name = "ipv4",
121 .type = QEMU_OPT_BOOL,
123 .name = "ipv6",
124 .type = QEMU_OPT_BOOL,
126 .name = "wait",
127 .type = QEMU_OPT_BOOL,
129 .name = "server",
130 .type = QEMU_OPT_BOOL,
132 .name = "delay",
133 .type = QEMU_OPT_BOOL,
135 .name = "telnet",
136 .type = QEMU_OPT_BOOL,
138 .name = "width",
139 .type = QEMU_OPT_NUMBER,
141 .name = "height",
142 .type = QEMU_OPT_NUMBER,
144 .name = "cols",
145 .type = QEMU_OPT_NUMBER,
147 .name = "rows",
148 .type = QEMU_OPT_NUMBER,
150 .name = "mux",
151 .type = QEMU_OPT_BOOL,
153 .name = "signal",
154 .type = QEMU_OPT_BOOL,
156 .name = "name",
157 .type = QEMU_OPT_STRING,
159 .name = "debug",
160 .type = QEMU_OPT_NUMBER,
162 { /* end of list */ }
166 QemuOptsList qemu_fsdev_opts = {
167 .name = "fsdev",
168 .implied_opt_name = "fstype",
169 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
170 .desc = {
172 .name = "fstype",
173 .type = QEMU_OPT_STRING,
174 }, {
175 .name = "path",
176 .type = QEMU_OPT_STRING,
177 }, {
178 .name = "security_model",
179 .type = QEMU_OPT_STRING,
181 { /*End of list */ }
185 QemuOptsList qemu_virtfs_opts = {
186 .name = "virtfs",
187 .implied_opt_name = "fstype",
188 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
189 .desc = {
191 .name = "fstype",
192 .type = QEMU_OPT_STRING,
193 }, {
194 .name = "path",
195 .type = QEMU_OPT_STRING,
196 }, {
197 .name = "mount_tag",
198 .type = QEMU_OPT_STRING,
199 }, {
200 .name = "security_model",
201 .type = QEMU_OPT_STRING,
204 { /*End of list */ }
208 static QemuOptsList qemu_device_opts = {
209 .name = "device",
210 .implied_opt_name = "driver",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
212 .desc = {
214 * no elements => accept any
215 * sanity checking will happen later
216 * when setting device properties
218 { /* end of list */ }
222 static QemuOptsList qemu_netdev_opts = {
223 .name = "netdev",
224 .implied_opt_name = "type",
225 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
226 .desc = {
228 * no elements => accept any params
229 * validation will happen later
231 { /* end of list */ }
235 static QemuOptsList qemu_net_opts = {
236 .name = "net",
237 .implied_opt_name = "type",
238 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
239 .desc = {
241 * no elements => accept any params
242 * validation will happen later
244 { /* end of list */ }
248 static QemuOptsList qemu_rtc_opts = {
249 .name = "rtc",
250 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
251 .desc = {
253 .name = "base",
254 .type = QEMU_OPT_STRING,
256 .name = "clock",
257 .type = QEMU_OPT_STRING,
259 .name = "driftfix",
260 .type = QEMU_OPT_STRING,
262 { /* end of list */ }
266 static QemuOptsList qemu_global_opts = {
267 .name = "global",
268 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
269 .desc = {
271 .name = "driver",
272 .type = QEMU_OPT_STRING,
274 .name = "property",
275 .type = QEMU_OPT_STRING,
277 .name = "value",
278 .type = QEMU_OPT_STRING,
280 { /* end of list */ }
284 static QemuOptsList qemu_mon_opts = {
285 .name = "mon",
286 .implied_opt_name = "chardev",
287 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
288 .desc = {
290 .name = "mode",
291 .type = QEMU_OPT_STRING,
293 .name = "chardev",
294 .type = QEMU_OPT_STRING,
296 .name = "default",
297 .type = QEMU_OPT_BOOL,
299 .name = "pretty",
300 .type = QEMU_OPT_BOOL,
302 { /* end of list */ }
306 #ifdef CONFIG_SIMPLE_TRACE
307 static QemuOptsList qemu_trace_opts = {
308 .name = "trace",
309 .implied_opt_name = "trace",
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
311 .desc = {
313 .name = "file",
314 .type = QEMU_OPT_STRING,
316 { /* end of list */ }
319 #endif
321 static QemuOptsList qemu_cpudef_opts = {
322 .name = "cpudef",
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
324 .desc = {
326 .name = "name",
327 .type = QEMU_OPT_STRING,
329 .name = "level",
330 .type = QEMU_OPT_NUMBER,
332 .name = "vendor",
333 .type = QEMU_OPT_STRING,
335 .name = "family",
336 .type = QEMU_OPT_NUMBER,
338 .name = "model",
339 .type = QEMU_OPT_NUMBER,
341 .name = "stepping",
342 .type = QEMU_OPT_NUMBER,
344 .name = "feature_edx", /* cpuid 0000_0001.edx */
345 .type = QEMU_OPT_STRING,
347 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
348 .type = QEMU_OPT_STRING,
350 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
351 .type = QEMU_OPT_STRING,
353 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
354 .type = QEMU_OPT_STRING,
356 .name = "xlevel",
357 .type = QEMU_OPT_NUMBER,
359 .name = "model_id",
360 .type = QEMU_OPT_STRING,
362 .name = "vendor_override",
363 .type = QEMU_OPT_NUMBER,
365 { /* end of list */ }
369 QemuOptsList qemu_spice_opts = {
370 .name = "spice",
371 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
372 .desc = {
374 .name = "port",
375 .type = QEMU_OPT_NUMBER,
377 .name = "tls-port",
378 .type = QEMU_OPT_NUMBER,
380 .name = "addr",
381 .type = QEMU_OPT_STRING,
383 .name = "ipv4",
384 .type = QEMU_OPT_BOOL,
386 .name = "ipv6",
387 .type = QEMU_OPT_BOOL,
389 .name = "password",
390 .type = QEMU_OPT_STRING,
392 .name = "disable-ticketing",
393 .type = QEMU_OPT_BOOL,
395 .name = "disable-copy-paste",
396 .type = QEMU_OPT_BOOL,
398 .name = "sasl",
399 .type = QEMU_OPT_BOOL,
401 .name = "x509-dir",
402 .type = QEMU_OPT_STRING,
404 .name = "x509-key-file",
405 .type = QEMU_OPT_STRING,
407 .name = "x509-key-password",
408 .type = QEMU_OPT_STRING,
410 .name = "x509-cert-file",
411 .type = QEMU_OPT_STRING,
413 .name = "x509-cacert-file",
414 .type = QEMU_OPT_STRING,
416 .name = "x509-dh-key-file",
417 .type = QEMU_OPT_STRING,
419 .name = "tls-ciphers",
420 .type = QEMU_OPT_STRING,
422 .name = "tls-channel",
423 .type = QEMU_OPT_STRING,
425 .name = "plaintext-channel",
426 .type = QEMU_OPT_STRING,
428 .name = "image-compression",
429 .type = QEMU_OPT_STRING,
431 .name = "jpeg-wan-compression",
432 .type = QEMU_OPT_STRING,
434 .name = "zlib-glz-wan-compression",
435 .type = QEMU_OPT_STRING,
437 .name = "streaming-video",
438 .type = QEMU_OPT_STRING,
440 .name = "agent-mouse",
441 .type = QEMU_OPT_BOOL,
443 .name = "playback-compression",
444 .type = QEMU_OPT_BOOL,
446 { /* end of list */ }
450 QemuOptsList qemu_option_rom_opts = {
451 .name = "option-rom",
452 .implied_opt_name = "romfile",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
454 .desc = {
456 .name = "bootindex",
457 .type = QEMU_OPT_NUMBER,
458 }, {
459 .name = "romfile",
460 .type = QEMU_OPT_STRING,
462 { /* end of list */ }
466 static QemuOptsList qemu_machine_opts = {
467 .name = "machine",
468 .implied_opt_name = "type",
469 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
470 .desc = {
472 .name = "type",
473 .type = QEMU_OPT_STRING,
474 .help = "emulated machine"
475 }, {
476 .name = "accel",
477 .type = QEMU_OPT_STRING,
478 .help = "accelerator list",
480 { /* End of list */ }
484 QemuOptsList qemu_boot_opts = {
485 .name = "boot-opts",
486 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
487 .desc = {
488 /* the three names below are not used now */
490 .name = "order",
491 .type = QEMU_OPT_STRING,
492 }, {
493 .name = "once",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "menu",
497 .type = QEMU_OPT_STRING,
498 /* following are really used */
499 }, {
500 .name = "splash",
501 .type = QEMU_OPT_STRING,
502 }, {
503 .name = "splash-time",
504 .type = QEMU_OPT_STRING,
506 { /*End of list */ }
510 static QemuOptsList *vm_config_groups[32] = {
511 &qemu_drive_opts,
512 &qemu_chardev_opts,
513 &qemu_device_opts,
514 &qemu_netdev_opts,
515 &qemu_net_opts,
516 &qemu_rtc_opts,
517 &qemu_global_opts,
518 &qemu_mon_opts,
519 &qemu_cpudef_opts,
520 #ifdef CONFIG_SIMPLE_TRACE
521 &qemu_trace_opts,
522 #endif
523 &qemu_option_rom_opts,
524 &qemu_machine_opts,
525 &qemu_boot_opts,
526 NULL,
529 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
531 int i;
533 for (i = 0; lists[i] != NULL; i++) {
534 if (strcmp(lists[i]->name, group) == 0)
535 break;
537 if (lists[i] == NULL) {
538 error_report("there is no option group \"%s\"", group);
540 return lists[i];
543 QemuOptsList *qemu_find_opts(const char *group)
545 return find_list(vm_config_groups, group);
548 void qemu_add_opts(QemuOptsList *list)
550 int entries, i;
552 entries = ARRAY_SIZE(vm_config_groups);
553 entries--; /* keep list NULL terminated */
554 for (i = 0; i < entries; i++) {
555 if (vm_config_groups[i] == NULL) {
556 vm_config_groups[i] = list;
557 return;
560 fprintf(stderr, "ran out of space in vm_config_groups");
561 abort();
564 int qemu_set_option(const char *str)
566 char group[64], id[64], arg[64];
567 QemuOptsList *list;
568 QemuOpts *opts;
569 int rc, offset;
571 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
572 if (rc < 3 || str[offset] != '=') {
573 error_report("can't parse: \"%s\"", str);
574 return -1;
577 list = qemu_find_opts(group);
578 if (list == NULL) {
579 return -1;
582 opts = qemu_opts_find(list, id);
583 if (!opts) {
584 error_report("there is no %s \"%s\" defined",
585 list->name, id);
586 return -1;
589 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
590 return -1;
592 return 0;
595 int qemu_global_option(const char *str)
597 char driver[64], property[64];
598 QemuOpts *opts;
599 int rc, offset;
601 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
602 if (rc < 2 || str[offset] != '=') {
603 error_report("can't parse: \"%s\"", str);
604 return -1;
607 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
608 qemu_opt_set(opts, "driver", driver);
609 qemu_opt_set(opts, "property", property);
610 qemu_opt_set(opts, "value", str+offset+1);
611 return 0;
614 struct ConfigWriteData {
615 QemuOptsList *list;
616 FILE *fp;
619 static int config_write_opt(const char *name, const char *value, void *opaque)
621 struct ConfigWriteData *data = opaque;
623 fprintf(data->fp, " %s = \"%s\"\n", name, value);
624 return 0;
627 static int config_write_opts(QemuOpts *opts, void *opaque)
629 struct ConfigWriteData *data = opaque;
630 const char *id = qemu_opts_id(opts);
632 if (id) {
633 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
634 } else {
635 fprintf(data->fp, "[%s]\n", data->list->name);
637 qemu_opt_foreach(opts, config_write_opt, data, 0);
638 fprintf(data->fp, "\n");
639 return 0;
642 void qemu_config_write(FILE *fp)
644 struct ConfigWriteData data = { .fp = fp };
645 QemuOptsList **lists = vm_config_groups;
646 int i;
648 fprintf(fp, "# qemu config file\n\n");
649 for (i = 0; lists[i] != NULL; i++) {
650 data.list = lists[i];
651 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
655 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
657 char line[1024], group[64], id[64], arg[64], value[1024];
658 Location loc;
659 QemuOptsList *list = NULL;
660 QemuOpts *opts = NULL;
661 int res = -1, lno = 0;
663 loc_push_none(&loc);
664 while (fgets(line, sizeof(line), fp) != NULL) {
665 loc_set_file(fname, ++lno);
666 if (line[0] == '\n') {
667 /* skip empty lines */
668 continue;
670 if (line[0] == '#') {
671 /* comment */
672 continue;
674 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
675 /* group with id */
676 list = find_list(lists, group);
677 if (list == NULL)
678 goto out;
679 opts = qemu_opts_create(list, id, 1);
680 continue;
682 if (sscanf(line, "[%63[^]]]", group) == 1) {
683 /* group without id */
684 list = find_list(lists, group);
685 if (list == NULL)
686 goto out;
687 opts = qemu_opts_create(list, NULL, 0);
688 continue;
690 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
691 /* arg = value */
692 if (opts == NULL) {
693 error_report("no group defined");
694 goto out;
696 if (qemu_opt_set(opts, arg, value) != 0) {
697 goto out;
699 continue;
701 error_report("parse error");
702 goto out;
704 if (ferror(fp)) {
705 error_report("error reading file");
706 goto out;
708 res = 0;
709 out:
710 loc_pop(&loc);
711 return res;
714 int qemu_read_config_file(const char *filename)
716 FILE *f = fopen(filename, "r");
717 int ret;
719 if (f == NULL) {
720 return -errno;
723 ret = qemu_config_parse(f, vm_config_groups, filename);
724 fclose(f);
726 if (ret == 0) {
727 return 0;
728 } else {
729 return -EINVAL;