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
,
150 { /* end of list */ }
154 QemuOptsList qemu_fsdev_opts
= {
156 .implied_opt_name
= "fstype",
157 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
161 .type
= QEMU_OPT_STRING
,
164 .type
= QEMU_OPT_STRING
,
166 .name
= "security_model",
167 .type
= QEMU_OPT_STRING
,
173 QemuOptsList qemu_virtfs_opts
= {
175 .implied_opt_name
= "fstype",
176 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
180 .type
= QEMU_OPT_STRING
,
183 .type
= QEMU_OPT_STRING
,
186 .type
= QEMU_OPT_STRING
,
188 .name
= "security_model",
189 .type
= QEMU_OPT_STRING
,
196 static QemuOptsList qemu_device_opts
= {
198 .implied_opt_name
= "driver",
199 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
202 * no elements => accept any
203 * sanity checking will happen later
204 * when setting device properties
206 { /* end of list */ }
210 static QemuOptsList qemu_netdev_opts
= {
212 .implied_opt_name
= "type",
213 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
216 * no elements => accept any params
217 * validation will happen later
219 { /* end of list */ }
223 static QemuOptsList qemu_net_opts
= {
225 .implied_opt_name
= "type",
226 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
229 * no elements => accept any params
230 * validation will happen later
232 { /* end of list */ }
236 static QemuOptsList qemu_rtc_opts
= {
238 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
242 .type
= QEMU_OPT_STRING
,
245 .type
= QEMU_OPT_STRING
,
248 .type
= QEMU_OPT_STRING
,
250 { /* end of list */ }
254 static QemuOptsList qemu_global_opts
= {
256 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_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_mon_opts
= {
274 .implied_opt_name
= "chardev",
275 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
279 .type
= QEMU_OPT_STRING
,
282 .type
= QEMU_OPT_STRING
,
285 .type
= QEMU_OPT_BOOL
,
287 { /* end of list */ }
291 #ifdef CONFIG_SIMPLE_TRACE
292 static QemuOptsList qemu_trace_opts
= {
294 .implied_opt_name
= "trace",
295 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
299 .type
= QEMU_OPT_STRING
,
301 { /* end if list */ }
306 static QemuOptsList qemu_cpudef_opts
= {
308 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
312 .type
= QEMU_OPT_STRING
,
315 .type
= QEMU_OPT_NUMBER
,
318 .type
= QEMU_OPT_STRING
,
321 .type
= QEMU_OPT_NUMBER
,
324 .type
= QEMU_OPT_NUMBER
,
327 .type
= QEMU_OPT_NUMBER
,
329 .name
= "feature_edx", /* cpuid 0000_0001.edx */
330 .type
= QEMU_OPT_STRING
,
332 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
333 .type
= QEMU_OPT_STRING
,
335 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
336 .type
= QEMU_OPT_STRING
,
338 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
339 .type
= QEMU_OPT_STRING
,
342 .type
= QEMU_OPT_NUMBER
,
345 .type
= QEMU_OPT_STRING
,
347 .name
= "vendor_override",
348 .type
= QEMU_OPT_NUMBER
,
350 { /* end of list */ }
354 QemuOptsList qemu_spice_opts
= {
356 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
360 .type
= QEMU_OPT_NUMBER
,
363 .type
= QEMU_OPT_STRING
,
365 .name
= "disable-ticketing",
366 .type
= QEMU_OPT_BOOL
,
368 { /* end if list */ }
372 static QemuOptsList
*vm_config_groups
[32] = {
382 #ifdef CONFIG_SIMPLE_TRACE
388 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
392 for (i
= 0; lists
[i
] != NULL
; i
++) {
393 if (strcmp(lists
[i
]->name
, group
) == 0)
396 if (lists
[i
] == NULL
) {
397 error_report("there is no option group \"%s\"", group
);
402 QemuOptsList
*qemu_find_opts(const char *group
)
404 return find_list(vm_config_groups
, group
);
407 void qemu_add_opts(QemuOptsList
*list
)
411 entries
= ARRAY_SIZE(vm_config_groups
);
412 entries
--; /* keep list NULL terminated */
413 for (i
= 0; i
< entries
; i
++) {
414 if (vm_config_groups
[i
] == NULL
) {
415 vm_config_groups
[i
] = list
;
419 fprintf(stderr
, "ran out of space in vm_config_groups");
423 int qemu_set_option(const char *str
)
425 char group
[64], id
[64], arg
[64];
430 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
431 if (rc
< 3 || str
[offset
] != '=') {
432 error_report("can't parse: \"%s\"", str
);
436 list
= qemu_find_opts(group
);
441 opts
= qemu_opts_find(list
, id
);
443 error_report("there is no %s \"%s\" defined",
448 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
454 int qemu_global_option(const char *str
)
456 char driver
[64], property
[64];
460 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
461 if (rc
< 2 || str
[offset
] != '=') {
462 error_report("can't parse: \"%s\"", str
);
466 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
467 qemu_opt_set(opts
, "driver", driver
);
468 qemu_opt_set(opts
, "property", property
);
469 qemu_opt_set(opts
, "value", str
+offset
+1);
473 struct ConfigWriteData
{
478 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
480 struct ConfigWriteData
*data
= opaque
;
482 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
486 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
488 struct ConfigWriteData
*data
= opaque
;
489 const char *id
= qemu_opts_id(opts
);
492 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
494 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
496 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
497 fprintf(data
->fp
, "\n");
501 void qemu_config_write(FILE *fp
)
503 struct ConfigWriteData data
= { .fp
= fp
};
504 QemuOptsList
**lists
= vm_config_groups
;
507 fprintf(fp
, "# qemu config file\n\n");
508 for (i
= 0; lists
[i
] != NULL
; i
++) {
509 data
.list
= lists
[i
];
510 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
514 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
516 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
518 QemuOptsList
*list
= NULL
;
519 QemuOpts
*opts
= NULL
;
520 int res
= -1, lno
= 0;
523 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
524 loc_set_file(fname
, ++lno
);
525 if (line
[0] == '\n') {
526 /* skip empty lines */
529 if (line
[0] == '#') {
533 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
535 list
= find_list(lists
, group
);
538 opts
= qemu_opts_create(list
, id
, 1);
541 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
542 /* group without id */
543 list
= find_list(lists
, group
);
546 opts
= qemu_opts_create(list
, NULL
, 0);
549 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
552 error_report("no group defined");
555 if (qemu_opt_set(opts
, arg
, value
) != 0) {
560 error_report("parse error");
564 error_report("error reading file");
573 int qemu_read_config_file(const char *filename
)
575 FILE *f
= fopen(filename
, "r");
582 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);