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_chardev_opts
= {
123 .implied_opt_name
= "backend",
124 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
128 .type
= QEMU_OPT_STRING
,
131 .type
= QEMU_OPT_STRING
,
134 .type
= QEMU_OPT_STRING
,
137 .type
= QEMU_OPT_STRING
,
140 .type
= QEMU_OPT_STRING
,
143 .type
= QEMU_OPT_STRING
,
146 .type
= QEMU_OPT_NUMBER
,
149 .type
= QEMU_OPT_BOOL
,
152 .type
= QEMU_OPT_BOOL
,
155 .type
= QEMU_OPT_BOOL
,
158 .type
= QEMU_OPT_BOOL
,
161 .type
= QEMU_OPT_BOOL
,
164 .type
= QEMU_OPT_BOOL
,
167 .type
= QEMU_OPT_NUMBER
,
170 .type
= QEMU_OPT_NUMBER
,
173 .type
= QEMU_OPT_NUMBER
,
176 .type
= QEMU_OPT_NUMBER
,
179 .type
= QEMU_OPT_BOOL
,
182 .type
= QEMU_OPT_BOOL
,
185 .type
= QEMU_OPT_STRING
,
188 .type
= QEMU_OPT_NUMBER
,
190 { /* end of list */ }
194 QemuOptsList qemu_fsdev_opts
= {
196 .implied_opt_name
= "fsdriver",
197 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
201 .type
= QEMU_OPT_STRING
,
204 .type
= QEMU_OPT_STRING
,
206 .name
= "security_model",
207 .type
= QEMU_OPT_STRING
,
210 .type
= QEMU_OPT_STRING
,
213 .type
= QEMU_OPT_BOOL
,
217 .type
= QEMU_OPT_STRING
,
220 .type
= QEMU_OPT_NUMBER
,
227 QemuOptsList qemu_virtfs_opts
= {
229 .implied_opt_name
= "fsdriver",
230 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
234 .type
= QEMU_OPT_STRING
,
237 .type
= QEMU_OPT_STRING
,
240 .type
= QEMU_OPT_STRING
,
242 .name
= "security_model",
243 .type
= QEMU_OPT_STRING
,
246 .type
= QEMU_OPT_STRING
,
249 .type
= QEMU_OPT_BOOL
,
252 .type
= QEMU_OPT_STRING
,
255 .type
= QEMU_OPT_NUMBER
,
262 static QemuOptsList qemu_device_opts
= {
264 .implied_opt_name
= "driver",
265 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
268 * no elements => accept any
269 * sanity checking will happen later
270 * when setting device properties
272 { /* end of list */ }
276 static QemuOptsList qemu_netdev_opts
= {
278 .implied_opt_name
= "type",
279 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
282 * no elements => accept any params
283 * validation will happen later
285 { /* end of list */ }
289 static QemuOptsList qemu_net_opts
= {
291 .implied_opt_name
= "type",
292 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
295 * no elements => accept any params
296 * validation will happen later
298 { /* end of list */ }
302 static QemuOptsList qemu_rtc_opts
= {
304 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
308 .type
= QEMU_OPT_STRING
,
311 .type
= QEMU_OPT_STRING
,
314 .type
= QEMU_OPT_STRING
,
316 { /* end of list */ }
320 static QemuOptsList qemu_global_opts
= {
322 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
326 .type
= QEMU_OPT_STRING
,
329 .type
= QEMU_OPT_STRING
,
332 .type
= QEMU_OPT_STRING
,
334 { /* end of list */ }
338 static QemuOptsList qemu_mon_opts
= {
340 .implied_opt_name
= "chardev",
341 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
345 .type
= QEMU_OPT_STRING
,
348 .type
= QEMU_OPT_STRING
,
351 .type
= QEMU_OPT_BOOL
,
354 .type
= QEMU_OPT_BOOL
,
356 { /* end of list */ }
360 static QemuOptsList qemu_trace_opts
= {
362 .implied_opt_name
= "trace",
363 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
367 .type
= QEMU_OPT_STRING
,
370 .type
= QEMU_OPT_STRING
,
372 { /* end of list */ }
376 static QemuOptsList qemu_cpudef_opts
= {
378 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
382 .type
= QEMU_OPT_STRING
,
385 .type
= QEMU_OPT_NUMBER
,
388 .type
= QEMU_OPT_STRING
,
391 .type
= QEMU_OPT_NUMBER
,
394 .type
= QEMU_OPT_NUMBER
,
397 .type
= QEMU_OPT_NUMBER
,
399 .name
= "feature_edx", /* cpuid 0000_0001.edx */
400 .type
= QEMU_OPT_STRING
,
402 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
403 .type
= QEMU_OPT_STRING
,
405 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
406 .type
= QEMU_OPT_STRING
,
408 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
409 .type
= QEMU_OPT_STRING
,
412 .type
= QEMU_OPT_NUMBER
,
415 .type
= QEMU_OPT_STRING
,
417 .name
= "vendor_override",
418 .type
= QEMU_OPT_NUMBER
,
420 { /* end of list */ }
424 QemuOptsList qemu_spice_opts
= {
426 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
430 .type
= QEMU_OPT_NUMBER
,
433 .type
= QEMU_OPT_NUMBER
,
436 .type
= QEMU_OPT_STRING
,
439 .type
= QEMU_OPT_BOOL
,
442 .type
= QEMU_OPT_BOOL
,
445 .type
= QEMU_OPT_STRING
,
447 .name
= "disable-ticketing",
448 .type
= QEMU_OPT_BOOL
,
450 .name
= "disable-copy-paste",
451 .type
= QEMU_OPT_BOOL
,
454 .type
= QEMU_OPT_BOOL
,
457 .type
= QEMU_OPT_STRING
,
459 .name
= "x509-key-file",
460 .type
= QEMU_OPT_STRING
,
462 .name
= "x509-key-password",
463 .type
= QEMU_OPT_STRING
,
465 .name
= "x509-cert-file",
466 .type
= QEMU_OPT_STRING
,
468 .name
= "x509-cacert-file",
469 .type
= QEMU_OPT_STRING
,
471 .name
= "x509-dh-key-file",
472 .type
= QEMU_OPT_STRING
,
474 .name
= "tls-ciphers",
475 .type
= QEMU_OPT_STRING
,
477 .name
= "tls-channel",
478 .type
= QEMU_OPT_STRING
,
480 .name
= "plaintext-channel",
481 .type
= QEMU_OPT_STRING
,
483 .name
= "image-compression",
484 .type
= QEMU_OPT_STRING
,
486 .name
= "jpeg-wan-compression",
487 .type
= QEMU_OPT_STRING
,
489 .name
= "zlib-glz-wan-compression",
490 .type
= QEMU_OPT_STRING
,
492 .name
= "streaming-video",
493 .type
= QEMU_OPT_STRING
,
495 .name
= "agent-mouse",
496 .type
= QEMU_OPT_BOOL
,
498 .name
= "playback-compression",
499 .type
= QEMU_OPT_BOOL
,
501 { /* end of list */ }
505 QemuOptsList qemu_option_rom_opts
= {
506 .name
= "option-rom",
507 .implied_opt_name
= "romfile",
508 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
512 .type
= QEMU_OPT_NUMBER
,
515 .type
= QEMU_OPT_STRING
,
517 { /* end of list */ }
521 static QemuOptsList qemu_machine_opts
= {
523 .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 { /* End of list */ }
543 QemuOptsList qemu_boot_opts
= {
545 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
547 /* the three names below are not used now */
550 .type
= QEMU_OPT_STRING
,
553 .type
= QEMU_OPT_STRING
,
556 .type
= QEMU_OPT_STRING
,
557 /* following are really used */
560 .type
= QEMU_OPT_STRING
,
562 .name
= "splash-time",
563 .type
= QEMU_OPT_STRING
,
569 static QemuOptsList
*vm_config_groups
[32] = {
580 &qemu_option_rom_opts
,
586 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
590 for (i
= 0; lists
[i
] != NULL
; i
++) {
591 if (strcmp(lists
[i
]->name
, group
) == 0)
594 if (lists
[i
] == NULL
) {
595 error_report("there is no option group \"%s\"", group
);
600 QemuOptsList
*qemu_find_opts(const char *group
)
602 return find_list(vm_config_groups
, group
);
605 void qemu_add_opts(QemuOptsList
*list
)
609 entries
= ARRAY_SIZE(vm_config_groups
);
610 entries
--; /* keep list NULL terminated */
611 for (i
= 0; i
< entries
; i
++) {
612 if (vm_config_groups
[i
] == NULL
) {
613 vm_config_groups
[i
] = list
;
617 fprintf(stderr
, "ran out of space in vm_config_groups");
621 int qemu_set_option(const char *str
)
623 char group
[64], id
[64], arg
[64];
628 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
629 if (rc
< 3 || str
[offset
] != '=') {
630 error_report("can't parse: \"%s\"", str
);
634 list
= qemu_find_opts(group
);
639 opts
= qemu_opts_find(list
, id
);
641 error_report("there is no %s \"%s\" defined",
646 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
652 int qemu_global_option(const char *str
)
654 char driver
[64], property
[64];
658 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
659 if (rc
< 2 || str
[offset
] != '=') {
660 error_report("can't parse: \"%s\"", str
);
664 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
665 qemu_opt_set(opts
, "driver", driver
);
666 qemu_opt_set(opts
, "property", property
);
667 qemu_opt_set(opts
, "value", str
+offset
+1);
671 struct ConfigWriteData
{
676 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
678 struct ConfigWriteData
*data
= opaque
;
680 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
684 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
686 struct ConfigWriteData
*data
= opaque
;
687 const char *id
= qemu_opts_id(opts
);
690 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
692 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
694 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
695 fprintf(data
->fp
, "\n");
699 void qemu_config_write(FILE *fp
)
701 struct ConfigWriteData data
= { .fp
= fp
};
702 QemuOptsList
**lists
= vm_config_groups
;
705 fprintf(fp
, "# qemu config file\n\n");
706 for (i
= 0; lists
[i
] != NULL
; i
++) {
707 data
.list
= lists
[i
];
708 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
712 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
714 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
716 QemuOptsList
*list
= NULL
;
717 QemuOpts
*opts
= NULL
;
718 int res
= -1, lno
= 0;
721 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
722 loc_set_file(fname
, ++lno
);
723 if (line
[0] == '\n') {
724 /* skip empty lines */
727 if (line
[0] == '#') {
731 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
733 list
= find_list(lists
, group
);
736 opts
= qemu_opts_create(list
, id
, 1);
739 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
740 /* group without id */
741 list
= find_list(lists
, group
);
744 opts
= qemu_opts_create(list
, NULL
, 0);
747 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
750 error_report("no group defined");
753 if (qemu_opt_set(opts
, arg
, value
) != 0) {
758 error_report("parse error");
762 error_report("error reading file");
771 int qemu_read_config_file(const char *filename
)
773 FILE *f
= fopen(filename
, "r");
780 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);