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 */ }
154 QemuOptsList qemu_device_opts
= {
156 .implied_opt_name
= "driver",
157 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
160 * no elements => accept any
161 * sanity checking will happen later
162 * when setting device properties
164 { /* end if list */ }
168 QemuOptsList qemu_netdev_opts
= {
170 .implied_opt_name
= "type",
171 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
174 * no elements => accept any params
175 * validation will happen later
177 { /* end of list */ }
181 QemuOptsList qemu_net_opts
= {
183 .implied_opt_name
= "type",
184 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
187 * no elements => accept any params
188 * validation will happen later
190 { /* end of list */ }
194 QemuOptsList qemu_rtc_opts
= {
196 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
200 .type
= QEMU_OPT_STRING
,
203 .type
= QEMU_OPT_STRING
,
207 .type
= QEMU_OPT_STRING
,
210 { /* end if list */ }
214 QemuOptsList qemu_global_opts
= {
216 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
220 .type
= QEMU_OPT_STRING
,
223 .type
= QEMU_OPT_STRING
,
226 .type
= QEMU_OPT_STRING
,
228 { /* end if list */ }
232 QemuOptsList qemu_mon_opts
= {
234 .implied_opt_name
= "chardev",
235 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
239 .type
= QEMU_OPT_STRING
,
242 .type
= QEMU_OPT_STRING
,
245 .type
= QEMU_OPT_BOOL
,
247 { /* end if list */ }
251 QemuOptsList qemu_cpudef_opts
= {
253 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_NUMBER
,
263 .type
= QEMU_OPT_STRING
,
266 .type
= QEMU_OPT_NUMBER
,
269 .type
= QEMU_OPT_NUMBER
,
272 .type
= QEMU_OPT_NUMBER
,
274 .name
= "feature_edx", /* cpuid 0000_0001.edx */
275 .type
= QEMU_OPT_STRING
,
277 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
278 .type
= QEMU_OPT_STRING
,
280 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
281 .type
= QEMU_OPT_STRING
,
283 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
284 .type
= QEMU_OPT_STRING
,
287 .type
= QEMU_OPT_NUMBER
,
290 .type
= QEMU_OPT_STRING
,
292 .name
= "vendor_override",
293 .type
= QEMU_OPT_NUMBER
,
295 { /* end of list */ }
299 static QemuOptsList
*vm_config_groups
[] = {
312 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
)
316 for (i
= 0; lists
[i
] != NULL
; i
++) {
317 if (strcmp(lists
[i
]->name
, group
) == 0)
320 if (lists
[i
] == NULL
) {
321 error_report("there is no option group \"%s\"", group
);
326 QemuOptsList
*qemu_find_opts(const char *group
)
328 return find_list(vm_config_groups
, group
);
331 int qemu_set_option(const char *str
)
333 char group
[64], id
[64], arg
[64];
338 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
339 if (rc
< 3 || str
[offset
] != '=') {
340 error_report("can't parse: \"%s\"", str
);
344 list
= qemu_find_opts(group
);
349 opts
= qemu_opts_find(list
, id
);
351 error_report("there is no %s \"%s\" defined",
356 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
362 int qemu_global_option(const char *str
)
364 char driver
[64], property
[64];
368 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
369 if (rc
< 2 || str
[offset
] != '=') {
370 error_report("can't parse: \"%s\"", str
);
374 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
375 qemu_opt_set(opts
, "driver", driver
);
376 qemu_opt_set(opts
, "property", property
);
377 qemu_opt_set(opts
, "value", str
+offset
+1);
381 struct ConfigWriteData
{
386 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
388 struct ConfigWriteData
*data
= opaque
;
390 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
394 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
396 struct ConfigWriteData
*data
= opaque
;
397 const char *id
= qemu_opts_id(opts
);
400 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
402 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
404 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
405 fprintf(data
->fp
, "\n");
409 void qemu_config_write(FILE *fp
)
411 struct ConfigWriteData data
= { .fp
= fp
};
412 QemuOptsList
**lists
= vm_config_groups
;
415 fprintf(fp
, "# qemu config file\n\n");
416 for (i
= 0; lists
[i
] != NULL
; i
++) {
417 data
.list
= lists
[i
];
418 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
422 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
424 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
426 QemuOptsList
*list
= NULL
;
427 QemuOpts
*opts
= NULL
;
428 int res
= -1, lno
= 0;
431 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
432 loc_set_file(fname
, ++lno
);
433 if (line
[0] == '\n') {
434 /* skip empty lines */
437 if (line
[0] == '#') {
441 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
443 list
= find_list(lists
, group
);
446 opts
= qemu_opts_create(list
, id
, 1);
449 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
450 /* group without id */
451 list
= find_list(lists
, group
);
454 opts
= qemu_opts_create(list
, NULL
, 0);
457 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
460 error_report("no group defined");
463 if (qemu_opt_set(opts
, arg
, value
) != 0) {
468 error_report("parse error");
472 error_report("error reading file");
481 int qemu_read_config_file(const char *filename
)
483 FILE *f
= fopen(filename
, "r");
488 if (qemu_config_parse(f
, vm_config_groups
, filename
) != 0) {