blob: 9f0f1435fde3c635c3e843ca1735355caa2aeb5b [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
8QemuOptsList qemu_drive_opts = {
9 .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,
57 .help = "host cache usage (none, writeback, writethrough)",
58 },{
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 },
83 { /* end if list */ }
84 },
85};
86
Gerd Hoffmann191bc012009-09-10 10:58:35 +020087QemuOptsList qemu_chardev_opts = {
88 .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 },
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200150 { /* end if list */ }
151 },
152};
153
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200154QemuOptsList qemu_device_opts = {
155 .name = "device",
Markus Armbruster8212c642010-02-10 19:52:18 +0100156 .implied_opt_name = "driver",
Blue Swirl72cf2d42009-09-12 07:36:22 +0000157 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200158 .desc = {
159 /*
160 * no elements => accept any
161 * sanity checking will happen later
162 * when setting device properties
163 */
164 { /* end if list */ }
165 },
166};
167
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100168QemuOptsList qemu_netdev_opts = {
169 .name = "netdev",
Markus Armbruster8212c642010-02-10 19:52:18 +0100170 .implied_opt_name = "type",
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100171 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
172 .desc = {
173 /*
174 * no elements => accept any params
175 * validation will happen later
176 */
177 { /* end of list */ }
178 },
179};
180
Mark McLoughlin8119b332009-10-06 12:17:05 +0100181QemuOptsList qemu_net_opts = {
182 .name = "net",
Markus Armbruster8212c642010-02-10 19:52:18 +0100183 .implied_opt_name = "type",
Mark McLoughlin8119b332009-10-06 12:17:05 +0100184 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
185 .desc = {
186 /*
187 * no elements => accept any params
188 * validation will happen later
189 */
190 { /* end of list */ }
191 },
192};
193
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200194QemuOptsList qemu_rtc_opts = {
195 .name = "rtc",
196 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
197 .desc = {
198 {
199 .name = "base",
200 .type = QEMU_OPT_STRING,
Jan Kiszka68752042009-09-15 13:36:04 +0200201 },{
202 .name = "clock",
203 .type = QEMU_OPT_STRING,
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200204#ifdef TARGET_I386
205 },{
206 .name = "driftfix",
207 .type = QEMU_OPT_STRING,
208#endif
209 },
210 { /* end if list */ }
211 },
212};
213
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100214QemuOptsList qemu_global_opts = {
215 .name = "global",
216 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
217 .desc = {
218 {
219 .name = "driver",
220 .type = QEMU_OPT_STRING,
221 },{
222 .name = "property",
223 .type = QEMU_OPT_STRING,
224 },{
225 .name = "value",
226 .type = QEMU_OPT_STRING,
227 },
228 { /* end if list */ }
229 },
230};
231
Gerd Hoffmann88589342009-12-08 13:11:50 +0100232QemuOptsList qemu_mon_opts = {
233 .name = "mon",
Markus Armbruster8212c642010-02-10 19:52:18 +0100234 .implied_opt_name = "chardev",
Gerd Hoffmann88589342009-12-08 13:11:50 +0100235 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
236 .desc = {
237 {
238 .name = "mode",
239 .type = QEMU_OPT_STRING,
240 },{
241 .name = "chardev",
242 .type = QEMU_OPT_STRING,
243 },{
244 .name = "default",
245 .type = QEMU_OPT_BOOL,
246 },
247 { /* end if list */ }
248 },
249};
250
john cooperb5ec5ce2010-02-20 11:14:59 -0600251QemuOptsList qemu_cpudef_opts = {
252 .name = "cpudef",
253 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
254 .desc = {
255 {
256 .name = "name",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "level",
260 .type = QEMU_OPT_NUMBER,
261 },{
262 .name = "vendor",
263 .type = QEMU_OPT_STRING,
264 },{
265 .name = "family",
266 .type = QEMU_OPT_NUMBER,
267 },{
268 .name = "model",
269 .type = QEMU_OPT_NUMBER,
270 },{
271 .name = "stepping",
272 .type = QEMU_OPT_NUMBER,
273 },{
274 .name = "feature_edx", /* cpuid 0000_0001.edx */
275 .type = QEMU_OPT_STRING,
276 },{
277 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
278 .type = QEMU_OPT_STRING,
279 },{
280 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
281 .type = QEMU_OPT_STRING,
282 },{
283 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
284 .type = QEMU_OPT_STRING,
285 },{
286 .name = "xlevel",
287 .type = QEMU_OPT_NUMBER,
288 },{
289 .name = "model_id",
290 .type = QEMU_OPT_STRING,
291 },{
292 .name = "vendor_override",
293 .type = QEMU_OPT_NUMBER,
294 },
295 { /* end of list */ }
296 },
297};
298
Kevin Wolf490b6482010-03-05 18:21:56 +0100299static QemuOptsList *vm_config_groups[] = {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200300 &qemu_drive_opts,
Gerd Hoffmann191bc012009-09-10 10:58:35 +0200301 &qemu_chardev_opts,
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +0200302 &qemu_device_opts,
Mark McLoughlina1ea4582009-10-08 19:58:26 +0100303 &qemu_netdev_opts,
Mark McLoughlin8119b332009-10-06 12:17:05 +0100304 &qemu_net_opts,
Jan Kiszka5fdfbf72009-10-06 12:16:50 +0100305 &qemu_rtc_opts,
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100306 &qemu_global_opts,
Gerd Hoffmann88589342009-12-08 13:11:50 +0100307 &qemu_mon_opts,
john cooperb5ec5ce2010-02-20 11:14:59 -0600308 &qemu_cpudef_opts,
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200309 NULL,
310};
311
Kevin Wolf490b6482010-03-05 18:21:56 +0100312static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200313{
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200314 int i;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200315
316 for (i = 0; lists[i] != NULL; i++) {
317 if (strcmp(lists[i]->name, group) == 0)
318 break;
319 }
320 if (lists[i] == NULL) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100321 error_report("there is no option group \"%s\"", group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200322 }
323 return lists[i];
324}
325
Kevin Wolf490b6482010-03-05 18:21:56 +0100326QemuOptsList *qemu_find_opts(const char *group)
327{
328 return find_list(vm_config_groups, group);
329}
330
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200331int qemu_set_option(const char *str)
332{
333 char group[64], id[64], arg[64];
334 QemuOptsList *list;
335 QemuOpts *opts;
336 int rc, offset;
337
338 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
339 if (rc < 3 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100340 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200341 return -1;
342 }
343
Markus Armbruster304329e2010-02-10 20:09:14 +0100344 list = qemu_find_opts(group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200345 if (list == NULL) {
346 return -1;
347 }
348
349 opts = qemu_opts_find(list, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200350 if (!opts) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100351 error_report("there is no %s \"%s\" defined",
352 list->name, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200353 return -1;
354 }
355
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100356 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200357 return -1;
358 }
359 return 0;
360}
361
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100362int qemu_global_option(const char *str)
363{
364 char driver[64], property[64];
365 QemuOpts *opts;
366 int rc, offset;
367
368 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
369 if (rc < 2 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100370 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +0100371 return -1;
372 }
373
374 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
375 qemu_opt_set(opts, "driver", driver);
376 qemu_opt_set(opts, "property", property);
377 qemu_opt_set(opts, "value", str+offset+1);
378 return 0;
379}
380
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200381struct ConfigWriteData {
382 QemuOptsList *list;
383 FILE *fp;
384};
385
386static int config_write_opt(const char *name, const char *value, void *opaque)
387{
388 struct ConfigWriteData *data = opaque;
389
390 fprintf(data->fp, " %s = \"%s\"\n", name, value);
391 return 0;
392}
393
394static int config_write_opts(QemuOpts *opts, void *opaque)
395{
396 struct ConfigWriteData *data = opaque;
397 const char *id = qemu_opts_id(opts);
398
399 if (id) {
400 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
401 } else {
402 fprintf(data->fp, "[%s]\n", data->list->name);
403 }
404 qemu_opt_foreach(opts, config_write_opt, data, 0);
405 fprintf(data->fp, "\n");
406 return 0;
407}
408
409void qemu_config_write(FILE *fp)
410{
411 struct ConfigWriteData data = { .fp = fp };
Kevin Wolf490b6482010-03-05 18:21:56 +0100412 QemuOptsList **lists = vm_config_groups;
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200413 int i;
414
415 fprintf(fp, "# qemu config file\n\n");
416 for (i = 0; lists[i] != NULL; i++) {
417 data.list = lists[i];
418 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
419 }
420}
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200421
Kevin Wolf490b6482010-03-05 18:21:56 +0100422int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200423{
424 char line[1024], group[64], id[64], arg[64], value[1024];
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100425 Location loc;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200426 QemuOptsList *list = NULL;
427 QemuOpts *opts = NULL;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100428 int res = -1, lno = 0;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200429
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100430 loc_push_none(&loc);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200431 while (fgets(line, sizeof(line), fp) != NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100432 loc_set_file(fname, ++lno);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200433 if (line[0] == '\n') {
434 /* skip empty lines */
435 continue;
436 }
437 if (line[0] == '#') {
438 /* comment */
439 continue;
440 }
441 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
442 /* group with id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100443 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200444 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100445 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200446 opts = qemu_opts_create(list, id, 1);
447 continue;
448 }
449 if (sscanf(line, "[%63[^]]]", group) == 1) {
450 /* group without id */
Kevin Wolf490b6482010-03-05 18:21:56 +0100451 list = find_list(lists, group);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200452 if (list == NULL)
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100453 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200454 opts = qemu_opts_create(list, NULL, 0);
455 continue;
456 }
457 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
458 /* arg = value */
459 if (opts == NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100460 error_report("no group defined");
461 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200462 }
463 if (qemu_opt_set(opts, arg, value) != 0) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100464 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200465 }
466 continue;
467 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100468 error_report("parse error");
469 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200470 }
Markus Armbrusteref825162010-02-18 19:56:01 +0100471 if (ferror(fp)) {
472 error_report("error reading file");
473 goto out;
474 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100475 res = 0;
476out:
477 loc_pop(&loc);
478 return res;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200479}
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100480
481int qemu_read_config_file(const char *filename)
482{
483 FILE *f = fopen(filename, "r");
484 if (f == NULL) {
485 return -errno;
486 }
487
Kevin Wolf490b6482010-03-05 18:21:56 +0100488 if (qemu_config_parse(f, vm_config_groups, filename) != 0) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100489 return -EINVAL;
490 }
491 fclose(f);
492
493 return 0;
494}