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 { /* End of list */ }
539 QemuOptsList qemu_boot_opts
= {
541 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
543 /* the three names below are not used now */
546 .type
= QEMU_OPT_STRING
,
549 .type
= QEMU_OPT_STRING
,
552 .type
= QEMU_OPT_STRING
,
553 /* following are really used */
556 .type
= QEMU_OPT_STRING
,
558 .name
= "splash-time",
559 .type
= QEMU_OPT_STRING
,
565 static QemuOptsList
*vm_config_groups
[32] = {
576 &qemu_option_rom_opts
,
582 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
586 for (i
= 0; lists
[i
] != NULL
; i
++) {
587 if (strcmp(lists
[i
]->name
, group
) == 0)
590 if (lists
[i
] == NULL
) {
591 error_report("there is no option group \"%s\"", group
);
596 QemuOptsList
*qemu_find_opts(const char *group
)
598 return find_list(vm_config_groups
, group
);
601 void qemu_add_opts(QemuOptsList
*list
)
605 entries
= ARRAY_SIZE(vm_config_groups
);
606 entries
--; /* keep list NULL terminated */
607 for (i
= 0; i
< entries
; i
++) {
608 if (vm_config_groups
[i
] == NULL
) {
609 vm_config_groups
[i
] = list
;
613 fprintf(stderr
, "ran out of space in vm_config_groups");
617 int qemu_set_option(const char *str
)
619 char group
[64], id
[64], arg
[64];
624 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
625 if (rc
< 3 || str
[offset
] != '=') {
626 error_report("can't parse: \"%s\"", str
);
630 list
= qemu_find_opts(group
);
635 opts
= qemu_opts_find(list
, id
);
637 error_report("there is no %s \"%s\" defined",
642 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
648 int qemu_global_option(const char *str
)
650 char driver
[64], property
[64];
654 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
655 if (rc
< 2 || str
[offset
] != '=') {
656 error_report("can't parse: \"%s\"", str
);
660 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
661 qemu_opt_set(opts
, "driver", driver
);
662 qemu_opt_set(opts
, "property", property
);
663 qemu_opt_set(opts
, "value", str
+offset
+1);
667 struct ConfigWriteData
{
672 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
674 struct ConfigWriteData
*data
= opaque
;
676 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
680 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
682 struct ConfigWriteData
*data
= opaque
;
683 const char *id
= qemu_opts_id(opts
);
686 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
688 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
690 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
691 fprintf(data
->fp
, "\n");
695 void qemu_config_write(FILE *fp
)
697 struct ConfigWriteData data
= { .fp
= fp
};
698 QemuOptsList
**lists
= vm_config_groups
;
701 fprintf(fp
, "# qemu config file\n\n");
702 for (i
= 0; lists
[i
] != NULL
; i
++) {
703 data
.list
= lists
[i
];
704 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
708 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
710 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
712 QemuOptsList
*list
= NULL
;
713 QemuOpts
*opts
= NULL
;
714 int res
= -1, lno
= 0;
717 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
718 loc_set_file(fname
, ++lno
);
719 if (line
[0] == '\n') {
720 /* skip empty lines */
723 if (line
[0] == '#') {
727 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
729 list
= find_list(lists
, group
);
732 opts
= qemu_opts_create(list
, id
, 1);
735 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
736 /* group without id */
737 list
= find_list(lists
, group
);
740 opts
= qemu_opts_create(list
, NULL
, 0);
743 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
746 error_report("no group defined");
749 if (qemu_opt_set(opts
, arg
, value
) != 0) {
754 error_report("parse error");
758 error_report("error reading file");
767 int qemu_read_config_file(const char *filename
)
769 FILE *f
= fopen(filename
, "r");
776 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);