1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
7 static QemuOptsList qemu_drive_opts
= {
9 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
13 .type
= QEMU_OPT_NUMBER
,
17 .type
= QEMU_OPT_NUMBER
,
18 .help
= "unit number (i.e. lun for scsi)",
21 .type
= QEMU_OPT_STRING
,
22 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
25 .type
= QEMU_OPT_NUMBER
,
26 .help
= "index number",
29 .type
= QEMU_OPT_NUMBER
,
30 .help
= "number of cylinders (ide disk geometry)",
33 .type
= QEMU_OPT_NUMBER
,
34 .help
= "number of heads (ide disk geometry)",
37 .type
= QEMU_OPT_NUMBER
,
38 .help
= "number of sectors (ide disk geometry)",
41 .type
= QEMU_OPT_STRING
,
42 .help
= "chs translation (auto, lba. none)",
45 .type
= QEMU_OPT_STRING
,
46 .help
= "media type (disk, cdrom)",
49 .type
= QEMU_OPT_BOOL
,
50 .help
= "enable/disable snapshot mode",
53 .type
= QEMU_OPT_STRING
,
57 .type
= QEMU_OPT_STRING
,
58 .help
= "host cache usage (none, writeback, writethrough, "
59 "directsync, unsafe)",
62 .type
= QEMU_OPT_STRING
,
63 .help
= "host AIO implementation (threads, native)",
66 .type
= QEMU_OPT_STRING
,
67 .help
= "disk format (raw, qcow2, ...)",
70 .type
= QEMU_OPT_STRING
,
71 .help
= "disk serial number",
74 .type
= QEMU_OPT_STRING
,
75 .help
= "read error action",
78 .type
= QEMU_OPT_STRING
,
79 .help
= "write error action",
82 .type
= QEMU_OPT_STRING
,
83 .help
= "pci address (virtio only)",
86 .type
= QEMU_OPT_BOOL
,
87 .help
= "open drive file as read-only",
90 .type
= QEMU_OPT_NUMBER
,
91 .help
= "limit total I/O operations per second",
94 .type
= QEMU_OPT_NUMBER
,
95 .help
= "limit read operations per second",
98 .type
= QEMU_OPT_NUMBER
,
99 .help
= "limit write operations per second",
102 .type
= QEMU_OPT_NUMBER
,
103 .help
= "limit total bytes per second",
106 .type
= QEMU_OPT_NUMBER
,
107 .help
= "limit read bytes per second",
110 .type
= QEMU_OPT_NUMBER
,
111 .help
= "limit write bytes per second",
113 .name
= "copy-on-read",
114 .type
= QEMU_OPT_BOOL
,
115 .help
= "copy read data from backing file into image file",
117 { /* end of list */ }
121 static QemuOptsList qemu_iscsi_opts
= {
123 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
127 .type
= QEMU_OPT_STRING
,
128 .help
= "username for CHAP authentication to target",
131 .type
= QEMU_OPT_STRING
,
132 .help
= "password for CHAP authentication to target",
134 .name
= "header-digest",
135 .type
= QEMU_OPT_STRING
,
136 .help
= "HeaderDigest setting. "
137 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
139 .name
= "initiator-name",
140 .type
= QEMU_OPT_STRING
,
141 .help
= "Initiator iqn name to use when connecting",
143 { /* end of list */ }
147 static QemuOptsList qemu_chardev_opts
= {
149 .implied_opt_name
= "backend",
150 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
154 .type
= QEMU_OPT_STRING
,
157 .type
= QEMU_OPT_STRING
,
160 .type
= QEMU_OPT_STRING
,
163 .type
= QEMU_OPT_STRING
,
166 .type
= QEMU_OPT_STRING
,
169 .type
= QEMU_OPT_STRING
,
172 .type
= QEMU_OPT_NUMBER
,
175 .type
= QEMU_OPT_BOOL
,
178 .type
= QEMU_OPT_BOOL
,
181 .type
= QEMU_OPT_BOOL
,
184 .type
= QEMU_OPT_BOOL
,
187 .type
= QEMU_OPT_BOOL
,
190 .type
= QEMU_OPT_BOOL
,
193 .type
= QEMU_OPT_NUMBER
,
196 .type
= QEMU_OPT_NUMBER
,
199 .type
= QEMU_OPT_NUMBER
,
202 .type
= QEMU_OPT_NUMBER
,
205 .type
= QEMU_OPT_BOOL
,
208 .type
= QEMU_OPT_BOOL
,
211 .type
= QEMU_OPT_STRING
,
214 .type
= QEMU_OPT_NUMBER
,
216 { /* end of list */ }
220 QemuOptsList qemu_fsdev_opts
= {
222 .implied_opt_name
= "fsdriver",
223 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
227 .type
= QEMU_OPT_STRING
,
230 .type
= QEMU_OPT_STRING
,
232 .name
= "security_model",
233 .type
= QEMU_OPT_STRING
,
236 .type
= QEMU_OPT_STRING
,
239 .type
= QEMU_OPT_BOOL
,
243 .type
= QEMU_OPT_STRING
,
246 .type
= QEMU_OPT_NUMBER
,
253 QemuOptsList qemu_virtfs_opts
= {
255 .implied_opt_name
= "fsdriver",
256 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
260 .type
= QEMU_OPT_STRING
,
263 .type
= QEMU_OPT_STRING
,
266 .type
= QEMU_OPT_STRING
,
268 .name
= "security_model",
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_STRING
,
275 .type
= QEMU_OPT_BOOL
,
278 .type
= QEMU_OPT_STRING
,
281 .type
= QEMU_OPT_NUMBER
,
288 static QemuOptsList qemu_device_opts
= {
290 .implied_opt_name
= "driver",
291 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
294 * no elements => accept any
295 * sanity checking will happen later
296 * when setting device properties
298 { /* end of list */ }
302 static QemuOptsList qemu_netdev_opts
= {
304 .implied_opt_name
= "type",
305 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
308 * no elements => accept any params
309 * validation will happen later
311 { /* end of list */ }
315 static QemuOptsList qemu_net_opts
= {
317 .implied_opt_name
= "type",
318 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
321 * no elements => accept any params
322 * validation will happen later
324 { /* end of list */ }
328 static QemuOptsList qemu_rtc_opts
= {
330 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
334 .type
= QEMU_OPT_STRING
,
337 .type
= QEMU_OPT_STRING
,
340 .type
= QEMU_OPT_STRING
,
342 { /* end of list */ }
346 static QemuOptsList qemu_global_opts
= {
348 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
352 .type
= QEMU_OPT_STRING
,
355 .type
= QEMU_OPT_STRING
,
358 .type
= QEMU_OPT_STRING
,
360 { /* end of list */ }
364 static QemuOptsList qemu_mon_opts
= {
366 .implied_opt_name
= "chardev",
367 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
371 .type
= QEMU_OPT_STRING
,
374 .type
= QEMU_OPT_STRING
,
377 .type
= QEMU_OPT_BOOL
,
380 .type
= QEMU_OPT_BOOL
,
382 { /* end of list */ }
386 static QemuOptsList qemu_trace_opts
= {
388 .implied_opt_name
= "trace",
389 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
393 .type
= QEMU_OPT_STRING
,
396 .type
= QEMU_OPT_STRING
,
398 { /* end of list */ }
402 static QemuOptsList qemu_cpudef_opts
= {
404 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
408 .type
= QEMU_OPT_STRING
,
411 .type
= QEMU_OPT_NUMBER
,
414 .type
= QEMU_OPT_STRING
,
417 .type
= QEMU_OPT_NUMBER
,
420 .type
= QEMU_OPT_NUMBER
,
423 .type
= QEMU_OPT_NUMBER
,
425 .name
= "feature_edx", /* cpuid 0000_0001.edx */
426 .type
= QEMU_OPT_STRING
,
428 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
429 .type
= QEMU_OPT_STRING
,
431 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
432 .type
= QEMU_OPT_STRING
,
434 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
435 .type
= QEMU_OPT_STRING
,
438 .type
= QEMU_OPT_NUMBER
,
441 .type
= QEMU_OPT_STRING
,
443 .name
= "vendor_override",
444 .type
= QEMU_OPT_NUMBER
,
446 { /* end of list */ }
450 QemuOptsList qemu_spice_opts
= {
452 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
456 .type
= QEMU_OPT_NUMBER
,
459 .type
= QEMU_OPT_NUMBER
,
462 .type
= QEMU_OPT_STRING
,
465 .type
= QEMU_OPT_BOOL
,
468 .type
= QEMU_OPT_BOOL
,
471 .type
= QEMU_OPT_STRING
,
473 .name
= "disable-ticketing",
474 .type
= QEMU_OPT_BOOL
,
476 .name
= "disable-copy-paste",
477 .type
= QEMU_OPT_BOOL
,
480 .type
= QEMU_OPT_BOOL
,
483 .type
= QEMU_OPT_STRING
,
485 .name
= "x509-key-file",
486 .type
= QEMU_OPT_STRING
,
488 .name
= "x509-key-password",
489 .type
= QEMU_OPT_STRING
,
491 .name
= "x509-cert-file",
492 .type
= QEMU_OPT_STRING
,
494 .name
= "x509-cacert-file",
495 .type
= QEMU_OPT_STRING
,
497 .name
= "x509-dh-key-file",
498 .type
= QEMU_OPT_STRING
,
500 .name
= "tls-ciphers",
501 .type
= QEMU_OPT_STRING
,
503 .name
= "tls-channel",
504 .type
= QEMU_OPT_STRING
,
506 .name
= "plaintext-channel",
507 .type
= QEMU_OPT_STRING
,
509 .name
= "image-compression",
510 .type
= QEMU_OPT_STRING
,
512 .name
= "jpeg-wan-compression",
513 .type
= QEMU_OPT_STRING
,
515 .name
= "zlib-glz-wan-compression",
516 .type
= QEMU_OPT_STRING
,
518 .name
= "streaming-video",
519 .type
= QEMU_OPT_STRING
,
521 .name
= "agent-mouse",
522 .type
= QEMU_OPT_BOOL
,
524 .name
= "playback-compression",
525 .type
= QEMU_OPT_BOOL
,
527 { /* end of list */ }
531 QemuOptsList qemu_option_rom_opts
= {
532 .name
= "option-rom",
533 .implied_opt_name
= "romfile",
534 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
538 .type
= QEMU_OPT_NUMBER
,
541 .type
= QEMU_OPT_STRING
,
543 { /* end of list */ }
547 static QemuOptsList qemu_machine_opts
= {
549 .implied_opt_name
= "type",
550 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
554 .type
= QEMU_OPT_STRING
,
555 .help
= "emulated machine"
558 .type
= QEMU_OPT_STRING
,
559 .help
= "accelerator list",
561 .name
= "kernel_irqchip",
562 .type
= QEMU_OPT_BOOL
,
563 .help
= "use KVM in-kernel irqchip",
565 .name
= "kvm_shadow_mem",
566 .type
= QEMU_OPT_SIZE
,
567 .help
= "KVM shadow MMU size",
569 { /* End of list */ }
573 QemuOptsList qemu_boot_opts
= {
575 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
577 /* the three names below are not used now */
580 .type
= QEMU_OPT_STRING
,
583 .type
= QEMU_OPT_STRING
,
586 .type
= QEMU_OPT_STRING
,
587 /* following are really used */
590 .type
= QEMU_OPT_STRING
,
592 .name
= "splash-time",
593 .type
= QEMU_OPT_STRING
,
599 static QemuOptsList
*vm_config_groups
[32] = {
610 &qemu_option_rom_opts
,
617 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
621 for (i
= 0; lists
[i
] != NULL
; i
++) {
622 if (strcmp(lists
[i
]->name
, group
) == 0)
625 if (lists
[i
] == NULL
) {
626 error_report("there is no option group \"%s\"", group
);
631 QemuOptsList
*qemu_find_opts(const char *group
)
633 return find_list(vm_config_groups
, group
);
636 void qemu_add_opts(QemuOptsList
*list
)
640 entries
= ARRAY_SIZE(vm_config_groups
);
641 entries
--; /* keep list NULL terminated */
642 for (i
= 0; i
< entries
; i
++) {
643 if (vm_config_groups
[i
] == NULL
) {
644 vm_config_groups
[i
] = list
;
648 fprintf(stderr
, "ran out of space in vm_config_groups");
652 int qemu_set_option(const char *str
)
654 char group
[64], id
[64], arg
[64];
659 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
660 if (rc
< 3 || str
[offset
] != '=') {
661 error_report("can't parse: \"%s\"", str
);
665 list
= qemu_find_opts(group
);
670 opts
= qemu_opts_find(list
, id
);
672 error_report("there is no %s \"%s\" defined",
677 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
683 int qemu_global_option(const char *str
)
685 char driver
[64], property
[64];
689 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
690 if (rc
< 2 || str
[offset
] != '=') {
691 error_report("can't parse: \"%s\"", str
);
695 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
696 qemu_opt_set(opts
, "driver", driver
);
697 qemu_opt_set(opts
, "property", property
);
698 qemu_opt_set(opts
, "value", str
+offset
+1);
702 struct ConfigWriteData
{
707 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
709 struct ConfigWriteData
*data
= opaque
;
711 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
715 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
717 struct ConfigWriteData
*data
= opaque
;
718 const char *id
= qemu_opts_id(opts
);
721 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
723 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
725 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
726 fprintf(data
->fp
, "\n");
730 void qemu_config_write(FILE *fp
)
732 struct ConfigWriteData data
= { .fp
= fp
};
733 QemuOptsList
**lists
= vm_config_groups
;
736 fprintf(fp
, "# qemu config file\n\n");
737 for (i
= 0; lists
[i
] != NULL
; i
++) {
738 data
.list
= lists
[i
];
739 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
743 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
745 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
747 QemuOptsList
*list
= NULL
;
748 QemuOpts
*opts
= NULL
;
749 int res
= -1, lno
= 0;
752 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
753 loc_set_file(fname
, ++lno
);
754 if (line
[0] == '\n') {
755 /* skip empty lines */
758 if (line
[0] == '#') {
762 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
764 list
= find_list(lists
, group
);
767 opts
= qemu_opts_create(list
, id
, 1);
770 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
771 /* group without id */
772 list
= find_list(lists
, group
);
775 opts
= qemu_opts_create(list
, NULL
, 0);
778 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
781 error_report("no group defined");
784 if (qemu_opt_set(opts
, arg
, value
) != 0) {
789 error_report("parse error");
793 error_report("error reading file");
802 int qemu_read_config_file(const char *filename
)
804 FILE *f
= fopen(filename
, "r");
811 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);