]> git.proxmox.com Git - qemu.git/blame - qemu-config.c
virtio-9p: Add stat and mode related helper functions.
[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"
a861c453 5#include "sysemu.h"
d0fef6fb 6#include "hw/qdev.h"
7282a033
GH
7
8QemuOptsList qemu_drive_opts = {
9 .name = "drive",
72cf2d4f 10 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
7282a033
GH
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 },{
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,
57 .help = "host cache usage (none, writeback, writethrough)",
5c6c3a6c
CH
58 },{
59 .name = "aio",
60 .type = QEMU_OPT_STRING,
61 .help = "host AIO implementation (threads, native)",
7282a033
GH
62 },{
63 .name = "format",
64 .type = QEMU_OPT_STRING,
65 .help = "disk format (raw, qcow2, ...)",
66 },{
67 .name = "serial",
68 .type = QEMU_OPT_STRING,
e9b2e818
KW
69 },{
70 .name = "rerror",
71 .type = QEMU_OPT_STRING,
7282a033
GH
72 },{
73 .name = "werror",
74 .type = QEMU_OPT_STRING,
75 },{
76 .name = "addr",
77 .type = QEMU_OPT_STRING,
78 .help = "pci address (virtio only)",
59f2689d
NS
79 },{
80 .name = "readonly",
81 .type = QEMU_OPT_BOOL,
7282a033
GH
82 },
83 { /* end if list */ }
84 },
85};
86
191bc01b
GH
87QemuOptsList qemu_chardev_opts = {
88 .name = "chardev",
8212c64f 89 .implied_opt_name = "backend",
72cf2d4f 90 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
191bc01b 91 .desc = {
7d31544f
GH
92 {
93 .name = "backend",
94 .type = QEMU_OPT_STRING,
95 },{
96 .name = "path",
97 .type = QEMU_OPT_STRING,
aeb2c47a
GH
98 },{
99 .name = "host",
100 .type = QEMU_OPT_STRING,
101 },{
102 .name = "port",
103 .type = QEMU_OPT_STRING,
7e1b35b4
GH
104 },{
105 .name = "localaddr",
106 .type = QEMU_OPT_STRING,
107 },{
108 .name = "localport",
109 .type = QEMU_OPT_STRING,
aeb2c47a
GH
110 },{
111 .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,
6ea314d9
GH
131 },{
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,
c845f401
GH
143 },{
144 .name = "mux",
145 .type = QEMU_OPT_BOOL,
5989020b
KK
146 },{
147 .name = "signal",
148 .type = QEMU_OPT_BOOL,
7d31544f 149 },
191bc01b
GH
150 { /* end if list */ }
151 },
152};
153
74db920c
GS
154#ifdef CONFIG_LINUX
155QemuOptsList qemu_fsdev_opts = {
156 .name = "fsdev",
157 .implied_opt_name = "fstype",
158 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
159 .desc = {
160 {
161 .name = "fstype",
162 .type = QEMU_OPT_STRING,
163 }, {
164 .name = "path",
165 .type = QEMU_OPT_STRING,
166 },
167 { /*End of list */ }
168 },
169};
170#endif
171
f31d07d1
GH
172QemuOptsList qemu_device_opts = {
173 .name = "device",
8212c64f 174 .implied_opt_name = "driver",
72cf2d4f 175 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
f31d07d1
GH
176 .desc = {
177 /*
178 * no elements => accept any
179 * sanity checking will happen later
180 * when setting device properties
181 */
182 { /* end if list */ }
183 },
184};
185
a1ea458f
MM
186QemuOptsList qemu_netdev_opts = {
187 .name = "netdev",
8212c64f 188 .implied_opt_name = "type",
a1ea458f
MM
189 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
190 .desc = {
191 /*
192 * no elements => accept any params
193 * validation will happen later
194 */
195 { /* end of list */ }
196 },
197};
198
8119b33d
MM
199QemuOptsList qemu_net_opts = {
200 .name = "net",
8212c64f 201 .implied_opt_name = "type",
8119b33d
MM
202 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
203 .desc = {
204 /*
205 * no elements => accept any params
206 * validation will happen later
207 */
208 { /* end of list */ }
209 },
210};
211
1ed2fc1f
JK
212QemuOptsList qemu_rtc_opts = {
213 .name = "rtc",
214 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
215 .desc = {
216 {
217 .name = "base",
218 .type = QEMU_OPT_STRING,
6875204c
JK
219 },{
220 .name = "clock",
221 .type = QEMU_OPT_STRING,
1ed2fc1f
JK
222#ifdef TARGET_I386
223 },{
224 .name = "driftfix",
225 .type = QEMU_OPT_STRING,
226#endif
227 },
228 { /* end if list */ }
229 },
230};
231
d0fef6fb
GH
232QemuOptsList qemu_global_opts = {
233 .name = "global",
234 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
235 .desc = {
236 {
237 .name = "driver",
238 .type = QEMU_OPT_STRING,
239 },{
240 .name = "property",
241 .type = QEMU_OPT_STRING,
242 },{
243 .name = "value",
244 .type = QEMU_OPT_STRING,
245 },
246 { /* end if list */ }
247 },
248};
249
88589343
GH
250QemuOptsList qemu_mon_opts = {
251 .name = "mon",
8212c64f 252 .implied_opt_name = "chardev",
88589343
GH
253 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
254 .desc = {
255 {
256 .name = "mode",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "chardev",
260 .type = QEMU_OPT_STRING,
261 },{
262 .name = "default",
263 .type = QEMU_OPT_BOOL,
264 },
265 { /* end if list */ }
266 },
267};
268
b5ec5ce0 269QemuOptsList qemu_cpudef_opts = {
270 .name = "cpudef",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
272 .desc = {
273 {
274 .name = "name",
275 .type = QEMU_OPT_STRING,
276 },{
277 .name = "level",
278 .type = QEMU_OPT_NUMBER,
279 },{
280 .name = "vendor",
281 .type = QEMU_OPT_STRING,
282 },{
283 .name = "family",
284 .type = QEMU_OPT_NUMBER,
285 },{
286 .name = "model",
287 .type = QEMU_OPT_NUMBER,
288 },{
289 .name = "stepping",
290 .type = QEMU_OPT_NUMBER,
291 },{
292 .name = "feature_edx", /* cpuid 0000_0001.edx */
293 .type = QEMU_OPT_STRING,
294 },{
295 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
296 .type = QEMU_OPT_STRING,
297 },{
298 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
299 .type = QEMU_OPT_STRING,
300 },{
301 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
302 .type = QEMU_OPT_STRING,
303 },{
304 .name = "xlevel",
305 .type = QEMU_OPT_NUMBER,
306 },{
307 .name = "model_id",
308 .type = QEMU_OPT_STRING,
309 },{
310 .name = "vendor_override",
311 .type = QEMU_OPT_NUMBER,
312 },
313 { /* end of list */ }
314 },
315};
316
490b648e 317static QemuOptsList *vm_config_groups[] = {
d058fe03 318 &qemu_drive_opts,
191bc01b 319 &qemu_chardev_opts,
f31d07d1 320 &qemu_device_opts,
a1ea458f 321 &qemu_netdev_opts,
8119b33d 322 &qemu_net_opts,
5fdfbf7e 323 &qemu_rtc_opts,
d0fef6fb 324 &qemu_global_opts,
88589343 325 &qemu_mon_opts,
b5ec5ce0 326 &qemu_cpudef_opts,
d058fe03
GH
327 NULL,
328};
329
490b648e 330static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
ddc97855
GH
331{
332 int i;
333
334 for (i = 0; lists[i] != NULL; i++) {
335 if (strcmp(lists[i]->name, group) == 0)
336 break;
337 }
338 if (lists[i] == NULL) {
1ecda02b 339 error_report("there is no option group \"%s\"", group);
ddc97855
GH
340 }
341 return lists[i];
342}
343
490b648e
KW
344QemuOptsList *qemu_find_opts(const char *group)
345{
346 return find_list(vm_config_groups, group);
347}
348
d058fe03
GH
349int qemu_set_option(const char *str)
350{
351 char group[64], id[64], arg[64];
ddc97855 352 QemuOptsList *list;
d058fe03 353 QemuOpts *opts;
ddc97855 354 int rc, offset;
d058fe03
GH
355
356 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
357 if (rc < 3 || str[offset] != '=') {
1ecda02b 358 error_report("can't parse: \"%s\"", str);
d058fe03
GH
359 return -1;
360 }
361
304329ee 362 list = qemu_find_opts(group);
ddc97855 363 if (list == NULL) {
d058fe03
GH
364 return -1;
365 }
366
ddc97855 367 opts = qemu_opts_find(list, id);
d058fe03 368 if (!opts) {
1ecda02b
MA
369 error_report("there is no %s \"%s\" defined",
370 list->name, id);
d058fe03
GH
371 return -1;
372 }
373
3df04ac3 374 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
d058fe03
GH
375 return -1;
376 }
377 return 0;
378}
379
d0fef6fb
GH
380int qemu_global_option(const char *str)
381{
382 char driver[64], property[64];
383 QemuOpts *opts;
384 int rc, offset;
385
386 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
387 if (rc < 2 || str[offset] != '=') {
1ecda02b 388 error_report("can't parse: \"%s\"", str);
d0fef6fb
GH
389 return -1;
390 }
391
392 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
393 qemu_opt_set(opts, "driver", driver);
394 qemu_opt_set(opts, "property", property);
395 qemu_opt_set(opts, "value", str+offset+1);
396 return 0;
397}
398
9d993394
GH
399struct ConfigWriteData {
400 QemuOptsList *list;
401 FILE *fp;
402};
403
404static int config_write_opt(const char *name, const char *value, void *opaque)
405{
406 struct ConfigWriteData *data = opaque;
407
408 fprintf(data->fp, " %s = \"%s\"\n", name, value);
409 return 0;
410}
411
412static int config_write_opts(QemuOpts *opts, void *opaque)
413{
414 struct ConfigWriteData *data = opaque;
415 const char *id = qemu_opts_id(opts);
416
417 if (id) {
418 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
419 } else {
420 fprintf(data->fp, "[%s]\n", data->list->name);
421 }
422 qemu_opt_foreach(opts, config_write_opt, data, 0);
423 fprintf(data->fp, "\n");
424 return 0;
425}
426
427void qemu_config_write(FILE *fp)
428{
429 struct ConfigWriteData data = { .fp = fp };
490b648e 430 QemuOptsList **lists = vm_config_groups;
9d993394
GH
431 int i;
432
433 fprintf(fp, "# qemu config file\n\n");
434 for (i = 0; lists[i] != NULL; i++) {
435 data.list = lists[i];
436 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
437 }
438}
42262ba8 439
490b648e 440int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
42262ba8
GH
441{
442 char line[1024], group[64], id[64], arg[64], value[1024];
cf5a65aa 443 Location loc;
42262ba8
GH
444 QemuOptsList *list = NULL;
445 QemuOpts *opts = NULL;
cf5a65aa 446 int res = -1, lno = 0;
42262ba8 447
cf5a65aa 448 loc_push_none(&loc);
42262ba8 449 while (fgets(line, sizeof(line), fp) != NULL) {
cf5a65aa 450 loc_set_file(fname, ++lno);
42262ba8
GH
451 if (line[0] == '\n') {
452 /* skip empty lines */
453 continue;
454 }
455 if (line[0] == '#') {
456 /* comment */
457 continue;
458 }
459 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
460 /* group with id */
490b648e 461 list = find_list(lists, group);
42262ba8 462 if (list == NULL)
cf5a65aa 463 goto out;
42262ba8
GH
464 opts = qemu_opts_create(list, id, 1);
465 continue;
466 }
467 if (sscanf(line, "[%63[^]]]", group) == 1) {
468 /* group without id */
490b648e 469 list = find_list(lists, group);
42262ba8 470 if (list == NULL)
cf5a65aa 471 goto out;
42262ba8
GH
472 opts = qemu_opts_create(list, NULL, 0);
473 continue;
474 }
475 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
476 /* arg = value */
477 if (opts == NULL) {
cf5a65aa
MA
478 error_report("no group defined");
479 goto out;
42262ba8
GH
480 }
481 if (qemu_opt_set(opts, arg, value) != 0) {
cf5a65aa 482 goto out;
42262ba8
GH
483 }
484 continue;
485 }
cf5a65aa
MA
486 error_report("parse error");
487 goto out;
42262ba8 488 }
ef82516d
MA
489 if (ferror(fp)) {
490 error_report("error reading file");
491 goto out;
492 }
cf5a65aa
MA
493 res = 0;
494out:
495 loc_pop(&loc);
496 return res;
42262ba8 497}
dcfb0939
KW
498
499int qemu_read_config_file(const char *filename)
500{
501 FILE *f = fopen(filename, "r");
502 if (f == NULL) {
503 return -errno;
504 }
505
490b648e 506 if (qemu_config_parse(f, vm_config_groups, filename) != 0) {
dcfb0939
KW
507 return -EINVAL;
508 }
509 fclose(f);
510
511 return 0;
512}