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
,
172 QemuOptsList qemu_device_opts
= {
174 .implied_opt_name
= "driver",
175 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
178 * no elements => accept any
179 * sanity checking will happen later
180 * when setting device properties
182 { /* end if list */ }
186 QemuOptsList qemu_netdev_opts
= {
188 .implied_opt_name
= "type",
189 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
192 * no elements => accept any params
193 * validation will happen later
195 { /* end of list */ }
199 QemuOptsList qemu_net_opts
= {
201 .implied_opt_name
= "type",
202 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
205 * no elements => accept any params
206 * validation will happen later
208 { /* end of list */ }
212 QemuOptsList qemu_rtc_opts
= {
214 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
218 .type
= QEMU_OPT_STRING
,
221 .type
= QEMU_OPT_STRING
,
225 .type
= QEMU_OPT_STRING
,
228 { /* end if list */ }
232 QemuOptsList qemu_global_opts
= {
234 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
238 .type
= QEMU_OPT_STRING
,
241 .type
= QEMU_OPT_STRING
,
244 .type
= QEMU_OPT_STRING
,
246 { /* end if list */ }
250 QemuOptsList qemu_mon_opts
= {
252 .implied_opt_name
= "chardev",
253 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_STRING
,
263 .type
= QEMU_OPT_BOOL
,
265 { /* end if list */ }
269 QemuOptsList qemu_cpudef_opts
= {
271 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
275 .type
= QEMU_OPT_STRING
,
278 .type
= QEMU_OPT_NUMBER
,
281 .type
= QEMU_OPT_STRING
,
284 .type
= QEMU_OPT_NUMBER
,
287 .type
= QEMU_OPT_NUMBER
,
290 .type
= QEMU_OPT_NUMBER
,
292 .name
= "feature_edx", /* cpuid 0000_0001.edx */
293 .type
= QEMU_OPT_STRING
,
295 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
296 .type
= QEMU_OPT_STRING
,
298 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
299 .type
= QEMU_OPT_STRING
,
301 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
302 .type
= QEMU_OPT_STRING
,
305 .type
= QEMU_OPT_NUMBER
,
308 .type
= QEMU_OPT_STRING
,
310 .name
= "vendor_override",
311 .type
= QEMU_OPT_NUMBER
,
313 { /* end of list */ }
317 static QemuOptsList
*vm_config_groups
[] = {
330 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
334 for (i
= 0; lists
[i
] != NULL
; i
++) {
335 if (strcmp(lists
[i
]->name
, group
) == 0)
338 if (lists
[i
] == NULL
) {
339 error_report("there is no option group \"%s\"", group
);
344 QemuOptsList
*qemu_find_opts(const char *group
)
346 return find_list(vm_config_groups
, group
);
349 int qemu_set_option(const char *str
)
351 char group
[64], id
[64], arg
[64];
356 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
357 if (rc
< 3 || str
[offset
] != '=') {
358 error_report("can't parse: \"%s\"", str
);
362 list
= qemu_find_opts(group
);
367 opts
= qemu_opts_find(list
, id
);
369 error_report("there is no %s \"%s\" defined",
374 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
380 int qemu_global_option(const char *str
)
382 char driver
[64], property
[64];
386 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
387 if (rc
< 2 || str
[offset
] != '=') {
388 error_report("can't parse: \"%s\"", str
);
392 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
393 qemu_opt_set(opts
, "driver", driver
);
394 qemu_opt_set(opts
, "property", property
);
395 qemu_opt_set(opts
, "value", str
+offset
+1);
399 struct ConfigWriteData
{
404 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
406 struct ConfigWriteData
*data
= opaque
;
408 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
412 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
414 struct ConfigWriteData
*data
= opaque
;
415 const char *id
= qemu_opts_id(opts
);
418 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
420 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
422 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
423 fprintf(data
->fp
, "\n");
427 void qemu_config_write(FILE *fp
)
429 struct ConfigWriteData data
= { .fp
= fp
};
430 QemuOptsList
**lists
= vm_config_groups
;
433 fprintf(fp
, "# qemu config file\n\n");
434 for (i
= 0; lists
[i
] != NULL
; i
++) {
435 data
.list
= lists
[i
];
436 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
440 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
442 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
444 QemuOptsList
*list
= NULL
;
445 QemuOpts
*opts
= NULL
;
446 int res
= -1, lno
= 0;
449 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
450 loc_set_file(fname
, ++lno
);
451 if (line
[0] == '\n') {
452 /* skip empty lines */
455 if (line
[0] == '#') {
459 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
461 list
= find_list(lists
, group
);
464 opts
= qemu_opts_create(list
, id
, 1);
467 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
468 /* group without id */
469 list
= find_list(lists
, group
);
472 opts
= qemu_opts_create(list
, NULL
, 0);
475 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
478 error_report("no group defined");
481 if (qemu_opt_set(opts
, arg
, value
) != 0) {
486 error_report("parse error");
490 error_report("error reading file");
499 int qemu_read_config_file(const char *filename
)
501 FILE *f
= fopen(filename
, "r");
506 if (qemu_config_parse(f
, vm_config_groups
, filename
) != 0) {