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
,
84 .type
= QEMU_OPT_BOOL
,
85 .help
= "make this a boot drive",
91 static QemuOptsList qemu_chardev_opts
= {
93 .implied_opt_name
= "backend",
94 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
98 .type
= QEMU_OPT_STRING
,
101 .type
= QEMU_OPT_STRING
,
104 .type
= QEMU_OPT_STRING
,
107 .type
= QEMU_OPT_STRING
,
110 .type
= QEMU_OPT_STRING
,
113 .type
= QEMU_OPT_STRING
,
116 .type
= QEMU_OPT_NUMBER
,
119 .type
= QEMU_OPT_BOOL
,
122 .type
= QEMU_OPT_BOOL
,
125 .type
= QEMU_OPT_BOOL
,
128 .type
= QEMU_OPT_BOOL
,
131 .type
= QEMU_OPT_BOOL
,
134 .type
= QEMU_OPT_BOOL
,
137 .type
= QEMU_OPT_NUMBER
,
140 .type
= QEMU_OPT_NUMBER
,
143 .type
= QEMU_OPT_NUMBER
,
146 .type
= QEMU_OPT_NUMBER
,
149 .type
= QEMU_OPT_BOOL
,
152 .type
= QEMU_OPT_BOOL
,
154 { /* end of list */ }
158 QemuOptsList qemu_fsdev_opts
= {
160 .implied_opt_name
= "fstype",
161 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
165 .type
= QEMU_OPT_STRING
,
168 .type
= QEMU_OPT_STRING
,
170 .name
= "security_model",
171 .type
= QEMU_OPT_STRING
,
177 QemuOptsList qemu_virtfs_opts
= {
179 .implied_opt_name
= "fstype",
180 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
184 .type
= QEMU_OPT_STRING
,
187 .type
= QEMU_OPT_STRING
,
190 .type
= QEMU_OPT_STRING
,
192 .name
= "security_model",
193 .type
= QEMU_OPT_STRING
,
200 static QemuOptsList qemu_device_opts
= {
202 .implied_opt_name
= "driver",
203 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
206 * no elements => accept any
207 * sanity checking will happen later
208 * when setting device properties
210 { /* end of list */ }
214 static QemuOptsList qemu_netdev_opts
= {
216 .implied_opt_name
= "type",
217 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
220 * no elements => accept any params
221 * validation will happen later
223 { /* end of list */ }
227 static QemuOptsList qemu_net_opts
= {
229 .implied_opt_name
= "type",
230 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
233 * no elements => accept any params
234 * validation will happen later
236 { /* end of list */ }
240 static QemuOptsList qemu_rtc_opts
= {
242 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
246 .type
= QEMU_OPT_STRING
,
249 .type
= QEMU_OPT_STRING
,
252 .type
= QEMU_OPT_STRING
,
254 { /* end of list */ }
258 static QemuOptsList qemu_global_opts
= {
260 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
264 .type
= QEMU_OPT_STRING
,
267 .type
= QEMU_OPT_STRING
,
270 .type
= QEMU_OPT_STRING
,
272 { /* end of list */ }
276 static QemuOptsList qemu_mon_opts
= {
278 .implied_opt_name
= "chardev",
279 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
283 .type
= QEMU_OPT_STRING
,
286 .type
= QEMU_OPT_STRING
,
289 .type
= QEMU_OPT_BOOL
,
292 .type
= QEMU_OPT_BOOL
,
294 { /* end of list */ }
298 #ifdef CONFIG_SIMPLE_TRACE
299 static QemuOptsList qemu_trace_opts
= {
301 .implied_opt_name
= "trace",
302 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
306 .type
= QEMU_OPT_STRING
,
308 { /* end if list */ }
313 static QemuOptsList qemu_cpudef_opts
= {
315 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
319 .type
= QEMU_OPT_STRING
,
322 .type
= QEMU_OPT_NUMBER
,
325 .type
= QEMU_OPT_STRING
,
328 .type
= QEMU_OPT_NUMBER
,
331 .type
= QEMU_OPT_NUMBER
,
334 .type
= QEMU_OPT_NUMBER
,
336 .name
= "feature_edx", /* cpuid 0000_0001.edx */
337 .type
= QEMU_OPT_STRING
,
339 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
340 .type
= QEMU_OPT_STRING
,
342 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
343 .type
= QEMU_OPT_STRING
,
345 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
346 .type
= QEMU_OPT_STRING
,
349 .type
= QEMU_OPT_NUMBER
,
352 .type
= QEMU_OPT_STRING
,
354 .name
= "vendor_override",
355 .type
= QEMU_OPT_NUMBER
,
357 { /* end of list */ }
361 QemuOptsList qemu_spice_opts
= {
363 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
367 .type
= QEMU_OPT_NUMBER
,
370 .type
= QEMU_OPT_STRING
,
372 .name
= "disable-ticketing",
373 .type
= QEMU_OPT_BOOL
,
375 { /* end if list */ }
379 static QemuOptsList
*vm_config_groups
[32] = {
389 #ifdef CONFIG_SIMPLE_TRACE
395 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
399 for (i
= 0; lists
[i
] != NULL
; i
++) {
400 if (strcmp(lists
[i
]->name
, group
) == 0)
403 if (lists
[i
] == NULL
) {
404 error_report("there is no option group \"%s\"", group
);
409 QemuOptsList
*qemu_find_opts(const char *group
)
411 return find_list(vm_config_groups
, group
);
414 void qemu_add_opts(QemuOptsList
*list
)
418 entries
= ARRAY_SIZE(vm_config_groups
);
419 entries
--; /* keep list NULL terminated */
420 for (i
= 0; i
< entries
; i
++) {
421 if (vm_config_groups
[i
] == NULL
) {
422 vm_config_groups
[i
] = list
;
426 fprintf(stderr
, "ran out of space in vm_config_groups");
430 int qemu_set_option(const char *str
)
432 char group
[64], id
[64], arg
[64];
437 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
438 if (rc
< 3 || str
[offset
] != '=') {
439 error_report("can't parse: \"%s\"", str
);
443 list
= qemu_find_opts(group
);
448 opts
= qemu_opts_find(list
, id
);
450 error_report("there is no %s \"%s\" defined",
455 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
461 int qemu_global_option(const char *str
)
463 char driver
[64], property
[64];
467 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
468 if (rc
< 2 || str
[offset
] != '=') {
469 error_report("can't parse: \"%s\"", str
);
473 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
474 qemu_opt_set(opts
, "driver", driver
);
475 qemu_opt_set(opts
, "property", property
);
476 qemu_opt_set(opts
, "value", str
+offset
+1);
480 struct ConfigWriteData
{
485 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
487 struct ConfigWriteData
*data
= opaque
;
489 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
493 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
495 struct ConfigWriteData
*data
= opaque
;
496 const char *id
= qemu_opts_id(opts
);
499 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
501 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
503 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
504 fprintf(data
->fp
, "\n");
508 void qemu_config_write(FILE *fp
)
510 struct ConfigWriteData data
= { .fp
= fp
};
511 QemuOptsList
**lists
= vm_config_groups
;
514 fprintf(fp
, "# qemu config file\n\n");
515 for (i
= 0; lists
[i
] != NULL
; i
++) {
516 data
.list
= lists
[i
];
517 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
521 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
523 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
525 QemuOptsList
*list
= NULL
;
526 QemuOpts
*opts
= NULL
;
527 int res
= -1, lno
= 0;
530 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
531 loc_set_file(fname
, ++lno
);
532 if (line
[0] == '\n') {
533 /* skip empty lines */
536 if (line
[0] == '#') {
540 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
542 list
= find_list(lists
, group
);
545 opts
= qemu_opts_create(list
, id
, 1);
548 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
549 /* group without id */
550 list
= find_list(lists
, group
);
553 opts
= qemu_opts_create(list
, NULL
, 0);
556 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
559 error_report("no group defined");
562 if (qemu_opt_set(opts
, arg
, value
) != 0) {
567 error_report("parse error");
571 error_report("error reading file");
580 int qemu_read_config_file(const char *filename
)
582 FILE *f
= fopen(filename
, "r");
589 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);