]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-config.c
Make 'uri' optional for migrate QAPI
[mirror_qemu.git] / util / qemu-config.c
CommitLineData
aafd7584 1#include "qemu/osdep.h"
609f45ea 2#include "block/qdict.h" /* for qdict_extract_subqdict() */
e688df6b 3#include "qapi/error.h"
112ed241 4#include "qapi/qapi-commands-misc.h"
452fcdbc 5#include "qapi/qmp/qdict.h"
47e6b297 6#include "qapi/qmp/qlist.h"
1de7afc9
PB
7#include "qemu/error-report.h"
8#include "qemu/option.h"
9#include "qemu/config-file.h"
2f129fc1 10#include "hw/boards.h"
7282a033 11
1ceaefbd 12static QemuOptsList *vm_config_groups[48];
c5f3014b 13static QemuOptsList *drive_config_groups[5];
d058fe03 14
2ac20613
LC
15static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
16 Error **errp)
ddc97855
GH
17{
18 int i;
19
632a8873 20 qemu_load_module_for_opts(group);
ddc97855
GH
21 for (i = 0; lists[i] != NULL; i++) {
22 if (strcmp(lists[i]->name, group) == 0)
23 break;
24 }
25 if (lists[i] == NULL) {
f231b88d 26 error_setg(errp, "There is no option group '%s'", group);
ddc97855
GH
27 }
28 return lists[i];
29}
30
490b648e
KW
31QemuOptsList *qemu_find_opts(const char *group)
32{
2ac20613
LC
33 QemuOptsList *ret;
34 Error *local_err = NULL;
35
36 ret = find_list(vm_config_groups, group, &local_err);
84d18f06 37 if (local_err) {
565f65d2 38 error_report_err(local_err);
2ac20613
LC
39 }
40
41 return ret;
490b648e
KW
42}
43
e96e5ae8
PB
44QemuOpts *qemu_find_opts_singleton(const char *group)
45{
46 QemuOptsList *list;
47 QemuOpts *opts;
48
49 list = qemu_find_opts(group);
50 assert(list);
51 opts = qemu_opts_find(list, NULL);
52 if (!opts) {
53 opts = qemu_opts_create(list, NULL, 0, &error_abort);
54 }
55 return opts;
56}
57
1f8f987d
AK
58static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
59{
54aa3de7 60 CommandLineParameterInfoList *param_list = NULL;
1f8f987d
AK
61 CommandLineParameterInfo *info;
62 int i;
63
64 for (i = 0; desc[i].name != NULL; i++) {
65 info = g_malloc0(sizeof(*info));
66 info->name = g_strdup(desc[i].name);
67
68 switch (desc[i].type) {
69 case QEMU_OPT_STRING:
70 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
71 break;
72 case QEMU_OPT_BOOL:
73 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
74 break;
75 case QEMU_OPT_NUMBER:
76 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
77 break;
78 case QEMU_OPT_SIZE:
79 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
80 break;
81 }
82
9492718b
MA
83 info->help = g_strdup(desc[i].help);
84 info->q_default = g_strdup(desc[i].def_value_str);
1f8f987d 85
54aa3de7 86 QAPI_LIST_PREPEND(param_list, info);
1f8f987d
AK
87 }
88
89 return param_list;
90}
91
968854c8
AK
92/* remove repeated entry from the info list */
93static void cleanup_infolist(CommandLineParameterInfoList *head)
94{
95 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
96
97 cur = head;
98 while (cur->next) {
99 pre_entry = head;
100 while (pre_entry != cur->next) {
101 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
102 del_entry = cur->next;
103 cur->next = cur->next->next;
b11e20fb
MAL
104 del_entry->next = NULL;
105 qapi_free_CommandLineParameterInfoList(del_entry);
968854c8
AK
106 break;
107 }
108 pre_entry = pre_entry->next;
109 }
110 cur = cur->next;
111 }
112}
113
114/* merge the description items of two parameter infolists */
115static void connect_infolist(CommandLineParameterInfoList *head,
116 CommandLineParameterInfoList *new)
117{
118 CommandLineParameterInfoList *cur;
119
120 cur = head;
121 while (cur->next) {
122 cur = cur->next;
123 }
124 cur->next = new;
125}
126
127/* access all the local QemuOptsLists for drive option */
128static CommandLineParameterInfoList *get_drive_infolist(void)
129{
130 CommandLineParameterInfoList *head = NULL, *cur;
131 int i;
132
133 for (i = 0; drive_config_groups[i] != NULL; i++) {
134 if (!head) {
135 head = query_option_descs(drive_config_groups[i]->desc);
136 } else {
137 cur = query_option_descs(drive_config_groups[i]->desc);
138 connect_infolist(head, cur);
139 }
140 }
141 cleanup_infolist(head);
142
143 return head;
144}
145
2f129fc1
TH
146static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop)
147{
148 CommandLineParameterInfo *info;
149
150 info = g_malloc0(sizeof(*info));
151 info->name = g_strdup(prop->name);
152
153 if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) {
154 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
155 } else if (g_str_equal(prop->type, "int")) {
156 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
157 } else if (g_str_equal(prop->type, "size")) {
158 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
159 } else {
160 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
161 }
162
163 if (prop->description) {
164 info->help = g_strdup(prop->description);
165 }
166
167 return info;
168}
169
170static CommandLineParameterInfoList *query_all_machine_properties(void)
171{
172 CommandLineParameterInfoList *params = NULL, *clpiter;
173 CommandLineParameterInfo *info;
174 GSList *machines, *curr_mach;
175 ObjectPropertyIterator op_iter;
176 ObjectProperty *prop;
177 bool is_new;
178
179 machines = object_class_get_list(TYPE_MACHINE, false);
180 assert(machines);
181
182 /* Loop over all machine classes */
183 for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) {
184 object_class_property_iter_init(&op_iter, curr_mach->data);
185 /* ... and over the properties of each machine: */
186 while ((prop = object_property_iter_next(&op_iter))) {
187 if (!prop->set) {
188 continue;
189 }
190 /*
191 * Check whether the property has already been put into the list
192 * (via another machine class)
193 */
194 is_new = true;
195 for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) {
196 if (g_str_equal(clpiter->value->name, prop->name)) {
197 is_new = false;
198 break;
199 }
200 }
201 /* If it hasn't been added before, add it now to the list */
202 if (is_new) {
203 info = objprop_to_cmdline_prop(prop);
204 QAPI_LIST_PREPEND(params, info);
205 }
206 }
0a7cf217 207 }
2f129fc1
TH
208
209 g_slist_free(machines);
210
211 /* Add entry for the "type" parameter */
212 info = g_malloc0(sizeof(*info));
213 info->name = g_strdup("type");
214 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
215 info->help = g_strdup("machine type");
216 QAPI_LIST_PREPEND(params, info);
217
218 return params;
219}
0a7cf217 220
9492718b 221CommandLineOptionInfoList *qmp_query_command_line_options(const char *option,
1f8f987d
AK
222 Error **errp)
223{
54aa3de7 224 CommandLineOptionInfoList *conf_list = NULL;
1f8f987d
AK
225 CommandLineOptionInfo *info;
226 int i;
227
228 for (i = 0; vm_config_groups[i] != NULL; i++) {
9492718b 229 if (!option || !strcmp(option, vm_config_groups[i]->name)) {
1f8f987d
AK
230 info = g_malloc0(sizeof(*info));
231 info->option = g_strdup(vm_config_groups[i]->name);
968854c8
AK
232 if (!strcmp("drive", vm_config_groups[i]->name)) {
233 info->parameters = get_drive_infolist();
234 } else {
235 info->parameters =
236 query_option_descs(vm_config_groups[i]->desc);
237 }
54aa3de7 238 QAPI_LIST_PREPEND(conf_list, info);
1f8f987d
AK
239 }
240 }
241
9492718b 242 if (!option || !strcmp(option, "machine")) {
40e07370
SH
243 info = g_malloc0(sizeof(*info));
244 info->option = g_strdup("machine");
2f129fc1 245 info->parameters = query_all_machine_properties();
40e07370
SH
246 QAPI_LIST_PREPEND(conf_list, info);
247 }
248
1f8f987d
AK
249 if (conf_list == NULL) {
250 error_setg(errp, "invalid option name: %s", option);
251 }
252
253 return conf_list;
254}
255
60d5666f
LC
256QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
257{
258 return find_list(vm_config_groups, group, errp);
259}
260
968854c8
AK
261void qemu_add_drive_opts(QemuOptsList *list)
262{
263 int entries, i;
264
265 entries = ARRAY_SIZE(drive_config_groups);
266 entries--; /* keep list NULL terminated */
267 for (i = 0; i < entries; i++) {
268 if (drive_config_groups[i] == NULL) {
269 drive_config_groups[i] = list;
270 return;
271 }
272 }
273 fprintf(stderr, "ran out of space in drive_config_groups");
274 abort();
275}
276
dfe795e7
GH
277void qemu_add_opts(QemuOptsList *list)
278{
279 int entries, i;
280
281 entries = ARRAY_SIZE(vm_config_groups);
282 entries--; /* keep list NULL terminated */
283 for (i = 0; i < entries; i++) {
284 if (vm_config_groups[i] == NULL) {
285 vm_config_groups[i] = list;
286 return;
287 }
288 }
289 fprintf(stderr, "ran out of space in vm_config_groups");
290 abort();
291}
292
e5766d6e 293/* Returns number of config groups on success, -errno on error */
37701411
PB
294static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
295 const char *fname, Error **errp)
42262ba8 296{
0a3090b1 297 ERRP_GUARD();
37701411 298 char line[1024], prev_group[64], group[64], arg[64], value[1024];
cf5a65aa 299 Location loc;
37701411 300 QDict *qdict = NULL;
e5766d6e
EH
301 int res = -EINVAL, lno = 0;
302 int count = 0;
42262ba8 303
cf5a65aa 304 loc_push_none(&loc);
42262ba8 305 while (fgets(line, sizeof(line), fp) != NULL) {
37701411 306 ++lno;
42262ba8
GH
307 if (line[0] == '\n') {
308 /* skip empty lines */
309 continue;
310 }
311 if (line[0] == '#') {
312 /* comment */
313 continue;
314 }
37701411
PB
315 if (line[0] == '[') {
316 QDict *prev = qdict;
317 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
318 qdict = qdict_new();
319 qdict_put_str(qdict, "id", value);
320 count++;
321 } else if (sscanf(line, "[%63[^]]]", group) == 1) {
322 qdict = qdict_new();
323 count++;
2ac20613 324 }
37701411
PB
325 if (qdict != prev) {
326 if (prev) {
0a3090b1 327 cb(prev_group, prev, opaque, errp);
37701411 328 qobject_unref(prev);
0a3090b1 329 if (*errp) {
37701411
PB
330 goto out;
331 }
332 }
333 strcpy(prev_group, group);
334 continue;
2ac20613 335 }
42262ba8 336 }
37701411 337 loc_set_file(fname, lno);
d9f7e29e
EH
338 value[0] = '\0';
339 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
340 sscanf(line, " %63s = \"\"", arg) == 1) {
42262ba8 341 /* arg = value */
37701411 342 if (qdict == NULL) {
f7544edc 343 error_setg(errp, "no group defined");
cf5a65aa 344 goto out;
42262ba8 345 }
37701411 346 qdict_put_str(qdict, arg, value);
42262ba8
GH
347 continue;
348 }
f7544edc 349 error_setg(errp, "parse error");
cf5a65aa 350 goto out;
42262ba8 351 }
ef82516d 352 if (ferror(fp)) {
f7544edc
PB
353 loc_pop(&loc);
354 error_setg_errno(errp, errno, "Cannot read config file");
461fea9b 355 goto out_no_loc;
ef82516d 356 }
e5766d6e 357 res = count;
37701411
PB
358 if (qdict) {
359 cb(group, qdict, opaque, errp);
37701411 360 }
e72f9524 361out:
cf5a65aa 362 loc_pop(&loc);
461fea9b 363out_no_loc:
e72f9524 364 qobject_unref(qdict);
cf5a65aa 365 return res;
42262ba8 366}
dcfb0939 367
37701411
PB
368void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
369{
370 QemuOptsList **lists = opaque;
37701411 371 QemuOptsList *list;
37701411
PB
372
373 list = find_list(lists, group, errp);
374 if (!list) {
375 return;
376 }
377
e7d85d95 378 qemu_opts_from_qdict(list, qdict, errp);
37701411
PB
379}
380
381int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
382{
383 return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
384}
385
386int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
dcfb0939
KW
387{
388 FILE *f = fopen(filename, "r");
019e78ba
KW
389 int ret;
390
dcfb0939 391 if (f == NULL) {
f7544edc 392 error_setg_file_open(errp, errno, filename);
dcfb0939
KW
393 return -errno;
394 }
395
37701411 396 ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
dcfb0939 397 fclose(f);
e5766d6e 398 return ret;
dcfb0939 399}
adf5c449 400
f766e6dc 401static bool config_parse_qdict_section(QDict *options, QemuOptsList *opts,
adf5c449
HR
402 Error **errp)
403{
404 QemuOpts *subopts;
f766e6dc
MA
405 g_autoptr(QDict) subqdict = NULL;
406 g_autoptr(QList) list = NULL;
adf5c449
HR
407 size_t orig_size, enum_size;
408 char *prefix;
409
410 prefix = g_strdup_printf("%s.", opts->name);
411 qdict_extract_subqdict(options, &subqdict, prefix);
412 g_free(prefix);
413 orig_size = qdict_size(subqdict);
414 if (!orig_size) {
f766e6dc 415 return true;
adf5c449
HR
416 }
417
c6ecec43
MA
418 subopts = qemu_opts_create(opts, NULL, 0, errp);
419 if (!subopts) {
f766e6dc 420 return false;
adf5c449
HR
421 }
422
668f62ec 423 if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
f766e6dc 424 return false;
adf5c449
HR
425 }
426
427 enum_size = qdict_size(subqdict);
428 if (enum_size < orig_size && enum_size) {
429 error_setg(errp, "Unknown option '%s' for [%s]",
430 qdict_first(subqdict)->key, opts->name);
f766e6dc 431 return false;
adf5c449
HR
432 }
433
434 if (enum_size) {
435 /* Multiple, enumerated sections */
436 QListEntry *list_entry;
437 unsigned i = 0;
438
439 /* Not required anymore */
440 qemu_opts_del(subopts);
441
442 qdict_array_split(subqdict, &list);
443 if (qdict_size(subqdict)) {
444 error_setg(errp, "Unused option '%s' for [%s]",
445 qdict_first(subqdict)->key, opts->name);
f766e6dc 446 return false;
adf5c449
HR
447 }
448
449 QLIST_FOREACH_ENTRY(list, list_entry) {
7dc847eb 450 QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
adf5c449
HR
451 char *opt_name;
452
ae39c4b2
HR
453 if (!section) {
454 error_setg(errp, "[%s] section (index %u) does not consist of "
455 "keys", opts->name, i);
f766e6dc 456 return false;
ae39c4b2
HR
457 }
458
adf5c449 459 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
c6ecec43 460 subopts = qemu_opts_create(opts, opt_name, 1, errp);
adf5c449 461 g_free(opt_name);
c6ecec43 462 if (!subopts) {
f766e6dc 463 return false;
adf5c449
HR
464 }
465
668f62ec 466 if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
adf5c449 467 qemu_opts_del(subopts);
f766e6dc 468 return false;
adf5c449
HR
469 }
470
471 if (qdict_size(section)) {
472 error_setg(errp, "[%s] section doesn't support the option '%s'",
473 opts->name, qdict_first(section)->key);
474 qemu_opts_del(subopts);
f766e6dc 475 return false;
adf5c449
HR
476 }
477 }
478 }
479
f766e6dc 480 return true;
adf5c449
HR
481}
482
f766e6dc 483bool qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
adf5c449
HR
484 Error **errp)
485{
486 int i;
adf5c449
HR
487
488 for (i = 0; lists[i]; i++) {
f766e6dc
MA
489 if (!config_parse_qdict_section(options, lists[i], errp)) {
490 return false;
adf5c449
HR
491 }
492 }
f766e6dc
MA
493
494 return true;
adf5c449 495}