Merge commit '6b37c87c96a5b148685e8e6bf09d0aca953cb1a8' into upstream-merge
[qemu-kvm/stefanha.git] / qemu-config.c
bloba2ab5571a0e7bad6282401b47a45a49d9df0c148
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 static 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,
82 },{
83 .name = "boot",
84 .type = QEMU_OPT_BOOL,
85 .help = "make this a boot drive",
87 { /* end of list */ }
91 static QemuOptsList qemu_chardev_opts = {
92 .name = "chardev",
93 .implied_opt_name = "backend",
94 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
95 .desc = {
97 .name = "backend",
98 .type = QEMU_OPT_STRING,
99 },{
100 .name = "path",
101 .type = QEMU_OPT_STRING,
103 .name = "host",
104 .type = QEMU_OPT_STRING,
106 .name = "port",
107 .type = QEMU_OPT_STRING,
109 .name = "localaddr",
110 .type = QEMU_OPT_STRING,
112 .name = "localport",
113 .type = QEMU_OPT_STRING,
115 .name = "to",
116 .type = QEMU_OPT_NUMBER,
118 .name = "ipv4",
119 .type = QEMU_OPT_BOOL,
121 .name = "ipv6",
122 .type = QEMU_OPT_BOOL,
124 .name = "wait",
125 .type = QEMU_OPT_BOOL,
127 .name = "server",
128 .type = QEMU_OPT_BOOL,
130 .name = "delay",
131 .type = QEMU_OPT_BOOL,
133 .name = "telnet",
134 .type = QEMU_OPT_BOOL,
136 .name = "width",
137 .type = QEMU_OPT_NUMBER,
139 .name = "height",
140 .type = QEMU_OPT_NUMBER,
142 .name = "cols",
143 .type = QEMU_OPT_NUMBER,
145 .name = "rows",
146 .type = QEMU_OPT_NUMBER,
148 .name = "mux",
149 .type = QEMU_OPT_BOOL,
151 .name = "signal",
152 .type = QEMU_OPT_BOOL,
154 { /* end of list */ }
158 QemuOptsList qemu_fsdev_opts = {
159 .name = "fsdev",
160 .implied_opt_name = "fstype",
161 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
162 .desc = {
164 .name = "fstype",
165 .type = QEMU_OPT_STRING,
166 }, {
167 .name = "path",
168 .type = QEMU_OPT_STRING,
169 }, {
170 .name = "security_model",
171 .type = QEMU_OPT_STRING,
173 { /*End of list */ }
177 QemuOptsList qemu_virtfs_opts = {
178 .name = "virtfs",
179 .implied_opt_name = "fstype",
180 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
181 .desc = {
183 .name = "fstype",
184 .type = QEMU_OPT_STRING,
185 }, {
186 .name = "path",
187 .type = QEMU_OPT_STRING,
188 }, {
189 .name = "mount_tag",
190 .type = QEMU_OPT_STRING,
191 }, {
192 .name = "security_model",
193 .type = QEMU_OPT_STRING,
196 { /*End of list */ }
200 static QemuOptsList qemu_device_opts = {
201 .name = "device",
202 .implied_opt_name = "driver",
203 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
204 .desc = {
206 * no elements => accept any
207 * sanity checking will happen later
208 * when setting device properties
210 { /* end of list */ }
214 static QemuOptsList qemu_netdev_opts = {
215 .name = "netdev",
216 .implied_opt_name = "type",
217 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
218 .desc = {
220 * no elements => accept any params
221 * validation will happen later
223 { /* end of list */ }
227 static QemuOptsList qemu_net_opts = {
228 .name = "net",
229 .implied_opt_name = "type",
230 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
231 .desc = {
233 * no elements => accept any params
234 * validation will happen later
236 { /* end of list */ }
240 static QemuOptsList qemu_rtc_opts = {
241 .name = "rtc",
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
243 .desc = {
245 .name = "base",
246 .type = QEMU_OPT_STRING,
248 .name = "clock",
249 .type = QEMU_OPT_STRING,
251 .name = "driftfix",
252 .type = QEMU_OPT_STRING,
254 { /* end of list */ }
258 static QemuOptsList qemu_global_opts = {
259 .name = "global",
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
261 .desc = {
263 .name = "driver",
264 .type = QEMU_OPT_STRING,
266 .name = "property",
267 .type = QEMU_OPT_STRING,
269 .name = "value",
270 .type = QEMU_OPT_STRING,
272 { /* end of list */ }
276 static QemuOptsList qemu_mon_opts = {
277 .name = "mon",
278 .implied_opt_name = "chardev",
279 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
280 .desc = {
282 .name = "mode",
283 .type = QEMU_OPT_STRING,
285 .name = "chardev",
286 .type = QEMU_OPT_STRING,
288 .name = "default",
289 .type = QEMU_OPT_BOOL,
291 { /* end of list */ }
295 #ifdef CONFIG_SIMPLE_TRACE
296 static QemuOptsList qemu_trace_opts = {
297 .name = "trace",
298 .implied_opt_name = "trace",
299 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
300 .desc = {
302 .name = "file",
303 .type = QEMU_OPT_STRING,
305 { /* end if list */ }
308 #endif
310 static QemuOptsList qemu_cpudef_opts = {
311 .name = "cpudef",
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
313 .desc = {
315 .name = "name",
316 .type = QEMU_OPT_STRING,
318 .name = "level",
319 .type = QEMU_OPT_NUMBER,
321 .name = "vendor",
322 .type = QEMU_OPT_STRING,
324 .name = "family",
325 .type = QEMU_OPT_NUMBER,
327 .name = "model",
328 .type = QEMU_OPT_NUMBER,
330 .name = "stepping",
331 .type = QEMU_OPT_NUMBER,
333 .name = "feature_edx", /* cpuid 0000_0001.edx */
334 .type = QEMU_OPT_STRING,
336 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
337 .type = QEMU_OPT_STRING,
339 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
340 .type = QEMU_OPT_STRING,
342 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
343 .type = QEMU_OPT_STRING,
345 .name = "xlevel",
346 .type = QEMU_OPT_NUMBER,
348 .name = "model_id",
349 .type = QEMU_OPT_STRING,
351 .name = "vendor_override",
352 .type = QEMU_OPT_NUMBER,
354 { /* end of list */ }
358 static QemuOptsList *vm_config_groups[32] = {
359 &qemu_drive_opts,
360 &qemu_chardev_opts,
361 &qemu_device_opts,
362 &qemu_netdev_opts,
363 &qemu_net_opts,
364 &qemu_rtc_opts,
365 &qemu_global_opts,
366 &qemu_mon_opts,
367 &qemu_cpudef_opts,
368 #ifdef CONFIG_SIMPLE_TRACE
369 &qemu_trace_opts,
370 #endif
371 NULL,
374 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
376 int i;
378 for (i = 0; lists[i] != NULL; i++) {
379 if (strcmp(lists[i]->name, group) == 0)
380 break;
382 if (lists[i] == NULL) {
383 error_report("there is no option group \"%s\"", group);
385 return lists[i];
388 QemuOptsList *qemu_find_opts(const char *group)
390 return find_list(vm_config_groups, group);
393 void qemu_add_opts(QemuOptsList *list)
395 int entries, i;
397 entries = ARRAY_SIZE(vm_config_groups);
398 entries--; /* keep list NULL terminated */
399 for (i = 0; i < entries; i++) {
400 if (vm_config_groups[i] == NULL) {
401 vm_config_groups[i] = list;
402 return;
405 fprintf(stderr, "ran out of space in vm_config_groups");
406 abort();
409 int qemu_set_option(const char *str)
411 char group[64], id[64], arg[64];
412 QemuOptsList *list;
413 QemuOpts *opts;
414 int rc, offset;
416 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
417 if (rc < 3 || str[offset] != '=') {
418 error_report("can't parse: \"%s\"", str);
419 return -1;
422 list = qemu_find_opts(group);
423 if (list == NULL) {
424 return -1;
427 opts = qemu_opts_find(list, id);
428 if (!opts) {
429 error_report("there is no %s \"%s\" defined",
430 list->name, id);
431 return -1;
434 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
435 return -1;
437 return 0;
440 int qemu_global_option(const char *str)
442 char driver[64], property[64];
443 QemuOpts *opts;
444 int rc, offset;
446 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
447 if (rc < 2 || str[offset] != '=') {
448 error_report("can't parse: \"%s\"", str);
449 return -1;
452 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
453 qemu_opt_set(opts, "driver", driver);
454 qemu_opt_set(opts, "property", property);
455 qemu_opt_set(opts, "value", str+offset+1);
456 return 0;
459 struct ConfigWriteData {
460 QemuOptsList *list;
461 FILE *fp;
464 static int config_write_opt(const char *name, const char *value, void *opaque)
466 struct ConfigWriteData *data = opaque;
468 fprintf(data->fp, " %s = \"%s\"\n", name, value);
469 return 0;
472 static int config_write_opts(QemuOpts *opts, void *opaque)
474 struct ConfigWriteData *data = opaque;
475 const char *id = qemu_opts_id(opts);
477 if (id) {
478 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
479 } else {
480 fprintf(data->fp, "[%s]\n", data->list->name);
482 qemu_opt_foreach(opts, config_write_opt, data, 0);
483 fprintf(data->fp, "\n");
484 return 0;
487 void qemu_config_write(FILE *fp)
489 struct ConfigWriteData data = { .fp = fp };
490 QemuOptsList **lists = vm_config_groups;
491 int i;
493 fprintf(fp, "# qemu config file\n\n");
494 for (i = 0; lists[i] != NULL; i++) {
495 data.list = lists[i];
496 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
500 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
502 char line[1024], group[64], id[64], arg[64], value[1024];
503 Location loc;
504 QemuOptsList *list = NULL;
505 QemuOpts *opts = NULL;
506 int res = -1, lno = 0;
508 loc_push_none(&loc);
509 while (fgets(line, sizeof(line), fp) != NULL) {
510 loc_set_file(fname, ++lno);
511 if (line[0] == '\n') {
512 /* skip empty lines */
513 continue;
515 if (line[0] == '#') {
516 /* comment */
517 continue;
519 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
520 /* group with id */
521 list = find_list(lists, group);
522 if (list == NULL)
523 goto out;
524 opts = qemu_opts_create(list, id, 1);
525 continue;
527 if (sscanf(line, "[%63[^]]]", group) == 1) {
528 /* group without id */
529 list = find_list(lists, group);
530 if (list == NULL)
531 goto out;
532 opts = qemu_opts_create(list, NULL, 0);
533 continue;
535 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
536 /* arg = value */
537 if (opts == NULL) {
538 error_report("no group defined");
539 goto out;
541 if (qemu_opt_set(opts, arg, value) != 0) {
542 goto out;
544 continue;
546 error_report("parse error");
547 goto out;
549 if (ferror(fp)) {
550 error_report("error reading file");
551 goto out;
553 res = 0;
554 out:
555 loc_pop(&loc);
556 return res;
559 int qemu_read_config_file(const char *filename)
561 FILE *f = fopen(filename, "r");
562 int ret;
564 if (f == NULL) {
565 return -errno;
568 ret = qemu_config_parse(f, vm_config_groups, filename);
569 fclose(f);
571 if (ret == 0) {
572 return 0;
573 } else {
574 return -EINVAL;