1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
8 static QemuOptsList qemu_drive_opts
= {
10 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
14 .type
= QEMU_OPT_NUMBER
,
18 .type
= QEMU_OPT_NUMBER
,
19 .help
= "unit number (i.e. lun for scsi)",
22 .type
= QEMU_OPT_STRING
,
23 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
26 .type
= QEMU_OPT_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
,
52 .type
= QEMU_OPT_STRING
,
56 .type
= QEMU_OPT_STRING
,
57 .help
= "host cache usage (none, writeback, writethrough, unsafe)",
60 .type
= QEMU_OPT_STRING
,
61 .help
= "host AIO implementation (threads, native)",
64 .type
= QEMU_OPT_STRING
,
65 .help
= "disk format (raw, qcow2, ...)",
68 .type
= QEMU_OPT_STRING
,
71 .type
= QEMU_OPT_STRING
,
74 .type
= QEMU_OPT_STRING
,
77 .type
= QEMU_OPT_STRING
,
78 .help
= "pci address (virtio only)",
81 .type
= QEMU_OPT_BOOL
,
87 static QemuOptsList qemu_chardev_opts
= {
89 .implied_opt_name
= "backend",
90 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
94 .type
= QEMU_OPT_STRING
,
97 .type
= QEMU_OPT_STRING
,
100 .type
= QEMU_OPT_STRING
,
103 .type
= QEMU_OPT_STRING
,
106 .type
= QEMU_OPT_STRING
,
109 .type
= QEMU_OPT_STRING
,
112 .type
= QEMU_OPT_NUMBER
,
115 .type
= QEMU_OPT_BOOL
,
118 .type
= QEMU_OPT_BOOL
,
121 .type
= QEMU_OPT_BOOL
,
124 .type
= QEMU_OPT_BOOL
,
127 .type
= QEMU_OPT_BOOL
,
130 .type
= QEMU_OPT_BOOL
,
133 .type
= QEMU_OPT_NUMBER
,
136 .type
= QEMU_OPT_NUMBER
,
139 .type
= QEMU_OPT_NUMBER
,
142 .type
= QEMU_OPT_NUMBER
,
145 .type
= QEMU_OPT_BOOL
,
148 .type
= QEMU_OPT_BOOL
,
151 .type
= QEMU_OPT_STRING
,
154 .type
= QEMU_OPT_NUMBER
,
156 { /* end of list */ }
160 QemuOptsList qemu_fsdev_opts
= {
162 .implied_opt_name
= "fstype",
163 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
167 .type
= QEMU_OPT_STRING
,
170 .type
= QEMU_OPT_STRING
,
172 .name
= "security_model",
173 .type
= QEMU_OPT_STRING
,
179 QemuOptsList qemu_virtfs_opts
= {
181 .implied_opt_name
= "fstype",
182 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
186 .type
= QEMU_OPT_STRING
,
189 .type
= QEMU_OPT_STRING
,
192 .type
= QEMU_OPT_STRING
,
194 .name
= "security_model",
195 .type
= QEMU_OPT_STRING
,
202 static QemuOptsList qemu_device_opts
= {
204 .implied_opt_name
= "driver",
205 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
208 * no elements => accept any
209 * sanity checking will happen later
210 * when setting device properties
212 { /* end of list */ }
216 static QemuOptsList qemu_netdev_opts
= {
218 .implied_opt_name
= "type",
219 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
222 * no elements => accept any params
223 * validation will happen later
225 { /* end of list */ }
229 static QemuOptsList qemu_net_opts
= {
231 .implied_opt_name
= "type",
232 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
235 * no elements => accept any params
236 * validation will happen later
238 { /* end of list */ }
242 static QemuOptsList qemu_rtc_opts
= {
244 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
248 .type
= QEMU_OPT_STRING
,
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_STRING
,
256 { /* end of list */ }
260 static QemuOptsList qemu_global_opts
= {
262 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
266 .type
= QEMU_OPT_STRING
,
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_STRING
,
274 { /* end of list */ }
278 static QemuOptsList qemu_mon_opts
= {
280 .implied_opt_name
= "chardev",
281 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
285 .type
= QEMU_OPT_STRING
,
288 .type
= QEMU_OPT_STRING
,
291 .type
= QEMU_OPT_BOOL
,
294 .type
= QEMU_OPT_BOOL
,
296 { /* end of list */ }
300 #ifdef CONFIG_SIMPLE_TRACE
301 static QemuOptsList qemu_trace_opts
= {
303 .implied_opt_name
= "trace",
304 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
308 .type
= QEMU_OPT_STRING
,
310 { /* end if list */ }
315 static QemuOptsList qemu_cpudef_opts
= {
317 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
321 .type
= QEMU_OPT_STRING
,
324 .type
= QEMU_OPT_NUMBER
,
327 .type
= QEMU_OPT_STRING
,
330 .type
= QEMU_OPT_NUMBER
,
333 .type
= QEMU_OPT_NUMBER
,
336 .type
= QEMU_OPT_NUMBER
,
338 .name
= "feature_edx", /* cpuid 0000_0001.edx */
339 .type
= QEMU_OPT_STRING
,
341 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
342 .type
= QEMU_OPT_STRING
,
344 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
345 .type
= QEMU_OPT_STRING
,
347 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
348 .type
= QEMU_OPT_STRING
,
351 .type
= QEMU_OPT_NUMBER
,
354 .type
= QEMU_OPT_STRING
,
356 .name
= "vendor_override",
357 .type
= QEMU_OPT_NUMBER
,
359 { /* end of list */ }
363 QemuOptsList qemu_spice_opts
= {
365 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
369 .type
= QEMU_OPT_NUMBER
,
372 .type
= QEMU_OPT_NUMBER
,
375 .type
= QEMU_OPT_STRING
,
378 .type
= QEMU_OPT_BOOL
,
381 .type
= QEMU_OPT_BOOL
,
384 .type
= QEMU_OPT_STRING
,
386 .name
= "disable-ticketing",
387 .type
= QEMU_OPT_BOOL
,
390 .type
= QEMU_OPT_STRING
,
392 .name
= "x509-key-file",
393 .type
= QEMU_OPT_STRING
,
395 .name
= "x509-key-password",
396 .type
= QEMU_OPT_STRING
,
398 .name
= "x509-cert-file",
399 .type
= QEMU_OPT_STRING
,
401 .name
= "x509-cacert-file",
402 .type
= QEMU_OPT_STRING
,
404 .name
= "x509-dh-key-file",
405 .type
= QEMU_OPT_STRING
,
407 .name
= "tls-ciphers",
408 .type
= QEMU_OPT_STRING
,
410 .name
= "tls-channel",
411 .type
= QEMU_OPT_STRING
,
413 .name
= "plaintext-channel",
414 .type
= QEMU_OPT_STRING
,
416 .name
= "image-compression",
417 .type
= QEMU_OPT_STRING
,
419 .name
= "jpeg-wan-compression",
420 .type
= QEMU_OPT_STRING
,
422 .name
= "zlib-glz-wan-compression",
423 .type
= QEMU_OPT_STRING
,
425 .name
= "streaming-video",
426 .type
= QEMU_OPT_STRING
,
428 .name
= "agent-mouse",
429 .type
= QEMU_OPT_BOOL
,
431 .name
= "playback-compression",
432 .type
= QEMU_OPT_BOOL
,
434 { /* end if list */ }
438 QemuOptsList qemu_option_rom_opts
= {
439 .name
= "option-rom",
440 .implied_opt_name
= "romfile",
441 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
445 .type
= QEMU_OPT_NUMBER
,
448 .type
= QEMU_OPT_STRING
,
450 { /* end if list */ }
454 static QemuOptsList
*vm_config_groups
[32] = {
464 #ifdef CONFIG_SIMPLE_TRACE
467 &qemu_option_rom_opts
,
471 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
475 for (i
= 0; lists
[i
] != NULL
; i
++) {
476 if (strcmp(lists
[i
]->name
, group
) == 0)
479 if (lists
[i
] == NULL
) {
480 error_report("there is no option group \"%s\"", group
);
485 QemuOptsList
*qemu_find_opts(const char *group
)
487 return find_list(vm_config_groups
, group
);
490 void qemu_add_opts(QemuOptsList
*list
)
494 entries
= ARRAY_SIZE(vm_config_groups
);
495 entries
--; /* keep list NULL terminated */
496 for (i
= 0; i
< entries
; i
++) {
497 if (vm_config_groups
[i
] == NULL
) {
498 vm_config_groups
[i
] = list
;
502 fprintf(stderr
, "ran out of space in vm_config_groups");
506 int qemu_set_option(const char *str
)
508 char group
[64], id
[64], arg
[64];
513 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
514 if (rc
< 3 || str
[offset
] != '=') {
515 error_report("can't parse: \"%s\"", str
);
519 list
= qemu_find_opts(group
);
524 opts
= qemu_opts_find(list
, id
);
526 error_report("there is no %s \"%s\" defined",
531 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
537 int qemu_global_option(const char *str
)
539 char driver
[64], property
[64];
543 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
544 if (rc
< 2 || str
[offset
] != '=') {
545 error_report("can't parse: \"%s\"", str
);
549 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
550 qemu_opt_set(opts
, "driver", driver
);
551 qemu_opt_set(opts
, "property", property
);
552 qemu_opt_set(opts
, "value", str
+offset
+1);
556 struct ConfigWriteData
{
561 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
563 struct ConfigWriteData
*data
= opaque
;
565 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
569 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
571 struct ConfigWriteData
*data
= opaque
;
572 const char *id
= qemu_opts_id(opts
);
575 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
577 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
579 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
580 fprintf(data
->fp
, "\n");
584 void qemu_config_write(FILE *fp
)
586 struct ConfigWriteData data
= { .fp
= fp
};
587 QemuOptsList
**lists
= vm_config_groups
;
590 fprintf(fp
, "# qemu config file\n\n");
591 for (i
= 0; lists
[i
] != NULL
; i
++) {
592 data
.list
= lists
[i
];
593 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
597 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
599 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
601 QemuOptsList
*list
= NULL
;
602 QemuOpts
*opts
= NULL
;
603 int res
= -1, lno
= 0;
606 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
607 loc_set_file(fname
, ++lno
);
608 if (line
[0] == '\n') {
609 /* skip empty lines */
612 if (line
[0] == '#') {
616 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
618 list
= find_list(lists
, group
);
621 opts
= qemu_opts_create(list
, id
, 1);
624 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
625 /* group without id */
626 list
= find_list(lists
, group
);
629 opts
= qemu_opts_create(list
, NULL
, 0);
632 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
635 error_report("no group defined");
638 if (qemu_opt_set(opts
, arg
, value
) != 0) {
643 error_report("parse error");
647 error_report("error reading file");
656 int qemu_read_config_file(const char *filename
)
658 FILE *f
= fopen(filename
, "r");
665 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);