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)",
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 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 if list */ }
155 QemuOptsList qemu_fsdev_opts
= {
157 .implied_opt_name
= "fstype",
158 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts
.head
),
162 .type
= QEMU_OPT_STRING
,
165 .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
,
194 QemuOptsList qemu_device_opts
= {
196 .implied_opt_name
= "driver",
197 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
200 * no elements => accept any
201 * sanity checking will happen later
202 * when setting device properties
204 { /* end if list */ }
208 QemuOptsList qemu_netdev_opts
= {
210 .implied_opt_name
= "type",
211 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
214 * no elements => accept any params
215 * validation will happen later
217 { /* end of list */ }
221 QemuOptsList qemu_net_opts
= {
223 .implied_opt_name
= "type",
224 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
227 * no elements => accept any params
228 * validation will happen later
230 { /* end of list */ }
234 QemuOptsList qemu_rtc_opts
= {
236 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
240 .type
= QEMU_OPT_STRING
,
243 .type
= QEMU_OPT_STRING
,
247 .type
= QEMU_OPT_STRING
,
250 { /* end if list */ }
254 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 if list */ }
272 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 if list */ }
291 QemuOptsList qemu_cpudef_opts
= {
293 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
297 .type
= QEMU_OPT_STRING
,
300 .type
= QEMU_OPT_NUMBER
,
303 .type
= QEMU_OPT_STRING
,
306 .type
= QEMU_OPT_NUMBER
,
309 .type
= QEMU_OPT_NUMBER
,
312 .type
= QEMU_OPT_NUMBER
,
314 .name
= "feature_edx", /* cpuid 0000_0001.edx */
315 .type
= QEMU_OPT_STRING
,
317 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
318 .type
= QEMU_OPT_STRING
,
320 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
321 .type
= QEMU_OPT_STRING
,
323 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
324 .type
= QEMU_OPT_STRING
,
327 .type
= QEMU_OPT_NUMBER
,
330 .type
= QEMU_OPT_STRING
,
332 .name
= "vendor_override",
333 .type
= QEMU_OPT_NUMBER
,
335 { /* end of list */ }
339 static QemuOptsList
*vm_config_groups
[] = {
352 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
356 for (i
= 0; lists
[i
] != NULL
; i
++) {
357 if (strcmp(lists
[i
]->name
, group
) == 0)
360 if (lists
[i
] == NULL
) {
361 error_report("there is no option group \"%s\"", group
);
366 QemuOptsList
*qemu_find_opts(const char *group
)
368 return find_list(vm_config_groups
, group
);
371 int qemu_set_option(const char *str
)
373 char group
[64], id
[64], arg
[64];
378 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
379 if (rc
< 3 || str
[offset
] != '=') {
380 error_report("can't parse: \"%s\"", str
);
384 list
= qemu_find_opts(group
);
389 opts
= qemu_opts_find(list
, id
);
391 error_report("there is no %s \"%s\" defined",
396 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
402 int qemu_global_option(const char *str
)
404 char driver
[64], property
[64];
408 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
409 if (rc
< 2 || str
[offset
] != '=') {
410 error_report("can't parse: \"%s\"", str
);
414 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
415 qemu_opt_set(opts
, "driver", driver
);
416 qemu_opt_set(opts
, "property", property
);
417 qemu_opt_set(opts
, "value", str
+offset
+1);
421 struct ConfigWriteData
{
426 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
428 struct ConfigWriteData
*data
= opaque
;
430 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
434 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
436 struct ConfigWriteData
*data
= opaque
;
437 const char *id
= qemu_opts_id(opts
);
440 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
442 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
444 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
445 fprintf(data
->fp
, "\n");
449 void qemu_config_write(FILE *fp
)
451 struct ConfigWriteData data
= { .fp
= fp
};
452 QemuOptsList
**lists
= vm_config_groups
;
455 fprintf(fp
, "# qemu config file\n\n");
456 for (i
= 0; lists
[i
] != NULL
; i
++) {
457 data
.list
= lists
[i
];
458 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
462 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
464 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
466 QemuOptsList
*list
= NULL
;
467 QemuOpts
*opts
= NULL
;
468 int res
= -1, lno
= 0;
471 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
472 loc_set_file(fname
, ++lno
);
473 if (line
[0] == '\n') {
474 /* skip empty lines */
477 if (line
[0] == '#') {
481 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
483 list
= find_list(lists
, group
);
486 opts
= qemu_opts_create(list
, id
, 1);
489 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
490 /* group without id */
491 list
= find_list(lists
, group
);
494 opts
= qemu_opts_create(list
, NULL
, 0);
497 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
500 error_report("no group defined");
503 if (qemu_opt_set(opts
, arg
, value
) != 0) {
508 error_report("parse error");
512 error_report("error reading file");
521 int qemu_read_config_file(const char *filename
)
523 FILE *f
= fopen(filename
, "r");
528 if (qemu_config_parse(f
, vm_config_groups
, filename
) != 0) {