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 QemuOptsList qemu_boot_opts
= {
485 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
487 /* the three names below are not used now */
490 .type
= QEMU_OPT_STRING
,
493 .type
= QEMU_OPT_STRING
,
496 .type
= QEMU_OPT_STRING
,
497 /* following are really used */
500 .type
= QEMU_OPT_STRING
,
502 .name
= "splash-time",
503 .type
= QEMU_OPT_STRING
,
509 static QemuOptsList
*vm_config_groups
[32] = {
519 #ifdef CONFIG_SIMPLE_TRACE
522 &qemu_option_rom_opts
,
528 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
532 for (i
= 0; lists
[i
] != NULL
; i
++) {
533 if (strcmp(lists
[i
]->name
, group
) == 0)
536 if (lists
[i
] == NULL
) {
537 error_report("there is no option group \"%s\"", group
);
542 QemuOptsList
*qemu_find_opts(const char *group
)
544 return find_list(vm_config_groups
, group
);
547 void qemu_add_opts(QemuOptsList
*list
)
551 entries
= ARRAY_SIZE(vm_config_groups
);
552 entries
--; /* keep list NULL terminated */
553 for (i
= 0; i
< entries
; i
++) {
554 if (vm_config_groups
[i
] == NULL
) {
555 vm_config_groups
[i
] = list
;
559 fprintf(stderr
, "ran out of space in vm_config_groups");
563 int qemu_set_option(const char *str
)
565 char group
[64], id
[64], arg
[64];
570 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
571 if (rc
< 3 || str
[offset
] != '=') {
572 error_report("can't parse: \"%s\"", str
);
576 list
= qemu_find_opts(group
);
581 opts
= qemu_opts_find(list
, id
);
583 error_report("there is no %s \"%s\" defined",
588 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
594 int qemu_global_option(const char *str
)
596 char driver
[64], property
[64];
600 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
601 if (rc
< 2 || str
[offset
] != '=') {
602 error_report("can't parse: \"%s\"", str
);
606 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
607 qemu_opt_set(opts
, "driver", driver
);
608 qemu_opt_set(opts
, "property", property
);
609 qemu_opt_set(opts
, "value", str
+offset
+1);
613 struct ConfigWriteData
{
618 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
620 struct ConfigWriteData
*data
= opaque
;
622 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
626 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
628 struct ConfigWriteData
*data
= opaque
;
629 const char *id
= qemu_opts_id(opts
);
632 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
634 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
636 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
637 fprintf(data
->fp
, "\n");
641 void qemu_config_write(FILE *fp
)
643 struct ConfigWriteData data
= { .fp
= fp
};
644 QemuOptsList
**lists
= vm_config_groups
;
647 fprintf(fp
, "# qemu config file\n\n");
648 for (i
= 0; lists
[i
] != NULL
; i
++) {
649 data
.list
= lists
[i
];
650 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
654 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
656 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
658 QemuOptsList
*list
= NULL
;
659 QemuOpts
*opts
= NULL
;
660 int res
= -1, lno
= 0;
663 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
664 loc_set_file(fname
, ++lno
);
665 if (line
[0] == '\n') {
666 /* skip empty lines */
669 if (line
[0] == '#') {
673 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
675 list
= find_list(lists
, group
);
678 opts
= qemu_opts_create(list
, id
, 1);
681 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
682 /* group without id */
683 list
= find_list(lists
, group
);
686 opts
= qemu_opts_create(list
, NULL
, 0);
689 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
692 error_report("no group defined");
695 if (qemu_opt_set(opts
, arg
, value
) != 0) {
700 error_report("parse error");
704 error_report("error reading file");
713 int qemu_read_config_file(const char *filename
)
715 FILE *f
= fopen(filename
, "r");
722 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);