1 #include "qemu/osdep.h"
2 #include "qemu-common.h"
3 #include "qemu/error-report.h"
4 #include "qemu/option.h"
5 #include "qemu/config-file.h"
6 #include "qmp-commands.h"
8 static QemuOptsList
*vm_config_groups
[48];
9 static QemuOptsList
*drive_config_groups
[4];
11 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
16 for (i
= 0; lists
[i
] != NULL
; i
++) {
17 if (strcmp(lists
[i
]->name
, group
) == 0)
20 if (lists
[i
] == NULL
) {
21 error_setg(errp
, "There is no option group '%s'", group
);
26 QemuOptsList
*qemu_find_opts(const char *group
)
29 Error
*local_err
= NULL
;
31 ret
= find_list(vm_config_groups
, group
, &local_err
);
33 error_report_err(local_err
);
39 QemuOpts
*qemu_find_opts_singleton(const char *group
)
44 list
= qemu_find_opts(group
);
46 opts
= qemu_opts_find(list
, NULL
);
48 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
53 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
55 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
56 CommandLineParameterInfo
*info
;
59 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
60 info
= g_malloc0(sizeof(*info
));
61 info
->name
= g_strdup(desc
[i
].name
);
63 switch (desc
[i
].type
) {
65 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
68 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
71 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
74 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
79 info
->has_help
= true;
80 info
->help
= g_strdup(desc
[i
].help
);
82 if (desc
[i
].def_value_str
) {
83 info
->has_q_default
= true;
84 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
87 entry
= g_malloc0(sizeof(*entry
));
89 entry
->next
= param_list
;
96 /* remove repeated entry from the info list */
97 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
99 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
104 while (pre_entry
!= cur
->next
) {
105 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
106 del_entry
= cur
->next
;
107 cur
->next
= cur
->next
->next
;
111 pre_entry
= pre_entry
->next
;
117 /* merge the description items of two parameter infolists */
118 static void connect_infolist(CommandLineParameterInfoList
*head
,
119 CommandLineParameterInfoList
*new)
121 CommandLineParameterInfoList
*cur
;
130 /* access all the local QemuOptsLists for drive option */
131 static CommandLineParameterInfoList
*get_drive_infolist(void)
133 CommandLineParameterInfoList
*head
= NULL
, *cur
;
136 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
138 head
= query_option_descs(drive_config_groups
[i
]->desc
);
140 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
141 connect_infolist(head
, cur
);
144 cleanup_infolist(head
);
149 /* restore machine options that are now machine's properties */
150 static QemuOptsList machine_opts
= {
152 .head
= QTAILQ_HEAD_INITIALIZER(machine_opts
.head
),
156 .type
= QEMU_OPT_STRING
,
157 .help
= "emulated machine"
160 .type
= QEMU_OPT_STRING
,
161 .help
= "accelerator list",
163 .name
= "kernel_irqchip",
164 .type
= QEMU_OPT_BOOL
,
165 .help
= "use KVM in-kernel irqchip",
167 .name
= "kvm_shadow_mem",
168 .type
= QEMU_OPT_SIZE
,
169 .help
= "KVM shadow MMU size",
172 .type
= QEMU_OPT_STRING
,
173 .help
= "Linux kernel image file",
176 .type
= QEMU_OPT_STRING
,
177 .help
= "Linux initial ramdisk file",
180 .type
= QEMU_OPT_STRING
,
181 .help
= "Linux kernel command line",
184 .type
= QEMU_OPT_STRING
,
185 .help
= "Linux kernel device tree file",
188 .type
= QEMU_OPT_STRING
,
189 .help
= "Dump current dtb to a file and quit",
191 .name
= "phandle_start",
192 .type
= QEMU_OPT_NUMBER
,
193 .help
= "The first phandle ID we may generate dynamically",
195 .name
= "dt_compatible",
196 .type
= QEMU_OPT_STRING
,
197 .help
= "Overrides the \"compatible\" property of the dt root node",
199 .name
= "dump-guest-core",
200 .type
= QEMU_OPT_BOOL
,
201 .help
= "Include guest memory in a core dump",
204 .type
= QEMU_OPT_BOOL
,
205 .help
= "enable/disable memory merge support",
208 .type
= QEMU_OPT_BOOL
,
209 .help
= "Set on/off to enable/disable usb",
212 .type
= QEMU_OPT_STRING
,
213 .help
= "firmware image",
216 .type
= QEMU_OPT_BOOL
,
217 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
219 .name
= "suppress-vmdesc",
220 .type
= QEMU_OPT_BOOL
,
221 .help
= "Set on to disable self-describing migration",
223 .name
= "aes-key-wrap",
224 .type
= QEMU_OPT_BOOL
,
225 .help
= "enable/disable AES key wrapping using the CPACF wrapping key",
227 .name
= "dea-key-wrap",
228 .type
= QEMU_OPT_BOOL
,
229 .help
= "enable/disable DEA key wrapping using the CPACF wrapping key",
231 { /* End of list */ }
235 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
239 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
240 CommandLineOptionInfo
*info
;
243 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
244 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
245 info
= g_malloc0(sizeof(*info
));
246 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
247 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
248 info
->parameters
= get_drive_infolist();
249 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
250 info
->parameters
= query_option_descs(machine_opts
.desc
);
253 query_option_descs(vm_config_groups
[i
]->desc
);
255 entry
= g_malloc0(sizeof(*entry
));
257 entry
->next
= conf_list
;
262 if (conf_list
== NULL
) {
263 error_setg(errp
, "invalid option name: %s", option
);
269 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
271 return find_list(vm_config_groups
, group
, errp
);
274 void qemu_add_drive_opts(QemuOptsList
*list
)
278 entries
= ARRAY_SIZE(drive_config_groups
);
279 entries
--; /* keep list NULL terminated */
280 for (i
= 0; i
< entries
; i
++) {
281 if (drive_config_groups
[i
] == NULL
) {
282 drive_config_groups
[i
] = list
;
286 fprintf(stderr
, "ran out of space in drive_config_groups");
290 void qemu_add_opts(QemuOptsList
*list
)
294 entries
= ARRAY_SIZE(vm_config_groups
);
295 entries
--; /* keep list NULL terminated */
296 for (i
= 0; i
< entries
; i
++) {
297 if (vm_config_groups
[i
] == NULL
) {
298 vm_config_groups
[i
] = list
;
302 fprintf(stderr
, "ran out of space in vm_config_groups");
306 int qemu_set_option(const char *str
)
308 Error
*local_err
= NULL
;
309 char group
[64], id
[64], arg
[64];
314 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
315 if (rc
< 3 || str
[offset
] != '=') {
316 error_report("can't parse: \"%s\"", str
);
320 list
= qemu_find_opts(group
);
325 opts
= qemu_opts_find(list
, id
);
327 error_report("there is no %s \"%s\" defined",
332 qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
);
334 error_report_err(local_err
);
340 struct ConfigWriteData
{
345 static int config_write_opt(void *opaque
, const char *name
, const char *value
,
348 struct ConfigWriteData
*data
= opaque
;
350 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
354 static int config_write_opts(void *opaque
, QemuOpts
*opts
, Error
**errp
)
356 struct ConfigWriteData
*data
= opaque
;
357 const char *id
= qemu_opts_id(opts
);
360 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
362 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
364 qemu_opt_foreach(opts
, config_write_opt
, data
, NULL
);
365 fprintf(data
->fp
, "\n");
369 void qemu_config_write(FILE *fp
)
371 struct ConfigWriteData data
= { .fp
= fp
};
372 QemuOptsList
**lists
= vm_config_groups
;
375 fprintf(fp
, "# qemu config file\n\n");
376 for (i
= 0; lists
[i
] != NULL
; i
++) {
377 data
.list
= lists
[i
];
378 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, NULL
);
382 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
384 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
386 QemuOptsList
*list
= NULL
;
387 Error
*local_err
= NULL
;
388 QemuOpts
*opts
= NULL
;
389 int res
= -1, lno
= 0;
392 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
393 loc_set_file(fname
, ++lno
);
394 if (line
[0] == '\n') {
395 /* skip empty lines */
398 if (line
[0] == '#') {
402 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
404 list
= find_list(lists
, group
, &local_err
);
406 error_report_err(local_err
);
409 opts
= qemu_opts_create(list
, id
, 1, NULL
);
412 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
413 /* group without id */
414 list
= find_list(lists
, group
, &local_err
);
416 error_report_err(local_err
);
419 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
423 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
424 sscanf(line
, " %63s = \"\"", arg
) == 1) {
427 error_report("no group defined");
430 qemu_opt_set(opts
, arg
, value
, &local_err
);
432 error_report_err(local_err
);
437 error_report("parse error");
441 error_report("error reading file");
450 int qemu_read_config_file(const char *filename
)
452 FILE *f
= fopen(filename
, "r");
459 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
469 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
475 Error
*local_err
= NULL
;
476 size_t orig_size
, enum_size
;
479 prefix
= g_strdup_printf("%s.", opts
->name
);
480 qdict_extract_subqdict(options
, &subqdict
, prefix
);
482 orig_size
= qdict_size(subqdict
);
487 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
489 error_propagate(errp
, local_err
);
493 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
495 error_propagate(errp
, local_err
);
499 enum_size
= qdict_size(subqdict
);
500 if (enum_size
< orig_size
&& enum_size
) {
501 error_setg(errp
, "Unknown option '%s' for [%s]",
502 qdict_first(subqdict
)->key
, opts
->name
);
507 /* Multiple, enumerated sections */
508 QListEntry
*list_entry
;
511 /* Not required anymore */
512 qemu_opts_del(subopts
);
514 qdict_array_split(subqdict
, &list
);
515 if (qdict_size(subqdict
)) {
516 error_setg(errp
, "Unused option '%s' for [%s]",
517 qdict_first(subqdict
)->key
, opts
->name
);
521 QLIST_FOREACH_ENTRY(list
, list_entry
) {
522 QDict
*section
= qobject_to_qdict(qlist_entry_obj(list_entry
));
526 error_setg(errp
, "[%s] section (index %u) does not consist of "
527 "keys", opts
->name
, i
);
531 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
532 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
535 error_propagate(errp
, local_err
);
539 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
541 error_propagate(errp
, local_err
);
542 qemu_opts_del(subopts
);
546 if (qdict_size(section
)) {
547 error_setg(errp
, "[%s] section doesn't support the option '%s'",
548 opts
->name
, qdict_first(section
)->key
);
549 qemu_opts_del(subopts
);
560 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
564 Error
*local_err
= NULL
;
566 for (i
= 0; lists
[i
]; i
++) {
567 config_parse_qdict_section(options
, lists
[i
], &local_err
);
569 error_propagate(errp
, local_err
);