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
,
28 .type
= QEMU_OPT_NUMBER
,
29 .help
= "number of cylinders (ide disk geometry)",
32 .type
= QEMU_OPT_NUMBER
,
33 .help
= "number of heads (ide disk geometry)",
36 .type
= QEMU_OPT_NUMBER
,
37 .help
= "number of sectors (ide disk geometry)",
40 .type
= QEMU_OPT_STRING
,
41 .help
= "chs translation (auto, lba. none)",
44 .type
= QEMU_OPT_STRING
,
45 .help
= "media type (disk, cdrom)",
48 .type
= QEMU_OPT_BOOL
,
51 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_STRING
,
56 .help
= "host cache usage (none, writeback, writethrough, unsafe)",
59 .type
= QEMU_OPT_STRING
,
60 .help
= "host AIO implementation (threads, native)",
63 .type
= QEMU_OPT_STRING
,
64 .help
= "disk format (raw, qcow2, ...)",
67 .type
= QEMU_OPT_STRING
,
70 .type
= QEMU_OPT_STRING
,
73 .type
= QEMU_OPT_STRING
,
76 .type
= QEMU_OPT_STRING
,
77 .help
= "pci address (virtio only)",
80 .type
= QEMU_OPT_BOOL
,
83 .type
= QEMU_OPT_BOOL
,
84 .help
= "make this a boot drive",
90 static QemuOptsList qemu_chardev_opts
= {
92 .implied_opt_name
= "backend",
93 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
97 .type
= QEMU_OPT_STRING
,
100 .type
= QEMU_OPT_STRING
,
103 .type
= QEMU_OPT_STRING
,
106 .type
= QEMU_OPT_STRING
,
109 .type
= QEMU_OPT_STRING
,
112 .type
= QEMU_OPT_STRING
,
115 .type
= QEMU_OPT_NUMBER
,
118 .type
= QEMU_OPT_BOOL
,
121 .type
= QEMU_OPT_BOOL
,
124 .type
= QEMU_OPT_BOOL
,
127 .type
= QEMU_OPT_BOOL
,
130 .type
= QEMU_OPT_BOOL
,
133 .type
= QEMU_OPT_BOOL
,
136 .type
= QEMU_OPT_NUMBER
,
139 .type
= QEMU_OPT_NUMBER
,
142 .type
= QEMU_OPT_NUMBER
,
145 .type
= QEMU_OPT_NUMBER
,
148 .type
= QEMU_OPT_BOOL
,
151 .type
= QEMU_OPT_BOOL
,
154 .type
= QEMU_OPT_STRING
,
157 .type
= QEMU_OPT_NUMBER
,
159 { /* end of list */ }
163 QemuOptsList qemu_fsdev_opts
= {
165 .implied_opt_name
= "fstype",
166 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
170 .type
= QEMU_OPT_STRING
,
173 .type
= QEMU_OPT_STRING
,
175 .name
= "security_model",
176 .type
= QEMU_OPT_STRING
,
182 QemuOptsList qemu_virtfs_opts
= {
184 .implied_opt_name
= "fstype",
185 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
189 .type
= QEMU_OPT_STRING
,
192 .type
= QEMU_OPT_STRING
,
195 .type
= QEMU_OPT_STRING
,
197 .name
= "security_model",
198 .type
= QEMU_OPT_STRING
,
205 static QemuOptsList qemu_device_opts
= {
207 .implied_opt_name
= "driver",
208 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
211 * no elements => accept any
212 * sanity checking will happen later
213 * when setting device properties
215 { /* end of list */ }
219 static QemuOptsList qemu_netdev_opts
= {
221 .implied_opt_name
= "type",
222 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
225 * no elements => accept any params
226 * validation will happen later
228 { /* end of list */ }
232 static QemuOptsList qemu_net_opts
= {
234 .implied_opt_name
= "type",
235 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
238 * no elements => accept any params
239 * validation will happen later
241 { /* end of list */ }
245 static QemuOptsList qemu_rtc_opts
= {
247 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_STRING
,
257 .type
= QEMU_OPT_STRING
,
259 { /* end of list */ }
263 static QemuOptsList qemu_global_opts
= {
265 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_STRING
,
275 .type
= QEMU_OPT_STRING
,
277 { /* end of list */ }
281 static QemuOptsList qemu_mon_opts
= {
283 .implied_opt_name
= "chardev",
284 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
288 .type
= QEMU_OPT_STRING
,
291 .type
= QEMU_OPT_STRING
,
294 .type
= QEMU_OPT_BOOL
,
297 .type
= QEMU_OPT_BOOL
,
299 { /* end of list */ }
303 #ifdef CONFIG_SIMPLE_TRACE
304 static QemuOptsList qemu_trace_opts
= {
306 .implied_opt_name
= "trace",
307 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
311 .type
= QEMU_OPT_STRING
,
313 { /* end of list */ }
318 static QemuOptsList qemu_cpudef_opts
= {
320 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
324 .type
= QEMU_OPT_STRING
,
327 .type
= QEMU_OPT_NUMBER
,
330 .type
= QEMU_OPT_STRING
,
333 .type
= QEMU_OPT_NUMBER
,
336 .type
= QEMU_OPT_NUMBER
,
339 .type
= QEMU_OPT_NUMBER
,
341 .name
= "feature_edx", /* cpuid 0000_0001.edx */
342 .type
= QEMU_OPT_STRING
,
344 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
345 .type
= QEMU_OPT_STRING
,
347 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
348 .type
= QEMU_OPT_STRING
,
350 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
351 .type
= QEMU_OPT_STRING
,
354 .type
= QEMU_OPT_NUMBER
,
357 .type
= QEMU_OPT_STRING
,
359 .name
= "vendor_override",
360 .type
= QEMU_OPT_NUMBER
,
362 { /* end of list */ }
366 QemuOptsList qemu_spice_opts
= {
368 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
372 .type
= QEMU_OPT_NUMBER
,
375 .type
= QEMU_OPT_NUMBER
,
378 .type
= QEMU_OPT_STRING
,
381 .type
= QEMU_OPT_BOOL
,
384 .type
= QEMU_OPT_BOOL
,
387 .type
= QEMU_OPT_STRING
,
389 .name
= "disable-ticketing",
390 .type
= QEMU_OPT_BOOL
,
392 .name
= "disable-copy-paste",
393 .type
= QEMU_OPT_BOOL
,
396 .type
= QEMU_OPT_BOOL
,
399 .type
= QEMU_OPT_STRING
,
401 .name
= "x509-key-file",
402 .type
= QEMU_OPT_STRING
,
404 .name
= "x509-key-password",
405 .type
= QEMU_OPT_STRING
,
407 .name
= "x509-cert-file",
408 .type
= QEMU_OPT_STRING
,
410 .name
= "x509-cacert-file",
411 .type
= QEMU_OPT_STRING
,
413 .name
= "x509-dh-key-file",
414 .type
= QEMU_OPT_STRING
,
416 .name
= "tls-ciphers",
417 .type
= QEMU_OPT_STRING
,
419 .name
= "tls-channel",
420 .type
= QEMU_OPT_STRING
,
422 .name
= "plaintext-channel",
423 .type
= QEMU_OPT_STRING
,
425 .name
= "image-compression",
426 .type
= QEMU_OPT_STRING
,
428 .name
= "jpeg-wan-compression",
429 .type
= QEMU_OPT_STRING
,
431 .name
= "zlib-glz-wan-compression",
432 .type
= QEMU_OPT_STRING
,
434 .name
= "streaming-video",
435 .type
= QEMU_OPT_STRING
,
437 .name
= "agent-mouse",
438 .type
= QEMU_OPT_BOOL
,
440 .name
= "playback-compression",
441 .type
= QEMU_OPT_BOOL
,
443 { /* end of list */ }
447 QemuOptsList qemu_option_rom_opts
= {
448 .name
= "option-rom",
449 .implied_opt_name
= "romfile",
450 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
454 .type
= QEMU_OPT_NUMBER
,
457 .type
= QEMU_OPT_STRING
,
459 { /* end of list */ }
463 static QemuOptsList qemu_machine_opts
= {
465 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
469 .type
= QEMU_OPT_STRING
,
470 .help
= "accelerator list",
472 { /* End of list */ }
476 static QemuOptsList
*vm_config_groups
[32] = {
486 #ifdef CONFIG_SIMPLE_TRACE
489 &qemu_option_rom_opts
,
494 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
498 for (i
= 0; lists
[i
] != NULL
; i
++) {
499 if (strcmp(lists
[i
]->name
, group
) == 0)
502 if (lists
[i
] == NULL
) {
503 error_report("there is no option group \"%s\"", group
);
508 QemuOptsList
*qemu_find_opts(const char *group
)
510 return find_list(vm_config_groups
, group
);
513 void qemu_add_opts(QemuOptsList
*list
)
517 entries
= ARRAY_SIZE(vm_config_groups
);
518 entries
--; /* keep list NULL terminated */
519 for (i
= 0; i
< entries
; i
++) {
520 if (vm_config_groups
[i
] == NULL
) {
521 vm_config_groups
[i
] = list
;
525 fprintf(stderr
, "ran out of space in vm_config_groups");
529 int qemu_set_option(const char *str
)
531 char group
[64], id
[64], arg
[64];
536 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
537 if (rc
< 3 || str
[offset
] != '=') {
538 error_report("can't parse: \"%s\"", str
);
542 list
= qemu_find_opts(group
);
547 opts
= qemu_opts_find(list
, id
);
549 error_report("there is no %s \"%s\" defined",
554 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
560 int qemu_global_option(const char *str
)
562 char driver
[64], property
[64];
566 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
567 if (rc
< 2 || str
[offset
] != '=') {
568 error_report("can't parse: \"%s\"", str
);
572 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
573 qemu_opt_set(opts
, "driver", driver
);
574 qemu_opt_set(opts
, "property", property
);
575 qemu_opt_set(opts
, "value", str
+offset
+1);
579 struct ConfigWriteData
{
584 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
586 struct ConfigWriteData
*data
= opaque
;
588 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
592 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
594 struct ConfigWriteData
*data
= opaque
;
595 const char *id
= qemu_opts_id(opts
);
598 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
600 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
602 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
603 fprintf(data
->fp
, "\n");
607 void qemu_config_write(FILE *fp
)
609 struct ConfigWriteData data
= { .fp
= fp
};
610 QemuOptsList
**lists
= vm_config_groups
;
613 fprintf(fp
, "# qemu config file\n\n");
614 for (i
= 0; lists
[i
] != NULL
; i
++) {
615 data
.list
= lists
[i
];
616 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
620 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
622 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
624 QemuOptsList
*list
= NULL
;
625 QemuOpts
*opts
= NULL
;
626 int res
= -1, lno
= 0;
629 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
630 loc_set_file(fname
, ++lno
);
631 if (line
[0] == '\n') {
632 /* skip empty lines */
635 if (line
[0] == '#') {
639 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
641 list
= find_list(lists
, group
);
644 opts
= qemu_opts_create(list
, id
, 1);
647 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
648 /* group without id */
649 list
= find_list(lists
, group
);
652 opts
= qemu_opts_create(list
, NULL
, 0);
655 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
658 error_report("no group defined");
661 if (qemu_opt_set(opts
, arg
, value
) != 0) {
666 error_report("parse error");
670 error_report("error reading file");
679 int qemu_read_config_file(const char *filename
)
681 FILE *f
= fopen(filename
, "r");
688 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);