1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
8 static QemuOptsList qemu_drive_opts
= {
10 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
14 .type
= QEMU_OPT_NUMBER
,
18 .type
= QEMU_OPT_NUMBER
,
19 .help
= "unit number (i.e. lun for scsi)",
22 .type
= QEMU_OPT_STRING
,
23 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
26 .type
= QEMU_OPT_NUMBER
,
27 .help
= "index number",
30 .type
= QEMU_OPT_NUMBER
,
31 .help
= "number of cylinders (ide disk geometry)",
34 .type
= QEMU_OPT_NUMBER
,
35 .help
= "number of heads (ide disk geometry)",
38 .type
= QEMU_OPT_NUMBER
,
39 .help
= "number of sectors (ide disk geometry)",
42 .type
= QEMU_OPT_STRING
,
43 .help
= "chs translation (auto, lba. none)",
46 .type
= QEMU_OPT_STRING
,
47 .help
= "media type (disk, cdrom)",
50 .type
= QEMU_OPT_BOOL
,
51 .help
= "enable/disable snapshot mode",
54 .type
= QEMU_OPT_STRING
,
58 .type
= QEMU_OPT_STRING
,
59 .help
= "host cache usage (none, writeback, writethrough, "
60 "directsync, unsafe)",
63 .type
= QEMU_OPT_STRING
,
64 .help
= "host AIO implementation (threads, native)",
67 .type
= QEMU_OPT_STRING
,
68 .help
= "disk format (raw, qcow2, ...)",
71 .type
= QEMU_OPT_STRING
,
72 .help
= "disk serial number",
75 .type
= QEMU_OPT_STRING
,
76 .help
= "read error action",
79 .type
= QEMU_OPT_STRING
,
80 .help
= "write error action",
83 .type
= QEMU_OPT_STRING
,
84 .help
= "pci address (virtio only)",
87 .type
= QEMU_OPT_BOOL
,
88 .help
= "open drive file as read-only",
91 .type
= QEMU_OPT_NUMBER
,
92 .help
= "limit total I/O operations per second",
95 .type
= QEMU_OPT_NUMBER
,
96 .help
= "limit read operations per second",
99 .type
= QEMU_OPT_NUMBER
,
100 .help
= "limit write operations per second",
103 .type
= QEMU_OPT_NUMBER
,
104 .help
= "limit total bytes per second",
107 .type
= QEMU_OPT_NUMBER
,
108 .help
= "limit read bytes per second",
111 .type
= QEMU_OPT_NUMBER
,
112 .help
= "limit write bytes per second",
114 .name
= "copy-on-read",
115 .type
= QEMU_OPT_BOOL
,
116 .help
= "copy read data from backing file into image file",
118 { /* end of list */ }
122 static QemuOptsList qemu_iscsi_opts
= {
124 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
128 .type
= QEMU_OPT_STRING
,
129 .help
= "username for CHAP authentication to target",
132 .type
= QEMU_OPT_STRING
,
133 .help
= "password for CHAP authentication to target",
135 .name
= "header-digest",
136 .type
= QEMU_OPT_STRING
,
137 .help
= "HeaderDigest setting. "
138 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
140 .name
= "initiator-name",
141 .type
= QEMU_OPT_STRING
,
142 .help
= "Initiator iqn name to use when connecting",
144 { /* end of list */ }
148 static QemuOptsList qemu_chardev_opts
= {
150 .implied_opt_name
= "backend",
151 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
155 .type
= QEMU_OPT_STRING
,
158 .type
= QEMU_OPT_STRING
,
161 .type
= QEMU_OPT_STRING
,
164 .type
= QEMU_OPT_STRING
,
167 .type
= QEMU_OPT_STRING
,
170 .type
= QEMU_OPT_STRING
,
173 .type
= QEMU_OPT_NUMBER
,
176 .type
= QEMU_OPT_BOOL
,
179 .type
= QEMU_OPT_BOOL
,
182 .type
= QEMU_OPT_BOOL
,
185 .type
= QEMU_OPT_BOOL
,
188 .type
= QEMU_OPT_BOOL
,
191 .type
= QEMU_OPT_BOOL
,
194 .type
= QEMU_OPT_NUMBER
,
197 .type
= QEMU_OPT_NUMBER
,
200 .type
= QEMU_OPT_NUMBER
,
203 .type
= QEMU_OPT_NUMBER
,
206 .type
= QEMU_OPT_BOOL
,
209 .type
= QEMU_OPT_BOOL
,
212 .type
= QEMU_OPT_STRING
,
215 .type
= QEMU_OPT_NUMBER
,
217 { /* end of list */ }
221 QemuOptsList qemu_fsdev_opts
= {
223 .implied_opt_name
= "fsdriver",
224 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
228 .type
= QEMU_OPT_STRING
,
231 .type
= QEMU_OPT_STRING
,
233 .name
= "security_model",
234 .type
= QEMU_OPT_STRING
,
237 .type
= QEMU_OPT_STRING
,
240 .type
= QEMU_OPT_BOOL
,
244 .type
= QEMU_OPT_STRING
,
247 .type
= QEMU_OPT_NUMBER
,
254 QemuOptsList qemu_virtfs_opts
= {
256 .implied_opt_name
= "fsdriver",
257 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
261 .type
= QEMU_OPT_STRING
,
264 .type
= QEMU_OPT_STRING
,
267 .type
= QEMU_OPT_STRING
,
269 .name
= "security_model",
270 .type
= QEMU_OPT_STRING
,
273 .type
= QEMU_OPT_STRING
,
276 .type
= QEMU_OPT_BOOL
,
279 .type
= QEMU_OPT_STRING
,
282 .type
= QEMU_OPT_NUMBER
,
289 static QemuOptsList qemu_device_opts
= {
291 .implied_opt_name
= "driver",
292 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
295 * no elements => accept any
296 * sanity checking will happen later
297 * when setting device properties
299 { /* end of list */ }
303 static QemuOptsList qemu_netdev_opts
= {
305 .implied_opt_name
= "type",
306 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
309 * no elements => accept any params
310 * validation will happen later
312 { /* end of list */ }
316 static QemuOptsList qemu_net_opts
= {
318 .implied_opt_name
= "type",
319 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
322 * no elements => accept any params
323 * validation will happen later
325 { /* end of list */ }
329 static QemuOptsList qemu_rtc_opts
= {
331 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
335 .type
= QEMU_OPT_STRING
,
338 .type
= QEMU_OPT_STRING
,
341 .type
= QEMU_OPT_STRING
,
343 { /* end of list */ }
347 static QemuOptsList qemu_global_opts
= {
349 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
353 .type
= QEMU_OPT_STRING
,
356 .type
= QEMU_OPT_STRING
,
359 .type
= QEMU_OPT_STRING
,
361 { /* end of list */ }
365 QemuOptsList qemu_sandbox_opts
= {
367 .implied_opt_name
= "enable",
368 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
372 .type
= QEMU_OPT_BOOL
,
374 { /* end of list */ }
378 static QemuOptsList qemu_mon_opts
= {
380 .implied_opt_name
= "chardev",
381 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
385 .type
= QEMU_OPT_STRING
,
388 .type
= QEMU_OPT_STRING
,
391 .type
= QEMU_OPT_BOOL
,
394 .type
= QEMU_OPT_BOOL
,
396 { /* end of list */ }
400 static QemuOptsList qemu_trace_opts
= {
402 .implied_opt_name
= "trace",
403 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
407 .type
= QEMU_OPT_STRING
,
410 .type
= QEMU_OPT_STRING
,
412 { /* end of list */ }
416 static QemuOptsList qemu_cpudef_opts
= {
418 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
422 .type
= QEMU_OPT_STRING
,
425 .type
= QEMU_OPT_NUMBER
,
428 .type
= QEMU_OPT_STRING
,
431 .type
= QEMU_OPT_NUMBER
,
434 .type
= QEMU_OPT_NUMBER
,
437 .type
= QEMU_OPT_NUMBER
,
439 .name
= "feature_edx", /* cpuid 0000_0001.edx */
440 .type
= QEMU_OPT_STRING
,
442 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
443 .type
= QEMU_OPT_STRING
,
445 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
446 .type
= QEMU_OPT_STRING
,
448 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
449 .type
= QEMU_OPT_STRING
,
452 .type
= QEMU_OPT_NUMBER
,
455 .type
= QEMU_OPT_STRING
,
457 .name
= "vendor_override",
458 .type
= QEMU_OPT_NUMBER
,
460 { /* end of list */ }
464 QemuOptsList qemu_spice_opts
= {
466 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
470 .type
= QEMU_OPT_NUMBER
,
473 .type
= QEMU_OPT_NUMBER
,
476 .type
= QEMU_OPT_STRING
,
479 .type
= QEMU_OPT_BOOL
,
482 .type
= QEMU_OPT_BOOL
,
485 .type
= QEMU_OPT_STRING
,
487 .name
= "disable-ticketing",
488 .type
= QEMU_OPT_BOOL
,
490 .name
= "disable-copy-paste",
491 .type
= QEMU_OPT_BOOL
,
494 .type
= QEMU_OPT_BOOL
,
497 .type
= QEMU_OPT_STRING
,
499 .name
= "x509-key-file",
500 .type
= QEMU_OPT_STRING
,
502 .name
= "x509-key-password",
503 .type
= QEMU_OPT_STRING
,
505 .name
= "x509-cert-file",
506 .type
= QEMU_OPT_STRING
,
508 .name
= "x509-cacert-file",
509 .type
= QEMU_OPT_STRING
,
511 .name
= "x509-dh-key-file",
512 .type
= QEMU_OPT_STRING
,
514 .name
= "tls-ciphers",
515 .type
= QEMU_OPT_STRING
,
517 .name
= "tls-channel",
518 .type
= QEMU_OPT_STRING
,
520 .name
= "plaintext-channel",
521 .type
= QEMU_OPT_STRING
,
523 .name
= "image-compression",
524 .type
= QEMU_OPT_STRING
,
526 .name
= "jpeg-wan-compression",
527 .type
= QEMU_OPT_STRING
,
529 .name
= "zlib-glz-wan-compression",
530 .type
= QEMU_OPT_STRING
,
532 .name
= "streaming-video",
533 .type
= QEMU_OPT_STRING
,
535 .name
= "agent-mouse",
536 .type
= QEMU_OPT_BOOL
,
538 .name
= "playback-compression",
539 .type
= QEMU_OPT_BOOL
,
541 .name
= "seamless-migration",
542 .type
= QEMU_OPT_BOOL
,
544 { /* end of list */ }
548 QemuOptsList qemu_option_rom_opts
= {
549 .name
= "option-rom",
550 .implied_opt_name
= "romfile",
551 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
555 .type
= QEMU_OPT_NUMBER
,
558 .type
= QEMU_OPT_STRING
,
560 { /* end of list */ }
564 static QemuOptsList qemu_machine_opts
= {
566 .implied_opt_name
= "type",
568 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
572 .type
= QEMU_OPT_STRING
,
573 .help
= "emulated machine"
576 .type
= QEMU_OPT_STRING
,
577 .help
= "accelerator list",
579 .name
= "kernel_irqchip",
580 .type
= QEMU_OPT_BOOL
,
581 .help
= "use KVM in-kernel irqchip",
583 .name
= "kvm_shadow_mem",
584 .type
= QEMU_OPT_SIZE
,
585 .help
= "KVM shadow MMU size",
588 .type
= QEMU_OPT_STRING
,
589 .help
= "Linux kernel image file",
592 .type
= QEMU_OPT_STRING
,
593 .help
= "Linux initial ramdisk file",
596 .type
= QEMU_OPT_STRING
,
597 .help
= "Linux kernel command line",
600 .type
= QEMU_OPT_STRING
,
601 .help
= "Linux kernel device tree file",
604 .type
= QEMU_OPT_STRING
,
605 .help
= "Dump current dtb to a file and quit",
607 .name
= "phandle_start",
608 .type
= QEMU_OPT_STRING
,
609 .help
= "The first phandle ID we may generate dynamically",
611 .name
= "dt_compatible",
612 .type
= QEMU_OPT_STRING
,
613 .help
= "Overrides the \"compatible\" property of the dt root node",
615 .name
= "dump-guest-core",
616 .type
= QEMU_OPT_BOOL
,
617 .help
= "Include guest memory in a core dump",
620 .type
= QEMU_OPT_BOOL
,
621 .help
= "enable/disable memory merge support",
624 .type
= QEMU_OPT_BOOL
,
625 .help
= "Set on/off to enable/disable usb",
627 { /* End of list */ }
631 QemuOptsList qemu_boot_opts
= {
633 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
635 /* the three names below are not used now */
638 .type
= QEMU_OPT_STRING
,
641 .type
= QEMU_OPT_STRING
,
644 .type
= QEMU_OPT_STRING
,
645 /* following are really used */
648 .type
= QEMU_OPT_STRING
,
650 .name
= "splash-time",
651 .type
= QEMU_OPT_STRING
,
653 .name
= "reboot-timeout",
654 .type
= QEMU_OPT_STRING
,
660 static QemuOptsList qemu_add_fd_opts
= {
662 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
666 .type
= QEMU_OPT_NUMBER
,
667 .help
= "file descriptor of which a duplicate is added to fd set",
670 .type
= QEMU_OPT_NUMBER
,
671 .help
= "ID of the fd set to add fd to",
674 .type
= QEMU_OPT_STRING
,
675 .help
= "free-form string used to describe fd",
677 { /* end of list */ }
681 static QemuOptsList
*vm_config_groups
[32] = {
692 &qemu_option_rom_opts
,
701 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
706 for (i
= 0; lists
[i
] != NULL
; i
++) {
707 if (strcmp(lists
[i
]->name
, group
) == 0)
710 if (lists
[i
] == NULL
) {
711 error_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
716 QemuOptsList
*qemu_find_opts(const char *group
)
719 Error
*local_err
= NULL
;
721 ret
= find_list(vm_config_groups
, group
, &local_err
);
722 if (error_is_set(&local_err
)) {
723 error_report("%s\n", error_get_pretty(local_err
));
724 error_free(local_err
);
730 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
732 return find_list(vm_config_groups
, group
, errp
);
735 void qemu_add_opts(QemuOptsList
*list
)
739 entries
= ARRAY_SIZE(vm_config_groups
);
740 entries
--; /* keep list NULL terminated */
741 for (i
= 0; i
< entries
; i
++) {
742 if (vm_config_groups
[i
] == NULL
) {
743 vm_config_groups
[i
] = list
;
747 fprintf(stderr
, "ran out of space in vm_config_groups");
751 int qemu_set_option(const char *str
)
753 char group
[64], id
[64], arg
[64];
758 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
759 if (rc
< 3 || str
[offset
] != '=') {
760 error_report("can't parse: \"%s\"", str
);
764 list
= qemu_find_opts(group
);
769 opts
= qemu_opts_find(list
, id
);
771 error_report("there is no %s \"%s\" defined",
776 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
782 int qemu_global_option(const char *str
)
784 char driver
[64], property
[64];
788 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
789 if (rc
< 2 || str
[offset
] != '=') {
790 error_report("can't parse: \"%s\"", str
);
794 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, NULL
);
795 qemu_opt_set(opts
, "driver", driver
);
796 qemu_opt_set(opts
, "property", property
);
797 qemu_opt_set(opts
, "value", str
+offset
+1);
801 struct ConfigWriteData
{
806 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
808 struct ConfigWriteData
*data
= opaque
;
810 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
814 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
816 struct ConfigWriteData
*data
= opaque
;
817 const char *id
= qemu_opts_id(opts
);
820 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
822 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
824 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
825 fprintf(data
->fp
, "\n");
829 void qemu_config_write(FILE *fp
)
831 struct ConfigWriteData data
= { .fp
= fp
};
832 QemuOptsList
**lists
= vm_config_groups
;
835 fprintf(fp
, "# qemu config file\n\n");
836 for (i
= 0; lists
[i
] != NULL
; i
++) {
837 data
.list
= lists
[i
];
838 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
842 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
844 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
846 QemuOptsList
*list
= NULL
;
847 Error
*local_err
= NULL
;
848 QemuOpts
*opts
= NULL
;
849 int res
= -1, lno
= 0;
852 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
853 loc_set_file(fname
, ++lno
);
854 if (line
[0] == '\n') {
855 /* skip empty lines */
858 if (line
[0] == '#') {
862 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
864 list
= find_list(lists
, group
, &local_err
);
865 if (error_is_set(&local_err
)) {
866 error_report("%s\n", error_get_pretty(local_err
));
867 error_free(local_err
);
870 opts
= qemu_opts_create(list
, id
, 1, NULL
);
873 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
874 /* group without id */
875 list
= find_list(lists
, group
, &local_err
);
876 if (error_is_set(&local_err
)) {
877 error_report("%s\n", error_get_pretty(local_err
));
878 error_free(local_err
);
881 opts
= qemu_opts_create(list
, NULL
, 0, NULL
);
884 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
887 error_report("no group defined");
890 if (qemu_opt_set(opts
, arg
, value
) != 0) {
895 error_report("parse error");
899 error_report("error reading file");
908 int qemu_read_config_file(const char *filename
)
910 FILE *f
= fopen(filename
, "r");
917 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);