]> git.proxmox.com Git - qemu.git/blame - qemu-config.c
dma-helpers: add accounting wrappers
[qemu.git] / qemu-config.c
CommitLineData
7282a033 1#include "qemu-common.h"
2f792016 2#include "qemu-error.h"
7282a033
GH
3#include "qemu-option.h"
4#include "qemu-config.h"
d0fef6fb 5#include "hw/qdev.h"
7282a033 6
3329f07b 7static QemuOptsList qemu_drive_opts = {
7282a033 8 .name = "drive",
72cf2d4f 9 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
7282a033
GH
10 .desc = {
11 {
12 .name = "bus",
13 .type = QEMU_OPT_NUMBER,
14 .help = "bus number",
15 },{
16 .name = "unit",
17 .type = QEMU_OPT_NUMBER,
18 .help = "unit number (i.e. lun for scsi)",
19 },{
20 .name = "if",
21 .type = QEMU_OPT_STRING,
22 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
23 },{
24 .name = "index",
25 .type = QEMU_OPT_NUMBER,
69d7e218 26 .help = "index number",
7282a033
GH
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,
69d7e218 50 .help = "enable/disable snapshot mode",
7282a033
GH
51 },{
52 .name = "file",
53 .type = QEMU_OPT_STRING,
54 .help = "disk image",
55 },{
56 .name = "cache",
57 .type = QEMU_OPT_STRING,
92196b2f
SH
58 .help = "host cache usage (none, writeback, writethrough, "
59 "directsync, unsafe)",
5c6c3a6c
CH
60 },{
61 .name = "aio",
62 .type = QEMU_OPT_STRING,
63 .help = "host AIO implementation (threads, native)",
7282a033
GH
64 },{
65 .name = "format",
66 .type = QEMU_OPT_STRING,
67 .help = "disk format (raw, qcow2, ...)",
68 },{
69 .name = "serial",
70 .type = QEMU_OPT_STRING,
69d7e218 71 .help = "disk serial number",
e9b2e818
KW
72 },{
73 .name = "rerror",
74 .type = QEMU_OPT_STRING,
69d7e218 75 .help = "read error action",
7282a033
GH
76 },{
77 .name = "werror",
78 .type = QEMU_OPT_STRING,
69d7e218 79 .help = "write error action",
7282a033
GH
80 },{
81 .name = "addr",
82 .type = QEMU_OPT_STRING,
83 .help = "pci address (virtio only)",
59f2689d
NS
84 },{
85 .name = "readonly",
86 .type = QEMU_OPT_BOOL,
69d7e218 87 .help = "open drive file as read-only",
0563e191
ZYW
88 },{
89 .name = "iops",
90 .type = QEMU_OPT_NUMBER,
91 .help = "limit total I/O operations per second",
92 },{
93 .name = "iops_rd",
94 .type = QEMU_OPT_NUMBER,
95 .help = "limit read operations per second",
96 },{
97 .name = "iops_wr",
98 .type = QEMU_OPT_NUMBER,
99 .help = "limit write operations per second",
100 },{
101 .name = "bps",
102 .type = QEMU_OPT_NUMBER,
103 .help = "limit total bytes per second",
104 },{
105 .name = "bps_rd",
106 .type = QEMU_OPT_NUMBER,
107 .help = "limit read bytes per second",
108 },{
109 .name = "bps_wr",
110 .type = QEMU_OPT_NUMBER,
111 .help = "limit write bytes per second",
fb0490f6
SH
112 },{
113 .name = "copy-on-read",
114 .type = QEMU_OPT_BOOL,
115 .help = "copy read data from backing file into image file",
7282a033 116 },
26056e0c 117 { /* end of list */ }
7282a033
GH
118 },
119};
120
f9dadc98
RS
121static QemuOptsList qemu_iscsi_opts = {
122 .name = "iscsi",
123 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
124 .desc = {
125 {
126 .name = "user",
127 .type = QEMU_OPT_STRING,
128 .help = "username for CHAP authentication to target",
129 },{
130 .name = "password",
131 .type = QEMU_OPT_STRING,
132 .help = "password for CHAP authentication to target",
133 },{
134 .name = "header-digest",
135 .type = QEMU_OPT_STRING,
136 .help = "HeaderDigest setting. "
137 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
138 },{
139 .name = "initiator-name",
140 .type = QEMU_OPT_STRING,
141 .help = "Initiator iqn name to use when connecting",
142 },
143 { /* end of list */ }
144 },
145};
146
3329f07b 147static QemuOptsList qemu_chardev_opts = {
191bc01b 148 .name = "chardev",
8212c64f 149 .implied_opt_name = "backend",
72cf2d4f 150 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
191bc01b 151 .desc = {
7d31544f
GH
152 {
153 .name = "backend",
154 .type = QEMU_OPT_STRING,
155 },{
156 .name = "path",
157 .type = QEMU_OPT_STRING,
aeb2c47a
GH
158 },{
159 .name = "host",
160 .type = QEMU_OPT_STRING,
161 },{
162 .name = "port",
163 .type = QEMU_OPT_STRING,
7e1b35b4
GH
164 },{
165 .name = "localaddr",
166 .type = QEMU_OPT_STRING,
167 },{
168 .name = "localport",
169 .type = QEMU_OPT_STRING,
aeb2c47a
GH
170 },{
171 .name = "to",
172 .type = QEMU_OPT_NUMBER,
173 },{
174 .name = "ipv4",
175 .type = QEMU_OPT_BOOL,
176 },{
177 .name = "ipv6",
178 .type = QEMU_OPT_BOOL,
179 },{
180 .name = "wait",
181 .type = QEMU_OPT_BOOL,
182 },{
183 .name = "server",
184 .type = QEMU_OPT_BOOL,
185 },{
186 .name = "delay",
187 .type = QEMU_OPT_BOOL,
188 },{
189 .name = "telnet",
190 .type = QEMU_OPT_BOOL,
6ea314d9
GH
191 },{
192 .name = "width",
193 .type = QEMU_OPT_NUMBER,
194 },{
195 .name = "height",
196 .type = QEMU_OPT_NUMBER,
197 },{
198 .name = "cols",
199 .type = QEMU_OPT_NUMBER,
200 },{
201 .name = "rows",
202 .type = QEMU_OPT_NUMBER,
c845f401
GH
203 },{
204 .name = "mux",
205 .type = QEMU_OPT_BOOL,
5989020b
KK
206 },{
207 .name = "signal",
208 .type = QEMU_OPT_BOOL,
cbcc6336
AL
209 },{
210 .name = "name",
211 .type = QEMU_OPT_STRING,
212 },{
213 .name = "debug",
214 .type = QEMU_OPT_NUMBER,
7d31544f 215 },
26056e0c 216 { /* end of list */ }
191bc01b
GH
217 },
218};
219
74db920c
GS
220QemuOptsList qemu_fsdev_opts = {
221 .name = "fsdev",
fbcbf101 222 .implied_opt_name = "fsdriver",
74db920c
GS
223 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
224 .desc = {
225 {
fbcbf101 226 .name = "fsdriver",
74db920c
GS
227 .type = QEMU_OPT_STRING,
228 }, {
229 .name = "path",
230 .type = QEMU_OPT_STRING,
9ce56db6
VJJ
231 }, {
232 .name = "security_model",
233 .type = QEMU_OPT_STRING,
d3ab98e6
AK
234 }, {
235 .name = "writeout",
236 .type = QEMU_OPT_STRING,
2c74c2cb
MK
237 }, {
238 .name = "readonly",
239 .type = QEMU_OPT_BOOL,
84a87cc4
MK
240
241 }, {
242 .name = "socket",
243 .type = QEMU_OPT_STRING,
4c793dda
MK
244 }, {
245 .name = "sock_fd",
246 .type = QEMU_OPT_NUMBER,
74db920c 247 },
2c74c2cb 248
74db920c
GS
249 { /*End of list */ }
250 },
251};
74db920c 252
3d54abc7
GS
253QemuOptsList qemu_virtfs_opts = {
254 .name = "virtfs",
fbcbf101 255 .implied_opt_name = "fsdriver",
3d54abc7
GS
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
257 .desc = {
258 {
fbcbf101 259 .name = "fsdriver",
3d54abc7
GS
260 .type = QEMU_OPT_STRING,
261 }, {
262 .name = "path",
263 .type = QEMU_OPT_STRING,
264 }, {
265 .name = "mount_tag",
266 .type = QEMU_OPT_STRING,
9ce56db6
VJJ
267 }, {
268 .name = "security_model",
269 .type = QEMU_OPT_STRING,
d3ab98e6
AK
270 }, {
271 .name = "writeout",
272 .type = QEMU_OPT_STRING,
2c74c2cb
MK
273 }, {
274 .name = "readonly",
275 .type = QEMU_OPT_BOOL,
84a87cc4
MK
276 }, {
277 .name = "socket",
278 .type = QEMU_OPT_STRING,
4c793dda
MK
279 }, {
280 .name = "sock_fd",
281 .type = QEMU_OPT_NUMBER,
3d54abc7
GS
282 },
283
284 { /*End of list */ }
285 },
286};
3d54abc7 287
3329f07b 288static QemuOptsList qemu_device_opts = {
f31d07d1 289 .name = "device",
8212c64f 290 .implied_opt_name = "driver",
72cf2d4f 291 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
f31d07d1
GH
292 .desc = {
293 /*
294 * no elements => accept any
295 * sanity checking will happen later
296 * when setting device properties
297 */
26056e0c 298 { /* end of list */ }
f31d07d1
GH
299 },
300};
301
3329f07b 302static QemuOptsList qemu_netdev_opts = {
a1ea458f 303 .name = "netdev",
8212c64f 304 .implied_opt_name = "type",
a1ea458f
MM
305 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
306 .desc = {
307 /*
308 * no elements => accept any params
309 * validation will happen later
310 */
311 { /* end of list */ }
312 },
313};
314
3329f07b 315static QemuOptsList qemu_net_opts = {
8119b33d 316 .name = "net",
8212c64f 317 .implied_opt_name = "type",
8119b33d
MM
318 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
319 .desc = {
320 /*
321 * no elements => accept any params
322 * validation will happen later
323 */
324 { /* end of list */ }
325 },
326};
327
3329f07b 328static QemuOptsList qemu_rtc_opts = {
1ed2fc1f
JK
329 .name = "rtc",
330 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
331 .desc = {
332 {
333 .name = "base",
334 .type = QEMU_OPT_STRING,
6875204c
JK
335 },{
336 .name = "clock",
337 .type = QEMU_OPT_STRING,
1ed2fc1f
JK
338 },{
339 .name = "driftfix",
340 .type = QEMU_OPT_STRING,
1ed2fc1f 341 },
26056e0c 342 { /* end of list */ }
1ed2fc1f
JK
343 },
344};
345
3329f07b 346static QemuOptsList qemu_global_opts = {
d0fef6fb
GH
347 .name = "global",
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
349 .desc = {
350 {
351 .name = "driver",
352 .type = QEMU_OPT_STRING,
353 },{
354 .name = "property",
355 .type = QEMU_OPT_STRING,
356 },{
357 .name = "value",
358 .type = QEMU_OPT_STRING,
359 },
26056e0c 360 { /* end of list */ }
d0fef6fb
GH
361 },
362};
363
3329f07b 364static QemuOptsList qemu_mon_opts = {
88589343 365 .name = "mon",
8212c64f 366 .implied_opt_name = "chardev",
88589343
GH
367 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
368 .desc = {
369 {
370 .name = "mode",
371 .type = QEMU_OPT_STRING,
372 },{
373 .name = "chardev",
374 .type = QEMU_OPT_STRING,
375 },{
376 .name = "default",
377 .type = QEMU_OPT_BOOL,
39eaab9a
DB
378 },{
379 .name = "pretty",
380 .type = QEMU_OPT_BOOL,
88589343 381 },
26056e0c 382 { /* end of list */ }
88589343
GH
383 },
384};
385
ab6540d5
PS
386static QemuOptsList qemu_trace_opts = {
387 .name = "trace",
388 .implied_opt_name = "trace",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
390 .desc = {
391 {
23d15e86
L
392 .name = "events",
393 .type = QEMU_OPT_STRING,
394 },{
ab6540d5
PS
395 .name = "file",
396 .type = QEMU_OPT_STRING,
397 },
44bd6907 398 { /* end of list */ }
ab6540d5
PS
399 },
400};
ab6540d5 401
3329f07b 402static QemuOptsList qemu_cpudef_opts = {
b5ec5ce0 403 .name = "cpudef",
404 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
405 .desc = {
406 {
407 .name = "name",
408 .type = QEMU_OPT_STRING,
409 },{
410 .name = "level",
411 .type = QEMU_OPT_NUMBER,
412 },{
413 .name = "vendor",
414 .type = QEMU_OPT_STRING,
415 },{
416 .name = "family",
417 .type = QEMU_OPT_NUMBER,
418 },{
419 .name = "model",
420 .type = QEMU_OPT_NUMBER,
421 },{
422 .name = "stepping",
423 .type = QEMU_OPT_NUMBER,
424 },{
425 .name = "feature_edx", /* cpuid 0000_0001.edx */
426 .type = QEMU_OPT_STRING,
427 },{
428 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
429 .type = QEMU_OPT_STRING,
430 },{
431 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
432 .type = QEMU_OPT_STRING,
433 },{
434 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
435 .type = QEMU_OPT_STRING,
436 },{
437 .name = "xlevel",
438 .type = QEMU_OPT_NUMBER,
439 },{
440 .name = "model_id",
441 .type = QEMU_OPT_STRING,
442 },{
443 .name = "vendor_override",
444 .type = QEMU_OPT_NUMBER,
445 },
446 { /* end of list */ }
447 },
448};
449
29b0040b
GH
450QemuOptsList qemu_spice_opts = {
451 .name = "spice",
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
453 .desc = {
454 {
455 .name = "port",
456 .type = QEMU_OPT_NUMBER,
c448e855
GH
457 },{
458 .name = "tls-port",
459 .type = QEMU_OPT_NUMBER,
333b0eeb
GH
460 },{
461 .name = "addr",
462 .type = QEMU_OPT_STRING,
463 },{
464 .name = "ipv4",
465 .type = QEMU_OPT_BOOL,
466 },{
467 .name = "ipv6",
468 .type = QEMU_OPT_BOOL,
29b0040b
GH
469 },{
470 .name = "password",
471 .type = QEMU_OPT_STRING,
472 },{
473 .name = "disable-ticketing",
474 .type = QEMU_OPT_BOOL,
d4970b07
HG
475 },{
476 .name = "disable-copy-paste",
477 .type = QEMU_OPT_BOOL,
48b3ed0a
MAL
478 },{
479 .name = "sasl",
480 .type = QEMU_OPT_BOOL,
c448e855
GH
481 },{
482 .name = "x509-dir",
483 .type = QEMU_OPT_STRING,
484 },{
485 .name = "x509-key-file",
486 .type = QEMU_OPT_STRING,
487 },{
488 .name = "x509-key-password",
489 .type = QEMU_OPT_STRING,
490 },{
491 .name = "x509-cert-file",
492 .type = QEMU_OPT_STRING,
493 },{
494 .name = "x509-cacert-file",
495 .type = QEMU_OPT_STRING,
496 },{
497 .name = "x509-dh-key-file",
498 .type = QEMU_OPT_STRING,
499 },{
500 .name = "tls-ciphers",
501 .type = QEMU_OPT_STRING,
17b6dea0
GH
502 },{
503 .name = "tls-channel",
504 .type = QEMU_OPT_STRING,
505 },{
506 .name = "plaintext-channel",
507 .type = QEMU_OPT_STRING,
9f04e09e
YH
508 },{
509 .name = "image-compression",
510 .type = QEMU_OPT_STRING,
511 },{
512 .name = "jpeg-wan-compression",
513 .type = QEMU_OPT_STRING,
514 },{
515 .name = "zlib-glz-wan-compression",
516 .type = QEMU_OPT_STRING,
84a23f25
GH
517 },{
518 .name = "streaming-video",
519 .type = QEMU_OPT_STRING,
520 },{
521 .name = "agent-mouse",
522 .type = QEMU_OPT_BOOL,
523 },{
524 .name = "playback-compression",
525 .type = QEMU_OPT_BOOL,
29b0040b 526 },
44bd6907 527 { /* end of list */ }
29b0040b
GH
528 },
529};
530
2e55e842
GN
531QemuOptsList qemu_option_rom_opts = {
532 .name = "option-rom",
533 .implied_opt_name = "romfile",
534 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
535 .desc = {
536 {
537 .name = "bootindex",
538 .type = QEMU_OPT_NUMBER,
539 }, {
540 .name = "romfile",
541 .type = QEMU_OPT_STRING,
542 },
44bd6907 543 { /* end of list */ }
2e55e842
GN
544 },
545};
546
303d4e86
AP
547static QemuOptsList qemu_machine_opts = {
548 .name = "machine",
9052ea6b 549 .implied_opt_name = "type",
9de36b1a 550 .merge_lists = true,
303d4e86
AP
551 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
552 .desc = {
553 {
9052ea6b
JK
554 .name = "type",
555 .type = QEMU_OPT_STRING,
556 .help = "emulated machine"
557 }, {
303d4e86
AP
558 .name = "accel",
559 .type = QEMU_OPT_STRING,
560 .help = "accelerator list",
6a48ffaa
JK
561 }, {
562 .name = "kernel_irqchip",
563 .type = QEMU_OPT_BOOL,
564 .help = "use KVM in-kernel irqchip",
39d6960a
JK
565 }, {
566 .name = "kvm_shadow_mem",
567 .type = QEMU_OPT_SIZE,
568 .help = "KVM shadow MMU size",
303d4e86
AP
569 },
570 { /* End of list */ }
571 },
572};
573
3d3b8303 574QemuOptsList qemu_boot_opts = {
575 .name = "boot-opts",
576 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
577 .desc = {
578 /* the three names below are not used now */
579 {
580 .name = "order",
581 .type = QEMU_OPT_STRING,
582 }, {
583 .name = "once",
584 .type = QEMU_OPT_STRING,
585 }, {
586 .name = "menu",
587 .type = QEMU_OPT_STRING,
588 /* following are really used */
589 }, {
590 .name = "splash",
591 .type = QEMU_OPT_STRING,
592 }, {
593 .name = "splash-time",
594 .type = QEMU_OPT_STRING,
595 },
596 { /*End of list */ }
597 },
598};
599
dfe795e7 600static QemuOptsList *vm_config_groups[32] = {
d058fe03 601 &qemu_drive_opts,
191bc01b 602 &qemu_chardev_opts,
f31d07d1 603 &qemu_device_opts,
a1ea458f 604 &qemu_netdev_opts,
8119b33d 605 &qemu_net_opts,
5fdfbf7e 606 &qemu_rtc_opts,
d0fef6fb 607 &qemu_global_opts,
88589343 608 &qemu_mon_opts,
b5ec5ce0 609 &qemu_cpudef_opts,
ab6540d5 610 &qemu_trace_opts,
2e55e842 611 &qemu_option_rom_opts,
303d4e86 612 &qemu_machine_opts,
3d3b8303 613 &qemu_boot_opts,
f9dadc98 614 &qemu_iscsi_opts,
d058fe03
GH
615 NULL,
616};
617
490b648e 618static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
ddc97855
GH
619{
620 int i;
621
622 for (i = 0; lists[i] != NULL; i++) {
623 if (strcmp(lists[i]->name, group) == 0)
624 break;
625 }
626 if (lists[i] == NULL) {
1ecda02b 627 error_report("there is no option group \"%s\"", group);
ddc97855
GH
628 }
629 return lists[i];
630}
631
490b648e
KW
632QemuOptsList *qemu_find_opts(const char *group)
633{
634 return find_list(vm_config_groups, group);
635}
636
dfe795e7
GH
637void qemu_add_opts(QemuOptsList *list)
638{
639 int entries, i;
640
641 entries = ARRAY_SIZE(vm_config_groups);
642 entries--; /* keep list NULL terminated */
643 for (i = 0; i < entries; i++) {
644 if (vm_config_groups[i] == NULL) {
645 vm_config_groups[i] = list;
646 return;
647 }
648 }
649 fprintf(stderr, "ran out of space in vm_config_groups");
650 abort();
651}
652
d058fe03
GH
653int qemu_set_option(const char *str)
654{
655 char group[64], id[64], arg[64];
ddc97855 656 QemuOptsList *list;
d058fe03 657 QemuOpts *opts;
ddc97855 658 int rc, offset;
d058fe03
GH
659
660 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
661 if (rc < 3 || str[offset] != '=') {
1ecda02b 662 error_report("can't parse: \"%s\"", str);
d058fe03
GH
663 return -1;
664 }
665
304329ee 666 list = qemu_find_opts(group);
ddc97855 667 if (list == NULL) {
d058fe03
GH
668 return -1;
669 }
670
ddc97855 671 opts = qemu_opts_find(list, id);
d058fe03 672 if (!opts) {
1ecda02b
MA
673 error_report("there is no %s \"%s\" defined",
674 list->name, id);
d058fe03
GH
675 return -1;
676 }
677
3df04ac3 678 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
d058fe03
GH
679 return -1;
680 }
681 return 0;
682}
683
d0fef6fb
GH
684int qemu_global_option(const char *str)
685{
686 char driver[64], property[64];
687 QemuOpts *opts;
688 int rc, offset;
689
690 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
691 if (rc < 2 || str[offset] != '=') {
1ecda02b 692 error_report("can't parse: \"%s\"", str);
d0fef6fb
GH
693 return -1;
694 }
695
696 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
697 qemu_opt_set(opts, "driver", driver);
698 qemu_opt_set(opts, "property", property);
699 qemu_opt_set(opts, "value", str+offset+1);
700 return 0;
701}
702
9d993394
GH
703struct ConfigWriteData {
704 QemuOptsList *list;
705 FILE *fp;
706};
707
708static int config_write_opt(const char *name, const char *value, void *opaque)
709{
710 struct ConfigWriteData *data = opaque;
711
712 fprintf(data->fp, " %s = \"%s\"\n", name, value);
713 return 0;
714}
715
716static int config_write_opts(QemuOpts *opts, void *opaque)
717{
718 struct ConfigWriteData *data = opaque;
719 const char *id = qemu_opts_id(opts);
720
721 if (id) {
722 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
723 } else {
724 fprintf(data->fp, "[%s]\n", data->list->name);
725 }
726 qemu_opt_foreach(opts, config_write_opt, data, 0);
727 fprintf(data->fp, "\n");
728 return 0;
729}
730
731void qemu_config_write(FILE *fp)
732{
733 struct ConfigWriteData data = { .fp = fp };
490b648e 734 QemuOptsList **lists = vm_config_groups;
9d993394
GH
735 int i;
736
737 fprintf(fp, "# qemu config file\n\n");
738 for (i = 0; lists[i] != NULL; i++) {
739 data.list = lists[i];
740 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
741 }
742}
42262ba8 743
490b648e 744int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
42262ba8
GH
745{
746 char line[1024], group[64], id[64], arg[64], value[1024];
cf5a65aa 747 Location loc;
42262ba8
GH
748 QemuOptsList *list = NULL;
749 QemuOpts *opts = NULL;
cf5a65aa 750 int res = -1, lno = 0;
42262ba8 751
cf5a65aa 752 loc_push_none(&loc);
42262ba8 753 while (fgets(line, sizeof(line), fp) != NULL) {
cf5a65aa 754 loc_set_file(fname, ++lno);
42262ba8
GH
755 if (line[0] == '\n') {
756 /* skip empty lines */
757 continue;
758 }
759 if (line[0] == '#') {
760 /* comment */
761 continue;
762 }
763 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
764 /* group with id */
490b648e 765 list = find_list(lists, group);
42262ba8 766 if (list == NULL)
cf5a65aa 767 goto out;
42262ba8
GH
768 opts = qemu_opts_create(list, id, 1);
769 continue;
770 }
771 if (sscanf(line, "[%63[^]]]", group) == 1) {
772 /* group without id */
490b648e 773 list = find_list(lists, group);
42262ba8 774 if (list == NULL)
cf5a65aa 775 goto out;
42262ba8
GH
776 opts = qemu_opts_create(list, NULL, 0);
777 continue;
778 }
779 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
780 /* arg = value */
781 if (opts == NULL) {
cf5a65aa
MA
782 error_report("no group defined");
783 goto out;
42262ba8
GH
784 }
785 if (qemu_opt_set(opts, arg, value) != 0) {
cf5a65aa 786 goto out;
42262ba8
GH
787 }
788 continue;
789 }
cf5a65aa
MA
790 error_report("parse error");
791 goto out;
42262ba8 792 }
ef82516d
MA
793 if (ferror(fp)) {
794 error_report("error reading file");
795 goto out;
796 }
cf5a65aa
MA
797 res = 0;
798out:
799 loc_pop(&loc);
800 return res;
42262ba8 801}
dcfb0939
KW
802
803int qemu_read_config_file(const char *filename)
804{
805 FILE *f = fopen(filename, "r");
019e78ba
KW
806 int ret;
807
dcfb0939
KW
808 if (f == NULL) {
809 return -errno;
810 }
811
019e78ba 812 ret = qemu_config_parse(f, vm_config_groups, filename);
dcfb0939
KW
813 fclose(f);
814
019e78ba
KW
815 if (ret == 0) {
816 return 0;
817 } else {
818 return -EINVAL;
819 }
dcfb0939 820}