blob: 323d3c2c292721e3ab4a8d19e4dc8c9137fd22ea [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,
Alon Levycbcc6332011-01-19 10:49:50 +0200149 },{
150 .name = "name",
151 .type = QEMU_OPT_STRING,
152 },{
153 .name = "debug",
154 .type = QEMU_OPT_NUMBER,
Gerd Hoffmann7d315442009-09-10 10:58:36 +0200155 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200156 { /* end of list */ }
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200157 },
158};
159
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530160QemuOptsList qemu_fsdev_opts = {
161 .name = "fsdev",
162 .implied_opt_name = "fstype",
163 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
164 .desc = {
165 {
166 .name = "fstype",
167 .type = QEMU_OPT_STRING,
168 }, {
169 .name = "path",
170 .type = QEMU_OPT_STRING,
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -0700171 }, {
172 .name = "security_model",
173 .type = QEMU_OPT_STRING,
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530174 },
175 { /*End of list */ }
176 },
177};
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530178
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530179QemuOptsList qemu_virtfs_opts = {
180 .name = "virtfs",
181 .implied_opt_name = "fstype",
182 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
183 .desc = {
184 {
185 .name = "fstype",
186 .type = QEMU_OPT_STRING,
187 }, {
188 .name = "path",
189 .type = QEMU_OPT_STRING,
190 }, {
191 .name = "mount_tag",
192 .type = QEMU_OPT_STRING,
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -0700193 }, {
194 .name = "security_model",
195 .type = QEMU_OPT_STRING,
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530196 },
197
198 { /*End of list */ }
199 },
200};
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +0530201
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200202static QemuOptsList qemu_device_opts = {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200203 .name = "device",
Markus Armbruster8212c642010-02-10 19:52:18 +0100204 .implied_opt_name = "driver",
Blue Swirl72cf2d42009-09-12 07:36:22 +0000205 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200206 .desc = {
207 /*
208 * no elements => accept any
209 * sanity checking will happen later
210 * when setting device properties
211 */
Jes Sorensen26056e02010-08-31 09:30:35 +0200212 { /* end of list */ }
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200213 },
214};
215
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200216static QemuOptsList qemu_netdev_opts = {
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100217 .name = "netdev",
Markus Armbruster8212c642010-02-10 19:52:18 +0100218 .implied_opt_name = "type",
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100219 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
220 .desc = {
221 /*
222 * no elements => accept any params
223 * validation will happen later
224 */
225 { /* end of list */ }
226 },
227};
228
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200229static QemuOptsList qemu_net_opts = {
Mark McLoughlin8119b332009-10-06 12:17:05 +0100230 .name = "net",
Markus Armbruster8212c642010-02-10 19:52:18 +0100231 .implied_opt_name = "type",
Mark McLoughlin8119b332009-10-06 12:17:05 +0100232 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
233 .desc = {
234 /*
235 * no elements => accept any params
236 * validation will happen later
237 */
238 { /* end of list */ }
239 },
240};
241
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200242static QemuOptsList qemu_rtc_opts = {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200243 .name = "rtc",
244 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
245 .desc = {
246 {
247 .name = "base",
248 .type = QEMU_OPT_STRING,
Jan Kiszka68752042009-09-15 13:36:04 +0200249 },{
250 .name = "clock",
251 .type = QEMU_OPT_STRING,
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200252 },{
253 .name = "driftfix",
254 .type = QEMU_OPT_STRING,
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200255 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200256 { /* end of list */ }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200257 },
258};
259
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200260static QemuOptsList qemu_global_opts = {
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100261 .name = "global",
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
263 .desc = {
264 {
265 .name = "driver",
266 .type = QEMU_OPT_STRING,
267 },{
268 .name = "property",
269 .type = QEMU_OPT_STRING,
270 },{
271 .name = "value",
272 .type = QEMU_OPT_STRING,
273 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200274 { /* end of list */ }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100275 },
276};
277
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200278static QemuOptsList qemu_mon_opts = {
Gerd Hoffmann88589342009-12-08 13:11:50 +0100279 .name = "mon",
Markus Armbruster8212c642010-02-10 19:52:18 +0100280 .implied_opt_name = "chardev",
Gerd Hoffmann88589342009-12-08 13:11:50 +0100281 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
282 .desc = {
283 {
284 .name = "mode",
285 .type = QEMU_OPT_STRING,
286 },{
287 .name = "chardev",
288 .type = QEMU_OPT_STRING,
289 },{
290 .name = "default",
291 .type = QEMU_OPT_BOOL,
Daniel P. Berrange39eaab92010-06-07 15:42:31 +0100292 },{
293 .name = "pretty",
294 .type = QEMU_OPT_BOOL,
Gerd Hoffmann88589342009-12-08 13:11:50 +0100295 },
Jes Sorensen26056e02010-08-31 09:30:35 +0200296 { /* end of list */ }
Gerd Hoffmann88589342009-12-08 13:11:50 +0100297 },
298};
299
Prerna Saxenaab6540d2010-08-09 11:48:32 +0100300#ifdef CONFIG_SIMPLE_TRACE
301static QemuOptsList qemu_trace_opts = {
302 .name = "trace",
303 .implied_opt_name = "trace",
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
305 .desc = {
306 {
307 .name = "file",
308 .type = QEMU_OPT_STRING,
309 },
310 { /* end if list */ }
311 },
312};
313#endif
314
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200315static QemuOptsList qemu_cpudef_opts = {
john cooperb5ec5ce2010-02-20 11:14:59 -0600316 .name = "cpudef",
317 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
318 .desc = {
319 {
320 .name = "name",
321 .type = QEMU_OPT_STRING,
322 },{
323 .name = "level",
324 .type = QEMU_OPT_NUMBER,
325 },{
326 .name = "vendor",
327 .type = QEMU_OPT_STRING,
328 },{
329 .name = "family",
330 .type = QEMU_OPT_NUMBER,
331 },{
332 .name = "model",
333 .type = QEMU_OPT_NUMBER,
334 },{
335 .name = "stepping",
336 .type = QEMU_OPT_NUMBER,
337 },{
338 .name = "feature_edx", /* cpuid 0000_0001.edx */
339 .type = QEMU_OPT_STRING,
340 },{
341 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
342 .type = QEMU_OPT_STRING,
343 },{
344 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
345 .type = QEMU_OPT_STRING,
346 },{
347 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
348 .type = QEMU_OPT_STRING,
349 },{
350 .name = "xlevel",
351 .type = QEMU_OPT_NUMBER,
352 },{
353 .name = "model_id",
354 .type = QEMU_OPT_STRING,
355 },{
356 .name = "vendor_override",
357 .type = QEMU_OPT_NUMBER,
358 },
359 { /* end of list */ }
360 },
361};
362
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300363QemuOptsList qemu_spice_opts = {
364 .name = "spice",
365 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
366 .desc = {
367 {
368 .name = "port",
369 .type = QEMU_OPT_NUMBER,
370 },{
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300371 .name = "tls-port",
372 .type = QEMU_OPT_NUMBER,
373 },{
Gerd Hoffmann333b0ee2010-08-27 14:29:16 +0200374 .name = "addr",
375 .type = QEMU_OPT_STRING,
376 },{
377 .name = "ipv4",
378 .type = QEMU_OPT_BOOL,
379 },{
380 .name = "ipv6",
381 .type = QEMU_OPT_BOOL,
382 },{
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300383 .name = "password",
384 .type = QEMU_OPT_STRING,
385 },{
386 .name = "disable-ticketing",
387 .type = QEMU_OPT_BOOL,
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300388 },{
389 .name = "x509-dir",
390 .type = QEMU_OPT_STRING,
391 },{
392 .name = "x509-key-file",
393 .type = QEMU_OPT_STRING,
394 },{
395 .name = "x509-key-password",
396 .type = QEMU_OPT_STRING,
397 },{
398 .name = "x509-cert-file",
399 .type = QEMU_OPT_STRING,
400 },{
401 .name = "x509-cacert-file",
402 .type = QEMU_OPT_STRING,
403 },{
404 .name = "x509-dh-key-file",
405 .type = QEMU_OPT_STRING,
406 },{
407 .name = "tls-ciphers",
408 .type = QEMU_OPT_STRING,
Yonit Halperin9f04e092010-07-14 13:26:34 +0300409 },{
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200410 .name = "tls-channel",
411 .type = QEMU_OPT_STRING,
412 },{
413 .name = "plaintext-channel",
414 .type = QEMU_OPT_STRING,
415 },{
Yonit Halperin9f04e092010-07-14 13:26:34 +0300416 .name = "image-compression",
417 .type = QEMU_OPT_STRING,
418 },{
419 .name = "jpeg-wan-compression",
420 .type = QEMU_OPT_STRING,
421 },{
422 .name = "zlib-glz-wan-compression",
423 .type = QEMU_OPT_STRING,
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200424 },{
425 .name = "streaming-video",
426 .type = QEMU_OPT_STRING,
427 },{
428 .name = "agent-mouse",
429 .type = QEMU_OPT_BOOL,
430 },{
431 .name = "playback-compression",
432 .type = QEMU_OPT_BOOL,
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300433 },
434 { /* end if list */ }
435 },
436};
437
Gleb Natapov2e55e842010-12-08 13:35:07 +0200438QemuOptsList qemu_option_rom_opts = {
439 .name = "option-rom",
440 .implied_opt_name = "romfile",
441 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
442 .desc = {
443 {
444 .name = "bootindex",
445 .type = QEMU_OPT_NUMBER,
446 }, {
447 .name = "romfile",
448 .type = QEMU_OPT_STRING,
449 },
450 { /* end if list */ }
451 },
452};
453
Gerd Hoffmanndfe795e2010-08-20 13:52:00 +0200454static QemuOptsList *vm_config_groups[32] = {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200455 &qemu_drive_opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200456 &qemu_chardev_opts,
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200457 &qemu_device_opts,
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100458 &qemu_netdev_opts,
Mark McLoughlin8119b332009-10-06 12:17:05 +0100459 &qemu_net_opts,
Jan Kiszka5fdfbf72009-10-06 12:16:50 +0100460 &qemu_rtc_opts,
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100461 &qemu_global_opts,
Gerd Hoffmann88589342009-12-08 13:11:50 +0100462 &qemu_mon_opts,
john cooperb5ec5ce2010-02-20 11:14:59 -0600463 &qemu_cpudef_opts,
Prerna Saxenaab6540d2010-08-09 11:48:32 +0100464#ifdef CONFIG_SIMPLE_TRACE
465 &qemu_trace_opts,
466#endif
Gleb Natapov2e55e842010-12-08 13:35:07 +0200467 &qemu_option_rom_opts,
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200468 NULL,
469};
470
Kevin Wolf490b6482010-03-05 18:21:56 +0100471static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200472{
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200473 int i;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200474
475 for (i = 0; lists[i] != NULL; i++) {
476 if (strcmp(lists[i]->name, group) == 0)
477 break;
478 }
479 if (lists[i] == NULL) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100480 error_report("there is no option group \"%s\"", group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200481 }
482 return lists[i];
483}
484
Kevin Wolf490b6482010-03-05 18:21:56 +0100485QemuOptsList *qemu_find_opts(const char *group)
486{
487 return find_list(vm_config_groups, group);
488}
489
Gerd Hoffmanndfe795e2010-08-20 13:52:00 +0200490void qemu_add_opts(QemuOptsList *list)
491{
492 int entries, i;
493
494 entries = ARRAY_SIZE(vm_config_groups);
495 entries--; /* keep list NULL terminated */
496 for (i = 0; i < entries; i++) {
497 if (vm_config_groups[i] == NULL) {
498 vm_config_groups[i] = list;
499 return;
500 }
501 }
502 fprintf(stderr, "ran out of space in vm_config_groups");
503 abort();
504}
505
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200506int qemu_set_option(const char *str)
507{
508 char group[64], id[64], arg[64];
509 QemuOptsList *list;
510 QemuOpts *opts;
511 int rc, offset;
512
513 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
514 if (rc < 3 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100515 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200516 return -1;
517 }
518
Markus Armbruster304329e2010-02-10 20:09:14 +0100519 list = qemu_find_opts(group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200520 if (list == NULL) {
521 return -1;
522 }
523
524 opts = qemu_opts_find(list, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200525 if (!opts) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100526 error_report("there is no %s \"%s\" defined",
527 list->name, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200528 return -1;
529 }
530
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100531 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200532 return -1;
533 }
534 return 0;
535}
536
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100537int qemu_global_option(const char *str)
538{
539 char driver[64], property[64];
540 QemuOpts *opts;
541 int rc, offset;
542
543 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
544 if (rc < 2 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100545 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100546 return -1;
547 }
548
549 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
550 qemu_opt_set(opts, "driver", driver);
551 qemu_opt_set(opts, "property", property);
552 qemu_opt_set(opts, "value", str+offset+1);
553 return 0;
554}
555
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200556struct ConfigWriteData {
557 QemuOptsList *list;
558 FILE *fp;
559};
560
561static int config_write_opt(const char *name, const char *value, void *opaque)
562{
563 struct ConfigWriteData *data = opaque;
564
565 fprintf(data->fp, " %s = \"%s\"\n", name, value);
566 return 0;
567}
568
569static int config_write_opts(QemuOpts *opts, void *opaque)
570{
571 struct ConfigWriteData *data = opaque;
572 const char *id = qemu_opts_id(opts);
573
574 if (id) {
575 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
576 } else {
577 fprintf(data->fp, "[%s]\n", data->list->name);
578 }
579 qemu_opt_foreach(opts, config_write_opt, data, 0);
580 fprintf(data->fp, "\n");
581 return 0;
582}
583
584void qemu_config_write(FILE *fp)
585{
586 struct ConfigWriteData data = { .fp = fp };
Kevin Wolf490b6482010-03-05 18:21:56 +0100587 QemuOptsList **lists = vm_config_groups;
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200588 int i;
589
590 fprintf(fp, "# qemu config file\n\n");
591 for (i = 0; lists[i] != NULL; i++) {
592 data.list = lists[i];
593 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
594 }
595}
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200596
Kevin Wolf490b6482010-03-05 18:21:56 +0100597int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200598{
599 char line[1024], group[64], id[64], arg[64], value[1024];
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100600 Location loc;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200601 QemuOptsList *list = NULL;
602 QemuOpts *opts = NULL;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100603 int res = -1, lno = 0;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200604
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100605 loc_push_none(&loc);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200606 while (fgets(line, sizeof(line), fp) != NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100607 loc_set_file(fname, ++lno);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200608 if (line[0] == '\n') {
609 /* skip empty lines */
610 continue;
611 }
612 if (line[0] == '#') {
613 /* comment */
614 continue;
615 }
616 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
617 /* group with id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100618 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200619 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100620 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200621 opts = qemu_opts_create(list, id, 1);
622 continue;
623 }
624 if (sscanf(line, "[%63[^]]]", group) == 1) {
625 /* group without id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100626 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200627 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100628 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200629 opts = qemu_opts_create(list, NULL, 0);
630 continue;
631 }
632 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
633 /* arg = value */
634 if (opts == NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100635 error_report("no group defined");
636 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200637 }
638 if (qemu_opt_set(opts, arg, value) != 0) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100639 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200640 }
641 continue;
642 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100643 error_report("parse error");
644 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200645 }
Markus Armbrusteref825162010-02-18 19:56:01 +0100646 if (ferror(fp)) {
647 error_report("error reading file");
648 goto out;
649 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100650 res = 0;
651out:
652 loc_pop(&loc);
653 return res;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200654}
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100655
656int qemu_read_config_file(const char *filename)
657{
658 FILE *f = fopen(filename, "r");
Kevin Wolf019e78b2010-05-17 10:36:47 +0200659 int ret;
660
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100661 if (f == NULL) {
662 return -errno;
663 }
664
Kevin Wolf019e78b2010-05-17 10:36:47 +0200665 ret = qemu_config_parse(f, vm_config_groups, filename);
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100666 fclose(f);
667
Kevin Wolf019e78b2010-05-17 10:36:47 +0200668 if (ret == 0) {
669 return 0;
670 } else {
671 return -EINVAL;
672 }
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100673}