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/qerror.h"
6 #include "qapi/qmp/qdict.h"
7 #include "qapi/qmp/qlist.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 qemu_load_module_for_opts(group
);
21 for (i
= 0; lists
[i
] != NULL
; i
++) {
22 if (strcmp(lists
[i
]->name
, group
) == 0)
25 if (lists
[i
] == NULL
) {
26 error_setg(errp
, "There is no option group '%s'", group
);
31 QemuOptsList
*qemu_find_opts(const char *group
)
34 Error
*local_err
= NULL
;
36 ret
= find_list(vm_config_groups
, group
, &local_err
);
38 error_report_err(local_err
);
44 QemuOpts
*qemu_find_opts_singleton(const char *group
)
49 list
= qemu_find_opts(group
);
51 opts
= qemu_opts_find(list
, NULL
);
53 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
58 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
60 CommandLineParameterInfoList
*param_list
= NULL
;
61 CommandLineParameterInfo
*info
;
64 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
65 info
= g_malloc0(sizeof(*info
));
66 info
->name
= g_strdup(desc
[i
].name
);
68 switch (desc
[i
].type
) {
70 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
73 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
76 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
79 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
84 info
->has_help
= true;
85 info
->help
= g_strdup(desc
[i
].help
);
87 if (desc
[i
].def_value_str
) {
88 info
->has_q_default
= true;
89 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
92 QAPI_LIST_PREPEND(param_list
, info
);
98 /* remove repeated entry from the info list */
99 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
101 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
106 while (pre_entry
!= cur
->next
) {
107 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
108 del_entry
= cur
->next
;
109 cur
->next
= cur
->next
->next
;
110 del_entry
->next
= NULL
;
111 qapi_free_CommandLineParameterInfoList(del_entry
);
114 pre_entry
= pre_entry
->next
;
120 /* merge the description items of two parameter infolists */
121 static void connect_infolist(CommandLineParameterInfoList
*head
,
122 CommandLineParameterInfoList
*new)
124 CommandLineParameterInfoList
*cur
;
133 /* access all the local QemuOptsLists for drive option */
134 static CommandLineParameterInfoList
*get_drive_infolist(void)
136 CommandLineParameterInfoList
*head
= NULL
, *cur
;
139 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
141 head
= query_option_descs(drive_config_groups
[i
]->desc
);
143 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
144 connect_infolist(head
, cur
);
147 cleanup_infolist(head
);
152 /* restore machine options that are now machine's properties */
153 static QemuOptsList machine_opts
= {
155 .head
= QTAILQ_HEAD_INITIALIZER(machine_opts
.head
),
159 .type
= QEMU_OPT_STRING
,
160 .help
= "emulated machine"
163 .type
= QEMU_OPT_STRING
,
164 .help
= "accelerator list",
166 .name
= "kernel_irqchip",
167 .type
= QEMU_OPT_BOOL
,
168 .help
= "use KVM in-kernel irqchip",
170 .name
= "kvm_shadow_mem",
171 .type
= QEMU_OPT_SIZE
,
172 .help
= "KVM shadow MMU size",
175 .type
= QEMU_OPT_STRING
,
176 .help
= "Linux kernel image file",
179 .type
= QEMU_OPT_STRING
,
180 .help
= "Linux initial ramdisk file",
183 .type
= QEMU_OPT_STRING
,
184 .help
= "Linux kernel command line",
187 .type
= QEMU_OPT_STRING
,
188 .help
= "Linux kernel device tree file",
191 .type
= QEMU_OPT_STRING
,
192 .help
= "Dump current dtb to a file and quit",
194 .name
= "phandle_start",
195 .type
= QEMU_OPT_NUMBER
,
196 .help
= "The first phandle ID we may generate dynamically",
198 .name
= "dt_compatible",
199 .type
= QEMU_OPT_STRING
,
200 .help
= "Overrides the \"compatible\" property of the dt root node",
202 .name
= "dump-guest-core",
203 .type
= QEMU_OPT_BOOL
,
204 .help
= "Include guest memory in a core dump",
207 .type
= QEMU_OPT_BOOL
,
208 .help
= "enable/disable memory merge support",
211 .type
= QEMU_OPT_BOOL
,
212 .help
= "Set on/off to enable/disable usb",
215 .type
= QEMU_OPT_STRING
,
216 .help
= "firmware image",
219 .type
= QEMU_OPT_BOOL
,
220 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
222 .name
= "suppress-vmdesc",
223 .type
= QEMU_OPT_BOOL
,
224 .help
= "Set on to disable self-describing migration",
226 .name
= "aes-key-wrap",
227 .type
= QEMU_OPT_BOOL
,
228 .help
= "enable/disable AES key wrapping using the CPACF wrapping key",
230 .name
= "dea-key-wrap",
231 .type
= QEMU_OPT_BOOL
,
232 .help
= "enable/disable DEA key wrapping using the CPACF wrapping key",
235 .type
= QEMU_OPT_STRING
,
236 .help
= "Up to 8 chars in set of [A-Za-z0-9. ](lower case chars"
237 " converted to upper case) to pass to machine"
238 " loader, boot manager, and guest kernel",
240 { /* End of list */ }
244 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
248 CommandLineOptionInfoList
*conf_list
= NULL
;
249 CommandLineOptionInfo
*info
;
252 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
253 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
254 info
= g_malloc0(sizeof(*info
));
255 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
256 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
257 info
->parameters
= get_drive_infolist();
258 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
259 info
->parameters
= query_option_descs(machine_opts
.desc
);
262 query_option_descs(vm_config_groups
[i
]->desc
);
264 QAPI_LIST_PREPEND(conf_list
, info
);
268 if (conf_list
== NULL
) {
269 error_setg(errp
, "invalid option name: %s", option
);
275 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
277 return find_list(vm_config_groups
, group
, errp
);
280 void qemu_add_drive_opts(QemuOptsList
*list
)
284 entries
= ARRAY_SIZE(drive_config_groups
);
285 entries
--; /* keep list NULL terminated */
286 for (i
= 0; i
< entries
; i
++) {
287 if (drive_config_groups
[i
] == NULL
) {
288 drive_config_groups
[i
] = list
;
292 fprintf(stderr
, "ran out of space in drive_config_groups");
296 void qemu_add_opts(QemuOptsList
*list
)
300 entries
= ARRAY_SIZE(vm_config_groups
);
301 entries
--; /* keep list NULL terminated */
302 for (i
= 0; i
< entries
; i
++) {
303 if (vm_config_groups
[i
] == NULL
) {
304 vm_config_groups
[i
] = list
;
308 fprintf(stderr
, "ran out of space in vm_config_groups");
312 struct ConfigWriteData
{
317 static int config_write_opt(void *opaque
, const char *name
, const char *value
,
320 struct ConfigWriteData
*data
= opaque
;
322 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
326 static int config_write_opts(void *opaque
, QemuOpts
*opts
, Error
**errp
)
328 struct ConfigWriteData
*data
= opaque
;
329 const char *id
= qemu_opts_id(opts
);
332 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
334 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
336 qemu_opt_foreach(opts
, config_write_opt
, data
, NULL
);
337 fprintf(data
->fp
, "\n");
341 void qemu_config_write(FILE *fp
)
343 struct ConfigWriteData data
= { .fp
= fp
};
344 QemuOptsList
**lists
= vm_config_groups
;
347 fprintf(fp
, "# qemu config file\n\n");
348 for (i
= 0; lists
[i
] != NULL
; i
++) {
349 data
.list
= lists
[i
];
350 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, NULL
);
354 /* Returns number of config groups on success, -errno on error */
355 static int qemu_config_foreach(FILE *fp
, QEMUConfigCB
*cb
, void *opaque
,
356 const char *fname
, Error
**errp
)
358 char line
[1024], prev_group
[64], group
[64], arg
[64], value
[1024];
360 Error
*local_err
= NULL
;
362 int res
= -EINVAL
, lno
= 0;
366 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
368 if (line
[0] == '\n') {
369 /* skip empty lines */
372 if (line
[0] == '#') {
376 if (line
[0] == '[') {
378 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, value
) == 2) {
380 qdict_put_str(qdict
, "id", value
);
382 } else if (sscanf(line
, "[%63[^]]]", group
) == 1) {
388 cb(prev_group
, prev
, opaque
, &local_err
);
391 error_propagate(errp
, local_err
);
395 strcpy(prev_group
, group
);
399 loc_set_file(fname
, lno
);
401 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
402 sscanf(line
, " %63s = \"\"", arg
) == 1) {
405 error_setg(errp
, "no group defined");
408 qdict_put_str(qdict
, arg
, value
);
411 error_setg(errp
, "parse error");
416 error_setg_errno(errp
, errno
, "Cannot read config file");
422 cb(group
, qdict
, opaque
, errp
);
423 qobject_unref(qdict
);
429 void qemu_config_do_parse(const char *group
, QDict
*qdict
, void *opaque
, Error
**errp
)
431 QemuOptsList
**lists
= opaque
;
432 const char *id
= qdict_get_try_str(qdict
, "id");
435 const QDictEntry
*unrecognized
;
437 list
= find_list(lists
, group
, errp
);
442 opts
= qemu_opts_create(list
, id
, 1, errp
);
446 if (!qemu_opts_absorb_qdict(opts
, qdict
, errp
)) {
450 unrecognized
= qdict_first(qdict
);
452 error_setg(errp
, QERR_INVALID_PARAMETER
, unrecognized
->key
);
457 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
, Error
**errp
)
459 return qemu_config_foreach(fp
, qemu_config_do_parse
, lists
, fname
, errp
);
462 int qemu_read_config_file(const char *filename
, QEMUConfigCB
*cb
, Error
**errp
)
464 FILE *f
= fopen(filename
, "r");
468 error_setg_file_open(errp
, errno
, filename
);
472 ret
= qemu_config_foreach(f
, cb
, vm_config_groups
, filename
, errp
);
477 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
483 size_t orig_size
, enum_size
;
486 prefix
= g_strdup_printf("%s.", opts
->name
);
487 qdict_extract_subqdict(options
, &subqdict
, prefix
);
489 orig_size
= qdict_size(subqdict
);
494 subopts
= qemu_opts_create(opts
, NULL
, 0, errp
);
499 if (!qemu_opts_absorb_qdict(subopts
, subqdict
, errp
)) {
503 enum_size
= qdict_size(subqdict
);
504 if (enum_size
< orig_size
&& enum_size
) {
505 error_setg(errp
, "Unknown option '%s' for [%s]",
506 qdict_first(subqdict
)->key
, opts
->name
);
511 /* Multiple, enumerated sections */
512 QListEntry
*list_entry
;
515 /* Not required anymore */
516 qemu_opts_del(subopts
);
518 qdict_array_split(subqdict
, &list
);
519 if (qdict_size(subqdict
)) {
520 error_setg(errp
, "Unused option '%s' for [%s]",
521 qdict_first(subqdict
)->key
, opts
->name
);
525 QLIST_FOREACH_ENTRY(list
, list_entry
) {
526 QDict
*section
= qobject_to(QDict
, qlist_entry_obj(list_entry
));
530 error_setg(errp
, "[%s] section (index %u) does not consist of "
531 "keys", opts
->name
, i
);
535 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
536 subopts
= qemu_opts_create(opts
, opt_name
, 1, errp
);
542 if (!qemu_opts_absorb_qdict(subopts
, section
, errp
)) {
543 qemu_opts_del(subopts
);
547 if (qdict_size(section
)) {
548 error_setg(errp
, "[%s] section doesn't support the option '%s'",
549 opts
->name
, qdict_first(section
)->key
);
550 qemu_opts_del(subopts
);
557 qobject_unref(subqdict
);
561 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
565 Error
*local_err
= NULL
;
567 for (i
= 0; lists
[i
]; i
++) {
568 config_parse_qdict_section(options
, lists
[i
], &local_err
);
570 error_propagate(errp
, local_err
);