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",
93 static QemuOptsList qemu_chardev_opts
= {
95 .implied_opt_name
= "backend",
96 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
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_STRING
,
118 .type
= QEMU_OPT_NUMBER
,
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_BOOL
,
139 .type
= QEMU_OPT_NUMBER
,
142 .type
= QEMU_OPT_NUMBER
,
145 .type
= QEMU_OPT_NUMBER
,
148 .type
= QEMU_OPT_NUMBER
,
151 .type
= QEMU_OPT_BOOL
,
154 .type
= QEMU_OPT_BOOL
,
157 .type
= QEMU_OPT_STRING
,
160 .type
= QEMU_OPT_NUMBER
,
162 { /* end of list */ }
166 QemuOptsList qemu_fsdev_opts
= {
168 .implied_opt_name
= "fsdriver",
169 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
173 .type
= QEMU_OPT_STRING
,
176 .type
= QEMU_OPT_STRING
,
178 .name
= "security_model",
179 .type
= QEMU_OPT_STRING
,
182 .type
= QEMU_OPT_STRING
,
185 .type
= QEMU_OPT_BOOL
,
192 QemuOptsList qemu_virtfs_opts
= {
194 .implied_opt_name
= "fsdriver",
195 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
199 .type
= QEMU_OPT_STRING
,
202 .type
= QEMU_OPT_STRING
,
205 .type
= QEMU_OPT_STRING
,
207 .name
= "security_model",
208 .type
= QEMU_OPT_STRING
,
211 .type
= QEMU_OPT_STRING
,
214 .type
= QEMU_OPT_BOOL
,
221 static QemuOptsList qemu_device_opts
= {
223 .implied_opt_name
= "driver",
224 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
227 * no elements => accept any
228 * sanity checking will happen later
229 * when setting device properties
231 { /* end of list */ }
235 static QemuOptsList qemu_netdev_opts
= {
237 .implied_opt_name
= "type",
238 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
241 * no elements => accept any params
242 * validation will happen later
244 { /* end of list */ }
248 static QemuOptsList qemu_net_opts
= {
250 .implied_opt_name
= "type",
251 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
254 * no elements => accept any params
255 * validation will happen later
257 { /* end of list */ }
261 static QemuOptsList qemu_rtc_opts
= {
263 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
267 .type
= QEMU_OPT_STRING
,
270 .type
= QEMU_OPT_STRING
,
273 .type
= QEMU_OPT_STRING
,
275 { /* end of list */ }
279 static QemuOptsList qemu_global_opts
= {
281 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
285 .type
= QEMU_OPT_STRING
,
288 .type
= QEMU_OPT_STRING
,
291 .type
= QEMU_OPT_STRING
,
293 { /* end of list */ }
297 static QemuOptsList qemu_mon_opts
= {
299 .implied_opt_name
= "chardev",
300 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
304 .type
= QEMU_OPT_STRING
,
307 .type
= QEMU_OPT_STRING
,
310 .type
= QEMU_OPT_BOOL
,
313 .type
= QEMU_OPT_BOOL
,
315 { /* end of list */ }
319 static QemuOptsList qemu_trace_opts
= {
321 .implied_opt_name
= "trace",
322 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
326 .type
= QEMU_OPT_STRING
,
329 .type
= QEMU_OPT_STRING
,
331 { /* end of list */ }
335 static QemuOptsList qemu_cpudef_opts
= {
337 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
341 .type
= QEMU_OPT_STRING
,
344 .type
= QEMU_OPT_NUMBER
,
347 .type
= QEMU_OPT_STRING
,
350 .type
= QEMU_OPT_NUMBER
,
353 .type
= QEMU_OPT_NUMBER
,
356 .type
= QEMU_OPT_NUMBER
,
358 .name
= "feature_edx", /* cpuid 0000_0001.edx */
359 .type
= QEMU_OPT_STRING
,
361 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
362 .type
= QEMU_OPT_STRING
,
364 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
365 .type
= QEMU_OPT_STRING
,
367 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
368 .type
= QEMU_OPT_STRING
,
371 .type
= QEMU_OPT_NUMBER
,
374 .type
= QEMU_OPT_STRING
,
376 .name
= "vendor_override",
377 .type
= QEMU_OPT_NUMBER
,
379 { /* end of list */ }
383 QemuOptsList qemu_spice_opts
= {
385 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
389 .type
= QEMU_OPT_NUMBER
,
392 .type
= QEMU_OPT_NUMBER
,
395 .type
= QEMU_OPT_STRING
,
398 .type
= QEMU_OPT_BOOL
,
401 .type
= QEMU_OPT_BOOL
,
404 .type
= QEMU_OPT_STRING
,
406 .name
= "disable-ticketing",
407 .type
= QEMU_OPT_BOOL
,
409 .name
= "disable-copy-paste",
410 .type
= QEMU_OPT_BOOL
,
413 .type
= QEMU_OPT_BOOL
,
416 .type
= QEMU_OPT_STRING
,
418 .name
= "x509-key-file",
419 .type
= QEMU_OPT_STRING
,
421 .name
= "x509-key-password",
422 .type
= QEMU_OPT_STRING
,
424 .name
= "x509-cert-file",
425 .type
= QEMU_OPT_STRING
,
427 .name
= "x509-cacert-file",
428 .type
= QEMU_OPT_STRING
,
430 .name
= "x509-dh-key-file",
431 .type
= QEMU_OPT_STRING
,
433 .name
= "tls-ciphers",
434 .type
= QEMU_OPT_STRING
,
436 .name
= "tls-channel",
437 .type
= QEMU_OPT_STRING
,
439 .name
= "plaintext-channel",
440 .type
= QEMU_OPT_STRING
,
442 .name
= "image-compression",
443 .type
= QEMU_OPT_STRING
,
445 .name
= "jpeg-wan-compression",
446 .type
= QEMU_OPT_STRING
,
448 .name
= "zlib-glz-wan-compression",
449 .type
= QEMU_OPT_STRING
,
451 .name
= "streaming-video",
452 .type
= QEMU_OPT_STRING
,
454 .name
= "agent-mouse",
455 .type
= QEMU_OPT_BOOL
,
457 .name
= "playback-compression",
458 .type
= QEMU_OPT_BOOL
,
460 { /* end of list */ }
464 QemuOptsList qemu_option_rom_opts
= {
465 .name
= "option-rom",
466 .implied_opt_name
= "romfile",
467 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
471 .type
= QEMU_OPT_NUMBER
,
474 .type
= QEMU_OPT_STRING
,
476 { /* end of list */ }
480 static QemuOptsList qemu_machine_opts
= {
482 .implied_opt_name
= "type",
483 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
487 .type
= QEMU_OPT_STRING
,
488 .help
= "emulated machine"
491 .type
= QEMU_OPT_STRING
,
492 .help
= "accelerator list",
494 { /* End of list */ }
498 QemuOptsList qemu_boot_opts
= {
500 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
502 /* the three names below are not used now */
505 .type
= QEMU_OPT_STRING
,
508 .type
= QEMU_OPT_STRING
,
511 .type
= QEMU_OPT_STRING
,
512 /* following are really used */
515 .type
= QEMU_OPT_STRING
,
517 .name
= "splash-time",
518 .type
= QEMU_OPT_STRING
,
524 static QemuOptsList
*vm_config_groups
[32] = {
535 &qemu_option_rom_opts
,
541 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
545 for (i
= 0; lists
[i
] != NULL
; i
++) {
546 if (strcmp(lists
[i
]->name
, group
) == 0)
549 if (lists
[i
] == NULL
) {
550 error_report("there is no option group \"%s\"", group
);
555 QemuOptsList
*qemu_find_opts(const char *group
)
557 return find_list(vm_config_groups
, group
);
560 void qemu_add_opts(QemuOptsList
*list
)
564 entries
= ARRAY_SIZE(vm_config_groups
);
565 entries
--; /* keep list NULL terminated */
566 for (i
= 0; i
< entries
; i
++) {
567 if (vm_config_groups
[i
] == NULL
) {
568 vm_config_groups
[i
] = list
;
572 fprintf(stderr
, "ran out of space in vm_config_groups");
576 int qemu_set_option(const char *str
)
578 char group
[64], id
[64], arg
[64];
583 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
584 if (rc
< 3 || str
[offset
] != '=') {
585 error_report("can't parse: \"%s\"", str
);
589 list
= qemu_find_opts(group
);
594 opts
= qemu_opts_find(list
, id
);
596 error_report("there is no %s \"%s\" defined",
601 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
607 int qemu_global_option(const char *str
)
609 char driver
[64], property
[64];
613 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
614 if (rc
< 2 || str
[offset
] != '=') {
615 error_report("can't parse: \"%s\"", str
);
619 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
620 qemu_opt_set(opts
, "driver", driver
);
621 qemu_opt_set(opts
, "property", property
);
622 qemu_opt_set(opts
, "value", str
+offset
+1);
626 struct ConfigWriteData
{
631 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
633 struct ConfigWriteData
*data
= opaque
;
635 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
639 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
641 struct ConfigWriteData
*data
= opaque
;
642 const char *id
= qemu_opts_id(opts
);
645 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
647 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
649 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
650 fprintf(data
->fp
, "\n");
654 void qemu_config_write(FILE *fp
)
656 struct ConfigWriteData data
= { .fp
= fp
};
657 QemuOptsList
**lists
= vm_config_groups
;
660 fprintf(fp
, "# qemu config file\n\n");
661 for (i
= 0; lists
[i
] != NULL
; i
++) {
662 data
.list
= lists
[i
];
663 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
667 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
669 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
671 QemuOptsList
*list
= NULL
;
672 QemuOpts
*opts
= NULL
;
673 int res
= -1, lno
= 0;
676 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
677 loc_set_file(fname
, ++lno
);
678 if (line
[0] == '\n') {
679 /* skip empty lines */
682 if (line
[0] == '#') {
686 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
688 list
= find_list(lists
, group
);
691 opts
= qemu_opts_create(list
, id
, 1);
694 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
695 /* group without id */
696 list
= find_list(lists
, group
);
699 opts
= qemu_opts_create(list
, NULL
, 0);
702 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
705 error_report("no group defined");
708 if (qemu_opt_set(opts
, arg
, value
) != 0) {
713 error_report("parse error");
717 error_report("error reading file");
726 int qemu_read_config_file(const char *filename
)
728 FILE *f
= fopen(filename
, "r");
735 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);