]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-config.c
qmp: add NBD server commands
[mirror_qemu.git] / qemu-config.c
1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
5 #include "hw/qdev.h"
6 #include "error.h"
7
8 static QemuOptsList qemu_drive_opts = {
9 .name = "drive",
10 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
11 .desc = {
12 {
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 .help = "index number",
28 },{
29 .name = "cyls",
30 .type = QEMU_OPT_NUMBER,
31 .help = "number of cylinders (ide disk geometry)",
32 },{
33 .name = "heads",
34 .type = QEMU_OPT_NUMBER,
35 .help = "number of heads (ide disk geometry)",
36 },{
37 .name = "secs",
38 .type = QEMU_OPT_NUMBER,
39 .help = "number of sectors (ide disk geometry)",
40 },{
41 .name = "trans",
42 .type = QEMU_OPT_STRING,
43 .help = "chs translation (auto, lba. none)",
44 },{
45 .name = "media",
46 .type = QEMU_OPT_STRING,
47 .help = "media type (disk, cdrom)",
48 },{
49 .name = "snapshot",
50 .type = QEMU_OPT_BOOL,
51 .help = "enable/disable snapshot mode",
52 },{
53 .name = "file",
54 .type = QEMU_OPT_STRING,
55 .help = "disk image",
56 },{
57 .name = "cache",
58 .type = QEMU_OPT_STRING,
59 .help = "host cache usage (none, writeback, writethrough, "
60 "directsync, unsafe)",
61 },{
62 .name = "aio",
63 .type = QEMU_OPT_STRING,
64 .help = "host AIO implementation (threads, native)",
65 },{
66 .name = "format",
67 .type = QEMU_OPT_STRING,
68 .help = "disk format (raw, qcow2, ...)",
69 },{
70 .name = "serial",
71 .type = QEMU_OPT_STRING,
72 .help = "disk serial number",
73 },{
74 .name = "rerror",
75 .type = QEMU_OPT_STRING,
76 .help = "read error action",
77 },{
78 .name = "werror",
79 .type = QEMU_OPT_STRING,
80 .help = "write error action",
81 },{
82 .name = "addr",
83 .type = QEMU_OPT_STRING,
84 .help = "pci address (virtio only)",
85 },{
86 .name = "readonly",
87 .type = QEMU_OPT_BOOL,
88 .help = "open drive file as read-only",
89 },{
90 .name = "iops",
91 .type = QEMU_OPT_NUMBER,
92 .help = "limit total I/O operations per second",
93 },{
94 .name = "iops_rd",
95 .type = QEMU_OPT_NUMBER,
96 .help = "limit read operations per second",
97 },{
98 .name = "iops_wr",
99 .type = QEMU_OPT_NUMBER,
100 .help = "limit write operations per second",
101 },{
102 .name = "bps",
103 .type = QEMU_OPT_NUMBER,
104 .help = "limit total bytes per second",
105 },{
106 .name = "bps_rd",
107 .type = QEMU_OPT_NUMBER,
108 .help = "limit read bytes per second",
109 },{
110 .name = "bps_wr",
111 .type = QEMU_OPT_NUMBER,
112 .help = "limit write bytes per second",
113 },{
114 .name = "copy-on-read",
115 .type = QEMU_OPT_BOOL,
116 .help = "copy read data from backing file into image file",
117 },
118 { /* end of list */ }
119 },
120 };
121
122 static QemuOptsList qemu_iscsi_opts = {
123 .name = "iscsi",
124 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
125 .desc = {
126 {
127 .name = "user",
128 .type = QEMU_OPT_STRING,
129 .help = "username for CHAP authentication to target",
130 },{
131 .name = "password",
132 .type = QEMU_OPT_STRING,
133 .help = "password for CHAP authentication to target",
134 },{
135 .name = "header-digest",
136 .type = QEMU_OPT_STRING,
137 .help = "HeaderDigest setting. "
138 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
139 },{
140 .name = "initiator-name",
141 .type = QEMU_OPT_STRING,
142 .help = "Initiator iqn name to use when connecting",
143 },
144 { /* end of list */ }
145 },
146 };
147
148 static QemuOptsList qemu_chardev_opts = {
149 .name = "chardev",
150 .implied_opt_name = "backend",
151 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
152 .desc = {
153 {
154 .name = "backend",
155 .type = QEMU_OPT_STRING,
156 },{
157 .name = "path",
158 .type = QEMU_OPT_STRING,
159 },{
160 .name = "host",
161 .type = QEMU_OPT_STRING,
162 },{
163 .name = "port",
164 .type = QEMU_OPT_STRING,
165 },{
166 .name = "localaddr",
167 .type = QEMU_OPT_STRING,
168 },{
169 .name = "localport",
170 .type = QEMU_OPT_STRING,
171 },{
172 .name = "to",
173 .type = QEMU_OPT_NUMBER,
174 },{
175 .name = "ipv4",
176 .type = QEMU_OPT_BOOL,
177 },{
178 .name = "ipv6",
179 .type = QEMU_OPT_BOOL,
180 },{
181 .name = "wait",
182 .type = QEMU_OPT_BOOL,
183 },{
184 .name = "server",
185 .type = QEMU_OPT_BOOL,
186 },{
187 .name = "delay",
188 .type = QEMU_OPT_BOOL,
189 },{
190 .name = "telnet",
191 .type = QEMU_OPT_BOOL,
192 },{
193 .name = "width",
194 .type = QEMU_OPT_NUMBER,
195 },{
196 .name = "height",
197 .type = QEMU_OPT_NUMBER,
198 },{
199 .name = "cols",
200 .type = QEMU_OPT_NUMBER,
201 },{
202 .name = "rows",
203 .type = QEMU_OPT_NUMBER,
204 },{
205 .name = "mux",
206 .type = QEMU_OPT_BOOL,
207 },{
208 .name = "signal",
209 .type = QEMU_OPT_BOOL,
210 },{
211 .name = "name",
212 .type = QEMU_OPT_STRING,
213 },{
214 .name = "debug",
215 .type = QEMU_OPT_NUMBER,
216 },
217 { /* end of list */ }
218 },
219 };
220
221 QemuOptsList qemu_fsdev_opts = {
222 .name = "fsdev",
223 .implied_opt_name = "fsdriver",
224 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
225 .desc = {
226 {
227 .name = "fsdriver",
228 .type = QEMU_OPT_STRING,
229 }, {
230 .name = "path",
231 .type = QEMU_OPT_STRING,
232 }, {
233 .name = "security_model",
234 .type = QEMU_OPT_STRING,
235 }, {
236 .name = "writeout",
237 .type = QEMU_OPT_STRING,
238 }, {
239 .name = "readonly",
240 .type = QEMU_OPT_BOOL,
241
242 }, {
243 .name = "socket",
244 .type = QEMU_OPT_STRING,
245 }, {
246 .name = "sock_fd",
247 .type = QEMU_OPT_NUMBER,
248 },
249
250 { /*End of list */ }
251 },
252 };
253
254 QemuOptsList qemu_virtfs_opts = {
255 .name = "virtfs",
256 .implied_opt_name = "fsdriver",
257 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
258 .desc = {
259 {
260 .name = "fsdriver",
261 .type = QEMU_OPT_STRING,
262 }, {
263 .name = "path",
264 .type = QEMU_OPT_STRING,
265 }, {
266 .name = "mount_tag",
267 .type = QEMU_OPT_STRING,
268 }, {
269 .name = "security_model",
270 .type = QEMU_OPT_STRING,
271 }, {
272 .name = "writeout",
273 .type = QEMU_OPT_STRING,
274 }, {
275 .name = "readonly",
276 .type = QEMU_OPT_BOOL,
277 }, {
278 .name = "socket",
279 .type = QEMU_OPT_STRING,
280 }, {
281 .name = "sock_fd",
282 .type = QEMU_OPT_NUMBER,
283 },
284
285 { /*End of list */ }
286 },
287 };
288
289 static QemuOptsList qemu_device_opts = {
290 .name = "device",
291 .implied_opt_name = "driver",
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
293 .desc = {
294 /*
295 * no elements => accept any
296 * sanity checking will happen later
297 * when setting device properties
298 */
299 { /* end of list */ }
300 },
301 };
302
303 static QemuOptsList qemu_netdev_opts = {
304 .name = "netdev",
305 .implied_opt_name = "type",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
307 .desc = {
308 /*
309 * no elements => accept any params
310 * validation will happen later
311 */
312 { /* end of list */ }
313 },
314 };
315
316 static QemuOptsList qemu_net_opts = {
317 .name = "net",
318 .implied_opt_name = "type",
319 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
320 .desc = {
321 /*
322 * no elements => accept any params
323 * validation will happen later
324 */
325 { /* end of list */ }
326 },
327 };
328
329 static QemuOptsList qemu_rtc_opts = {
330 .name = "rtc",
331 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
332 .desc = {
333 {
334 .name = "base",
335 .type = QEMU_OPT_STRING,
336 },{
337 .name = "clock",
338 .type = QEMU_OPT_STRING,
339 },{
340 .name = "driftfix",
341 .type = QEMU_OPT_STRING,
342 },
343 { /* end of list */ }
344 },
345 };
346
347 static QemuOptsList qemu_global_opts = {
348 .name = "global",
349 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
350 .desc = {
351 {
352 .name = "driver",
353 .type = QEMU_OPT_STRING,
354 },{
355 .name = "property",
356 .type = QEMU_OPT_STRING,
357 },{
358 .name = "value",
359 .type = QEMU_OPT_STRING,
360 },
361 { /* end of list */ }
362 },
363 };
364
365 QemuOptsList qemu_sandbox_opts = {
366 .name = "sandbox",
367 .implied_opt_name = "enable",
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
369 .desc = {
370 {
371 .name = "enable",
372 .type = QEMU_OPT_BOOL,
373 },
374 { /* end of list */ }
375 },
376 };
377
378 static QemuOptsList qemu_mon_opts = {
379 .name = "mon",
380 .implied_opt_name = "chardev",
381 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
382 .desc = {
383 {
384 .name = "mode",
385 .type = QEMU_OPT_STRING,
386 },{
387 .name = "chardev",
388 .type = QEMU_OPT_STRING,
389 },{
390 .name = "default",
391 .type = QEMU_OPT_BOOL,
392 },{
393 .name = "pretty",
394 .type = QEMU_OPT_BOOL,
395 },
396 { /* end of list */ }
397 },
398 };
399
400 static QemuOptsList qemu_trace_opts = {
401 .name = "trace",
402 .implied_opt_name = "trace",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
404 .desc = {
405 {
406 .name = "events",
407 .type = QEMU_OPT_STRING,
408 },{
409 .name = "file",
410 .type = QEMU_OPT_STRING,
411 },
412 { /* end of list */ }
413 },
414 };
415
416 static QemuOptsList qemu_cpudef_opts = {
417 .name = "cpudef",
418 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
419 .desc = {
420 {
421 .name = "name",
422 .type = QEMU_OPT_STRING,
423 },{
424 .name = "level",
425 .type = QEMU_OPT_NUMBER,
426 },{
427 .name = "vendor",
428 .type = QEMU_OPT_STRING,
429 },{
430 .name = "family",
431 .type = QEMU_OPT_NUMBER,
432 },{
433 .name = "model",
434 .type = QEMU_OPT_NUMBER,
435 },{
436 .name = "stepping",
437 .type = QEMU_OPT_NUMBER,
438 },{
439 .name = "feature_edx", /* cpuid 0000_0001.edx */
440 .type = QEMU_OPT_STRING,
441 },{
442 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
443 .type = QEMU_OPT_STRING,
444 },{
445 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
446 .type = QEMU_OPT_STRING,
447 },{
448 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
449 .type = QEMU_OPT_STRING,
450 },{
451 .name = "xlevel",
452 .type = QEMU_OPT_NUMBER,
453 },{
454 .name = "model_id",
455 .type = QEMU_OPT_STRING,
456 },{
457 .name = "vendor_override",
458 .type = QEMU_OPT_NUMBER,
459 },
460 { /* end of list */ }
461 },
462 };
463
464 QemuOptsList qemu_spice_opts = {
465 .name = "spice",
466 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
467 .desc = {
468 {
469 .name = "port",
470 .type = QEMU_OPT_NUMBER,
471 },{
472 .name = "tls-port",
473 .type = QEMU_OPT_NUMBER,
474 },{
475 .name = "addr",
476 .type = QEMU_OPT_STRING,
477 },{
478 .name = "ipv4",
479 .type = QEMU_OPT_BOOL,
480 },{
481 .name = "ipv6",
482 .type = QEMU_OPT_BOOL,
483 },{
484 .name = "password",
485 .type = QEMU_OPT_STRING,
486 },{
487 .name = "disable-ticketing",
488 .type = QEMU_OPT_BOOL,
489 },{
490 .name = "disable-copy-paste",
491 .type = QEMU_OPT_BOOL,
492 },{
493 .name = "sasl",
494 .type = QEMU_OPT_BOOL,
495 },{
496 .name = "x509-dir",
497 .type = QEMU_OPT_STRING,
498 },{
499 .name = "x509-key-file",
500 .type = QEMU_OPT_STRING,
501 },{
502 .name = "x509-key-password",
503 .type = QEMU_OPT_STRING,
504 },{
505 .name = "x509-cert-file",
506 .type = QEMU_OPT_STRING,
507 },{
508 .name = "x509-cacert-file",
509 .type = QEMU_OPT_STRING,
510 },{
511 .name = "x509-dh-key-file",
512 .type = QEMU_OPT_STRING,
513 },{
514 .name = "tls-ciphers",
515 .type = QEMU_OPT_STRING,
516 },{
517 .name = "tls-channel",
518 .type = QEMU_OPT_STRING,
519 },{
520 .name = "plaintext-channel",
521 .type = QEMU_OPT_STRING,
522 },{
523 .name = "image-compression",
524 .type = QEMU_OPT_STRING,
525 },{
526 .name = "jpeg-wan-compression",
527 .type = QEMU_OPT_STRING,
528 },{
529 .name = "zlib-glz-wan-compression",
530 .type = QEMU_OPT_STRING,
531 },{
532 .name = "streaming-video",
533 .type = QEMU_OPT_STRING,
534 },{
535 .name = "agent-mouse",
536 .type = QEMU_OPT_BOOL,
537 },{
538 .name = "playback-compression",
539 .type = QEMU_OPT_BOOL,
540 }, {
541 .name = "seamless-migration",
542 .type = QEMU_OPT_BOOL,
543 },
544 { /* end of list */ }
545 },
546 };
547
548 QemuOptsList qemu_option_rom_opts = {
549 .name = "option-rom",
550 .implied_opt_name = "romfile",
551 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
552 .desc = {
553 {
554 .name = "bootindex",
555 .type = QEMU_OPT_NUMBER,
556 }, {
557 .name = "romfile",
558 .type = QEMU_OPT_STRING,
559 },
560 { /* end of list */ }
561 },
562 };
563
564 static QemuOptsList qemu_machine_opts = {
565 .name = "machine",
566 .implied_opt_name = "type",
567 .merge_lists = true,
568 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
569 .desc = {
570 {
571 .name = "type",
572 .type = QEMU_OPT_STRING,
573 .help = "emulated machine"
574 }, {
575 .name = "accel",
576 .type = QEMU_OPT_STRING,
577 .help = "accelerator list",
578 }, {
579 .name = "kernel_irqchip",
580 .type = QEMU_OPT_BOOL,
581 .help = "use KVM in-kernel irqchip",
582 }, {
583 .name = "kvm_shadow_mem",
584 .type = QEMU_OPT_SIZE,
585 .help = "KVM shadow MMU size",
586 }, {
587 .name = "kernel",
588 .type = QEMU_OPT_STRING,
589 .help = "Linux kernel image file",
590 }, {
591 .name = "initrd",
592 .type = QEMU_OPT_STRING,
593 .help = "Linux initial ramdisk file",
594 }, {
595 .name = "append",
596 .type = QEMU_OPT_STRING,
597 .help = "Linux kernel command line",
598 }, {
599 .name = "dtb",
600 .type = QEMU_OPT_STRING,
601 .help = "Linux kernel device tree file",
602 }, {
603 .name = "dumpdtb",
604 .type = QEMU_OPT_STRING,
605 .help = "Dump current dtb to a file and quit",
606 }, {
607 .name = "phandle_start",
608 .type = QEMU_OPT_STRING,
609 .help = "The first phandle ID we may generate dynamically",
610 }, {
611 .name = "dt_compatible",
612 .type = QEMU_OPT_STRING,
613 .help = "Overrides the \"compatible\" property of the dt root node",
614 }, {
615 .name = "dump-guest-core",
616 .type = QEMU_OPT_BOOL,
617 .help = "Include guest memory in a core dump",
618 }, {
619 .name = "mem-merge",
620 .type = QEMU_OPT_BOOL,
621 .help = "enable/disable memory merge support",
622 },
623 { /* End of list */ }
624 },
625 };
626
627 QemuOptsList qemu_boot_opts = {
628 .name = "boot-opts",
629 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
630 .desc = {
631 /* the three names below are not used now */
632 {
633 .name = "order",
634 .type = QEMU_OPT_STRING,
635 }, {
636 .name = "once",
637 .type = QEMU_OPT_STRING,
638 }, {
639 .name = "menu",
640 .type = QEMU_OPT_STRING,
641 /* following are really used */
642 }, {
643 .name = "splash",
644 .type = QEMU_OPT_STRING,
645 }, {
646 .name = "splash-time",
647 .type = QEMU_OPT_STRING,
648 }, {
649 .name = "reboot-timeout",
650 .type = QEMU_OPT_STRING,
651 },
652 { /*End of list */ }
653 },
654 };
655
656 static QemuOptsList *vm_config_groups[32] = {
657 &qemu_drive_opts,
658 &qemu_chardev_opts,
659 &qemu_device_opts,
660 &qemu_netdev_opts,
661 &qemu_net_opts,
662 &qemu_rtc_opts,
663 &qemu_global_opts,
664 &qemu_mon_opts,
665 &qemu_cpudef_opts,
666 &qemu_trace_opts,
667 &qemu_option_rom_opts,
668 &qemu_machine_opts,
669 &qemu_boot_opts,
670 &qemu_iscsi_opts,
671 &qemu_sandbox_opts,
672 NULL,
673 };
674
675 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
676 Error **errp)
677 {
678 int i;
679
680 for (i = 0; lists[i] != NULL; i++) {
681 if (strcmp(lists[i]->name, group) == 0)
682 break;
683 }
684 if (lists[i] == NULL) {
685 error_set(errp, QERR_INVALID_OPTION_GROUP, group);
686 }
687 return lists[i];
688 }
689
690 QemuOptsList *qemu_find_opts(const char *group)
691 {
692 QemuOptsList *ret;
693 Error *local_err = NULL;
694
695 ret = find_list(vm_config_groups, group, &local_err);
696 if (error_is_set(&local_err)) {
697 error_report("%s\n", error_get_pretty(local_err));
698 error_free(local_err);
699 }
700
701 return ret;
702 }
703
704 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
705 {
706 return find_list(vm_config_groups, group, errp);
707 }
708
709 void qemu_add_opts(QemuOptsList *list)
710 {
711 int entries, i;
712
713 entries = ARRAY_SIZE(vm_config_groups);
714 entries--; /* keep list NULL terminated */
715 for (i = 0; i < entries; i++) {
716 if (vm_config_groups[i] == NULL) {
717 vm_config_groups[i] = list;
718 return;
719 }
720 }
721 fprintf(stderr, "ran out of space in vm_config_groups");
722 abort();
723 }
724
725 int qemu_set_option(const char *str)
726 {
727 char group[64], id[64], arg[64];
728 QemuOptsList *list;
729 QemuOpts *opts;
730 int rc, offset;
731
732 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
733 if (rc < 3 || str[offset] != '=') {
734 error_report("can't parse: \"%s\"", str);
735 return -1;
736 }
737
738 list = qemu_find_opts(group);
739 if (list == NULL) {
740 return -1;
741 }
742
743 opts = qemu_opts_find(list, id);
744 if (!opts) {
745 error_report("there is no %s \"%s\" defined",
746 list->name, id);
747 return -1;
748 }
749
750 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
751 return -1;
752 }
753 return 0;
754 }
755
756 int qemu_global_option(const char *str)
757 {
758 char driver[64], property[64];
759 QemuOpts *opts;
760 int rc, offset;
761
762 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
763 if (rc < 2 || str[offset] != '=') {
764 error_report("can't parse: \"%s\"", str);
765 return -1;
766 }
767
768 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, NULL);
769 qemu_opt_set(opts, "driver", driver);
770 qemu_opt_set(opts, "property", property);
771 qemu_opt_set(opts, "value", str+offset+1);
772 return 0;
773 }
774
775 struct ConfigWriteData {
776 QemuOptsList *list;
777 FILE *fp;
778 };
779
780 static int config_write_opt(const char *name, const char *value, void *opaque)
781 {
782 struct ConfigWriteData *data = opaque;
783
784 fprintf(data->fp, " %s = \"%s\"\n", name, value);
785 return 0;
786 }
787
788 static int config_write_opts(QemuOpts *opts, void *opaque)
789 {
790 struct ConfigWriteData *data = opaque;
791 const char *id = qemu_opts_id(opts);
792
793 if (id) {
794 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
795 } else {
796 fprintf(data->fp, "[%s]\n", data->list->name);
797 }
798 qemu_opt_foreach(opts, config_write_opt, data, 0);
799 fprintf(data->fp, "\n");
800 return 0;
801 }
802
803 void qemu_config_write(FILE *fp)
804 {
805 struct ConfigWriteData data = { .fp = fp };
806 QemuOptsList **lists = vm_config_groups;
807 int i;
808
809 fprintf(fp, "# qemu config file\n\n");
810 for (i = 0; lists[i] != NULL; i++) {
811 data.list = lists[i];
812 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
813 }
814 }
815
816 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
817 {
818 char line[1024], group[64], id[64], arg[64], value[1024];
819 Location loc;
820 QemuOptsList *list = NULL;
821 Error *local_err = NULL;
822 QemuOpts *opts = NULL;
823 int res = -1, lno = 0;
824
825 loc_push_none(&loc);
826 while (fgets(line, sizeof(line), fp) != NULL) {
827 loc_set_file(fname, ++lno);
828 if (line[0] == '\n') {
829 /* skip empty lines */
830 continue;
831 }
832 if (line[0] == '#') {
833 /* comment */
834 continue;
835 }
836 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
837 /* group with id */
838 list = find_list(lists, group, &local_err);
839 if (error_is_set(&local_err)) {
840 error_report("%s\n", error_get_pretty(local_err));
841 error_free(local_err);
842 goto out;
843 }
844 opts = qemu_opts_create(list, id, 1, NULL);
845 continue;
846 }
847 if (sscanf(line, "[%63[^]]]", group) == 1) {
848 /* group without id */
849 list = find_list(lists, group, &local_err);
850 if (error_is_set(&local_err)) {
851 error_report("%s\n", error_get_pretty(local_err));
852 error_free(local_err);
853 goto out;
854 }
855 opts = qemu_opts_create(list, NULL, 0, NULL);
856 continue;
857 }
858 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
859 /* arg = value */
860 if (opts == NULL) {
861 error_report("no group defined");
862 goto out;
863 }
864 if (qemu_opt_set(opts, arg, value) != 0) {
865 goto out;
866 }
867 continue;
868 }
869 error_report("parse error");
870 goto out;
871 }
872 if (ferror(fp)) {
873 error_report("error reading file");
874 goto out;
875 }
876 res = 0;
877 out:
878 loc_pop(&loc);
879 return res;
880 }
881
882 int qemu_read_config_file(const char *filename)
883 {
884 FILE *f = fopen(filename, "r");
885 int ret;
886
887 if (f == NULL) {
888 return -errno;
889 }
890
891 ret = qemu_config_parse(f, vm_config_groups, filename);
892 fclose(f);
893
894 if (ret == 0) {
895 return 0;
896 } else {
897 return -EINVAL;
898 }
899 }