1 #include "qemu-common.h"
2 #include "qemu-option.h"
3 #include "qemu-config.h"
6 QemuOptsList qemu_drive_opts
= {
8 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
12 .type
= QEMU_OPT_NUMBER
,
16 .type
= QEMU_OPT_NUMBER
,
17 .help
= "unit number (i.e. lun for scsi)",
20 .type
= QEMU_OPT_STRING
,
21 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
24 .type
= QEMU_OPT_NUMBER
,
27 .type
= QEMU_OPT_NUMBER
,
28 .help
= "number of cylinders (ide disk geometry)",
31 .type
= QEMU_OPT_NUMBER
,
32 .help
= "number of heads (ide disk geometry)",
35 .type
= QEMU_OPT_NUMBER
,
36 .help
= "number of sectors (ide disk geometry)",
39 .type
= QEMU_OPT_STRING
,
40 .help
= "chs translation (auto, lba. none)",
43 .type
= QEMU_OPT_STRING
,
44 .help
= "media type (disk, cdrom)",
47 .type
= QEMU_OPT_BOOL
,
50 .type
= QEMU_OPT_STRING
,
54 .type
= QEMU_OPT_STRING
,
55 .help
= "host cache usage (none, writeback, writethrough)",
58 .type
= QEMU_OPT_STRING
,
59 .help
= "host AIO implementation (threads, native)",
62 .type
= QEMU_OPT_STRING
,
63 .help
= "disk format (raw, qcow2, ...)",
66 .type
= QEMU_OPT_STRING
,
69 .type
= QEMU_OPT_STRING
,
72 .type
= QEMU_OPT_STRING
,
75 .type
= QEMU_OPT_STRING
,
76 .help
= "pci address (virtio only)",
79 .type
= QEMU_OPT_BOOL
,
85 QemuOptsList qemu_chardev_opts
= {
87 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
91 .type
= QEMU_OPT_STRING
,
94 .type
= QEMU_OPT_STRING
,
97 .type
= QEMU_OPT_STRING
,
100 .type
= QEMU_OPT_STRING
,
103 .type
= QEMU_OPT_STRING
,
106 .type
= QEMU_OPT_STRING
,
109 .type
= QEMU_OPT_NUMBER
,
112 .type
= QEMU_OPT_BOOL
,
115 .type
= QEMU_OPT_BOOL
,
118 .type
= QEMU_OPT_BOOL
,
121 .type
= QEMU_OPT_BOOL
,
124 .type
= QEMU_OPT_BOOL
,
127 .type
= QEMU_OPT_BOOL
,
130 .type
= QEMU_OPT_NUMBER
,
133 .type
= QEMU_OPT_NUMBER
,
136 .type
= QEMU_OPT_NUMBER
,
139 .type
= QEMU_OPT_NUMBER
,
142 .type
= QEMU_OPT_BOOL
,
145 .type
= QEMU_OPT_BOOL
,
147 { /* end if list */ }
151 QemuOptsList qemu_device_opts
= {
153 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
156 * no elements => accept any
157 * sanity checking will happen later
158 * when setting device properties
160 { /* end if list */ }
164 QemuOptsList qemu_netdev_opts
= {
166 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
169 * no elements => accept any params
170 * validation will happen later
172 { /* end of list */ }
176 QemuOptsList qemu_net_opts
= {
178 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
181 * no elements => accept any params
182 * validation will happen later
184 { /* end of list */ }
188 QemuOptsList qemu_rtc_opts
= {
190 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
194 .type
= QEMU_OPT_STRING
,
197 .type
= QEMU_OPT_STRING
,
201 .type
= QEMU_OPT_STRING
,
204 { /* end if list */ }
208 static QemuOptsList
*lists
[] = {
218 static QemuOptsList
*find_list(const char *group
)
222 for (i
= 0; lists
[i
] != NULL
; i
++) {
223 if (strcmp(lists
[i
]->name
, group
) == 0)
226 if (lists
[i
] == NULL
) {
227 qemu_error("there is no option group \"%s\"\n", group
);
232 int qemu_set_option(const char *str
)
234 char group
[64], id
[64], arg
[64];
239 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
240 if (rc
< 3 || str
[offset
] != '=') {
241 qemu_error("can't parse: \"%s\"\n", str
);
245 list
= find_list(group
);
250 opts
= qemu_opts_find(list
, id
);
252 qemu_error("there is no %s \"%s\" defined\n",
257 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
263 struct ConfigWriteData
{
268 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
270 struct ConfigWriteData
*data
= opaque
;
272 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
276 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
278 struct ConfigWriteData
*data
= opaque
;
279 const char *id
= qemu_opts_id(opts
);
282 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
284 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
286 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
287 fprintf(data
->fp
, "\n");
291 void qemu_config_write(FILE *fp
)
293 struct ConfigWriteData data
= { .fp
= fp
};
296 fprintf(fp
, "# qemu config file\n\n");
297 for (i
= 0; lists
[i
] != NULL
; i
++) {
298 data
.list
= lists
[i
];
299 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
303 int qemu_config_parse(FILE *fp
)
305 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
306 QemuOptsList
*list
= NULL
;
307 QemuOpts
*opts
= NULL
;
309 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
310 if (line
[0] == '\n') {
311 /* skip empty lines */
314 if (line
[0] == '#') {
318 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
320 list
= find_list(group
);
323 opts
= qemu_opts_create(list
, id
, 1);
326 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
327 /* group without id */
328 list
= find_list(group
);
331 opts
= qemu_opts_create(list
, NULL
, 0);
334 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
337 fprintf(stderr
, "no group defined\n");
340 if (qemu_opt_set(opts
, arg
, value
) != 0) {
341 fprintf(stderr
, "failed to set \"%s\" for %s\n",
347 fprintf(stderr
, "parse error: %s\n", line
);