machine: package all init arguments into a QemuOpts (v2)
[qemu/aliguori-queue.git] / qemu-config.c
blob3679a9fb1109c2f8a8b0ecd7d417542a9223362c
1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
5 #include "sysemu.h"
6 #include "hw/qdev.h"
8 QemuOptsList qemu_drive_opts = {
9 .name = "drive",
10 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
11 .desc = {
13 .name = "bus",
14 .type = QEMU_OPT_NUMBER,
15 .help = "bus number",
16 },{
17 .name = "unit",
18 .type = QEMU_OPT_NUMBER,
19 .help = "unit number (i.e. lun for scsi)",
20 },{
21 .name = "if",
22 .type = QEMU_OPT_STRING,
23 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
24 },{
25 .name = "index",
26 .type = QEMU_OPT_NUMBER,
27 },{
28 .name = "cyls",
29 .type = QEMU_OPT_NUMBER,
30 .help = "number of cylinders (ide disk geometry)",
31 },{
32 .name = "heads",
33 .type = QEMU_OPT_NUMBER,
34 .help = "number of heads (ide disk geometry)",
35 },{
36 .name = "secs",
37 .type = QEMU_OPT_NUMBER,
38 .help = "number of sectors (ide disk geometry)",
39 },{
40 .name = "trans",
41 .type = QEMU_OPT_STRING,
42 .help = "chs translation (auto, lba. none)",
43 },{
44 .name = "media",
45 .type = QEMU_OPT_STRING,
46 .help = "media type (disk, cdrom)",
47 },{
48 .name = "snapshot",
49 .type = QEMU_OPT_BOOL,
50 },{
51 .name = "file",
52 .type = QEMU_OPT_STRING,
53 .help = "disk image",
54 },{
55 .name = "cache",
56 .type = QEMU_OPT_STRING,
57 .help = "host cache usage (none, writeback, writethrough, unsafe)",
58 },{
59 .name = "aio",
60 .type = QEMU_OPT_STRING,
61 .help = "host AIO implementation (threads, native)",
62 },{
63 .name = "format",
64 .type = QEMU_OPT_STRING,
65 .help = "disk format (raw, qcow2, ...)",
66 },{
67 .name = "serial",
68 .type = QEMU_OPT_STRING,
69 },{
70 .name = "rerror",
71 .type = QEMU_OPT_STRING,
72 },{
73 .name = "werror",
74 .type = QEMU_OPT_STRING,
75 },{
76 .name = "addr",
77 .type = QEMU_OPT_STRING,
78 .help = "pci address (virtio only)",
79 },{
80 .name = "readonly",
81 .type = QEMU_OPT_BOOL,
83 { /* end if list */ }
87 QemuOptsList qemu_chardev_opts = {
88 .name = "chardev",
89 .implied_opt_name = "backend",
90 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
91 .desc = {
93 .name = "backend",
94 .type = QEMU_OPT_STRING,
95 },{
96 .name = "path",
97 .type = QEMU_OPT_STRING,
98 },{
99 .name = "host",
100 .type = QEMU_OPT_STRING,
102 .name = "port",
103 .type = QEMU_OPT_STRING,
105 .name = "localaddr",
106 .type = QEMU_OPT_STRING,
108 .name = "localport",
109 .type = QEMU_OPT_STRING,
111 .name = "to",
112 .type = QEMU_OPT_NUMBER,
114 .name = "ipv4",
115 .type = QEMU_OPT_BOOL,
117 .name = "ipv6",
118 .type = QEMU_OPT_BOOL,
120 .name = "wait",
121 .type = QEMU_OPT_BOOL,
123 .name = "server",
124 .type = QEMU_OPT_BOOL,
126 .name = "delay",
127 .type = QEMU_OPT_BOOL,
129 .name = "telnet",
130 .type = QEMU_OPT_BOOL,
132 .name = "width",
133 .type = QEMU_OPT_NUMBER,
135 .name = "height",
136 .type = QEMU_OPT_NUMBER,
138 .name = "cols",
139 .type = QEMU_OPT_NUMBER,
141 .name = "rows",
142 .type = QEMU_OPT_NUMBER,
144 .name = "mux",
145 .type = QEMU_OPT_BOOL,
147 .name = "signal",
148 .type = QEMU_OPT_BOOL,
150 { /* end if list */ }
154 #ifdef CONFIG_LINUX
155 QemuOptsList qemu_fsdev_opts = {
156 .name = "fsdev",
157 .implied_opt_name = "fstype",
158 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
159 .desc = {
161 .name = "fstype",
162 .type = QEMU_OPT_STRING,
163 }, {
164 .name = "path",
165 .type = QEMU_OPT_STRING,
167 { /*End of list */ }
170 #endif
172 #ifdef CONFIG_LINUX
173 QemuOptsList qemu_virtfs_opts = {
174 .name = "virtfs",
175 .implied_opt_name = "fstype",
176 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
177 .desc = {
179 .name = "fstype",
180 .type = QEMU_OPT_STRING,
181 }, {
182 .name = "path",
183 .type = QEMU_OPT_STRING,
184 }, {
185 .name = "mount_tag",
186 .type = QEMU_OPT_STRING,
189 { /*End of list */ }
192 #endif
194 QemuOptsList qemu_device_opts = {
195 .name = "device",
196 .implied_opt_name = "driver",
197 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
198 .desc = {
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 = {
209 .name = "netdev",
210 .implied_opt_name = "type",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
212 .desc = {
214 * no elements => accept any params
215 * validation will happen later
217 { /* end of list */ }
221 QemuOptsList qemu_net_opts = {
222 .name = "net",
223 .implied_opt_name = "type",
224 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
225 .desc = {
227 * no elements => accept any params
228 * validation will happen later
230 { /* end of list */ }
234 QemuOptsList qemu_rtc_opts = {
235 .name = "rtc",
236 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
237 .desc = {
239 .name = "base",
240 .type = QEMU_OPT_STRING,
242 .name = "clock",
243 .type = QEMU_OPT_STRING,
244 #ifdef TARGET_I386
246 .name = "driftfix",
247 .type = QEMU_OPT_STRING,
248 #endif
250 { /* end if list */ }
254 QemuOptsList qemu_global_opts = {
255 .name = "global",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
257 .desc = {
259 .name = "driver",
260 .type = QEMU_OPT_STRING,
262 .name = "property",
263 .type = QEMU_OPT_STRING,
265 .name = "value",
266 .type = QEMU_OPT_STRING,
268 { /* end if list */ }
272 QemuOptsList qemu_mon_opts = {
273 .name = "mon",
274 .implied_opt_name = "chardev",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
276 .desc = {
278 .name = "mode",
279 .type = QEMU_OPT_STRING,
281 .name = "chardev",
282 .type = QEMU_OPT_STRING,
284 .name = "default",
285 .type = QEMU_OPT_BOOL,
287 { /* end if list */ }
291 QemuOptsList qemu_cpudef_opts = {
292 .name = "cpudef",
293 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
294 .desc = {
296 .name = "name",
297 .type = QEMU_OPT_STRING,
299 .name = "level",
300 .type = QEMU_OPT_NUMBER,
302 .name = "vendor",
303 .type = QEMU_OPT_STRING,
305 .name = "family",
306 .type = QEMU_OPT_NUMBER,
308 .name = "model",
309 .type = QEMU_OPT_NUMBER,
311 .name = "stepping",
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,
326 .name = "xlevel",
327 .type = QEMU_OPT_NUMBER,
329 .name = "model_id",
330 .type = QEMU_OPT_STRING,
332 .name = "vendor_override",
333 .type = QEMU_OPT_NUMBER,
335 { /* end of list */ }
339 QemuOptsList qemu_machine_opts = {
340 .name = "machine",
341 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
342 .desc = {
343 { /* end of list */ }
347 static QemuOptsList *vm_config_groups[] = {
348 &qemu_drive_opts,
349 &qemu_chardev_opts,
350 &qemu_device_opts,
351 &qemu_netdev_opts,
352 &qemu_net_opts,
353 &qemu_rtc_opts,
354 &qemu_global_opts,
355 &qemu_mon_opts,
356 &qemu_cpudef_opts,
357 &qemu_machine_opts,
358 NULL,
361 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
363 int i;
365 for (i = 0; lists[i] != NULL; i++) {
366 if (strcmp(lists[i]->name, group) == 0)
367 break;
369 if (lists[i] == NULL) {
370 error_report("there is no option group \"%s\"", group);
372 return lists[i];
375 QemuOptsList *qemu_find_opts(const char *group)
377 return find_list(vm_config_groups, group);
380 int qemu_set_option(const char *str)
382 char group[64], id[64], arg[64];
383 QemuOptsList *list;
384 QemuOpts *opts;
385 int rc, offset;
387 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
388 if (rc < 3 || str[offset] != '=') {
389 error_report("can't parse: \"%s\"", str);
390 return -1;
393 list = qemu_find_opts(group);
394 if (list == NULL) {
395 return -1;
398 opts = qemu_opts_find(list, id);
399 if (!opts) {
400 error_report("there is no %s \"%s\" defined",
401 list->name, id);
402 return -1;
405 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
406 return -1;
408 return 0;
411 int qemu_global_option(const char *str)
413 char driver[64], property[64];
414 QemuOpts *opts;
415 int rc, offset;
417 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
418 if (rc < 2 || str[offset] != '=') {
419 error_report("can't parse: \"%s\"", str);
420 return -1;
423 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
424 qemu_opt_set(opts, "driver", driver);
425 qemu_opt_set(opts, "property", property);
426 qemu_opt_set(opts, "value", str+offset+1);
427 return 0;
430 struct ConfigWriteData {
431 QemuOptsList *list;
432 FILE *fp;
435 static int config_write_opt(const char *name, const char *value, void *opaque)
437 struct ConfigWriteData *data = opaque;
439 fprintf(data->fp, " %s = \"%s\"\n", name, value);
440 return 0;
443 static int config_write_opts(QemuOpts *opts, void *opaque)
445 struct ConfigWriteData *data = opaque;
446 const char *id = qemu_opts_id(opts);
448 if (id) {
449 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
450 } else {
451 fprintf(data->fp, "[%s]\n", data->list->name);
453 qemu_opt_foreach(opts, config_write_opt, data, 0);
454 fprintf(data->fp, "\n");
455 return 0;
458 void qemu_config_write(FILE *fp)
460 struct ConfigWriteData data = { .fp = fp };
461 QemuOptsList **lists = vm_config_groups;
462 int i;
464 fprintf(fp, "# qemu config file\n\n");
465 for (i = 0; lists[i] != NULL; i++) {
466 data.list = lists[i];
467 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
471 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
473 char line[1024], group[64], id[64], arg[64], value[1024];
474 Location loc;
475 QemuOptsList *list = NULL;
476 QemuOpts *opts = NULL;
477 int res = -1, lno = 0;
479 loc_push_none(&loc);
480 while (fgets(line, sizeof(line), fp) != NULL) {
481 loc_set_file(fname, ++lno);
482 if (line[0] == '\n') {
483 /* skip empty lines */
484 continue;
486 if (line[0] == '#') {
487 /* comment */
488 continue;
490 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
491 /* group with id */
492 list = find_list(lists, group);
493 if (list == NULL)
494 goto out;
495 opts = qemu_opts_create(list, id, 1);
496 continue;
498 if (sscanf(line, "[%63[^]]]", group) == 1) {
499 /* group without id */
500 list = find_list(lists, group);
501 if (list == NULL)
502 goto out;
503 opts = qemu_opts_create(list, NULL, 0);
504 continue;
506 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
507 /* arg = value */
508 if (opts == NULL) {
509 error_report("no group defined");
510 goto out;
512 if (qemu_opt_set(opts, arg, value) != 0) {
513 goto out;
515 continue;
517 error_report("parse error");
518 goto out;
520 if (ferror(fp)) {
521 error_report("error reading file");
522 goto out;
524 res = 0;
525 out:
526 loc_pop(&loc);
527 return res;
530 int qemu_read_config_file(const char *filename)
532 FILE *f = fopen(filename, "r");
533 int ret;
535 if (f == NULL) {
536 return -errno;
539 ret = qemu_config_parse(f, vm_config_groups, filename);
540 fclose(f);
542 if (ret == 0) {
543 return 0;
544 } else {
545 return -EINVAL;