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 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
471 .type
= QEMU_OPT_STRING
,
472 .help
= "accelerator list",
474 { /* End of list */ }
478 static QemuOptsList
*vm_config_groups
[32] = {
488 #ifdef CONFIG_SIMPLE_TRACE
491 &qemu_option_rom_opts
,
496 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
500 for (i
= 0; lists
[i
] != NULL
; i
++) {
501 if (strcmp(lists
[i
]->name
, group
) == 0)
504 if (lists
[i
] == NULL
) {
505 error_report("there is no option group \"%s\"", group
);
510 QemuOptsList
*qemu_find_opts(const char *group
)
512 return find_list(vm_config_groups
, group
);
515 void qemu_add_opts(QemuOptsList
*list
)
519 entries
= ARRAY_SIZE(vm_config_groups
);
520 entries
--; /* keep list NULL terminated */
521 for (i
= 0; i
< entries
; i
++) {
522 if (vm_config_groups
[i
] == NULL
) {
523 vm_config_groups
[i
] = list
;
527 fprintf(stderr
, "ran out of space in vm_config_groups");
531 int qemu_set_option(const char *str
)
533 char group
[64], id
[64], arg
[64];
538 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
539 if (rc
< 3 || str
[offset
] != '=') {
540 error_report("can't parse: \"%s\"", str
);
544 list
= qemu_find_opts(group
);
549 opts
= qemu_opts_find(list
, id
);
551 error_report("there is no %s \"%s\" defined",
556 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
562 int qemu_global_option(const char *str
)
564 char driver
[64], property
[64];
568 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
569 if (rc
< 2 || str
[offset
] != '=') {
570 error_report("can't parse: \"%s\"", str
);
574 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
575 qemu_opt_set(opts
, "driver", driver
);
576 qemu_opt_set(opts
, "property", property
);
577 qemu_opt_set(opts
, "value", str
+offset
+1);
581 struct ConfigWriteData
{
586 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
588 struct ConfigWriteData
*data
= opaque
;
590 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
594 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
596 struct ConfigWriteData
*data
= opaque
;
597 const char *id
= qemu_opts_id(opts
);
600 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
602 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
604 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
605 fprintf(data
->fp
, "\n");
609 void qemu_config_write(FILE *fp
)
611 struct ConfigWriteData data
= { .fp
= fp
};
612 QemuOptsList
**lists
= vm_config_groups
;
615 fprintf(fp
, "# qemu config file\n\n");
616 for (i
= 0; lists
[i
] != NULL
; i
++) {
617 data
.list
= lists
[i
];
618 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
622 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
624 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
626 QemuOptsList
*list
= NULL
;
627 QemuOpts
*opts
= NULL
;
628 int res
= -1, lno
= 0;
631 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
632 loc_set_file(fname
, ++lno
);
633 if (line
[0] == '\n') {
634 /* skip empty lines */
637 if (line
[0] == '#') {
641 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
643 list
= find_list(lists
, group
);
646 opts
= qemu_opts_create(list
, id
, 1);
649 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
650 /* group without id */
651 list
= find_list(lists
, group
);
654 opts
= qemu_opts_create(list
, NULL
, 0);
657 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
660 error_report("no group defined");
663 if (qemu_opt_set(opts
, arg
, value
) != 0) {
668 error_report("parse error");
672 error_report("error reading file");
681 int qemu_read_config_file(const char *filename
)
683 FILE *f
= fopen(filename
, "r");
690 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);