root / qemu-config.c @ 3329f07b
History | View | Annotate | Download (13.8 kB)
1 |
#include "qemu-common.h" |
---|---|
2 |
#include "qemu-error.h" |
3 |
#include "qemu-option.h" |
4 |
#include "qemu-config.h" |
5 |
#include "sysemu.h" |
6 |
#include "hw/qdev.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 |
},{ |
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, unsafe)",
|
58 |
},{ |
59 |
.name = "aio",
|
60 |
.type = QEMU_OPT_STRING, |
61 |
.help = "host AIO implementation (threads, native)",
|
62 |
},{ |
63 |
.name = "format",
|
64 |
.type = QEMU_OPT_STRING, |
65 |
.help = "disk format (raw, qcow2, ...)",
|
66 |
},{ |
67 |
.name = "serial",
|
68 |
.type = QEMU_OPT_STRING, |
69 |
},{ |
70 |
.name = "rerror",
|
71 |
.type = QEMU_OPT_STRING, |
72 |
},{ |
73 |
.name = "werror",
|
74 |
.type = QEMU_OPT_STRING, |
75 |
},{ |
76 |
.name = "addr",
|
77 |
.type = QEMU_OPT_STRING, |
78 |
.help = "pci address (virtio only)",
|
79 |
},{ |
80 |
.name = "readonly",
|
81 |
.type = QEMU_OPT_BOOL, |
82 |
}, |
83 |
{ /* end if list */ }
|
84 |
}, |
85 |
}; |
86 |
|
87 |
static QemuOptsList qemu_chardev_opts = {
|
88 |
.name = "chardev",
|
89 |
.implied_opt_name = "backend",
|
90 |
.head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
91 |
.desc = { |
92 |
{ |
93 |
.name = "backend",
|
94 |
.type = QEMU_OPT_STRING, |
95 |
},{ |
96 |
.name = "path",
|
97 |
.type = QEMU_OPT_STRING, |
98 |
},{ |
99 |
.name = "host",
|
100 |
.type = QEMU_OPT_STRING, |
101 |
},{ |
102 |
.name = "port",
|
103 |
.type = QEMU_OPT_STRING, |
104 |
},{ |
105 |
.name = "localaddr",
|
106 |
.type = QEMU_OPT_STRING, |
107 |
},{ |
108 |
.name = "localport",
|
109 |
.type = QEMU_OPT_STRING, |
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, |
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, |
143 |
},{ |
144 |
.name = "mux",
|
145 |
.type = QEMU_OPT_BOOL, |
146 |
},{ |
147 |
.name = "signal",
|
148 |
.type = QEMU_OPT_BOOL, |
149 |
}, |
150 |
{ /* end if list */ }
|
151 |
}, |
152 |
}; |
153 |
|
154 |
QemuOptsList 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, |
165 |
}, { |
166 |
.name = "security_model",
|
167 |
.type = QEMU_OPT_STRING, |
168 |
}, |
169 |
{ /*End of list */ }
|
170 |
}, |
171 |
}; |
172 |
|
173 |
QemuOptsList 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, |
187 |
}, { |
188 |
.name = "security_model",
|
189 |
.type = QEMU_OPT_STRING, |
190 |
}, |
191 |
|
192 |
{ /*End of list */ }
|
193 |
}, |
194 |
}; |
195 |
|
196 |
static QemuOptsList qemu_device_opts = {
|
197 |
.name = "device",
|
198 |
.implied_opt_name = "driver",
|
199 |
.head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
200 |
.desc = { |
201 |
/*
|
202 |
* no elements => accept any
|
203 |
* sanity checking will happen later
|
204 |
* when setting device properties
|
205 |
*/
|
206 |
{ /* end if list */ }
|
207 |
}, |
208 |
}; |
209 |
|
210 |
static QemuOptsList qemu_netdev_opts = {
|
211 |
.name = "netdev",
|
212 |
.implied_opt_name = "type",
|
213 |
.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 |
|
223 |
static QemuOptsList qemu_net_opts = {
|
224 |
.name = "net",
|
225 |
.implied_opt_name = "type",
|
226 |
.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 |
|
236 |
static QemuOptsList qemu_rtc_opts = {
|
237 |
.name = "rtc",
|
238 |
.head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head), |
239 |
.desc = { |
240 |
{ |
241 |
.name = "base",
|
242 |
.type = QEMU_OPT_STRING, |
243 |
},{ |
244 |
.name = "clock",
|
245 |
.type = QEMU_OPT_STRING, |
246 |
},{ |
247 |
.name = "driftfix",
|
248 |
.type = QEMU_OPT_STRING, |
249 |
}, |
250 |
{ /* end if list */ }
|
251 |
}, |
252 |
}; |
253 |
|
254 |
static QemuOptsList qemu_global_opts = {
|
255 |
.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 |
}, |
268 |
{ /* end if list */ }
|
269 |
}, |
270 |
}; |
271 |
|
272 |
static QemuOptsList qemu_mon_opts = {
|
273 |
.name = "mon",
|
274 |
.implied_opt_name = "chardev",
|
275 |
.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, |
286 |
}, |
287 |
{ /* end if list */ }
|
288 |
}, |
289 |
}; |
290 |
|
291 |
static QemuOptsList qemu_cpudef_opts = {
|
292 |
.name = "cpudef",
|
293 |
.head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head), |
294 |
.desc = { |
295 |
{ |
296 |
.name = "name",
|
297 |
.type = QEMU_OPT_STRING, |
298 |
},{ |
299 |
.name = "level",
|
300 |
.type = QEMU_OPT_NUMBER, |
301 |
},{ |
302 |
.name = "vendor",
|
303 |
.type = QEMU_OPT_STRING, |
304 |
},{ |
305 |
.name = "family",
|
306 |
.type = QEMU_OPT_NUMBER, |
307 |
},{ |
308 |
.name = "model",
|
309 |
.type = QEMU_OPT_NUMBER, |
310 |
},{ |
311 |
.name = "stepping",
|
312 |
.type = QEMU_OPT_NUMBER, |
313 |
},{ |
314 |
.name = "feature_edx", /* cpuid 0000_0001.edx */ |
315 |
.type = QEMU_OPT_STRING, |
316 |
},{ |
317 |
.name = "feature_ecx", /* cpuid 0000_0001.ecx */ |
318 |
.type = QEMU_OPT_STRING, |
319 |
},{ |
320 |
.name = "extfeature_edx", /* cpuid 8000_0001.edx */ |
321 |
.type = QEMU_OPT_STRING, |
322 |
},{ |
323 |
.name = "extfeature_ecx", /* cpuid 8000_0001.ecx */ |
324 |
.type = QEMU_OPT_STRING, |
325 |
},{ |
326 |
.name = "xlevel",
|
327 |
.type = QEMU_OPT_NUMBER, |
328 |
},{ |
329 |
.name = "model_id",
|
330 |
.type = QEMU_OPT_STRING, |
331 |
},{ |
332 |
.name = "vendor_override",
|
333 |
.type = QEMU_OPT_NUMBER, |
334 |
}, |
335 |
{ /* end of list */ }
|
336 |
}, |
337 |
}; |
338 |
|
339 |
static QemuOptsList *vm_config_groups[32] = { |
340 |
&qemu_drive_opts, |
341 |
&qemu_chardev_opts, |
342 |
&qemu_device_opts, |
343 |
&qemu_netdev_opts, |
344 |
&qemu_net_opts, |
345 |
&qemu_rtc_opts, |
346 |
&qemu_global_opts, |
347 |
&qemu_mon_opts, |
348 |
&qemu_cpudef_opts, |
349 |
NULL,
|
350 |
}; |
351 |
|
352 |
static QemuOptsList *find_list(QemuOptsList **lists, const char *group) |
353 |
{ |
354 |
int i;
|
355 |
|
356 |
for (i = 0; lists[i] != NULL; i++) { |
357 |
if (strcmp(lists[i]->name, group) == 0) |
358 |
break;
|
359 |
} |
360 |
if (lists[i] == NULL) { |
361 |
error_report("there is no option group \"%s\"", group);
|
362 |
} |
363 |
return lists[i];
|
364 |
} |
365 |
|
366 |
QemuOptsList *qemu_find_opts(const char *group) |
367 |
{ |
368 |
return find_list(vm_config_groups, group);
|
369 |
} |
370 |
|
371 |
void qemu_add_opts(QemuOptsList *list)
|
372 |
{ |
373 |
int entries, i;
|
374 |
|
375 |
entries = ARRAY_SIZE(vm_config_groups); |
376 |
entries--; /* keep list NULL terminated */
|
377 |
for (i = 0; i < entries; i++) { |
378 |
if (vm_config_groups[i] == NULL) { |
379 |
vm_config_groups[i] = list; |
380 |
return;
|
381 |
} |
382 |
} |
383 |
fprintf(stderr, "ran out of space in vm_config_groups");
|
384 |
abort(); |
385 |
} |
386 |
|
387 |
int qemu_set_option(const char *str) |
388 |
{ |
389 |
char group[64], id[64], arg[64]; |
390 |
QemuOptsList *list; |
391 |
QemuOpts *opts; |
392 |
int rc, offset;
|
393 |
|
394 |
rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
|
395 |
if (rc < 3 || str[offset] != '=') { |
396 |
error_report("can't parse: \"%s\"", str);
|
397 |
return -1; |
398 |
} |
399 |
|
400 |
list = qemu_find_opts(group); |
401 |
if (list == NULL) { |
402 |
return -1; |
403 |
} |
404 |
|
405 |
opts = qemu_opts_find(list, id); |
406 |
if (!opts) {
|
407 |
error_report("there is no %s \"%s\" defined",
|
408 |
list->name, id); |
409 |
return -1; |
410 |
} |
411 |
|
412 |
if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
413 |
return -1; |
414 |
} |
415 |
return 0; |
416 |
} |
417 |
|
418 |
int qemu_global_option(const char *str) |
419 |
{ |
420 |
char driver[64], property[64]; |
421 |
QemuOpts *opts; |
422 |
int rc, offset;
|
423 |
|
424 |
rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
|
425 |
if (rc < 2 || str[offset] != '=') { |
426 |
error_report("can't parse: \"%s\"", str);
|
427 |
return -1; |
428 |
} |
429 |
|
430 |
opts = qemu_opts_create(&qemu_global_opts, NULL, 0); |
431 |
qemu_opt_set(opts, "driver", driver);
|
432 |
qemu_opt_set(opts, "property", property);
|
433 |
qemu_opt_set(opts, "value", str+offset+1); |
434 |
return 0; |
435 |
} |
436 |
|
437 |
struct ConfigWriteData {
|
438 |
QemuOptsList *list; |
439 |
FILE *fp; |
440 |
}; |
441 |
|
442 |
static int config_write_opt(const char *name, const char *value, void *opaque) |
443 |
{ |
444 |
struct ConfigWriteData *data = opaque;
|
445 |
|
446 |
fprintf(data->fp, " %s = \"%s\"\n", name, value);
|
447 |
return 0; |
448 |
} |
449 |
|
450 |
static int config_write_opts(QemuOpts *opts, void *opaque) |
451 |
{ |
452 |
struct ConfigWriteData *data = opaque;
|
453 |
const char *id = qemu_opts_id(opts); |
454 |
|
455 |
if (id) {
|
456 |
fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
|
457 |
} else {
|
458 |
fprintf(data->fp, "[%s]\n", data->list->name);
|
459 |
} |
460 |
qemu_opt_foreach(opts, config_write_opt, data, 0);
|
461 |
fprintf(data->fp, "\n");
|
462 |
return 0; |
463 |
} |
464 |
|
465 |
void qemu_config_write(FILE *fp)
|
466 |
{ |
467 |
struct ConfigWriteData data = { .fp = fp };
|
468 |
QemuOptsList **lists = vm_config_groups; |
469 |
int i;
|
470 |
|
471 |
fprintf(fp, "# qemu config file\n\n");
|
472 |
for (i = 0; lists[i] != NULL; i++) { |
473 |
data.list = lists[i]; |
474 |
qemu_opts_foreach(data.list, config_write_opts, &data, 0);
|
475 |
} |
476 |
} |
477 |
|
478 |
int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
479 |
{ |
480 |
char line[1024], group[64], id[64], arg[64], value[1024]; |
481 |
Location loc; |
482 |
QemuOptsList *list = NULL;
|
483 |
QemuOpts *opts = NULL;
|
484 |
int res = -1, lno = 0; |
485 |
|
486 |
loc_push_none(&loc); |
487 |
while (fgets(line, sizeof(line), fp) != NULL) { |
488 |
loc_set_file(fname, ++lno); |
489 |
if (line[0] == '\n') { |
490 |
/* skip empty lines */
|
491 |
continue;
|
492 |
} |
493 |
if (line[0] == '#') { |
494 |
/* comment */
|
495 |
continue;
|
496 |
} |
497 |
if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { |
498 |
/* group with id */
|
499 |
list = find_list(lists, group); |
500 |
if (list == NULL) |
501 |
goto out;
|
502 |
opts = qemu_opts_create(list, id, 1);
|
503 |
continue;
|
504 |
} |
505 |
if (sscanf(line, "[%63[^]]]", group) == 1) { |
506 |
/* group without id */
|
507 |
list = find_list(lists, group); |
508 |
if (list == NULL) |
509 |
goto out;
|
510 |
opts = qemu_opts_create(list, NULL, 0); |
511 |
continue;
|
512 |
} |
513 |
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { |
514 |
/* arg = value */
|
515 |
if (opts == NULL) { |
516 |
error_report("no group defined");
|
517 |
goto out;
|
518 |
} |
519 |
if (qemu_opt_set(opts, arg, value) != 0) { |
520 |
goto out;
|
521 |
} |
522 |
continue;
|
523 |
} |
524 |
error_report("parse error");
|
525 |
goto out;
|
526 |
} |
527 |
if (ferror(fp)) {
|
528 |
error_report("error reading file");
|
529 |
goto out;
|
530 |
} |
531 |
res = 0;
|
532 |
out:
|
533 |
loc_pop(&loc); |
534 |
return res;
|
535 |
} |
536 |
|
537 |
int qemu_read_config_file(const char *filename) |
538 |
{ |
539 |
FILE *f = fopen(filename, "r");
|
540 |
int ret;
|
541 |
|
542 |
if (f == NULL) { |
543 |
return -errno;
|
544 |
} |
545 |
|
546 |
ret = qemu_config_parse(f, vm_config_groups, filename); |
547 |
fclose(f); |
548 |
|
549 |
if (ret == 0) { |
550 |
return 0; |
551 |
} else {
|
552 |
return -EINVAL;
|
553 |
} |
554 |
} |