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, unsafe)",
61 .type
= QEMU_OPT_STRING
,
62 .help
= "host AIO implementation (threads, native)",
65 .type
= QEMU_OPT_STRING
,
66 .help
= "disk format (raw, qcow2, ...)",
69 .type
= QEMU_OPT_STRING
,
70 .help
= "disk serial number",
73 .type
= QEMU_OPT_STRING
,
74 .help
= "read error action",
77 .type
= QEMU_OPT_STRING
,
78 .help
= "write error action",
81 .type
= QEMU_OPT_STRING
,
82 .help
= "pci address (virtio only)",
85 .type
= QEMU_OPT_BOOL
,
86 .help
= "open drive file as read-only",
92 static QemuOptsList qemu_chardev_opts
= {
94 .implied_opt_name
= "backend",
95 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
99 .type
= QEMU_OPT_STRING
,
102 .type
= QEMU_OPT_STRING
,
105 .type
= QEMU_OPT_STRING
,
108 .type
= QEMU_OPT_STRING
,
111 .type
= QEMU_OPT_STRING
,
114 .type
= QEMU_OPT_STRING
,
117 .type
= QEMU_OPT_NUMBER
,
120 .type
= QEMU_OPT_BOOL
,
123 .type
= QEMU_OPT_BOOL
,
126 .type
= QEMU_OPT_BOOL
,
129 .type
= QEMU_OPT_BOOL
,
132 .type
= QEMU_OPT_BOOL
,
135 .type
= QEMU_OPT_BOOL
,
138 .type
= QEMU_OPT_NUMBER
,
141 .type
= QEMU_OPT_NUMBER
,
144 .type
= QEMU_OPT_NUMBER
,
147 .type
= QEMU_OPT_NUMBER
,
150 .type
= QEMU_OPT_BOOL
,
153 .type
= QEMU_OPT_BOOL
,
156 .type
= QEMU_OPT_STRING
,
159 .type
= QEMU_OPT_NUMBER
,
161 { /* end of list */ }
165 QemuOptsList qemu_fsdev_opts
= {
167 .implied_opt_name
= "fstype",
168 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
172 .type
= QEMU_OPT_STRING
,
175 .type
= QEMU_OPT_STRING
,
177 .name
= "security_model",
178 .type
= QEMU_OPT_STRING
,
184 QemuOptsList qemu_virtfs_opts
= {
186 .implied_opt_name
= "fstype",
187 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
191 .type
= QEMU_OPT_STRING
,
194 .type
= QEMU_OPT_STRING
,
197 .type
= QEMU_OPT_STRING
,
199 .name
= "security_model",
200 .type
= QEMU_OPT_STRING
,
207 static QemuOptsList qemu_device_opts
= {
209 .implied_opt_name
= "driver",
210 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
213 * no elements => accept any
214 * sanity checking will happen later
215 * when setting device properties
217 { /* end of list */ }
221 static QemuOptsList qemu_netdev_opts
= {
223 .implied_opt_name
= "type",
224 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
227 * no elements => accept any params
228 * validation will happen later
230 { /* end of list */ }
234 static QemuOptsList qemu_net_opts
= {
236 .implied_opt_name
= "type",
237 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
240 * no elements => accept any params
241 * validation will happen later
243 { /* end of list */ }
247 static QemuOptsList qemu_rtc_opts
= {
249 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
253 .type
= QEMU_OPT_STRING
,
256 .type
= QEMU_OPT_STRING
,
259 .type
= QEMU_OPT_STRING
,
261 { /* end of list */ }
265 static QemuOptsList qemu_global_opts
= {
267 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
271 .type
= QEMU_OPT_STRING
,
274 .type
= QEMU_OPT_STRING
,
277 .type
= QEMU_OPT_STRING
,
279 { /* end of list */ }
283 static QemuOptsList qemu_mon_opts
= {
285 .implied_opt_name
= "chardev",
286 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
290 .type
= QEMU_OPT_STRING
,
293 .type
= QEMU_OPT_STRING
,
296 .type
= QEMU_OPT_BOOL
,
299 .type
= QEMU_OPT_BOOL
,
301 { /* end of list */ }
305 #ifdef CONFIG_SIMPLE_TRACE
306 static QemuOptsList qemu_trace_opts
= {
308 .implied_opt_name
= "trace",
309 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
313 .type
= QEMU_OPT_STRING
,
315 { /* end of list */ }
320 static QemuOptsList qemu_cpudef_opts
= {
322 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
326 .type
= QEMU_OPT_STRING
,
329 .type
= QEMU_OPT_NUMBER
,
332 .type
= QEMU_OPT_STRING
,
335 .type
= QEMU_OPT_NUMBER
,
338 .type
= QEMU_OPT_NUMBER
,
341 .type
= QEMU_OPT_NUMBER
,
343 .name
= "feature_edx", /* cpuid 0000_0001.edx */
344 .type
= QEMU_OPT_STRING
,
346 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
347 .type
= QEMU_OPT_STRING
,
349 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
350 .type
= QEMU_OPT_STRING
,
352 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
353 .type
= QEMU_OPT_STRING
,
356 .type
= QEMU_OPT_NUMBER
,
359 .type
= QEMU_OPT_STRING
,
361 .name
= "vendor_override",
362 .type
= QEMU_OPT_NUMBER
,
364 { /* end of list */ }
368 QemuOptsList qemu_spice_opts
= {
370 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
374 .type
= QEMU_OPT_NUMBER
,
377 .type
= QEMU_OPT_NUMBER
,
380 .type
= QEMU_OPT_STRING
,
383 .type
= QEMU_OPT_BOOL
,
386 .type
= QEMU_OPT_BOOL
,
389 .type
= QEMU_OPT_STRING
,
391 .name
= "disable-ticketing",
392 .type
= QEMU_OPT_BOOL
,
394 .name
= "disable-copy-paste",
395 .type
= QEMU_OPT_BOOL
,
398 .type
= QEMU_OPT_BOOL
,
401 .type
= QEMU_OPT_STRING
,
403 .name
= "x509-key-file",
404 .type
= QEMU_OPT_STRING
,
406 .name
= "x509-key-password",
407 .type
= QEMU_OPT_STRING
,
409 .name
= "x509-cert-file",
410 .type
= QEMU_OPT_STRING
,
412 .name
= "x509-cacert-file",
413 .type
= QEMU_OPT_STRING
,
415 .name
= "x509-dh-key-file",
416 .type
= QEMU_OPT_STRING
,
418 .name
= "tls-ciphers",
419 .type
= QEMU_OPT_STRING
,
421 .name
= "tls-channel",
422 .type
= QEMU_OPT_STRING
,
424 .name
= "plaintext-channel",
425 .type
= QEMU_OPT_STRING
,
427 .name
= "image-compression",
428 .type
= QEMU_OPT_STRING
,
430 .name
= "jpeg-wan-compression",
431 .type
= QEMU_OPT_STRING
,
433 .name
= "zlib-glz-wan-compression",
434 .type
= QEMU_OPT_STRING
,
436 .name
= "streaming-video",
437 .type
= QEMU_OPT_STRING
,
439 .name
= "agent-mouse",
440 .type
= QEMU_OPT_BOOL
,
442 .name
= "playback-compression",
443 .type
= QEMU_OPT_BOOL
,
445 { /* end of list */ }
449 QemuOptsList qemu_option_rom_opts
= {
450 .name
= "option-rom",
451 .implied_opt_name
= "romfile",
452 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
456 .type
= QEMU_OPT_NUMBER
,
459 .type
= QEMU_OPT_STRING
,
461 { /* end of list */ }
465 static QemuOptsList qemu_machine_opts
= {
467 .implied_opt_name
= "type",
468 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
472 .type
= QEMU_OPT_STRING
,
473 .help
= "emulated machine"
476 .type
= QEMU_OPT_STRING
,
477 .help
= "accelerator list",
479 { /* End of list */ }
483 static QemuOptsList
*vm_config_groups
[32] = {
493 #ifdef CONFIG_SIMPLE_TRACE
496 &qemu_option_rom_opts
,
501 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
505 for (i
= 0; lists
[i
] != NULL
; i
++) {
506 if (strcmp(lists
[i
]->name
, group
) == 0)
509 if (lists
[i
] == NULL
) {
510 error_report("there is no option group \"%s\"", group
);
515 QemuOptsList
*qemu_find_opts(const char *group
)
517 return find_list(vm_config_groups
, group
);
520 void qemu_add_opts(QemuOptsList
*list
)
524 entries
= ARRAY_SIZE(vm_config_groups
);
525 entries
--; /* keep list NULL terminated */
526 for (i
= 0; i
< entries
; i
++) {
527 if (vm_config_groups
[i
] == NULL
) {
528 vm_config_groups
[i
] = list
;
532 fprintf(stderr
, "ran out of space in vm_config_groups");
536 int qemu_set_option(const char *str
)
538 char group
[64], id
[64], arg
[64];
543 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
544 if (rc
< 3 || str
[offset
] != '=') {
545 error_report("can't parse: \"%s\"", str
);
549 list
= qemu_find_opts(group
);
554 opts
= qemu_opts_find(list
, id
);
556 error_report("there is no %s \"%s\" defined",
561 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
567 int qemu_global_option(const char *str
)
569 char driver
[64], property
[64];
573 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
574 if (rc
< 2 || str
[offset
] != '=') {
575 error_report("can't parse: \"%s\"", str
);
579 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
580 qemu_opt_set(opts
, "driver", driver
);
581 qemu_opt_set(opts
, "property", property
);
582 qemu_opt_set(opts
, "value", str
+offset
+1);
586 struct ConfigWriteData
{
591 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
593 struct ConfigWriteData
*data
= opaque
;
595 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
599 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
601 struct ConfigWriteData
*data
= opaque
;
602 const char *id
= qemu_opts_id(opts
);
605 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
607 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
609 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
610 fprintf(data
->fp
, "\n");
614 void qemu_config_write(FILE *fp
)
616 struct ConfigWriteData data
= { .fp
= fp
};
617 QemuOptsList
**lists
= vm_config_groups
;
620 fprintf(fp
, "# qemu config file\n\n");
621 for (i
= 0; lists
[i
] != NULL
; i
++) {
622 data
.list
= lists
[i
];
623 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
627 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
629 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
631 QemuOptsList
*list
= NULL
;
632 QemuOpts
*opts
= NULL
;
633 int res
= -1, lno
= 0;
636 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
637 loc_set_file(fname
, ++lno
);
638 if (line
[0] == '\n') {
639 /* skip empty lines */
642 if (line
[0] == '#') {
646 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
648 list
= find_list(lists
, group
);
651 opts
= qemu_opts_create(list
, id
, 1);
654 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
655 /* group without id */
656 list
= find_list(lists
, group
);
659 opts
= qemu_opts_create(list
, NULL
, 0);
662 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
665 error_report("no group defined");
668 if (qemu_opt_set(opts
, arg
, value
) != 0) {
673 error_report("parse error");
677 error_report("error reading file");
686 int qemu_read_config_file(const char *filename
)
688 FILE *f
= fopen(filename
, "r");
695 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);