1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qapi/qapi-commands-misc.h"
4 #include "qapi/qmp/qdict.h"
5 #include "qapi/qmp/qlist.h"
6 #include "qemu-common.h"
7 #include "qemu/error-report.h"
8 #include "qemu/option.h"
9 #include "qemu/config-file.h"
11 static QemuOptsList
*vm_config_groups
[48];
12 static QemuOptsList
*drive_config_groups
[5];
14 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
19 for (i
= 0; lists
[i
] != NULL
; i
++) {
20 if (strcmp(lists
[i
]->name
, group
) == 0)
23 if (lists
[i
] == NULL
) {
24 error_setg(errp
, "There is no option group '%s'", group
);
29 QemuOptsList
*qemu_find_opts(const char *group
)
32 Error
*local_err
= NULL
;
34 ret
= find_list(vm_config_groups
, group
, &local_err
);
36 error_report_err(local_err
);
42 QemuOpts
*qemu_find_opts_singleton(const char *group
)
47 list
= qemu_find_opts(group
);
49 opts
= qemu_opts_find(list
, NULL
);
51 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
56 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
58 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
59 CommandLineParameterInfo
*info
;
62 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
63 info
= g_malloc0(sizeof(*info
));
64 info
->name
= g_strdup(desc
[i
].name
);
66 switch (desc
[i
].type
) {
68 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
71 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
74 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
77 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
82 info
->has_help
= true;
83 info
->help
= g_strdup(desc
[i
].help
);
85 if (desc
[i
].def_value_str
) {
86 info
->has_q_default
= true;
87 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
90 entry
= g_malloc0(sizeof(*entry
));
92 entry
->next
= param_list
;
99 /* remove repeated entry from the info list */
100 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
102 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
107 while (pre_entry
!= cur
->next
) {
108 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
109 del_entry
= cur
->next
;
110 cur
->next
= cur
->next
->next
;
111 del_entry
->next
= NULL
;
112 qapi_free_CommandLineParameterInfoList(del_entry
);
115 pre_entry
= pre_entry
->next
;
121 /* merge the description items of two parameter infolists */
122 static void connect_infolist(CommandLineParameterInfoList
*head
,
123 CommandLineParameterInfoList
*new)
125 CommandLineParameterInfoList
*cur
;
134 /* access all the local QemuOptsLists for drive option */
135 static CommandLineParameterInfoList
*get_drive_infolist(void)
137 CommandLineParameterInfoList
*head
= NULL
, *cur
;
140 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
142 head
= query_option_descs(drive_config_groups
[i
]->desc
);
144 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
145 connect_infolist(head
, cur
);
148 cleanup_infolist(head
);
153 /* restore machine options that are now machine's properties */
154 static QemuOptsList machine_opts
= {
156 .head
= QTAILQ_HEAD_INITIALIZER(machine_opts
.head
),
160 .type
= QEMU_OPT_STRING
,
161 .help
= "emulated machine"
164 .type
= QEMU_OPT_STRING
,
165 .help
= "accelerator list",
167 .name
= "kernel_irqchip",
168 .type
= QEMU_OPT_BOOL
,
169 .help
= "use KVM in-kernel irqchip",
171 .name
= "kvm_shadow_mem",
172 .type
= QEMU_OPT_SIZE
,
173 .help
= "KVM shadow MMU size",
176 .type
= QEMU_OPT_STRING
,
177 .help
= "Linux kernel image file",
180 .type
= QEMU_OPT_STRING
,
181 .help
= "Linux initial ramdisk file",
184 .type
= QEMU_OPT_STRING
,
185 .help
= "Linux kernel command line",
188 .type
= QEMU_OPT_STRING
,
189 .help
= "Linux kernel device tree file",
192 .type
= QEMU_OPT_STRING
,
193 .help
= "Dump current dtb to a file and quit",
195 .name
= "phandle_start",
196 .type
= QEMU_OPT_NUMBER
,
197 .help
= "The first phandle ID we may generate dynamically",
199 .name
= "dt_compatible",
200 .type
= QEMU_OPT_STRING
,
201 .help
= "Overrides the \"compatible\" property of the dt root node",
203 .name
= "dump-guest-core",
204 .type
= QEMU_OPT_BOOL
,
205 .help
= "Include guest memory in a core dump",
208 .type
= QEMU_OPT_BOOL
,
209 .help
= "enable/disable memory merge support",
212 .type
= QEMU_OPT_BOOL
,
213 .help
= "Set on/off to enable/disable usb",
216 .type
= QEMU_OPT_STRING
,
217 .help
= "firmware image",
220 .type
= QEMU_OPT_BOOL
,
221 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
223 .name
= "suppress-vmdesc",
224 .type
= QEMU_OPT_BOOL
,
225 .help
= "Set on to disable self-describing migration",
227 .name
= "aes-key-wrap",
228 .type
= QEMU_OPT_BOOL
,
229 .help
= "enable/disable AES key wrapping using the CPACF wrapping key",
231 .name
= "dea-key-wrap",
232 .type
= QEMU_OPT_BOOL
,
233 .help
= "enable/disable DEA key wrapping using the CPACF wrapping key",
236 .type
= QEMU_OPT_STRING
,
237 .help
= "Up to 8 chars in set of [A-Za-z0-9. ](lower case chars"
238 " converted to upper case) to pass to machine"
239 " loader, boot manager, and guest kernel",
241 { /* End of list */ }
245 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
249 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
250 CommandLineOptionInfo
*info
;
253 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
254 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
255 info
= g_malloc0(sizeof(*info
));
256 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
257 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
258 info
->parameters
= get_drive_infolist();
259 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
260 info
->parameters
= query_option_descs(machine_opts
.desc
);
263 query_option_descs(vm_config_groups
[i
]->desc
);
265 entry
= g_malloc0(sizeof(*entry
));
267 entry
->next
= conf_list
;
272 if (conf_list
== NULL
) {
273 error_setg(errp
, "invalid option name: %s", option
);
279 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
281 return find_list(vm_config_groups
, group
, errp
);
284 void qemu_add_drive_opts(QemuOptsList
*list
)
288 entries
= ARRAY_SIZE(drive_config_groups
);
289 entries
--; /* keep list NULL terminated */
290 for (i
= 0; i
< entries
; i
++) {
291 if (drive_config_groups
[i
] == NULL
) {
292 drive_config_groups
[i
] = list
;
296 fprintf(stderr
, "ran out of space in drive_config_groups");
300 void qemu_add_opts(QemuOptsList
*list
)
304 entries
= ARRAY_SIZE(vm_config_groups
);
305 entries
--; /* keep list NULL terminated */
306 for (i
= 0; i
< entries
; i
++) {
307 if (vm_config_groups
[i
] == NULL
) {
308 vm_config_groups
[i
] = list
;
312 fprintf(stderr
, "ran out of space in vm_config_groups");
316 int qemu_set_option(const char *str
)
318 Error
*local_err
= NULL
;
319 char group
[64], id
[64], arg
[64];
324 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
325 if (rc
< 3 || str
[offset
] != '=') {
326 error_report("can't parse: \"%s\"", str
);
330 list
= qemu_find_opts(group
);
335 opts
= qemu_opts_find(list
, id
);
337 error_report("there is no %s \"%s\" defined",
342 qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
);
344 error_report_err(local_err
);
350 struct ConfigWriteData
{
355 static int config_write_opt(void *opaque
, const char *name
, const char *value
,
358 struct ConfigWriteData
*data
= opaque
;
360 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
364 static int config_write_opts(void *opaque
, QemuOpts
*opts
, Error
**errp
)
366 struct ConfigWriteData
*data
= opaque
;
367 const char *id
= qemu_opts_id(opts
);
370 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
372 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
374 qemu_opt_foreach(opts
, config_write_opt
, data
, NULL
);
375 fprintf(data
->fp
, "\n");
379 void qemu_config_write(FILE *fp
)
381 struct ConfigWriteData data
= { .fp
= fp
};
382 QemuOptsList
**lists
= vm_config_groups
;
385 fprintf(fp
, "# qemu config file\n\n");
386 for (i
= 0; lists
[i
] != NULL
; i
++) {
387 data
.list
= lists
[i
];
388 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, NULL
);
392 /* Returns number of config groups on success, -errno on error */
393 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
395 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
397 QemuOptsList
*list
= NULL
;
398 Error
*local_err
= NULL
;
399 QemuOpts
*opts
= NULL
;
400 int res
= -EINVAL
, lno
= 0;
404 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
405 loc_set_file(fname
, ++lno
);
406 if (line
[0] == '\n') {
407 /* skip empty lines */
410 if (line
[0] == '#') {
414 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
416 list
= find_list(lists
, group
, &local_err
);
418 error_report_err(local_err
);
421 opts
= qemu_opts_create(list
, id
, 1, NULL
);
425 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
426 /* group without id */
427 list
= find_list(lists
, group
, &local_err
);
429 error_report_err(local_err
);
432 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
437 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
438 sscanf(line
, " %63s = \"\"", arg
) == 1) {
441 error_report("no group defined");
444 qemu_opt_set(opts
, arg
, value
, &local_err
);
446 error_report_err(local_err
);
451 error_report("parse error");
455 error_report("error reading file");
464 int qemu_read_config_file(const char *filename
)
466 FILE *f
= fopen(filename
, "r");
473 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
478 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
484 Error
*local_err
= NULL
;
485 size_t orig_size
, enum_size
;
488 prefix
= g_strdup_printf("%s.", opts
->name
);
489 qdict_extract_subqdict(options
, &subqdict
, prefix
);
491 orig_size
= qdict_size(subqdict
);
496 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
498 error_propagate(errp
, local_err
);
502 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
504 error_propagate(errp
, local_err
);
508 enum_size
= qdict_size(subqdict
);
509 if (enum_size
< orig_size
&& enum_size
) {
510 error_setg(errp
, "Unknown option '%s' for [%s]",
511 qdict_first(subqdict
)->key
, opts
->name
);
516 /* Multiple, enumerated sections */
517 QListEntry
*list_entry
;
520 /* Not required anymore */
521 qemu_opts_del(subopts
);
523 qdict_array_split(subqdict
, &list
);
524 if (qdict_size(subqdict
)) {
525 error_setg(errp
, "Unused option '%s' for [%s]",
526 qdict_first(subqdict
)->key
, opts
->name
);
530 QLIST_FOREACH_ENTRY(list
, list_entry
) {
531 QDict
*section
= qobject_to(QDict
, qlist_entry_obj(list_entry
));
535 error_setg(errp
, "[%s] section (index %u) does not consist of "
536 "keys", opts
->name
, i
);
540 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
541 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
544 error_propagate(errp
, local_err
);
548 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
550 error_propagate(errp
, local_err
);
551 qemu_opts_del(subopts
);
555 if (qdict_size(section
)) {
556 error_setg(errp
, "[%s] section doesn't support the option '%s'",
557 opts
->name
, qdict_first(section
)->key
);
558 qemu_opts_del(subopts
);
565 qobject_unref(subqdict
);
569 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
573 Error
*local_err
= NULL
;
575 for (i
= 0; lists
[i
]; i
++) {
576 config_parse_qdict_section(options
, lists
[i
], &local_err
);
578 error_propagate(errp
, local_err
);