1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
8 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 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 if list */ }
159 QemuOptsList qemu_fsdev_opts
= {
161 .implied_opt_name
= "fstype",
162 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
166 .type
= QEMU_OPT_STRING
,
169 .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
,
198 QemuOptsList qemu_device_opts
= {
200 .implied_opt_name
= "driver",
201 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
204 * no elements => accept any
205 * sanity checking will happen later
206 * when setting device properties
208 { /* end if list */ }
212 QemuOptsList qemu_netdev_opts
= {
214 .implied_opt_name
= "type",
215 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
218 * no elements => accept any params
219 * validation will happen later
221 { /* end of list */ }
225 QemuOptsList qemu_net_opts
= {
227 .implied_opt_name
= "type",
228 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
231 * no elements => accept any params
232 * validation will happen later
234 { /* end of list */ }
238 QemuOptsList qemu_rtc_opts
= {
240 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
244 .type
= QEMU_OPT_STRING
,
247 .type
= QEMU_OPT_STRING
,
251 .type
= QEMU_OPT_STRING
,
254 { /* end if list */ }
258 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 if list */ }
276 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
,
291 { /* end if list */ }
295 QemuOptsList qemu_cpudef_opts
= {
297 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
301 .type
= QEMU_OPT_STRING
,
304 .type
= QEMU_OPT_NUMBER
,
307 .type
= QEMU_OPT_STRING
,
310 .type
= QEMU_OPT_NUMBER
,
313 .type
= QEMU_OPT_NUMBER
,
316 .type
= QEMU_OPT_NUMBER
,
318 .name
= "feature_edx", /* cpuid 0000_0001.edx */
319 .type
= QEMU_OPT_STRING
,
321 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
322 .type
= QEMU_OPT_STRING
,
324 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
325 .type
= QEMU_OPT_STRING
,
327 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
328 .type
= QEMU_OPT_STRING
,
331 .type
= QEMU_OPT_NUMBER
,
334 .type
= QEMU_OPT_STRING
,
336 .name
= "vendor_override",
337 .type
= QEMU_OPT_NUMBER
,
339 { /* end of list */ }
343 static QemuOptsList
*vm_config_groups
[] = {
356 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
360 for (i
= 0; lists
[i
] != NULL
; i
++) {
361 if (strcmp(lists
[i
]->name
, group
) == 0)
364 if (lists
[i
] == NULL
) {
365 error_report("there is no option group \"%s\"", group
);
370 QemuOptsList
*qemu_find_opts(const char *group
)
372 return find_list(vm_config_groups
, group
);
375 int qemu_set_option(const char *str
)
377 char group
[64], id
[64], arg
[64];
382 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
383 if (rc
< 3 || str
[offset
] != '=') {
384 error_report("can't parse: \"%s\"", str
);
388 list
= qemu_find_opts(group
);
393 opts
= qemu_opts_find(list
, id
);
395 error_report("there is no %s \"%s\" defined",
400 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
406 int qemu_global_option(const char *str
)
408 char driver
[64], property
[64];
412 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
413 if (rc
< 2 || str
[offset
] != '=') {
414 error_report("can't parse: \"%s\"", str
);
418 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
419 qemu_opt_set(opts
, "driver", driver
);
420 qemu_opt_set(opts
, "property", property
);
421 qemu_opt_set(opts
, "value", str
+offset
+1);
425 struct ConfigWriteData
{
430 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
432 struct ConfigWriteData
*data
= opaque
;
434 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
438 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
440 struct ConfigWriteData
*data
= opaque
;
441 const char *id
= qemu_opts_id(opts
);
444 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
446 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
448 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
449 fprintf(data
->fp
, "\n");
453 void qemu_config_write(FILE *fp
)
455 struct ConfigWriteData data
= { .fp
= fp
};
456 QemuOptsList
**lists
= vm_config_groups
;
459 fprintf(fp
, "# qemu config file\n\n");
460 for (i
= 0; lists
[i
] != NULL
; i
++) {
461 data
.list
= lists
[i
];
462 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
466 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
468 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
470 QemuOptsList
*list
= NULL
;
471 QemuOpts
*opts
= NULL
;
472 int res
= -1, lno
= 0;
475 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
476 loc_set_file(fname
, ++lno
);
477 if (line
[0] == '\n') {
478 /* skip empty lines */
481 if (line
[0] == '#') {
485 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
487 list
= find_list(lists
, group
);
490 opts
= qemu_opts_create(list
, id
, 1);
493 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
494 /* group without id */
495 list
= find_list(lists
, group
);
498 opts
= qemu_opts_create(list
, NULL
, 0);
501 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
504 error_report("no group defined");
507 if (qemu_opt_set(opts
, arg
, value
) != 0) {
512 error_report("parse error");
516 error_report("error reading file");
525 int qemu_read_config_file(const char *filename
)
527 FILE *f
= fopen(filename
, "r");
534 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);