1 #include "qemu-common.h"
2 #include "qemu-option.h"
3 #include "qemu-config.h"
7 QemuOptsList qemu_drive_opts
= {
9 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
13 .type
= QEMU_OPT_NUMBER
,
17 .type
= QEMU_OPT_NUMBER
,
18 .help
= "unit number (i.e. lun for scsi)",
21 .type
= QEMU_OPT_STRING
,
22 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
25 .type
= QEMU_OPT_NUMBER
,
28 .type
= QEMU_OPT_NUMBER
,
29 .help
= "number of cylinders (ide disk geometry)",
32 .type
= QEMU_OPT_NUMBER
,
33 .help
= "number of heads (ide disk geometry)",
36 .type
= QEMU_OPT_NUMBER
,
37 .help
= "number of sectors (ide disk geometry)",
40 .type
= QEMU_OPT_STRING
,
41 .help
= "chs translation (auto, lba. none)",
44 .type
= QEMU_OPT_STRING
,
45 .help
= "media type (disk, cdrom)",
48 .type
= QEMU_OPT_BOOL
,
51 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_STRING
,
56 .help
= "host cache usage (none, writeback, writethrough)",
59 .type
= QEMU_OPT_STRING
,
60 .help
= "host AIO implementation (threads, native)",
63 .type
= QEMU_OPT_STRING
,
64 .help
= "disk format (raw, qcow2, ...)",
67 .type
= QEMU_OPT_STRING
,
70 .type
= QEMU_OPT_STRING
,
73 .type
= QEMU_OPT_STRING
,
76 .type
= QEMU_OPT_STRING
,
77 .help
= "pci address (virtio only)",
80 .type
= QEMU_OPT_BOOL
,
83 .type
= QEMU_OPT_BOOL
,
84 .help
= "make this a boot drive",
90 QemuOptsList qemu_chardev_opts
= {
92 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
96 .type
= QEMU_OPT_STRING
,
99 .type
= QEMU_OPT_STRING
,
102 .type
= QEMU_OPT_STRING
,
105 .type
= QEMU_OPT_STRING
,
108 .type
= QEMU_OPT_STRING
,
111 .type
= QEMU_OPT_STRING
,
114 .type
= QEMU_OPT_NUMBER
,
117 .type
= QEMU_OPT_BOOL
,
120 .type
= QEMU_OPT_BOOL
,
123 .type
= QEMU_OPT_BOOL
,
126 .type
= QEMU_OPT_BOOL
,
129 .type
= QEMU_OPT_BOOL
,
132 .type
= QEMU_OPT_BOOL
,
135 .type
= QEMU_OPT_NUMBER
,
138 .type
= QEMU_OPT_NUMBER
,
141 .type
= QEMU_OPT_NUMBER
,
144 .type
= QEMU_OPT_NUMBER
,
147 .type
= QEMU_OPT_BOOL
,
150 .type
= QEMU_OPT_BOOL
,
152 { /* end if list */ }
156 QemuOptsList qemu_device_opts
= {
158 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
161 * no elements => accept any
162 * sanity checking will happen later
163 * when setting device properties
165 { /* end if list */ }
169 QemuOptsList qemu_netdev_opts
= {
171 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
174 * no elements => accept any params
175 * validation will happen later
177 { /* end of list */ }
181 QemuOptsList qemu_net_opts
= {
183 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
186 * no elements => accept any params
187 * validation will happen later
189 { /* end of list */ }
193 QemuOptsList qemu_rtc_opts
= {
195 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
199 .type
= QEMU_OPT_STRING
,
202 .type
= QEMU_OPT_STRING
,
206 .type
= QEMU_OPT_STRING
,
209 { /* end if list */ }
213 QemuOptsList qemu_global_opts
= {
215 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
219 .type
= QEMU_OPT_STRING
,
222 .type
= QEMU_OPT_STRING
,
225 .type
= QEMU_OPT_STRING
,
227 { /* end if list */ }
231 QemuOptsList qemu_mon_opts
= {
233 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
237 .type
= QEMU_OPT_STRING
,
240 .type
= QEMU_OPT_STRING
,
243 .type
= QEMU_OPT_BOOL
,
245 { /* end if list */ }
249 static QemuOptsList
*lists
[] = {
261 static QemuOptsList
*find_list(const char *group
)
265 for (i
= 0; lists
[i
] != NULL
; i
++) {
266 if (strcmp(lists
[i
]->name
, group
) == 0)
269 if (lists
[i
] == NULL
) {
270 qemu_error("there is no option group \"%s\"\n", group
);
275 int qemu_set_option(const char *str
)
277 char group
[64], id
[64], arg
[64];
282 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
283 if (rc
< 3 || str
[offset
] != '=') {
284 qemu_error("can't parse: \"%s\"\n", str
);
288 list
= find_list(group
);
293 opts
= qemu_opts_find(list
, id
);
295 qemu_error("there is no %s \"%s\" defined\n",
300 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
306 int qemu_global_option(const char *str
)
308 char driver
[64], property
[64];
312 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
313 if (rc
< 2 || str
[offset
] != '=') {
314 qemu_error("can't parse: \"%s\"\n", str
);
318 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
319 qemu_opt_set(opts
, "driver", driver
);
320 qemu_opt_set(opts
, "property", property
);
321 qemu_opt_set(opts
, "value", str
+offset
+1);
325 static int qemu_add_one_global(QemuOpts
*opts
, void *opaque
)
329 g
= qemu_mallocz(sizeof(*g
));
330 g
->driver
= qemu_opt_get(opts
, "driver");
331 g
->property
= qemu_opt_get(opts
, "property");
332 g
->value
= qemu_opt_get(opts
, "value");
333 qdev_prop_register_global(g
);
337 void qemu_add_globals(void)
339 qemu_opts_foreach(&qemu_global_opts
, qemu_add_one_global
, NULL
, 0);
342 struct ConfigWriteData
{
347 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
349 struct ConfigWriteData
*data
= opaque
;
351 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
355 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
357 struct ConfigWriteData
*data
= opaque
;
358 const char *id
= qemu_opts_id(opts
);
361 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
363 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
365 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
366 fprintf(data
->fp
, "\n");
370 void qemu_config_write(FILE *fp
)
372 struct ConfigWriteData data
= { .fp
= fp
};
375 fprintf(fp
, "# qemu config file\n\n");
376 for (i
= 0; lists
[i
] != NULL
; i
++) {
377 data
.list
= lists
[i
];
378 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
382 int qemu_config_parse(FILE *fp
)
384 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
385 QemuOptsList
*list
= NULL
;
386 QemuOpts
*opts
= NULL
;
388 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
389 if (line
[0] == '\n') {
390 /* skip empty lines */
393 if (line
[0] == '#') {
397 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
399 list
= find_list(group
);
402 opts
= qemu_opts_create(list
, id
, 1);
405 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
406 /* group without id */
407 list
= find_list(group
);
410 opts
= qemu_opts_create(list
, NULL
, 0);
413 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
416 fprintf(stderr
, "no group defined\n");
419 if (qemu_opt_set(opts
, arg
, value
) != 0) {
420 fprintf(stderr
, "failed to set \"%s\" for %s\n",
426 fprintf(stderr
, "parse error: %s\n", line
);