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",
90 .type
= QEMU_OPT_NUMBER
,
91 .help
= "limit total I/O operations per second",
94 .type
= QEMU_OPT_NUMBER
,
95 .help
= "limit read operations per second",
98 .type
= QEMU_OPT_NUMBER
,
99 .help
= "limit write operations per second",
102 .type
= QEMU_OPT_NUMBER
,
103 .help
= "limit total bytes per second",
106 .type
= QEMU_OPT_NUMBER
,
107 .help
= "limit read bytes per second",
110 .type
= QEMU_OPT_NUMBER
,
111 .help
= "limit write bytes per second",
113 .name
= "copy-on-read",
114 .type
= QEMU_OPT_BOOL
,
115 .help
= "copy read data from backing file into image file",
117 { /* end of list */ }
121 static QemuOptsList qemu_chardev_opts
= {
123 .implied_opt_name
= "backend",
124 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
128 .type
= QEMU_OPT_STRING
,
131 .type
= QEMU_OPT_STRING
,
134 .type
= QEMU_OPT_STRING
,
137 .type
= QEMU_OPT_STRING
,
140 .type
= QEMU_OPT_STRING
,
143 .type
= QEMU_OPT_STRING
,
146 .type
= QEMU_OPT_NUMBER
,
149 .type
= QEMU_OPT_BOOL
,
152 .type
= QEMU_OPT_BOOL
,
155 .type
= QEMU_OPT_BOOL
,
158 .type
= QEMU_OPT_BOOL
,
161 .type
= QEMU_OPT_BOOL
,
164 .type
= QEMU_OPT_BOOL
,
167 .type
= QEMU_OPT_NUMBER
,
170 .type
= QEMU_OPT_NUMBER
,
173 .type
= QEMU_OPT_NUMBER
,
176 .type
= QEMU_OPT_NUMBER
,
179 .type
= QEMU_OPT_BOOL
,
182 .type
= QEMU_OPT_BOOL
,
185 .type
= QEMU_OPT_STRING
,
188 .type
= QEMU_OPT_NUMBER
,
190 { /* end of list */ }
194 QemuOptsList qemu_fsdev_opts
= {
196 .implied_opt_name
= "fsdriver",
197 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
201 .type
= QEMU_OPT_STRING
,
204 .type
= QEMU_OPT_STRING
,
206 .name
= "security_model",
207 .type
= QEMU_OPT_STRING
,
210 .type
= QEMU_OPT_STRING
,
213 .type
= QEMU_OPT_BOOL
,
220 QemuOptsList qemu_virtfs_opts
= {
222 .implied_opt_name
= "fsdriver",
223 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
227 .type
= QEMU_OPT_STRING
,
230 .type
= QEMU_OPT_STRING
,
233 .type
= QEMU_OPT_STRING
,
235 .name
= "security_model",
236 .type
= QEMU_OPT_STRING
,
239 .type
= QEMU_OPT_STRING
,
242 .type
= QEMU_OPT_BOOL
,
249 static QemuOptsList qemu_device_opts
= {
251 .implied_opt_name
= "driver",
252 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
255 * no elements => accept any
256 * sanity checking will happen later
257 * when setting device properties
259 { /* end of list */ }
263 static QemuOptsList qemu_netdev_opts
= {
265 .implied_opt_name
= "type",
266 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
269 * no elements => accept any params
270 * validation will happen later
272 { /* end of list */ }
276 static QemuOptsList qemu_net_opts
= {
278 .implied_opt_name
= "type",
279 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
282 * no elements => accept any params
283 * validation will happen later
285 { /* end of list */ }
289 static QemuOptsList qemu_rtc_opts
= {
291 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
295 .type
= QEMU_OPT_STRING
,
298 .type
= QEMU_OPT_STRING
,
301 .type
= QEMU_OPT_STRING
,
303 { /* end of list */ }
307 static QemuOptsList qemu_global_opts
= {
309 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
313 .type
= QEMU_OPT_STRING
,
316 .type
= QEMU_OPT_STRING
,
319 .type
= QEMU_OPT_STRING
,
321 { /* end of list */ }
325 static QemuOptsList qemu_mon_opts
= {
327 .implied_opt_name
= "chardev",
328 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
332 .type
= QEMU_OPT_STRING
,
335 .type
= QEMU_OPT_STRING
,
338 .type
= QEMU_OPT_BOOL
,
341 .type
= QEMU_OPT_BOOL
,
343 { /* end of list */ }
347 static QemuOptsList qemu_trace_opts
= {
349 .implied_opt_name
= "trace",
350 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
354 .type
= QEMU_OPT_STRING
,
357 .type
= QEMU_OPT_STRING
,
359 { /* end of list */ }
363 static QemuOptsList qemu_cpudef_opts
= {
365 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
369 .type
= QEMU_OPT_STRING
,
372 .type
= QEMU_OPT_NUMBER
,
375 .type
= QEMU_OPT_STRING
,
378 .type
= QEMU_OPT_NUMBER
,
381 .type
= QEMU_OPT_NUMBER
,
384 .type
= QEMU_OPT_NUMBER
,
386 .name
= "feature_edx", /* cpuid 0000_0001.edx */
387 .type
= QEMU_OPT_STRING
,
389 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
390 .type
= QEMU_OPT_STRING
,
392 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
393 .type
= QEMU_OPT_STRING
,
395 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
396 .type
= QEMU_OPT_STRING
,
399 .type
= QEMU_OPT_NUMBER
,
402 .type
= QEMU_OPT_STRING
,
404 .name
= "vendor_override",
405 .type
= QEMU_OPT_NUMBER
,
407 { /* end of list */ }
411 QemuOptsList qemu_spice_opts
= {
413 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
417 .type
= QEMU_OPT_NUMBER
,
420 .type
= QEMU_OPT_NUMBER
,
423 .type
= QEMU_OPT_STRING
,
426 .type
= QEMU_OPT_BOOL
,
429 .type
= QEMU_OPT_BOOL
,
432 .type
= QEMU_OPT_STRING
,
434 .name
= "disable-ticketing",
435 .type
= QEMU_OPT_BOOL
,
437 .name
= "disable-copy-paste",
438 .type
= QEMU_OPT_BOOL
,
441 .type
= QEMU_OPT_BOOL
,
444 .type
= QEMU_OPT_STRING
,
446 .name
= "x509-key-file",
447 .type
= QEMU_OPT_STRING
,
449 .name
= "x509-key-password",
450 .type
= QEMU_OPT_STRING
,
452 .name
= "x509-cert-file",
453 .type
= QEMU_OPT_STRING
,
455 .name
= "x509-cacert-file",
456 .type
= QEMU_OPT_STRING
,
458 .name
= "x509-dh-key-file",
459 .type
= QEMU_OPT_STRING
,
461 .name
= "tls-ciphers",
462 .type
= QEMU_OPT_STRING
,
464 .name
= "tls-channel",
465 .type
= QEMU_OPT_STRING
,
467 .name
= "plaintext-channel",
468 .type
= QEMU_OPT_STRING
,
470 .name
= "image-compression",
471 .type
= QEMU_OPT_STRING
,
473 .name
= "jpeg-wan-compression",
474 .type
= QEMU_OPT_STRING
,
476 .name
= "zlib-glz-wan-compression",
477 .type
= QEMU_OPT_STRING
,
479 .name
= "streaming-video",
480 .type
= QEMU_OPT_STRING
,
482 .name
= "agent-mouse",
483 .type
= QEMU_OPT_BOOL
,
485 .name
= "playback-compression",
486 .type
= QEMU_OPT_BOOL
,
488 { /* end of list */ }
492 QemuOptsList qemu_option_rom_opts
= {
493 .name
= "option-rom",
494 .implied_opt_name
= "romfile",
495 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
499 .type
= QEMU_OPT_NUMBER
,
502 .type
= QEMU_OPT_STRING
,
504 { /* end of list */ }
508 static QemuOptsList qemu_machine_opts
= {
510 .implied_opt_name
= "type",
511 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
515 .type
= QEMU_OPT_STRING
,
516 .help
= "emulated machine"
519 .type
= QEMU_OPT_STRING
,
520 .help
= "accelerator list",
522 { /* End of list */ }
526 QemuOptsList qemu_boot_opts
= {
528 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
530 /* the three names below are not used now */
533 .type
= QEMU_OPT_STRING
,
536 .type
= QEMU_OPT_STRING
,
539 .type
= QEMU_OPT_STRING
,
540 /* following are really used */
543 .type
= QEMU_OPT_STRING
,
545 .name
= "splash-time",
546 .type
= QEMU_OPT_STRING
,
552 static QemuOptsList
*vm_config_groups
[32] = {
563 &qemu_option_rom_opts
,
569 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
573 for (i
= 0; lists
[i
] != NULL
; i
++) {
574 if (strcmp(lists
[i
]->name
, group
) == 0)
577 if (lists
[i
] == NULL
) {
578 error_report("there is no option group \"%s\"", group
);
583 QemuOptsList
*qemu_find_opts(const char *group
)
585 return find_list(vm_config_groups
, group
);
588 void qemu_add_opts(QemuOptsList
*list
)
592 entries
= ARRAY_SIZE(vm_config_groups
);
593 entries
--; /* keep list NULL terminated */
594 for (i
= 0; i
< entries
; i
++) {
595 if (vm_config_groups
[i
] == NULL
) {
596 vm_config_groups
[i
] = list
;
600 fprintf(stderr
, "ran out of space in vm_config_groups");
604 int qemu_set_option(const char *str
)
606 char group
[64], id
[64], arg
[64];
611 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
612 if (rc
< 3 || str
[offset
] != '=') {
613 error_report("can't parse: \"%s\"", str
);
617 list
= qemu_find_opts(group
);
622 opts
= qemu_opts_find(list
, id
);
624 error_report("there is no %s \"%s\" defined",
629 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
635 int qemu_global_option(const char *str
)
637 char driver
[64], property
[64];
641 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
642 if (rc
< 2 || str
[offset
] != '=') {
643 error_report("can't parse: \"%s\"", str
);
647 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
648 qemu_opt_set(opts
, "driver", driver
);
649 qemu_opt_set(opts
, "property", property
);
650 qemu_opt_set(opts
, "value", str
+offset
+1);
654 struct ConfigWriteData
{
659 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
661 struct ConfigWriteData
*data
= opaque
;
663 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
667 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
669 struct ConfigWriteData
*data
= opaque
;
670 const char *id
= qemu_opts_id(opts
);
673 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
675 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
677 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
678 fprintf(data
->fp
, "\n");
682 void qemu_config_write(FILE *fp
)
684 struct ConfigWriteData data
= { .fp
= fp
};
685 QemuOptsList
**lists
= vm_config_groups
;
688 fprintf(fp
, "# qemu config file\n\n");
689 for (i
= 0; lists
[i
] != NULL
; i
++) {
690 data
.list
= lists
[i
];
691 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
695 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
697 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
699 QemuOptsList
*list
= NULL
;
700 QemuOpts
*opts
= NULL
;
701 int res
= -1, lno
= 0;
704 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
705 loc_set_file(fname
, ++lno
);
706 if (line
[0] == '\n') {
707 /* skip empty lines */
710 if (line
[0] == '#') {
714 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
716 list
= find_list(lists
, group
);
719 opts
= qemu_opts_create(list
, id
, 1);
722 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
723 /* group without id */
724 list
= find_list(lists
, group
);
727 opts
= qemu_opts_create(list
, NULL
, 0);
730 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
733 error_report("no group defined");
736 if (qemu_opt_set(opts
, arg
, value
) != 0) {
741 error_report("parse error");
745 error_report("error reading file");
754 int qemu_read_config_file(const char *filename
)
756 FILE *f
= fopen(filename
, "r");
763 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);