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
,
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
,
167 .name
= "security_model",
168 .type
= QEMU_OPT_STRING
,
176 QemuOptsList qemu_virtfs_opts
= {
178 .implied_opt_name
= "fstype",
179 .head
= QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts
.head
),
183 .type
= QEMU_OPT_STRING
,
186 .type
= QEMU_OPT_STRING
,
189 .type
= QEMU_OPT_STRING
,
191 .name
= "security_model",
192 .type
= QEMU_OPT_STRING
,
200 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 if list */ }
214 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 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 QemuOptsList qemu_rtc_opts
= {
242 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
246 .type
= QEMU_OPT_STRING
,
249 .type
= QEMU_OPT_STRING
,
253 .type
= QEMU_OPT_STRING
,
256 { /* end if list */ }
260 QemuOptsList qemu_global_opts
= {
262 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
266 .type
= QEMU_OPT_STRING
,
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_STRING
,
274 { /* end if list */ }
278 QemuOptsList qemu_mon_opts
= {
280 .implied_opt_name
= "chardev",
281 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
285 .type
= QEMU_OPT_STRING
,
288 .type
= QEMU_OPT_STRING
,
291 .type
= QEMU_OPT_BOOL
,
293 { /* end if list */ }
297 QemuOptsList qemu_cpudef_opts
= {
299 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
303 .type
= QEMU_OPT_STRING
,
306 .type
= QEMU_OPT_NUMBER
,
309 .type
= QEMU_OPT_STRING
,
312 .type
= QEMU_OPT_NUMBER
,
315 .type
= QEMU_OPT_NUMBER
,
318 .type
= QEMU_OPT_NUMBER
,
320 .name
= "feature_edx", /* cpuid 0000_0001.edx */
321 .type
= QEMU_OPT_STRING
,
323 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
324 .type
= QEMU_OPT_STRING
,
326 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
327 .type
= QEMU_OPT_STRING
,
329 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
330 .type
= QEMU_OPT_STRING
,
333 .type
= QEMU_OPT_NUMBER
,
336 .type
= QEMU_OPT_STRING
,
338 .name
= "vendor_override",
339 .type
= QEMU_OPT_NUMBER
,
341 { /* end of list */ }
345 static QemuOptsList
*vm_config_groups
[] = {
358 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
362 for (i
= 0; lists
[i
] != NULL
; i
++) {
363 if (strcmp(lists
[i
]->name
, group
) == 0)
366 if (lists
[i
] == NULL
) {
367 error_report("there is no option group \"%s\"", group
);
372 QemuOptsList
*qemu_find_opts(const char *group
)
374 return find_list(vm_config_groups
, group
);
377 int qemu_set_option(const char *str
)
379 char group
[64], id
[64], arg
[64];
384 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
385 if (rc
< 3 || str
[offset
] != '=') {
386 error_report("can't parse: \"%s\"", str
);
390 list
= qemu_find_opts(group
);
395 opts
= qemu_opts_find(list
, id
);
397 error_report("there is no %s \"%s\" defined",
402 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
408 int qemu_global_option(const char *str
)
410 char driver
[64], property
[64];
414 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
415 if (rc
< 2 || str
[offset
] != '=') {
416 error_report("can't parse: \"%s\"", str
);
420 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
421 qemu_opt_set(opts
, "driver", driver
);
422 qemu_opt_set(opts
, "property", property
);
423 qemu_opt_set(opts
, "value", str
+offset
+1);
427 struct ConfigWriteData
{
432 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
434 struct ConfigWriteData
*data
= opaque
;
436 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
440 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
442 struct ConfigWriteData
*data
= opaque
;
443 const char *id
= qemu_opts_id(opts
);
446 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
448 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
450 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
451 fprintf(data
->fp
, "\n");
455 void qemu_config_write(FILE *fp
)
457 struct ConfigWriteData data
= { .fp
= fp
};
458 QemuOptsList
**lists
= vm_config_groups
;
461 fprintf(fp
, "# qemu config file\n\n");
462 for (i
= 0; lists
[i
] != NULL
; i
++) {
463 data
.list
= lists
[i
];
464 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
468 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
470 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
472 QemuOptsList
*list
= NULL
;
473 QemuOpts
*opts
= NULL
;
474 int res
= -1, lno
= 0;
477 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
478 loc_set_file(fname
, ++lno
);
479 if (line
[0] == '\n') {
480 /* skip empty lines */
483 if (line
[0] == '#') {
487 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
489 list
= find_list(lists
, group
);
492 opts
= qemu_opts_create(list
, id
, 1);
495 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
496 /* group without id */
497 list
= find_list(lists
, group
);
500 opts
= qemu_opts_create(list
, NULL
, 0);
503 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
506 error_report("no group defined");
509 if (qemu_opt_set(opts
, arg
, value
) != 0) {
514 error_report("parse error");
518 error_report("error reading file");
527 int qemu_read_config_file(const char *filename
)
529 FILE *f
= fopen(filename
, "r");
536 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);