1 #include "qemu/osdep.h"
2 #include "block/qdict.h" /* for qdict_extract_subqdict() */
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-misc.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qlist.h"
7 #include "qemu-common.h"
8 #include "qemu/error-report.h"
9 #include "qemu/option.h"
10 #include "qemu/config-file.h"
12 static QemuOptsList
*vm_config_groups
[48];
13 static QemuOptsList
*drive_config_groups
[5];
15 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
20 for (i
= 0; lists
[i
] != NULL
; i
++) {
21 if (strcmp(lists
[i
]->name
, group
) == 0)
24 if (lists
[i
] == NULL
) {
25 error_setg(errp
, "There is no option group '%s'", group
);
30 QemuOptsList
*qemu_find_opts(const char *group
)
33 Error
*local_err
= NULL
;
35 ret
= find_list(vm_config_groups
, group
, &local_err
);
37 error_report_err(local_err
);
43 QemuOpts
*qemu_find_opts_singleton(const char *group
)
48 list
= qemu_find_opts(group
);
50 opts
= qemu_opts_find(list
, NULL
);
52 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
57 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
59 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
60 CommandLineParameterInfo
*info
;
63 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
64 info
= g_malloc0(sizeof(*info
));
65 info
->name
= g_strdup(desc
[i
].name
);
67 switch (desc
[i
].type
) {
69 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
72 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
75 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
78 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
83 info
->has_help
= true;
84 info
->help
= g_strdup(desc
[i
].help
);
86 if (desc
[i
].def_value_str
) {
87 info
->has_q_default
= true;
88 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
91 entry
= g_malloc0(sizeof(*entry
));
93 entry
->next
= param_list
;
100 /* remove repeated entry from the info list */
101 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
103 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
108 while (pre_entry
!= cur
->next
) {
109 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
110 del_entry
= cur
->next
;
111 cur
->next
= cur
->next
->next
;
112 del_entry
->next
= NULL
;
113 qapi_free_CommandLineParameterInfoList(del_entry
);
116 pre_entry
= pre_entry
->next
;
122 /* merge the description items of two parameter infolists */
123 static void connect_infolist(CommandLineParameterInfoList
*head
,
124 CommandLineParameterInfoList
*new)
126 CommandLineParameterInfoList
*cur
;
135 /* access all the local QemuOptsLists for drive option */
136 static CommandLineParameterInfoList
*get_drive_infolist(void)
138 CommandLineParameterInfoList
*head
= NULL
, *cur
;
141 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
143 head
= query_option_descs(drive_config_groups
[i
]->desc
);
145 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
146 connect_infolist(head
, cur
);
149 cleanup_infolist(head
);
154 /* restore machine options that are now machine's properties */
155 static QemuOptsList machine_opts
= {
157 .head
= QTAILQ_HEAD_INITIALIZER(machine_opts
.head
),
161 .type
= QEMU_OPT_STRING
,
162 .help
= "emulated machine"
165 .type
= QEMU_OPT_STRING
,
166 .help
= "accelerator list",
168 .name
= "kernel_irqchip",
169 .type
= QEMU_OPT_BOOL
,
170 .help
= "use KVM in-kernel irqchip",
172 .name
= "kvm_shadow_mem",
173 .type
= QEMU_OPT_SIZE
,
174 .help
= "KVM shadow MMU size",
177 .type
= QEMU_OPT_STRING
,
178 .help
= "Linux kernel image file",
181 .type
= QEMU_OPT_STRING
,
182 .help
= "Linux initial ramdisk file",
185 .type
= QEMU_OPT_STRING
,
186 .help
= "Linux kernel command line",
189 .type
= QEMU_OPT_STRING
,
190 .help
= "Linux kernel device tree file",
193 .type
= QEMU_OPT_STRING
,
194 .help
= "Dump current dtb to a file and quit",
196 .name
= "phandle_start",
197 .type
= QEMU_OPT_NUMBER
,
198 .help
= "The first phandle ID we may generate dynamically",
200 .name
= "dt_compatible",
201 .type
= QEMU_OPT_STRING
,
202 .help
= "Overrides the \"compatible\" property of the dt root node",
204 .name
= "dump-guest-core",
205 .type
= QEMU_OPT_BOOL
,
206 .help
= "Include guest memory in a core dump",
209 .type
= QEMU_OPT_BOOL
,
210 .help
= "enable/disable memory merge support",
213 .type
= QEMU_OPT_BOOL
,
214 .help
= "Set on/off to enable/disable usb",
217 .type
= QEMU_OPT_STRING
,
218 .help
= "firmware image",
221 .type
= QEMU_OPT_BOOL
,
222 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
224 .name
= "suppress-vmdesc",
225 .type
= QEMU_OPT_BOOL
,
226 .help
= "Set on to disable self-describing migration",
228 .name
= "aes-key-wrap",
229 .type
= QEMU_OPT_BOOL
,
230 .help
= "enable/disable AES key wrapping using the CPACF wrapping key",
232 .name
= "dea-key-wrap",
233 .type
= QEMU_OPT_BOOL
,
234 .help
= "enable/disable DEA key wrapping using the CPACF wrapping key",
237 .type
= QEMU_OPT_STRING
,
238 .help
= "Up to 8 chars in set of [A-Za-z0-9. ](lower case chars"
239 " converted to upper case) to pass to machine"
240 " loader, boot manager, and guest kernel",
242 { /* End of list */ }
246 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
250 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
251 CommandLineOptionInfo
*info
;
254 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
255 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
256 info
= g_malloc0(sizeof(*info
));
257 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
258 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
259 info
->parameters
= get_drive_infolist();
260 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
261 info
->parameters
= query_option_descs(machine_opts
.desc
);
264 query_option_descs(vm_config_groups
[i
]->desc
);
266 entry
= g_malloc0(sizeof(*entry
));
268 entry
->next
= conf_list
;
273 if (conf_list
== NULL
) {
274 error_setg(errp
, "invalid option name: %s", option
);
280 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
282 return find_list(vm_config_groups
, group
, errp
);
285 void qemu_add_drive_opts(QemuOptsList
*list
)
289 entries
= ARRAY_SIZE(drive_config_groups
);
290 entries
--; /* keep list NULL terminated */
291 for (i
= 0; i
< entries
; i
++) {
292 if (drive_config_groups
[i
] == NULL
) {
293 drive_config_groups
[i
] = list
;
297 fprintf(stderr
, "ran out of space in drive_config_groups");
301 void qemu_add_opts(QemuOptsList
*list
)
305 entries
= ARRAY_SIZE(vm_config_groups
);
306 entries
--; /* keep list NULL terminated */
307 for (i
= 0; i
< entries
; i
++) {
308 if (vm_config_groups
[i
] == NULL
) {
309 vm_config_groups
[i
] = list
;
313 fprintf(stderr
, "ran out of space in vm_config_groups");
317 int qemu_set_option(const char *str
)
319 Error
*local_err
= NULL
;
320 char group
[64], id
[64], arg
[64];
325 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
326 if (rc
< 3 || str
[offset
] != '=') {
327 error_report("can't parse: \"%s\"", str
);
331 list
= qemu_find_opts(group
);
336 opts
= qemu_opts_find(list
, id
);
338 error_report("there is no %s \"%s\" defined",
343 qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
);
345 error_report_err(local_err
);
351 struct ConfigWriteData
{
356 static int config_write_opt(void *opaque
, const char *name
, const char *value
,
359 struct ConfigWriteData
*data
= opaque
;
361 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
365 static int config_write_opts(void *opaque
, QemuOpts
*opts
, Error
**errp
)
367 struct ConfigWriteData
*data
= opaque
;
368 const char *id
= qemu_opts_id(opts
);
371 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
373 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
375 qemu_opt_foreach(opts
, config_write_opt
, data
, NULL
);
376 fprintf(data
->fp
, "\n");
380 void qemu_config_write(FILE *fp
)
382 struct ConfigWriteData data
= { .fp
= fp
};
383 QemuOptsList
**lists
= vm_config_groups
;
386 fprintf(fp
, "# qemu config file\n\n");
387 for (i
= 0; lists
[i
] != NULL
; i
++) {
388 data
.list
= lists
[i
];
389 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, NULL
);
393 /* Returns number of config groups on success, -errno on error */
394 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
396 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
398 QemuOptsList
*list
= NULL
;
399 Error
*local_err
= NULL
;
400 QemuOpts
*opts
= NULL
;
401 int res
= -EINVAL
, lno
= 0;
405 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
406 loc_set_file(fname
, ++lno
);
407 if (line
[0] == '\n') {
408 /* skip empty lines */
411 if (line
[0] == '#') {
415 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
417 list
= find_list(lists
, group
, &local_err
);
419 error_report_err(local_err
);
422 opts
= qemu_opts_create(list
, id
, 1, NULL
);
426 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
427 /* group without id */
428 list
= find_list(lists
, group
, &local_err
);
430 error_report_err(local_err
);
433 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
438 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
439 sscanf(line
, " %63s = \"\"", arg
) == 1) {
442 error_report("no group defined");
445 qemu_opt_set(opts
, arg
, value
, &local_err
);
447 error_report_err(local_err
);
452 error_report("parse error");
456 error_report("error reading file");
465 int qemu_read_config_file(const char *filename
)
467 FILE *f
= fopen(filename
, "r");
474 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
479 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
485 Error
*local_err
= NULL
;
486 size_t orig_size
, enum_size
;
489 prefix
= g_strdup_printf("%s.", opts
->name
);
490 qdict_extract_subqdict(options
, &subqdict
, prefix
);
492 orig_size
= qdict_size(subqdict
);
497 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
499 error_propagate(errp
, local_err
);
503 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
505 error_propagate(errp
, local_err
);
509 enum_size
= qdict_size(subqdict
);
510 if (enum_size
< orig_size
&& enum_size
) {
511 error_setg(errp
, "Unknown option '%s' for [%s]",
512 qdict_first(subqdict
)->key
, opts
->name
);
517 /* Multiple, enumerated sections */
518 QListEntry
*list_entry
;
521 /* Not required anymore */
522 qemu_opts_del(subopts
);
524 qdict_array_split(subqdict
, &list
);
525 if (qdict_size(subqdict
)) {
526 error_setg(errp
, "Unused option '%s' for [%s]",
527 qdict_first(subqdict
)->key
, opts
->name
);
531 QLIST_FOREACH_ENTRY(list
, list_entry
) {
532 QDict
*section
= qobject_to(QDict
, qlist_entry_obj(list_entry
));
536 error_setg(errp
, "[%s] section (index %u) does not consist of "
537 "keys", opts
->name
, i
);
541 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
542 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
545 error_propagate(errp
, local_err
);
549 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
551 error_propagate(errp
, local_err
);
552 qemu_opts_del(subopts
);
556 if (qdict_size(section
)) {
557 error_setg(errp
, "[%s] section doesn't support the option '%s'",
558 opts
->name
, qdict_first(section
)->key
);
559 qemu_opts_del(subopts
);
566 qobject_unref(subqdict
);
570 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
574 Error
*local_err
= NULL
;
576 for (i
= 0; lists
[i
]; i
++) {
577 config_parse_qdict_section(options
, lists
[i
], &local_err
);
579 error_propagate(errp
, local_err
);