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
,
86 static QemuOptsList qemu_chardev_opts
= {
88 .implied_opt_name
= "backend",
89 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
93 .type
= QEMU_OPT_STRING
,
96 .type
= QEMU_OPT_STRING
,
99 .type
= QEMU_OPT_STRING
,
102 .type
= QEMU_OPT_STRING
,
105 .type
= QEMU_OPT_STRING
,
108 .type
= QEMU_OPT_STRING
,
111 .type
= QEMU_OPT_NUMBER
,
114 .type
= QEMU_OPT_BOOL
,
117 .type
= QEMU_OPT_BOOL
,
120 .type
= QEMU_OPT_BOOL
,
123 .type
= QEMU_OPT_BOOL
,
126 .type
= QEMU_OPT_BOOL
,
129 .type
= QEMU_OPT_BOOL
,
132 .type
= QEMU_OPT_NUMBER
,
135 .type
= QEMU_OPT_NUMBER
,
138 .type
= QEMU_OPT_NUMBER
,
141 .type
= QEMU_OPT_NUMBER
,
144 .type
= QEMU_OPT_BOOL
,
147 .type
= QEMU_OPT_BOOL
,
150 .type
= QEMU_OPT_STRING
,
153 .type
= QEMU_OPT_NUMBER
,
155 { /* end of list */ }
159 QemuOptsList qemu_fsdev_opts
= {
161 .implied_opt_name
= "fstype",
162 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
166 .type
= QEMU_OPT_STRING
,
169 .type
= QEMU_OPT_STRING
,
171 .name
= "security_model",
172 .type
= QEMU_OPT_STRING
,
178 QemuOptsList qemu_virtfs_opts
= {
180 .implied_opt_name
= "fstype",
181 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
185 .type
= QEMU_OPT_STRING
,
188 .type
= QEMU_OPT_STRING
,
191 .type
= QEMU_OPT_STRING
,
193 .name
= "security_model",
194 .type
= QEMU_OPT_STRING
,
201 static QemuOptsList qemu_device_opts
= {
203 .implied_opt_name
= "driver",
204 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
207 * no elements => accept any
208 * sanity checking will happen later
209 * when setting device properties
211 { /* end of list */ }
215 static QemuOptsList qemu_netdev_opts
= {
217 .implied_opt_name
= "type",
218 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
221 * no elements => accept any params
222 * validation will happen later
224 { /* end of list */ }
228 static QemuOptsList qemu_net_opts
= {
230 .implied_opt_name
= "type",
231 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
234 * no elements => accept any params
235 * validation will happen later
237 { /* end of list */ }
241 static QemuOptsList qemu_rtc_opts
= {
243 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
247 .type
= QEMU_OPT_STRING
,
250 .type
= QEMU_OPT_STRING
,
253 .type
= QEMU_OPT_STRING
,
255 { /* end of list */ }
259 static QemuOptsList qemu_global_opts
= {
261 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
265 .type
= QEMU_OPT_STRING
,
268 .type
= QEMU_OPT_STRING
,
271 .type
= QEMU_OPT_STRING
,
273 { /* end of list */ }
277 static QemuOptsList qemu_mon_opts
= {
279 .implied_opt_name
= "chardev",
280 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
284 .type
= QEMU_OPT_STRING
,
287 .type
= QEMU_OPT_STRING
,
290 .type
= QEMU_OPT_BOOL
,
293 .type
= QEMU_OPT_BOOL
,
295 { /* end of list */ }
299 #ifdef CONFIG_SIMPLE_TRACE
300 static QemuOptsList qemu_trace_opts
= {
302 .implied_opt_name
= "trace",
303 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
307 .type
= QEMU_OPT_STRING
,
309 { /* end if list */ }
314 static QemuOptsList qemu_cpudef_opts
= {
316 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
320 .type
= QEMU_OPT_STRING
,
323 .type
= QEMU_OPT_NUMBER
,
326 .type
= QEMU_OPT_STRING
,
329 .type
= QEMU_OPT_NUMBER
,
332 .type
= QEMU_OPT_NUMBER
,
335 .type
= QEMU_OPT_NUMBER
,
337 .name
= "feature_edx", /* cpuid 0000_0001.edx */
338 .type
= QEMU_OPT_STRING
,
340 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
341 .type
= QEMU_OPT_STRING
,
343 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
344 .type
= QEMU_OPT_STRING
,
346 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
347 .type
= QEMU_OPT_STRING
,
350 .type
= QEMU_OPT_NUMBER
,
353 .type
= QEMU_OPT_STRING
,
355 .name
= "vendor_override",
356 .type
= QEMU_OPT_NUMBER
,
358 { /* end of list */ }
362 QemuOptsList qemu_spice_opts
= {
364 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
368 .type
= QEMU_OPT_NUMBER
,
371 .type
= QEMU_OPT_NUMBER
,
374 .type
= QEMU_OPT_STRING
,
377 .type
= QEMU_OPT_BOOL
,
380 .type
= QEMU_OPT_BOOL
,
383 .type
= QEMU_OPT_STRING
,
385 .name
= "disable-ticketing",
386 .type
= QEMU_OPT_BOOL
,
389 .type
= QEMU_OPT_STRING
,
391 .name
= "x509-key-file",
392 .type
= QEMU_OPT_STRING
,
394 .name
= "x509-key-password",
395 .type
= QEMU_OPT_STRING
,
397 .name
= "x509-cert-file",
398 .type
= QEMU_OPT_STRING
,
400 .name
= "x509-cacert-file",
401 .type
= QEMU_OPT_STRING
,
403 .name
= "x509-dh-key-file",
404 .type
= QEMU_OPT_STRING
,
406 .name
= "tls-ciphers",
407 .type
= QEMU_OPT_STRING
,
409 .name
= "tls-channel",
410 .type
= QEMU_OPT_STRING
,
412 .name
= "plaintext-channel",
413 .type
= QEMU_OPT_STRING
,
415 .name
= "image-compression",
416 .type
= QEMU_OPT_STRING
,
418 .name
= "jpeg-wan-compression",
419 .type
= QEMU_OPT_STRING
,
421 .name
= "zlib-glz-wan-compression",
422 .type
= QEMU_OPT_STRING
,
424 .name
= "streaming-video",
425 .type
= QEMU_OPT_STRING
,
427 .name
= "agent-mouse",
428 .type
= QEMU_OPT_BOOL
,
430 .name
= "playback-compression",
431 .type
= QEMU_OPT_BOOL
,
433 { /* end if list */ }
437 QemuOptsList qemu_option_rom_opts
= {
438 .name
= "option-rom",
439 .implied_opt_name
= "romfile",
440 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
444 .type
= QEMU_OPT_NUMBER
,
447 .type
= QEMU_OPT_STRING
,
449 { /* end if list */ }
453 static QemuOptsList qemu_machine_opts
= {
455 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
459 .type
= QEMU_OPT_STRING
,
460 .help
= "accelerator list",
462 { /* End of list */ }
466 static QemuOptsList
*vm_config_groups
[32] = {
476 #ifdef CONFIG_SIMPLE_TRACE
479 &qemu_option_rom_opts
,
484 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
488 for (i
= 0; lists
[i
] != NULL
; i
++) {
489 if (strcmp(lists
[i
]->name
, group
) == 0)
492 if (lists
[i
] == NULL
) {
493 error_report("there is no option group \"%s\"", group
);
498 QemuOptsList
*qemu_find_opts(const char *group
)
500 return find_list(vm_config_groups
, group
);
503 void qemu_add_opts(QemuOptsList
*list
)
507 entries
= ARRAY_SIZE(vm_config_groups
);
508 entries
--; /* keep list NULL terminated */
509 for (i
= 0; i
< entries
; i
++) {
510 if (vm_config_groups
[i
] == NULL
) {
511 vm_config_groups
[i
] = list
;
515 fprintf(stderr
, "ran out of space in vm_config_groups");
519 int qemu_set_option(const char *str
)
521 char group
[64], id
[64], arg
[64];
526 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
527 if (rc
< 3 || str
[offset
] != '=') {
528 error_report("can't parse: \"%s\"", str
);
532 list
= qemu_find_opts(group
);
537 opts
= qemu_opts_find(list
, id
);
539 error_report("there is no %s \"%s\" defined",
544 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
550 int qemu_global_option(const char *str
)
552 char driver
[64], property
[64];
556 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
557 if (rc
< 2 || str
[offset
] != '=') {
558 error_report("can't parse: \"%s\"", str
);
562 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
563 qemu_opt_set(opts
, "driver", driver
);
564 qemu_opt_set(opts
, "property", property
);
565 qemu_opt_set(opts
, "value", str
+offset
+1);
569 struct ConfigWriteData
{
574 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
576 struct ConfigWriteData
*data
= opaque
;
578 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
582 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
584 struct ConfigWriteData
*data
= opaque
;
585 const char *id
= qemu_opts_id(opts
);
588 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
590 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
592 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
593 fprintf(data
->fp
, "\n");
597 void qemu_config_write(FILE *fp
)
599 struct ConfigWriteData data
= { .fp
= fp
};
600 QemuOptsList
**lists
= vm_config_groups
;
603 fprintf(fp
, "# qemu config file\n\n");
604 for (i
= 0; lists
[i
] != NULL
; i
++) {
605 data
.list
= lists
[i
];
606 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
610 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
612 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
614 QemuOptsList
*list
= NULL
;
615 QemuOpts
*opts
= NULL
;
616 int res
= -1, lno
= 0;
619 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
620 loc_set_file(fname
, ++lno
);
621 if (line
[0] == '\n') {
622 /* skip empty lines */
625 if (line
[0] == '#') {
629 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
631 list
= find_list(lists
, group
);
634 opts
= qemu_opts_create(list
, id
, 1);
637 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
638 /* group without id */
639 list
= find_list(lists
, group
);
642 opts
= qemu_opts_create(list
, NULL
, 0);
645 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
648 error_report("no group defined");
651 if (qemu_opt_set(opts
, arg
, value
) != 0) {
656 error_report("parse error");
660 error_report("error reading file");
669 int qemu_read_config_file(const char *filename
)
671 FILE *f
= fopen(filename
, "r");
678 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);