]>
Commit | Line | Data |
---|---|---|
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 | 7 | static 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, | |
016f5cf6 | 58 | .help = "host cache usage (none, writeback, writethrough, unsafe)", |
5c6c3a6c CH |
59 | },{ |
60 | .name = "aio", | |
61 | .type = QEMU_OPT_STRING, | |
62 | .help = "host AIO implementation (threads, native)", | |
7282a033 GH |
63 | },{ |
64 | .name = "format", | |
65 | .type = QEMU_OPT_STRING, | |
66 | .help = "disk format (raw, qcow2, ...)", | |
67 | },{ | |
68 | .name = "serial", | |
69 | .type = QEMU_OPT_STRING, | |
69d7e218 | 70 | .help = "disk serial number", |
e9b2e818 KW |
71 | },{ |
72 | .name = "rerror", | |
73 | .type = QEMU_OPT_STRING, | |
69d7e218 | 74 | .help = "read error action", |
7282a033 GH |
75 | },{ |
76 | .name = "werror", | |
77 | .type = QEMU_OPT_STRING, | |
69d7e218 | 78 | .help = "write error action", |
7282a033 GH |
79 | },{ |
80 | .name = "addr", | |
81 | .type = QEMU_OPT_STRING, | |
82 | .help = "pci address (virtio only)", | |
59f2689d NS |
83 | },{ |
84 | .name = "readonly", | |
85 | .type = QEMU_OPT_BOOL, | |
69d7e218 | 86 | .help = "open drive file as read-only", |
7282a033 | 87 | }, |
26056e0c | 88 | { /* end of list */ } |
7282a033 GH |
89 | }, |
90 | }; | |
91 | ||
3329f07b | 92 | static QemuOptsList qemu_chardev_opts = { |
191bc01b | 93 | .name = "chardev", |
8212c64f | 94 | .implied_opt_name = "backend", |
72cf2d4f | 95 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
191bc01b | 96 | .desc = { |
7d31544f GH |
97 | { |
98 | .name = "backend", | |
99 | .type = QEMU_OPT_STRING, | |
100 | },{ | |
101 | .name = "path", | |
102 | .type = QEMU_OPT_STRING, | |
aeb2c47a GH |
103 | },{ |
104 | .name = "host", | |
105 | .type = QEMU_OPT_STRING, | |
106 | },{ | |
107 | .name = "port", | |
108 | .type = QEMU_OPT_STRING, | |
7e1b35b4 GH |
109 | },{ |
110 | .name = "localaddr", | |
111 | .type = QEMU_OPT_STRING, | |
112 | },{ | |
113 | .name = "localport", | |
114 | .type = QEMU_OPT_STRING, | |
aeb2c47a GH |
115 | },{ |
116 | .name = "to", | |
117 | .type = QEMU_OPT_NUMBER, | |
118 | },{ | |
119 | .name = "ipv4", | |
120 | .type = QEMU_OPT_BOOL, | |
121 | },{ | |
122 | .name = "ipv6", | |
123 | .type = QEMU_OPT_BOOL, | |
124 | },{ | |
125 | .name = "wait", | |
126 | .type = QEMU_OPT_BOOL, | |
127 | },{ | |
128 | .name = "server", | |
129 | .type = QEMU_OPT_BOOL, | |
130 | },{ | |
131 | .name = "delay", | |
132 | .type = QEMU_OPT_BOOL, | |
133 | },{ | |
134 | .name = "telnet", | |
135 | .type = QEMU_OPT_BOOL, | |
6ea314d9 GH |
136 | },{ |
137 | .name = "width", | |
138 | .type = QEMU_OPT_NUMBER, | |
139 | },{ | |
140 | .name = "height", | |
141 | .type = QEMU_OPT_NUMBER, | |
142 | },{ | |
143 | .name = "cols", | |
144 | .type = QEMU_OPT_NUMBER, | |
145 | },{ | |
146 | .name = "rows", | |
147 | .type = QEMU_OPT_NUMBER, | |
c845f401 GH |
148 | },{ |
149 | .name = "mux", | |
150 | .type = QEMU_OPT_BOOL, | |
5989020b KK |
151 | },{ |
152 | .name = "signal", | |
153 | .type = QEMU_OPT_BOOL, | |
cbcc6336 AL |
154 | },{ |
155 | .name = "name", | |
156 | .type = QEMU_OPT_STRING, | |
157 | },{ | |
158 | .name = "debug", | |
159 | .type = QEMU_OPT_NUMBER, | |
7d31544f | 160 | }, |
26056e0c | 161 | { /* end of list */ } |
191bc01b GH |
162 | }, |
163 | }; | |
164 | ||
74db920c GS |
165 | QemuOptsList qemu_fsdev_opts = { |
166 | .name = "fsdev", | |
167 | .implied_opt_name = "fstype", | |
168 | .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head), | |
169 | .desc = { | |
170 | { | |
171 | .name = "fstype", | |
172 | .type = QEMU_OPT_STRING, | |
173 | }, { | |
174 | .name = "path", | |
175 | .type = QEMU_OPT_STRING, | |
9ce56db6 VJ |
176 | }, { |
177 | .name = "security_model", | |
178 | .type = QEMU_OPT_STRING, | |
74db920c GS |
179 | }, |
180 | { /*End of list */ } | |
181 | }, | |
182 | }; | |
74db920c | 183 | |
3d54abc7 GS |
184 | QemuOptsList qemu_virtfs_opts = { |
185 | .name = "virtfs", | |
186 | .implied_opt_name = "fstype", | |
187 | .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head), | |
188 | .desc = { | |
189 | { | |
190 | .name = "fstype", | |
191 | .type = QEMU_OPT_STRING, | |
192 | }, { | |
193 | .name = "path", | |
194 | .type = QEMU_OPT_STRING, | |
195 | }, { | |
196 | .name = "mount_tag", | |
197 | .type = QEMU_OPT_STRING, | |
9ce56db6 VJ |
198 | }, { |
199 | .name = "security_model", | |
200 | .type = QEMU_OPT_STRING, | |
3d54abc7 GS |
201 | }, |
202 | ||
203 | { /*End of list */ } | |
204 | }, | |
205 | }; | |
3d54abc7 | 206 | |
3329f07b | 207 | static QemuOptsList qemu_device_opts = { |
f31d07d1 | 208 | .name = "device", |
8212c64f | 209 | .implied_opt_name = "driver", |
72cf2d4f | 210 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
f31d07d1 GH |
211 | .desc = { |
212 | /* | |
213 | * no elements => accept any | |
214 | * sanity checking will happen later | |
215 | * when setting device properties | |
216 | */ | |
26056e0c | 217 | { /* end of list */ } |
f31d07d1 GH |
218 | }, |
219 | }; | |
220 | ||
3329f07b | 221 | static QemuOptsList qemu_netdev_opts = { |
a1ea458f | 222 | .name = "netdev", |
8212c64f | 223 | .implied_opt_name = "type", |
a1ea458f MM |
224 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), |
225 | .desc = { | |
226 | /* | |
227 | * no elements => accept any params | |
228 | * validation will happen later | |
229 | */ | |
230 | { /* end of list */ } | |
231 | }, | |
232 | }; | |
233 | ||
3329f07b | 234 | static QemuOptsList qemu_net_opts = { |
8119b33d | 235 | .name = "net", |
8212c64f | 236 | .implied_opt_name = "type", |
8119b33d MM |
237 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), |
238 | .desc = { | |
239 | /* | |
240 | * no elements => accept any params | |
241 | * validation will happen later | |
242 | */ | |
243 | { /* end of list */ } | |
244 | }, | |
245 | }; | |
246 | ||
3329f07b | 247 | static QemuOptsList qemu_rtc_opts = { |
1ed2fc1f JK |
248 | .name = "rtc", |
249 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head), | |
250 | .desc = { | |
251 | { | |
252 | .name = "base", | |
253 | .type = QEMU_OPT_STRING, | |
6875204c JK |
254 | },{ |
255 | .name = "clock", | |
256 | .type = QEMU_OPT_STRING, | |
1ed2fc1f JK |
257 | },{ |
258 | .name = "driftfix", | |
259 | .type = QEMU_OPT_STRING, | |
1ed2fc1f | 260 | }, |
26056e0c | 261 | { /* end of list */ } |
1ed2fc1f JK |
262 | }, |
263 | }; | |
264 | ||
3329f07b | 265 | static QemuOptsList qemu_global_opts = { |
d0fef6fb GH |
266 | .name = "global", |
267 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), | |
268 | .desc = { | |
269 | { | |
270 | .name = "driver", | |
271 | .type = QEMU_OPT_STRING, | |
272 | },{ | |
273 | .name = "property", | |
274 | .type = QEMU_OPT_STRING, | |
275 | },{ | |
276 | .name = "value", | |
277 | .type = QEMU_OPT_STRING, | |
278 | }, | |
26056e0c | 279 | { /* end of list */ } |
d0fef6fb GH |
280 | }, |
281 | }; | |
282 | ||
3329f07b | 283 | static QemuOptsList qemu_mon_opts = { |
88589343 | 284 | .name = "mon", |
8212c64f | 285 | .implied_opt_name = "chardev", |
88589343 GH |
286 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
287 | .desc = { | |
288 | { | |
289 | .name = "mode", | |
290 | .type = QEMU_OPT_STRING, | |
291 | },{ | |
292 | .name = "chardev", | |
293 | .type = QEMU_OPT_STRING, | |
294 | },{ | |
295 | .name = "default", | |
296 | .type = QEMU_OPT_BOOL, | |
39eaab9a DB |
297 | },{ |
298 | .name = "pretty", | |
299 | .type = QEMU_OPT_BOOL, | |
88589343 | 300 | }, |
26056e0c | 301 | { /* end of list */ } |
88589343 GH |
302 | }, |
303 | }; | |
304 | ||
ab6540d5 PS |
305 | #ifdef CONFIG_SIMPLE_TRACE |
306 | static QemuOptsList qemu_trace_opts = { | |
307 | .name = "trace", | |
308 | .implied_opt_name = "trace", | |
309 | .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), | |
310 | .desc = { | |
311 | { | |
312 | .name = "file", | |
313 | .type = QEMU_OPT_STRING, | |
314 | }, | |
44bd6907 | 315 | { /* end of list */ } |
ab6540d5 PS |
316 | }, |
317 | }; | |
318 | #endif | |
319 | ||
3329f07b | 320 | static QemuOptsList qemu_cpudef_opts = { |
b5ec5ce0 | 321 | .name = "cpudef", |
322 | .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head), | |
323 | .desc = { | |
324 | { | |
325 | .name = "name", | |
326 | .type = QEMU_OPT_STRING, | |
327 | },{ | |
328 | .name = "level", | |
329 | .type = QEMU_OPT_NUMBER, | |
330 | },{ | |
331 | .name = "vendor", | |
332 | .type = QEMU_OPT_STRING, | |
333 | },{ | |
334 | .name = "family", | |
335 | .type = QEMU_OPT_NUMBER, | |
336 | },{ | |
337 | .name = "model", | |
338 | .type = QEMU_OPT_NUMBER, | |
339 | },{ | |
340 | .name = "stepping", | |
341 | .type = QEMU_OPT_NUMBER, | |
342 | },{ | |
343 | .name = "feature_edx", /* cpuid 0000_0001.edx */ | |
344 | .type = QEMU_OPT_STRING, | |
345 | },{ | |
346 | .name = "feature_ecx", /* cpuid 0000_0001.ecx */ | |
347 | .type = QEMU_OPT_STRING, | |
348 | },{ | |
349 | .name = "extfeature_edx", /* cpuid 8000_0001.edx */ | |
350 | .type = QEMU_OPT_STRING, | |
351 | },{ | |
352 | .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */ | |
353 | .type = QEMU_OPT_STRING, | |
354 | },{ | |
355 | .name = "xlevel", | |
356 | .type = QEMU_OPT_NUMBER, | |
357 | },{ | |
358 | .name = "model_id", | |
359 | .type = QEMU_OPT_STRING, | |
360 | },{ | |
361 | .name = "vendor_override", | |
362 | .type = QEMU_OPT_NUMBER, | |
363 | }, | |
364 | { /* end of list */ } | |
365 | }, | |
366 | }; | |
367 | ||
29b0040b GH |
368 | QemuOptsList qemu_spice_opts = { |
369 | .name = "spice", | |
370 | .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head), | |
371 | .desc = { | |
372 | { | |
373 | .name = "port", | |
374 | .type = QEMU_OPT_NUMBER, | |
c448e855 GH |
375 | },{ |
376 | .name = "tls-port", | |
377 | .type = QEMU_OPT_NUMBER, | |
333b0eeb GH |
378 | },{ |
379 | .name = "addr", | |
380 | .type = QEMU_OPT_STRING, | |
381 | },{ | |
382 | .name = "ipv4", | |
383 | .type = QEMU_OPT_BOOL, | |
384 | },{ | |
385 | .name = "ipv6", | |
386 | .type = QEMU_OPT_BOOL, | |
29b0040b GH |
387 | },{ |
388 | .name = "password", | |
389 | .type = QEMU_OPT_STRING, | |
390 | },{ | |
391 | .name = "disable-ticketing", | |
392 | .type = QEMU_OPT_BOOL, | |
d4970b07 HG |
393 | },{ |
394 | .name = "disable-copy-paste", | |
395 | .type = QEMU_OPT_BOOL, | |
48b3ed0a MAL |
396 | },{ |
397 | .name = "sasl", | |
398 | .type = QEMU_OPT_BOOL, | |
c448e855 GH |
399 | },{ |
400 | .name = "x509-dir", | |
401 | .type = QEMU_OPT_STRING, | |
402 | },{ | |
403 | .name = "x509-key-file", | |
404 | .type = QEMU_OPT_STRING, | |
405 | },{ | |
406 | .name = "x509-key-password", | |
407 | .type = QEMU_OPT_STRING, | |
408 | },{ | |
409 | .name = "x509-cert-file", | |
410 | .type = QEMU_OPT_STRING, | |
411 | },{ | |
412 | .name = "x509-cacert-file", | |
413 | .type = QEMU_OPT_STRING, | |
414 | },{ | |
415 | .name = "x509-dh-key-file", | |
416 | .type = QEMU_OPT_STRING, | |
417 | },{ | |
418 | .name = "tls-ciphers", | |
419 | .type = QEMU_OPT_STRING, | |
17b6dea0 GH |
420 | },{ |
421 | .name = "tls-channel", | |
422 | .type = QEMU_OPT_STRING, | |
423 | },{ | |
424 | .name = "plaintext-channel", | |
425 | .type = QEMU_OPT_STRING, | |
9f04e09e YH |
426 | },{ |
427 | .name = "image-compression", | |
428 | .type = QEMU_OPT_STRING, | |
429 | },{ | |
430 | .name = "jpeg-wan-compression", | |
431 | .type = QEMU_OPT_STRING, | |
432 | },{ | |
433 | .name = "zlib-glz-wan-compression", | |
434 | .type = QEMU_OPT_STRING, | |
84a23f25 GH |
435 | },{ |
436 | .name = "streaming-video", | |
437 | .type = QEMU_OPT_STRING, | |
438 | },{ | |
439 | .name = "agent-mouse", | |
440 | .type = QEMU_OPT_BOOL, | |
441 | },{ | |
442 | .name = "playback-compression", | |
443 | .type = QEMU_OPT_BOOL, | |
29b0040b | 444 | }, |
44bd6907 | 445 | { /* end of list */ } |
29b0040b GH |
446 | }, |
447 | }; | |
448 | ||
2e55e842 GN |
449 | QemuOptsList qemu_option_rom_opts = { |
450 | .name = "option-rom", | |
451 | .implied_opt_name = "romfile", | |
452 | .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head), | |
453 | .desc = { | |
454 | { | |
455 | .name = "bootindex", | |
456 | .type = QEMU_OPT_NUMBER, | |
457 | }, { | |
458 | .name = "romfile", | |
459 | .type = QEMU_OPT_STRING, | |
460 | }, | |
44bd6907 | 461 | { /* end of list */ } |
2e55e842 GN |
462 | }, |
463 | }; | |
464 | ||
303d4e86 AP |
465 | static QemuOptsList qemu_machine_opts = { |
466 | .name = "machine", | |
9052ea6b | 467 | .implied_opt_name = "type", |
303d4e86 AP |
468 | .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head), |
469 | .desc = { | |
470 | { | |
9052ea6b JK |
471 | .name = "type", |
472 | .type = QEMU_OPT_STRING, | |
473 | .help = "emulated machine" | |
474 | }, { | |
303d4e86 AP |
475 | .name = "accel", |
476 | .type = QEMU_OPT_STRING, | |
477 | .help = "accelerator list", | |
478 | }, | |
479 | { /* End of list */ } | |
480 | }, | |
481 | }; | |
482 | ||
dfe795e7 | 483 | static QemuOptsList *vm_config_groups[32] = { |
d058fe03 | 484 | &qemu_drive_opts, |
191bc01b | 485 | &qemu_chardev_opts, |
f31d07d1 | 486 | &qemu_device_opts, |
a1ea458f | 487 | &qemu_netdev_opts, |
8119b33d | 488 | &qemu_net_opts, |
5fdfbf7e | 489 | &qemu_rtc_opts, |
d0fef6fb | 490 | &qemu_global_opts, |
88589343 | 491 | &qemu_mon_opts, |
b5ec5ce0 | 492 | &qemu_cpudef_opts, |
ab6540d5 PS |
493 | #ifdef CONFIG_SIMPLE_TRACE |
494 | &qemu_trace_opts, | |
495 | #endif | |
2e55e842 | 496 | &qemu_option_rom_opts, |
303d4e86 | 497 | &qemu_machine_opts, |
d058fe03 GH |
498 | NULL, |
499 | }; | |
500 | ||
490b648e | 501 | static QemuOptsList *find_list(QemuOptsList **lists, const char *group) |
ddc97855 GH |
502 | { |
503 | int i; | |
504 | ||
505 | for (i = 0; lists[i] != NULL; i++) { | |
506 | if (strcmp(lists[i]->name, group) == 0) | |
507 | break; | |
508 | } | |
509 | if (lists[i] == NULL) { | |
1ecda02b | 510 | error_report("there is no option group \"%s\"", group); |
ddc97855 GH |
511 | } |
512 | return lists[i]; | |
513 | } | |
514 | ||
490b648e KW |
515 | QemuOptsList *qemu_find_opts(const char *group) |
516 | { | |
517 | return find_list(vm_config_groups, group); | |
518 | } | |
519 | ||
dfe795e7 GH |
520 | void qemu_add_opts(QemuOptsList *list) |
521 | { | |
522 | int entries, i; | |
523 | ||
524 | entries = ARRAY_SIZE(vm_config_groups); | |
525 | entries--; /* keep list NULL terminated */ | |
526 | for (i = 0; i < entries; i++) { | |
527 | if (vm_config_groups[i] == NULL) { | |
528 | vm_config_groups[i] = list; | |
529 | return; | |
530 | } | |
531 | } | |
532 | fprintf(stderr, "ran out of space in vm_config_groups"); | |
533 | abort(); | |
534 | } | |
535 | ||
d058fe03 GH |
536 | int qemu_set_option(const char *str) |
537 | { | |
538 | char group[64], id[64], arg[64]; | |
ddc97855 | 539 | QemuOptsList *list; |
d058fe03 | 540 | QemuOpts *opts; |
ddc97855 | 541 | int rc, offset; |
d058fe03 GH |
542 | |
543 | rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset); | |
544 | if (rc < 3 || str[offset] != '=') { | |
1ecda02b | 545 | error_report("can't parse: \"%s\"", str); |
d058fe03 GH |
546 | return -1; |
547 | } | |
548 | ||
304329ee | 549 | list = qemu_find_opts(group); |
ddc97855 | 550 | if (list == NULL) { |
d058fe03 GH |
551 | return -1; |
552 | } | |
553 | ||
ddc97855 | 554 | opts = qemu_opts_find(list, id); |
d058fe03 | 555 | if (!opts) { |
1ecda02b MA |
556 | error_report("there is no %s \"%s\" defined", |
557 | list->name, id); | |
d058fe03 GH |
558 | return -1; |
559 | } | |
560 | ||
3df04ac3 | 561 | if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
d058fe03 GH |
562 | return -1; |
563 | } | |
564 | return 0; | |
565 | } | |
566 | ||
d0fef6fb GH |
567 | int qemu_global_option(const char *str) |
568 | { | |
569 | char driver[64], property[64]; | |
570 | QemuOpts *opts; | |
571 | int rc, offset; | |
572 | ||
573 | rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset); | |
574 | if (rc < 2 || str[offset] != '=') { | |
1ecda02b | 575 | error_report("can't parse: \"%s\"", str); |
d0fef6fb GH |
576 | return -1; |
577 | } | |
578 | ||
579 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0); | |
580 | qemu_opt_set(opts, "driver", driver); | |
581 | qemu_opt_set(opts, "property", property); | |
582 | qemu_opt_set(opts, "value", str+offset+1); | |
583 | return 0; | |
584 | } | |
585 | ||
9d993394 GH |
586 | struct ConfigWriteData { |
587 | QemuOptsList *list; | |
588 | FILE *fp; | |
589 | }; | |
590 | ||
591 | static int config_write_opt(const char *name, const char *value, void *opaque) | |
592 | { | |
593 | struct ConfigWriteData *data = opaque; | |
594 | ||
595 | fprintf(data->fp, " %s = \"%s\"\n", name, value); | |
596 | return 0; | |
597 | } | |
598 | ||
599 | static int config_write_opts(QemuOpts *opts, void *opaque) | |
600 | { | |
601 | struct ConfigWriteData *data = opaque; | |
602 | const char *id = qemu_opts_id(opts); | |
603 | ||
604 | if (id) { | |
605 | fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id); | |
606 | } else { | |
607 | fprintf(data->fp, "[%s]\n", data->list->name); | |
608 | } | |
609 | qemu_opt_foreach(opts, config_write_opt, data, 0); | |
610 | fprintf(data->fp, "\n"); | |
611 | return 0; | |
612 | } | |
613 | ||
614 | void qemu_config_write(FILE *fp) | |
615 | { | |
616 | struct ConfigWriteData data = { .fp = fp }; | |
490b648e | 617 | QemuOptsList **lists = vm_config_groups; |
9d993394 GH |
618 | int i; |
619 | ||
620 | fprintf(fp, "# qemu config file\n\n"); | |
621 | for (i = 0; lists[i] != NULL; i++) { | |
622 | data.list = lists[i]; | |
623 | qemu_opts_foreach(data.list, config_write_opts, &data, 0); | |
624 | } | |
625 | } | |
42262ba8 | 626 | |
490b648e | 627 | int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
42262ba8 GH |
628 | { |
629 | char line[1024], group[64], id[64], arg[64], value[1024]; | |
cf5a65aa | 630 | Location loc; |
42262ba8 GH |
631 | QemuOptsList *list = NULL; |
632 | QemuOpts *opts = NULL; | |
cf5a65aa | 633 | int res = -1, lno = 0; |
42262ba8 | 634 | |
cf5a65aa | 635 | loc_push_none(&loc); |
42262ba8 | 636 | while (fgets(line, sizeof(line), fp) != NULL) { |
cf5a65aa | 637 | loc_set_file(fname, ++lno); |
42262ba8 GH |
638 | if (line[0] == '\n') { |
639 | /* skip empty lines */ | |
640 | continue; | |
641 | } | |
642 | if (line[0] == '#') { | |
643 | /* comment */ | |
644 | continue; | |
645 | } | |
646 | if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { | |
647 | /* group with id */ | |
490b648e | 648 | list = find_list(lists, group); |
42262ba8 | 649 | if (list == NULL) |
cf5a65aa | 650 | goto out; |
42262ba8 GH |
651 | opts = qemu_opts_create(list, id, 1); |
652 | continue; | |
653 | } | |
654 | if (sscanf(line, "[%63[^]]]", group) == 1) { | |
655 | /* group without id */ | |
490b648e | 656 | list = find_list(lists, group); |
42262ba8 | 657 | if (list == NULL) |
cf5a65aa | 658 | goto out; |
42262ba8 GH |
659 | opts = qemu_opts_create(list, NULL, 0); |
660 | continue; | |
661 | } | |
662 | if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { | |
663 | /* arg = value */ | |
664 | if (opts == NULL) { | |
cf5a65aa MA |
665 | error_report("no group defined"); |
666 | goto out; | |
42262ba8 GH |
667 | } |
668 | if (qemu_opt_set(opts, arg, value) != 0) { | |
cf5a65aa | 669 | goto out; |
42262ba8 GH |
670 | } |
671 | continue; | |
672 | } | |
cf5a65aa MA |
673 | error_report("parse error"); |
674 | goto out; | |
42262ba8 | 675 | } |
ef82516d MA |
676 | if (ferror(fp)) { |
677 | error_report("error reading file"); | |
678 | goto out; | |
679 | } | |
cf5a65aa MA |
680 | res = 0; |
681 | out: | |
682 | loc_pop(&loc); | |
683 | return res; | |
42262ba8 | 684 | } |
dcfb0939 KW |
685 | |
686 | int qemu_read_config_file(const char *filename) | |
687 | { | |
688 | FILE *f = fopen(filename, "r"); | |
019e78ba KW |
689 | int ret; |
690 | ||
dcfb0939 KW |
691 | if (f == NULL) { |
692 | return -errno; | |
693 | } | |
694 | ||
019e78ba | 695 | ret = qemu_config_parse(f, vm_config_groups, filename); |
dcfb0939 KW |
696 | fclose(f); |
697 | ||
019e78ba KW |
698 | if (ret == 0) { |
699 | return 0; | |
700 | } else { | |
701 | return -EINVAL; | |
702 | } | |
dcfb0939 | 703 | } |