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
,
188 QemuOptsList qemu_virtfs_opts
= {
190 .implied_opt_name
= "fsdriver",
191 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
195 .type
= QEMU_OPT_STRING
,
198 .type
= QEMU_OPT_STRING
,
201 .type
= QEMU_OPT_STRING
,
203 .name
= "security_model",
204 .type
= QEMU_OPT_STRING
,
207 .type
= QEMU_OPT_STRING
,
214 static QemuOptsList qemu_device_opts
= {
216 .implied_opt_name
= "driver",
217 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
220 * no elements => accept any
221 * sanity checking will happen later
222 * when setting device properties
224 { /* end of list */ }
228 static QemuOptsList qemu_netdev_opts
= {
230 .implied_opt_name
= "type",
231 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
234 * no elements => accept any params
235 * validation will happen later
237 { /* end of list */ }
241 static QemuOptsList qemu_net_opts
= {
243 .implied_opt_name
= "type",
244 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
247 * no elements => accept any params
248 * validation will happen later
250 { /* end of list */ }
254 static QemuOptsList qemu_rtc_opts
= {
256 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
260 .type
= QEMU_OPT_STRING
,
263 .type
= QEMU_OPT_STRING
,
266 .type
= QEMU_OPT_STRING
,
268 { /* end of list */ }
272 static QemuOptsList qemu_global_opts
= {
274 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
278 .type
= QEMU_OPT_STRING
,
281 .type
= QEMU_OPT_STRING
,
284 .type
= QEMU_OPT_STRING
,
286 { /* end of list */ }
290 static QemuOptsList qemu_mon_opts
= {
292 .implied_opt_name
= "chardev",
293 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
297 .type
= QEMU_OPT_STRING
,
300 .type
= QEMU_OPT_STRING
,
303 .type
= QEMU_OPT_BOOL
,
306 .type
= QEMU_OPT_BOOL
,
308 { /* end of list */ }
312 static QemuOptsList qemu_trace_opts
= {
314 .implied_opt_name
= "trace",
315 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
319 .type
= QEMU_OPT_STRING
,
322 .type
= QEMU_OPT_STRING
,
324 { /* end of list */ }
328 static QemuOptsList qemu_cpudef_opts
= {
330 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
334 .type
= QEMU_OPT_STRING
,
337 .type
= QEMU_OPT_NUMBER
,
340 .type
= QEMU_OPT_STRING
,
343 .type
= QEMU_OPT_NUMBER
,
346 .type
= QEMU_OPT_NUMBER
,
349 .type
= QEMU_OPT_NUMBER
,
351 .name
= "feature_edx", /* cpuid 0000_0001.edx */
352 .type
= QEMU_OPT_STRING
,
354 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
355 .type
= QEMU_OPT_STRING
,
357 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
358 .type
= QEMU_OPT_STRING
,
360 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
361 .type
= QEMU_OPT_STRING
,
364 .type
= QEMU_OPT_NUMBER
,
367 .type
= QEMU_OPT_STRING
,
369 .name
= "vendor_override",
370 .type
= QEMU_OPT_NUMBER
,
372 { /* end of list */ }
376 QemuOptsList qemu_spice_opts
= {
378 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
382 .type
= QEMU_OPT_NUMBER
,
385 .type
= QEMU_OPT_NUMBER
,
388 .type
= QEMU_OPT_STRING
,
391 .type
= QEMU_OPT_BOOL
,
394 .type
= QEMU_OPT_BOOL
,
397 .type
= QEMU_OPT_STRING
,
399 .name
= "disable-ticketing",
400 .type
= QEMU_OPT_BOOL
,
402 .name
= "disable-copy-paste",
403 .type
= QEMU_OPT_BOOL
,
406 .type
= QEMU_OPT_BOOL
,
409 .type
= QEMU_OPT_STRING
,
411 .name
= "x509-key-file",
412 .type
= QEMU_OPT_STRING
,
414 .name
= "x509-key-password",
415 .type
= QEMU_OPT_STRING
,
417 .name
= "x509-cert-file",
418 .type
= QEMU_OPT_STRING
,
420 .name
= "x509-cacert-file",
421 .type
= QEMU_OPT_STRING
,
423 .name
= "x509-dh-key-file",
424 .type
= QEMU_OPT_STRING
,
426 .name
= "tls-ciphers",
427 .type
= QEMU_OPT_STRING
,
429 .name
= "tls-channel",
430 .type
= QEMU_OPT_STRING
,
432 .name
= "plaintext-channel",
433 .type
= QEMU_OPT_STRING
,
435 .name
= "image-compression",
436 .type
= QEMU_OPT_STRING
,
438 .name
= "jpeg-wan-compression",
439 .type
= QEMU_OPT_STRING
,
441 .name
= "zlib-glz-wan-compression",
442 .type
= QEMU_OPT_STRING
,
444 .name
= "streaming-video",
445 .type
= QEMU_OPT_STRING
,
447 .name
= "agent-mouse",
448 .type
= QEMU_OPT_BOOL
,
450 .name
= "playback-compression",
451 .type
= QEMU_OPT_BOOL
,
453 { /* end of list */ }
457 QemuOptsList qemu_option_rom_opts
= {
458 .name
= "option-rom",
459 .implied_opt_name
= "romfile",
460 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
464 .type
= QEMU_OPT_NUMBER
,
467 .type
= QEMU_OPT_STRING
,
469 { /* end of list */ }
473 static QemuOptsList qemu_machine_opts
= {
475 .implied_opt_name
= "type",
476 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
480 .type
= QEMU_OPT_STRING
,
481 .help
= "emulated machine"
484 .type
= QEMU_OPT_STRING
,
485 .help
= "accelerator list",
487 { /* End of list */ }
491 QemuOptsList qemu_boot_opts
= {
493 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
495 /* the three names below are not used now */
498 .type
= QEMU_OPT_STRING
,
501 .type
= QEMU_OPT_STRING
,
504 .type
= QEMU_OPT_STRING
,
505 /* following are really used */
508 .type
= QEMU_OPT_STRING
,
510 .name
= "splash-time",
511 .type
= QEMU_OPT_STRING
,
517 static QemuOptsList
*vm_config_groups
[32] = {
528 &qemu_option_rom_opts
,
534 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
538 for (i
= 0; lists
[i
] != NULL
; i
++) {
539 if (strcmp(lists
[i
]->name
, group
) == 0)
542 if (lists
[i
] == NULL
) {
543 error_report("there is no option group \"%s\"", group
);
548 QemuOptsList
*qemu_find_opts(const char *group
)
550 return find_list(vm_config_groups
, group
);
553 void qemu_add_opts(QemuOptsList
*list
)
557 entries
= ARRAY_SIZE(vm_config_groups
);
558 entries
--; /* keep list NULL terminated */
559 for (i
= 0; i
< entries
; i
++) {
560 if (vm_config_groups
[i
] == NULL
) {
561 vm_config_groups
[i
] = list
;
565 fprintf(stderr
, "ran out of space in vm_config_groups");
569 int qemu_set_option(const char *str
)
571 char group
[64], id
[64], arg
[64];
576 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
577 if (rc
< 3 || str
[offset
] != '=') {
578 error_report("can't parse: \"%s\"", str
);
582 list
= qemu_find_opts(group
);
587 opts
= qemu_opts_find(list
, id
);
589 error_report("there is no %s \"%s\" defined",
594 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
600 int qemu_global_option(const char *str
)
602 char driver
[64], property
[64];
606 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
607 if (rc
< 2 || str
[offset
] != '=') {
608 error_report("can't parse: \"%s\"", str
);
612 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
613 qemu_opt_set(opts
, "driver", driver
);
614 qemu_opt_set(opts
, "property", property
);
615 qemu_opt_set(opts
, "value", str
+offset
+1);
619 struct ConfigWriteData
{
624 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
626 struct ConfigWriteData
*data
= opaque
;
628 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
632 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
634 struct ConfigWriteData
*data
= opaque
;
635 const char *id
= qemu_opts_id(opts
);
638 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
640 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
642 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
643 fprintf(data
->fp
, "\n");
647 void qemu_config_write(FILE *fp
)
649 struct ConfigWriteData data
= { .fp
= fp
};
650 QemuOptsList
**lists
= vm_config_groups
;
653 fprintf(fp
, "# qemu config file\n\n");
654 for (i
= 0; lists
[i
] != NULL
; i
++) {
655 data
.list
= lists
[i
];
656 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
660 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
662 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
664 QemuOptsList
*list
= NULL
;
665 QemuOpts
*opts
= NULL
;
666 int res
= -1, lno
= 0;
669 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
670 loc_set_file(fname
, ++lno
);
671 if (line
[0] == '\n') {
672 /* skip empty lines */
675 if (line
[0] == '#') {
679 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
681 list
= find_list(lists
, group
);
684 opts
= qemu_opts_create(list
, id
, 1);
687 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
688 /* group without id */
689 list
= find_list(lists
, group
);
692 opts
= qemu_opts_create(list
, NULL
, 0);
695 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
698 error_report("no group defined");
701 if (qemu_opt_set(opts
, arg
, value
) != 0) {
706 error_report("parse error");
710 error_report("error reading file");
719 int qemu_read_config_file(const char *filename
)
721 FILE *f
= fopen(filename
, "r");
728 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);