1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
6 #include "qapi/error.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",
119 .type
= QEMU_OPT_BOOL
,
120 .help
= "(deprecated, ignored)",
122 { /* end of list */ }
126 static QemuOptsList qemu_iscsi_opts
= {
128 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
132 .type
= QEMU_OPT_STRING
,
133 .help
= "username for CHAP authentication to target",
136 .type
= QEMU_OPT_STRING
,
137 .help
= "password for CHAP authentication to target",
139 .name
= "header-digest",
140 .type
= QEMU_OPT_STRING
,
141 .help
= "HeaderDigest setting. "
142 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
144 .name
= "initiator-name",
145 .type
= QEMU_OPT_STRING
,
146 .help
= "Initiator iqn name to use when connecting",
148 { /* end of list */ }
152 static QemuOptsList qemu_chardev_opts
= {
154 .implied_opt_name
= "backend",
155 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
159 .type
= QEMU_OPT_STRING
,
162 .type
= QEMU_OPT_STRING
,
165 .type
= QEMU_OPT_STRING
,
168 .type
= QEMU_OPT_STRING
,
171 .type
= QEMU_OPT_STRING
,
174 .type
= QEMU_OPT_STRING
,
177 .type
= QEMU_OPT_NUMBER
,
180 .type
= QEMU_OPT_BOOL
,
183 .type
= QEMU_OPT_BOOL
,
186 .type
= QEMU_OPT_BOOL
,
189 .type
= QEMU_OPT_BOOL
,
192 .type
= QEMU_OPT_BOOL
,
195 .type
= QEMU_OPT_BOOL
,
198 .type
= QEMU_OPT_NUMBER
,
201 .type
= QEMU_OPT_NUMBER
,
204 .type
= QEMU_OPT_NUMBER
,
207 .type
= QEMU_OPT_NUMBER
,
210 .type
= QEMU_OPT_BOOL
,
213 .type
= QEMU_OPT_BOOL
,
216 .type
= QEMU_OPT_STRING
,
219 .type
= QEMU_OPT_NUMBER
,
221 { /* end of list */ }
225 QemuOptsList qemu_fsdev_opts
= {
227 .implied_opt_name
= "fsdriver",
228 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
232 .type
= QEMU_OPT_STRING
,
235 .type
= QEMU_OPT_STRING
,
237 .name
= "security_model",
238 .type
= QEMU_OPT_STRING
,
241 .type
= QEMU_OPT_STRING
,
244 .type
= QEMU_OPT_BOOL
,
248 .type
= QEMU_OPT_STRING
,
251 .type
= QEMU_OPT_NUMBER
,
258 QemuOptsList qemu_virtfs_opts
= {
260 .implied_opt_name
= "fsdriver",
261 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
265 .type
= QEMU_OPT_STRING
,
268 .type
= QEMU_OPT_STRING
,
271 .type
= QEMU_OPT_STRING
,
273 .name
= "security_model",
274 .type
= QEMU_OPT_STRING
,
277 .type
= QEMU_OPT_STRING
,
280 .type
= QEMU_OPT_BOOL
,
283 .type
= QEMU_OPT_STRING
,
286 .type
= QEMU_OPT_NUMBER
,
293 static QemuOptsList qemu_device_opts
= {
295 .implied_opt_name
= "driver",
296 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
299 * no elements => accept any
300 * sanity checking will happen later
301 * when setting device properties
303 { /* end of list */ }
307 static QemuOptsList qemu_netdev_opts
= {
309 .implied_opt_name
= "type",
310 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
313 * no elements => accept any params
314 * validation will happen later
316 { /* end of list */ }
320 static QemuOptsList qemu_net_opts
= {
322 .implied_opt_name
= "type",
323 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
326 * no elements => accept any params
327 * validation will happen later
329 { /* end of list */ }
333 static QemuOptsList qemu_rtc_opts
= {
335 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
339 .type
= QEMU_OPT_STRING
,
342 .type
= QEMU_OPT_STRING
,
345 .type
= QEMU_OPT_STRING
,
347 { /* end of list */ }
351 static QemuOptsList qemu_global_opts
= {
353 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
357 .type
= QEMU_OPT_STRING
,
360 .type
= QEMU_OPT_STRING
,
363 .type
= QEMU_OPT_STRING
,
365 { /* end of list */ }
369 QemuOptsList qemu_sandbox_opts
= {
371 .implied_opt_name
= "enable",
372 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
376 .type
= QEMU_OPT_BOOL
,
378 { /* end of list */ }
382 static QemuOptsList qemu_mon_opts
= {
384 .implied_opt_name
= "chardev",
385 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
389 .type
= QEMU_OPT_STRING
,
392 .type
= QEMU_OPT_STRING
,
395 .type
= QEMU_OPT_BOOL
,
398 .type
= QEMU_OPT_BOOL
,
400 { /* end of list */ }
404 static QemuOptsList qemu_trace_opts
= {
406 .implied_opt_name
= "trace",
407 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
411 .type
= QEMU_OPT_STRING
,
414 .type
= QEMU_OPT_STRING
,
416 { /* end of list */ }
420 QemuOptsList qemu_spice_opts
= {
422 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
426 .type
= QEMU_OPT_NUMBER
,
429 .type
= QEMU_OPT_NUMBER
,
432 .type
= QEMU_OPT_STRING
,
435 .type
= QEMU_OPT_BOOL
,
438 .type
= QEMU_OPT_BOOL
,
441 .type
= QEMU_OPT_STRING
,
443 .name
= "disable-ticketing",
444 .type
= QEMU_OPT_BOOL
,
446 .name
= "disable-copy-paste",
447 .type
= QEMU_OPT_BOOL
,
450 .type
= QEMU_OPT_BOOL
,
453 .type
= QEMU_OPT_STRING
,
455 .name
= "x509-key-file",
456 .type
= QEMU_OPT_STRING
,
458 .name
= "x509-key-password",
459 .type
= QEMU_OPT_STRING
,
461 .name
= "x509-cert-file",
462 .type
= QEMU_OPT_STRING
,
464 .name
= "x509-cacert-file",
465 .type
= QEMU_OPT_STRING
,
467 .name
= "x509-dh-key-file",
468 .type
= QEMU_OPT_STRING
,
470 .name
= "tls-ciphers",
471 .type
= QEMU_OPT_STRING
,
473 .name
= "tls-channel",
474 .type
= QEMU_OPT_STRING
,
476 .name
= "plaintext-channel",
477 .type
= QEMU_OPT_STRING
,
479 .name
= "image-compression",
480 .type
= QEMU_OPT_STRING
,
482 .name
= "jpeg-wan-compression",
483 .type
= QEMU_OPT_STRING
,
485 .name
= "zlib-glz-wan-compression",
486 .type
= QEMU_OPT_STRING
,
488 .name
= "streaming-video",
489 .type
= QEMU_OPT_STRING
,
491 .name
= "agent-mouse",
492 .type
= QEMU_OPT_BOOL
,
494 .name
= "playback-compression",
495 .type
= QEMU_OPT_BOOL
,
497 .name
= "seamless-migration",
498 .type
= QEMU_OPT_BOOL
,
500 { /* end of list */ }
504 QemuOptsList qemu_option_rom_opts
= {
505 .name
= "option-rom",
506 .implied_opt_name
= "romfile",
507 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
511 .type
= QEMU_OPT_NUMBER
,
514 .type
= QEMU_OPT_STRING
,
516 { /* end of list */ }
520 static QemuOptsList qemu_machine_opts
= {
522 .implied_opt_name
= "type",
524 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
528 .type
= QEMU_OPT_STRING
,
529 .help
= "emulated machine"
532 .type
= QEMU_OPT_STRING
,
533 .help
= "accelerator list",
535 .name
= "kernel_irqchip",
536 .type
= QEMU_OPT_BOOL
,
537 .help
= "use KVM in-kernel irqchip",
539 .name
= "kvm_shadow_mem",
540 .type
= QEMU_OPT_SIZE
,
541 .help
= "KVM shadow MMU size",
544 .type
= QEMU_OPT_STRING
,
545 .help
= "Linux kernel image file",
548 .type
= QEMU_OPT_STRING
,
549 .help
= "Linux initial ramdisk file",
552 .type
= QEMU_OPT_STRING
,
553 .help
= "Linux kernel command line",
556 .type
= QEMU_OPT_STRING
,
557 .help
= "Linux kernel device tree file",
560 .type
= QEMU_OPT_STRING
,
561 .help
= "Dump current dtb to a file and quit",
563 .name
= "phandle_start",
564 .type
= QEMU_OPT_STRING
,
565 .help
= "The first phandle ID we may generate dynamically",
567 .name
= "dt_compatible",
568 .type
= QEMU_OPT_STRING
,
569 .help
= "Overrides the \"compatible\" property of the dt root node",
571 .name
= "dump-guest-core",
572 .type
= QEMU_OPT_BOOL
,
573 .help
= "Include guest memory in a core dump",
576 .type
= QEMU_OPT_BOOL
,
577 .help
= "enable/disable memory merge support",
580 .type
= QEMU_OPT_BOOL
,
581 .help
= "Set on/off to enable/disable usb",
584 .type
= QEMU_OPT_STRING
,
585 .help
= "Drive backing persistent NVRAM",
587 { /* End of list */ }
591 QemuOptsList qemu_boot_opts
= {
593 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
595 /* the three names below are not used now */
598 .type
= QEMU_OPT_STRING
,
601 .type
= QEMU_OPT_STRING
,
604 .type
= QEMU_OPT_STRING
,
605 /* following are really used */
608 .type
= QEMU_OPT_STRING
,
610 .name
= "splash-time",
611 .type
= QEMU_OPT_STRING
,
613 .name
= "reboot-timeout",
614 .type
= QEMU_OPT_STRING
,
620 static QemuOptsList qemu_add_fd_opts
= {
622 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
626 .type
= QEMU_OPT_NUMBER
,
627 .help
= "file descriptor of which a duplicate is added to fd set",
630 .type
= QEMU_OPT_NUMBER
,
631 .help
= "ID of the fd set to add fd to",
634 .type
= QEMU_OPT_STRING
,
635 .help
= "free-form string used to describe fd",
637 { /* end of list */ }
641 static QemuOptsList qemu_object_opts
= {
643 .implied_opt_name
= "qom-type",
644 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
650 static QemuOptsList
*vm_config_groups
[32] = {
660 &qemu_option_rom_opts
,
670 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
675 for (i
= 0; lists
[i
] != NULL
; i
++) {
676 if (strcmp(lists
[i
]->name
, group
) == 0)
679 if (lists
[i
] == NULL
) {
680 error_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
685 QemuOptsList
*qemu_find_opts(const char *group
)
688 Error
*local_err
= NULL
;
690 ret
= find_list(vm_config_groups
, group
, &local_err
);
691 if (error_is_set(&local_err
)) {
692 error_report("%s\n", error_get_pretty(local_err
));
693 error_free(local_err
);
699 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
701 return find_list(vm_config_groups
, group
, errp
);
704 void qemu_add_opts(QemuOptsList
*list
)
708 entries
= ARRAY_SIZE(vm_config_groups
);
709 entries
--; /* keep list NULL terminated */
710 for (i
= 0; i
< entries
; i
++) {
711 if (vm_config_groups
[i
] == NULL
) {
712 vm_config_groups
[i
] = list
;
716 fprintf(stderr
, "ran out of space in vm_config_groups");
720 int qemu_set_option(const char *str
)
722 char group
[64], id
[64], arg
[64];
727 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
728 if (rc
< 3 || str
[offset
] != '=') {
729 error_report("can't parse: \"%s\"", str
);
733 list
= qemu_find_opts(group
);
738 opts
= qemu_opts_find(list
, id
);
740 error_report("there is no %s \"%s\" defined",
745 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
751 int qemu_global_option(const char *str
)
753 char driver
[64], property
[64];
757 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
758 if (rc
< 2 || str
[offset
] != '=') {
759 error_report("can't parse: \"%s\"", str
);
763 opts
= qemu_opts_create_nofail(&qemu_global_opts
);
764 qemu_opt_set(opts
, "driver", driver
);
765 qemu_opt_set(opts
, "property", property
);
766 qemu_opt_set(opts
, "value", str
+offset
+1);
770 struct ConfigWriteData
{
775 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
777 struct ConfigWriteData
*data
= opaque
;
779 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
783 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
785 struct ConfigWriteData
*data
= opaque
;
786 const char *id
= qemu_opts_id(opts
);
789 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
791 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
793 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
794 fprintf(data
->fp
, "\n");
798 void qemu_config_write(FILE *fp
)
800 struct ConfigWriteData data
= { .fp
= fp
};
801 QemuOptsList
**lists
= vm_config_groups
;
804 fprintf(fp
, "# qemu config file\n\n");
805 for (i
= 0; lists
[i
] != NULL
; i
++) {
806 data
.list
= lists
[i
];
807 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
811 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
813 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
815 QemuOptsList
*list
= NULL
;
816 Error
*local_err
= NULL
;
817 QemuOpts
*opts
= NULL
;
818 int res
= -1, lno
= 0;
821 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
822 loc_set_file(fname
, ++lno
);
823 if (line
[0] == '\n') {
824 /* skip empty lines */
827 if (line
[0] == '#') {
831 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
833 list
= find_list(lists
, group
, &local_err
);
834 if (error_is_set(&local_err
)) {
835 error_report("%s\n", error_get_pretty(local_err
));
836 error_free(local_err
);
839 opts
= qemu_opts_create(list
, id
, 1, NULL
);
842 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
843 /* group without id */
844 list
= find_list(lists
, group
, &local_err
);
845 if (error_is_set(&local_err
)) {
846 error_report("%s\n", error_get_pretty(local_err
));
847 error_free(local_err
);
850 opts
= qemu_opts_create_nofail(list
);
853 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
856 error_report("no group defined");
859 if (qemu_opt_set(opts
, arg
, value
) != 0) {
864 error_report("parse error");
868 error_report("error reading file");
877 int qemu_read_config_file(const char *filename
)
879 FILE *f
= fopen(filename
, "r");
886 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);