blob: 52f18bef3194f2cbbdafea739482b3dcdef11941 [file] [log] [blame]
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001#include "qemu-common.h"
Markus Armbruster2f792012010-02-18 16:24:31 +01002#include "qemu-error.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +02003#include "qemu-option.h"
4#include "qemu-config.h"
Gerd Hoffmanna861c452009-09-25 21:42:40 +02005#include "sysemu.h"
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01006#include "hw/qdev.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +02007
Gerd Hoffmann3329f072010-08-20 13:52:01 +02008static QemuOptsList qemu_drive_opts = {
Gerd Hoffmann7282a032009-07-31 12:25:35 +02009 .name = "drive",
Blue Swirl72cf2d42009-09-12 07:36:22 +000010 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
Gerd Hoffmann7282a032009-07-31 12:25:35 +020011 .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 },{
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,
Alexander Graf016f5cf2010-05-26 17:51:49 +020057 .help = "host cache usage (none, writeback, writethrough, unsafe)",
Gerd Hoffmann7282a032009-07-31 12:25:35 +020058 },{
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +020059 .name = "aio",
60 .type = QEMU_OPT_STRING,
61 .help = "host AIO implementation (threads, native)",
62 },{
Gerd Hoffmann7282a032009-07-31 12:25:35 +020063 .name = "format",
64 .type = QEMU_OPT_STRING,
65 .help = "disk format (raw, qcow2, ...)",
66 },{
67 .name = "serial",
68 .type = QEMU_OPT_STRING,
69 },{
Kevin Wolfe9b2e812009-11-27 13:25:37 +010070 .name = "rerror",
71 .type = QEMU_OPT_STRING,
72 },{
Gerd Hoffmann7282a032009-07-31 12:25:35 +020073 .name = "werror",
74 .type = QEMU_OPT_STRING,
75 },{
76 .name = "addr",
77 .type = QEMU_OPT_STRING,
78 .help = "pci address (virtio only)",
Naphtali Sprei59f26892009-10-26 16:25:16 +020079 },{
80 .name = "readonly",
81 .type = QEMU_OPT_BOOL,
Gerd Hoffmann7282a032009-07-31 12:25:35 +020082 },
Jes Sorensen26056e02010-08-31 09:30:35 +020083 { /* end of list */ }
Gerd Hoffmann7282a032009-07-31 12:25:35 +020084 },
85};
86
Gerd Hoffmann3329f072010-08-20 13:52:01 +020087static QemuOptsList qemu_chardev_opts = {
Gerd Hoffmann191bc012009-09-10 10:58:35 +020088 .name = "chardev",
Markus Armbruster8212c642010-02-10 19:52:18 +010089 .implied_opt_name = "backend",
Blue Swirl72cf2d42009-09-12 07:36:22 +000090 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
Gerd Hoffmann191bc012009-09-10 10:58:35 +020091 .desc = {
Gerd Hoffmann7d315442009-09-10 10:58:36 +020092 {
93 .name = "backend",
94 .type = QEMU_OPT_STRING,
95 },{
96 .name = "path",
97 .type = QEMU_OPT_STRING,
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +020098 },{
99 .name = "host",
100 .type = QEMU_OPT_STRING,
101 },{
102 .name = "port",
103 .type = QEMU_OPT_STRING,
104 },{
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200105 .name = "localaddr",
106 .type = QEMU_OPT_STRING,
107 },{
108 .name = "localport",
109 .type = QEMU_OPT_STRING,
110 },{
Gerd Hoffmannaeb2c472009-09-10 10:58:42 +0200111 .name = "to",
112 .type = QEMU_OPT_NUMBER,
113 },{
114 .name = "ipv4",
115 .type = QEMU_OPT_BOOL,
116 },{
117 .name = "ipv6",
118 .type = QEMU_OPT_BOOL,
119 },{
120 .name = "wait",
121 .type = QEMU_OPT_BOOL,
122 },{
123 .name = "server",
124 .type = QEMU_OPT_BOOL,
125 },{
126 .name = "delay",
127 .type = QEMU_OPT_BOOL,
128 },{
129 .name = "telnet",
130 .type = QEMU_OPT_BOOL,
Gerd Hoffmann6ea314d2009-09-10 10:58:49 +0200131 },{
132 .name = "width",
133 .type = QEMU_OPT_NUMBER,
134 },{
135 .name = "height",
136 .type = QEMU_OPT_NUMBER,
137 },{
138 .name = "cols",
139 .type = QEMU_OPT_NUMBER,
140 },{
141 .name = "rows",
142 .type = QEMU_OPT_NUMBER,
Gerd Hoffmannc845f402009-09-10 10:58:52 +0200143 },{
144 .name = "mux",
145 .type = QEMU_OPT_BOOL,
Kusanagi Kouichi59890202009-10-16 22:31:38 +0900146 },{
147 .name = "signal",
148 .type = QEMU_OPT_BOOL,
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200149 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200150 { /* end of list */ }
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200151 },
152};
153
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530154QemuOptsList qemu_fsdev_opts = {
155 .name = "fsdev",
156 .implied_opt_name = "fstype",
157 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
158 .desc = {
159 {
160 .name = "fstype",
161 .type = QEMU_OPT_STRING,
162 }, {
163 .name = "path",
164 .type = QEMU_OPT_STRING,
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -0700165 }, {
166 .name = "security_model",
167 .type = QEMU_OPT_STRING,
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530168 },
169 { /*End of list */ }
170 },
171};
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530172
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530173QemuOptsList qemu_virtfs_opts = {
174 .name = "virtfs",
175 .implied_opt_name = "fstype",
176 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
177 .desc = {
178 {
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,
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -0700187 }, {
188 .name = "security_model",
189 .type = QEMU_OPT_STRING,
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530190 },
191
192 { /*End of list */ }
193 },
194};
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530195
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200196static QemuOptsList qemu_device_opts = {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200197 .name = "device",
Markus Armbruster8212c642010-02-10 19:52:18 +0100198 .implied_opt_name = "driver",
Blue Swirl72cf2d42009-09-12 07:36:22 +0000199 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200200 .desc = {
201 /*
202 * no elements => accept any
203 * sanity checking will happen later
204 * when setting device properties
205 */
Jes Sorensen26056e02010-08-31 09:30:35 +0200206 { /* end of list */ }
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200207 },
208};
209
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200210static QemuOptsList qemu_netdev_opts = {
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100211 .name = "netdev",
Markus Armbruster8212c642010-02-10 19:52:18 +0100212 .implied_opt_name = "type",
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100213 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
214 .desc = {
215 /*
216 * no elements => accept any params
217 * validation will happen later
218 */
219 { /* end of list */ }
220 },
221};
222
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200223static QemuOptsList qemu_net_opts = {
Mark McLoughlin8119b332009-10-06 12:17:05 +0100224 .name = "net",
Markus Armbruster8212c642010-02-10 19:52:18 +0100225 .implied_opt_name = "type",
Mark McLoughlin8119b332009-10-06 12:17:05 +0100226 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
227 .desc = {
228 /*
229 * no elements => accept any params
230 * validation will happen later
231 */
232 { /* end of list */ }
233 },
234};
235
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200236static QemuOptsList qemu_rtc_opts = {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200237 .name = "rtc",
238 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
239 .desc = {
240 {
241 .name = "base",
242 .type = QEMU_OPT_STRING,
Jan Kiszka68752042009-09-15 13:36:04 +0200243 },{
244 .name = "clock",
245 .type = QEMU_OPT_STRING,
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200246 },{
247 .name = "driftfix",
248 .type = QEMU_OPT_STRING,
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200249 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200250 { /* end of list */ }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200251 },
252};
253
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200254static QemuOptsList qemu_global_opts = {
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100255 .name = "global",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
257 .desc = {
258 {
259 .name = "driver",
260 .type = QEMU_OPT_STRING,
261 },{
262 .name = "property",
263 .type = QEMU_OPT_STRING,
264 },{
265 .name = "value",
266 .type = QEMU_OPT_STRING,
267 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200268 { /* end of list */ }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100269 },
270};
271
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200272static QemuOptsList qemu_mon_opts = {
Gerd Hoffmann88589342009-12-08 13:11:50 +0100273 .name = "mon",
Markus Armbruster8212c642010-02-10 19:52:18 +0100274 .implied_opt_name = "chardev",
Gerd Hoffmann88589342009-12-08 13:11:50 +0100275 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
276 .desc = {
277 {
278 .name = "mode",
279 .type = QEMU_OPT_STRING,
280 },{
281 .name = "chardev",
282 .type = QEMU_OPT_STRING,
283 },{
284 .name = "default",
285 .type = QEMU_OPT_BOOL,
Daniel P. Berrange39eaab92010-06-07 15:42:31 +0100286 },{
287 .name = "pretty",
288 .type = QEMU_OPT_BOOL,
Gerd Hoffmann88589342009-12-08 13:11:50 +0100289 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200290 { /* end of list */ }
Gerd Hoffmann88589342009-12-08 13:11:50 +0100291 },
292};
293
Prerna Saxenaab6540d2010-08-09 11:48:32 +0100294#ifdef CONFIG_SIMPLE_TRACE
295static QemuOptsList qemu_trace_opts = {
296 .name = "trace",
297 .implied_opt_name = "trace",
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
299 .desc = {
300 {
301 .name = "file",
302 .type = QEMU_OPT_STRING,
303 },
304 { /* end if list */ }
305 },
306};
307#endif
308
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200309static QemuOptsList qemu_cpudef_opts = {
john cooperb5ec5ce2010-02-20 11:14:59 -0600310 .name = "cpudef",
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
312 .desc = {
313 {
314 .name = "name",
315 .type = QEMU_OPT_STRING,
316 },{
317 .name = "level",
318 .type = QEMU_OPT_NUMBER,
319 },{
320 .name = "vendor",
321 .type = QEMU_OPT_STRING,
322 },{
323 .name = "family",
324 .type = QEMU_OPT_NUMBER,
325 },{
326 .name = "model",
327 .type = QEMU_OPT_NUMBER,
328 },{
329 .name = "stepping",
330 .type = QEMU_OPT_NUMBER,
331 },{
332 .name = "feature_edx", /* cpuid 0000_0001.edx */
333 .type = QEMU_OPT_STRING,
334 },{
335 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
336 .type = QEMU_OPT_STRING,
337 },{
338 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
339 .type = QEMU_OPT_STRING,
340 },{
341 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
342 .type = QEMU_OPT_STRING,
343 },{
344 .name = "xlevel",
345 .type = QEMU_OPT_NUMBER,
346 },{
347 .name = "model_id",
348 .type = QEMU_OPT_STRING,
349 },{
350 .name = "vendor_override",
351 .type = QEMU_OPT_NUMBER,
352 },
353 { /* end of list */ }
354 },
355};
356
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300357QemuOptsList qemu_spice_opts = {
358 .name = "spice",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
360 .desc = {
361 {
362 .name = "port",
363 .type = QEMU_OPT_NUMBER,
364 },{
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300365 .name = "tls-port",
366 .type = QEMU_OPT_NUMBER,
367 },{
Gerd Hoffmann333b0ee2010-08-27 14:29:16 +0200368 .name = "addr",
369 .type = QEMU_OPT_STRING,
370 },{
371 .name = "ipv4",
372 .type = QEMU_OPT_BOOL,
373 },{
374 .name = "ipv6",
375 .type = QEMU_OPT_BOOL,
376 },{
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300377 .name = "password",
378 .type = QEMU_OPT_STRING,
379 },{
380 .name = "disable-ticketing",
381 .type = QEMU_OPT_BOOL,
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300382 },{
383 .name = "x509-dir",
384 .type = QEMU_OPT_STRING,
385 },{
386 .name = "x509-key-file",
387 .type = QEMU_OPT_STRING,
388 },{
389 .name = "x509-key-password",
390 .type = QEMU_OPT_STRING,
391 },{
392 .name = "x509-cert-file",
393 .type = QEMU_OPT_STRING,
394 },{
395 .name = "x509-cacert-file",
396 .type = QEMU_OPT_STRING,
397 },{
398 .name = "x509-dh-key-file",
399 .type = QEMU_OPT_STRING,
400 },{
401 .name = "tls-ciphers",
402 .type = QEMU_OPT_STRING,
Yonit Halperin9f04e092010-07-14 13:26:34 +0300403 },{
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200404 .name = "tls-channel",
405 .type = QEMU_OPT_STRING,
406 },{
407 .name = "plaintext-channel",
408 .type = QEMU_OPT_STRING,
409 },{
Yonit Halperin9f04e092010-07-14 13:26:34 +0300410 .name = "image-compression",
411 .type = QEMU_OPT_STRING,
412 },{
413 .name = "jpeg-wan-compression",
414 .type = QEMU_OPT_STRING,
415 },{
416 .name = "zlib-glz-wan-compression",
417 .type = QEMU_OPT_STRING,
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200418 },{
419 .name = "streaming-video",
420 .type = QEMU_OPT_STRING,
421 },{
422 .name = "agent-mouse",
423 .type = QEMU_OPT_BOOL,
424 },{
425 .name = "playback-compression",
426 .type = QEMU_OPT_BOOL,
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300427 },
428 { /* end if list */ }
429 },
430};
431
Gerd Hoffmanndfe795e2010-08-20 13:52:00 +0200432static QemuOptsList *vm_config_groups[32] = {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200433 &qemu_drive_opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200434 &qemu_chardev_opts,
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200435 &qemu_device_opts,
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100436 &qemu_netdev_opts,
Mark McLoughlin8119b332009-10-06 12:17:05 +0100437 &qemu_net_opts,
Jan Kiszka5fdfbf72009-10-06 12:16:50 +0100438 &qemu_rtc_opts,
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100439 &qemu_global_opts,
Gerd Hoffmann88589342009-12-08 13:11:50 +0100440 &qemu_mon_opts,
john cooperb5ec5ce2010-02-20 11:14:59 -0600441 &qemu_cpudef_opts,
Prerna Saxenaab6540d2010-08-09 11:48:32 +0100442#ifdef CONFIG_SIMPLE_TRACE
443 &qemu_trace_opts,
444#endif
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200445 NULL,
446};
447
Kevin Wolf490b6482010-03-05 18:21:56 +0100448static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200449{
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200450 int i;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200451
452 for (i = 0; lists[i] != NULL; i++) {
453 if (strcmp(lists[i]->name, group) == 0)
454 break;
455 }
456 if (lists[i] == NULL) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100457 error_report("there is no option group \"%s\"", group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200458 }
459 return lists[i];
460}
461
Kevin Wolf490b6482010-03-05 18:21:56 +0100462QemuOptsList *qemu_find_opts(const char *group)
463{
464 return find_list(vm_config_groups, group);
465}
466
Gerd Hoffmanndfe795e2010-08-20 13:52:00 +0200467void qemu_add_opts(QemuOptsList *list)
468{
469 int entries, i;
470
471 entries = ARRAY_SIZE(vm_config_groups);
472 entries--; /* keep list NULL terminated */
473 for (i = 0; i < entries; i++) {
474 if (vm_config_groups[i] == NULL) {
475 vm_config_groups[i] = list;
476 return;
477 }
478 }
479 fprintf(stderr, "ran out of space in vm_config_groups");
480 abort();
481}
482
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200483int qemu_set_option(const char *str)
484{
485 char group[64], id[64], arg[64];
486 QemuOptsList *list;
487 QemuOpts *opts;
488 int rc, offset;
489
490 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
491 if (rc < 3 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100492 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200493 return -1;
494 }
495
Markus Armbruster304329e2010-02-10 20:09:14 +0100496 list = qemu_find_opts(group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200497 if (list == NULL) {
498 return -1;
499 }
500
501 opts = qemu_opts_find(list, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200502 if (!opts) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100503 error_report("there is no %s \"%s\" defined",
504 list->name, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200505 return -1;
506 }
507
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100508 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200509 return -1;
510 }
511 return 0;
512}
513
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100514int qemu_global_option(const char *str)
515{
516 char driver[64], property[64];
517 QemuOpts *opts;
518 int rc, offset;
519
520 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
521 if (rc < 2 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100522 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100523 return -1;
524 }
525
526 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
527 qemu_opt_set(opts, "driver", driver);
528 qemu_opt_set(opts, "property", property);
529 qemu_opt_set(opts, "value", str+offset+1);
530 return 0;
531}
532
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200533struct ConfigWriteData {
534 QemuOptsList *list;
535 FILE *fp;
536};
537
538static int config_write_opt(const char *name, const char *value, void *opaque)
539{
540 struct ConfigWriteData *data = opaque;
541
542 fprintf(data->fp, " %s = \"%s\"\n", name, value);
543 return 0;
544}
545
546static int config_write_opts(QemuOpts *opts, void *opaque)
547{
548 struct ConfigWriteData *data = opaque;
549 const char *id = qemu_opts_id(opts);
550
551 if (id) {
552 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
553 } else {
554 fprintf(data->fp, "[%s]\n", data->list->name);
555 }
556 qemu_opt_foreach(opts, config_write_opt, data, 0);
557 fprintf(data->fp, "\n");
558 return 0;
559}
560
561void qemu_config_write(FILE *fp)
562{
563 struct ConfigWriteData data = { .fp = fp };
Kevin Wolf490b6482010-03-05 18:21:56 +0100564 QemuOptsList **lists = vm_config_groups;
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200565 int i;
566
567 fprintf(fp, "# qemu config file\n\n");
568 for (i = 0; lists[i] != NULL; i++) {
569 data.list = lists[i];
570 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
571 }
572}
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200573
Kevin Wolf490b6482010-03-05 18:21:56 +0100574int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200575{
576 char line[1024], group[64], id[64], arg[64], value[1024];
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100577 Location loc;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200578 QemuOptsList *list = NULL;
579 QemuOpts *opts = NULL;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100580 int res = -1, lno = 0;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200581
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100582 loc_push_none(&loc);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200583 while (fgets(line, sizeof(line), fp) != NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100584 loc_set_file(fname, ++lno);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200585 if (line[0] == '\n') {
586 /* skip empty lines */
587 continue;
588 }
589 if (line[0] == '#') {
590 /* comment */
591 continue;
592 }
593 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
594 /* group with id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100595 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200596 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100597 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200598 opts = qemu_opts_create(list, id, 1);
599 continue;
600 }
601 if (sscanf(line, "[%63[^]]]", group) == 1) {
602 /* group without id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100603 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200604 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100605 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200606 opts = qemu_opts_create(list, NULL, 0);
607 continue;
608 }
609 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
610 /* arg = value */
611 if (opts == NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100612 error_report("no group defined");
613 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200614 }
615 if (qemu_opt_set(opts, arg, value) != 0) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100616 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200617 }
618 continue;
619 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100620 error_report("parse error");
621 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200622 }
Markus Armbrusteref825162010-02-18 19:56:01 +0100623 if (ferror(fp)) {
624 error_report("error reading file");
625 goto out;
626 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100627 res = 0;
628out:
629 loc_pop(&loc);
630 return res;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200631}
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100632
633int qemu_read_config_file(const char *filename)
634{
635 FILE *f = fopen(filename, "r");
Kevin Wolf019e78b2010-05-17 10:36:47 +0200636 int ret;
637
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100638 if (f == NULL) {
639 return -errno;
640 }
641
Kevin Wolf019e78b2010-05-17 10:36:47 +0200642 ret = qemu_config_parse(f, vm_config_groups, filename);
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100643 fclose(f);
644
Kevin Wolf019e78b2010-05-17 10:36:47 +0200645 if (ret == 0) {
646 return 0;
647 } else {
648 return -EINVAL;
649 }
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100650}