1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qemu-common.h"
4 #include "qemu/error-report.h"
5 #include "qemu/option.h"
6 #include "qemu/config-file.h"
7 #include "qmp-commands.h"
9 static QemuOptsList
*vm_config_groups
[48];
10 static QemuOptsList
*drive_config_groups
[5];
12 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
17 for (i
= 0; lists
[i
] != NULL
; i
++) {
18 if (strcmp(lists
[i
]->name
, group
) == 0)
21 if (lists
[i
] == NULL
) {
22 error_setg(errp
, "There is no option group '%s'", group
);
27 QemuOptsList
*qemu_find_opts(const char *group
)
30 Error
*local_err
= NULL
;
32 ret
= find_list(vm_config_groups
, group
, &local_err
);
34 error_report_err(local_err
);
40 QemuOpts
*qemu_find_opts_singleton(const char *group
)
45 list
= qemu_find_opts(group
);
47 opts
= qemu_opts_find(list
, NULL
);
49 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
54 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
56 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
57 CommandLineParameterInfo
*info
;
60 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
61 info
= g_malloc0(sizeof(*info
));
62 info
->name
= g_strdup(desc
[i
].name
);
64 switch (desc
[i
].type
) {
66 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
69 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
72 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
75 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
80 info
->has_help
= true;
81 info
->help
= g_strdup(desc
[i
].help
);
83 if (desc
[i
].def_value_str
) {
84 info
->has_q_default
= true;
85 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
88 entry
= g_malloc0(sizeof(*entry
));
90 entry
->next
= param_list
;
97 /* remove repeated entry from the info list */
98 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
100 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
105 while (pre_entry
!= cur
->next
) {
106 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
107 del_entry
= cur
->next
;
108 cur
->next
= cur
->next
->next
;
109 del_entry
->next
= NULL
;
110 qapi_free_CommandLineParameterInfoList(del_entry
);
113 pre_entry
= pre_entry
->next
;
119 /* merge the description items of two parameter infolists */
120 static void connect_infolist(CommandLineParameterInfoList
*head
,
121 CommandLineParameterInfoList
*new)
123 CommandLineParameterInfoList
*cur
;
132 /* access all the local QemuOptsLists for drive option */
133 static CommandLineParameterInfoList
*get_drive_infolist(void)
135 CommandLineParameterInfoList
*head
= NULL
, *cur
;
138 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
140 head
= query_option_descs(drive_config_groups
[i
]->desc
);
142 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
143 connect_infolist(head
, cur
);
146 cleanup_infolist(head
);
151 /* restore machine options that are now machine's properties */
152 static QemuOptsList machine_opts
= {
154 .head
= QTAILQ_HEAD_INITIALIZER(machine_opts
.head
),
158 .type
= QEMU_OPT_STRING
,
159 .help
= "emulated machine"
162 .type
= QEMU_OPT_STRING
,
163 .help
= "accelerator list",
165 .name
= "kernel_irqchip",
166 .type
= QEMU_OPT_BOOL
,
167 .help
= "use KVM in-kernel irqchip",
169 .name
= "kvm_shadow_mem",
170 .type
= QEMU_OPT_SIZE
,
171 .help
= "KVM shadow MMU size",
174 .type
= QEMU_OPT_STRING
,
175 .help
= "Linux kernel image file",
178 .type
= QEMU_OPT_STRING
,
179 .help
= "Linux initial ramdisk file",
182 .type
= QEMU_OPT_STRING
,
183 .help
= "Linux kernel command line",
186 .type
= QEMU_OPT_STRING
,
187 .help
= "Linux kernel device tree file",
190 .type
= QEMU_OPT_STRING
,
191 .help
= "Dump current dtb to a file and quit",
193 .name
= "phandle_start",
194 .type
= QEMU_OPT_NUMBER
,
195 .help
= "The first phandle ID we may generate dynamically",
197 .name
= "dt_compatible",
198 .type
= QEMU_OPT_STRING
,
199 .help
= "Overrides the \"compatible\" property of the dt root node",
201 .name
= "dump-guest-core",
202 .type
= QEMU_OPT_BOOL
,
203 .help
= "Include guest memory in a core dump",
206 .type
= QEMU_OPT_BOOL
,
207 .help
= "enable/disable memory merge support",
210 .type
= QEMU_OPT_BOOL
,
211 .help
= "Set on/off to enable/disable usb",
214 .type
= QEMU_OPT_STRING
,
215 .help
= "firmware image",
218 .type
= QEMU_OPT_BOOL
,
219 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
221 .name
= "suppress-vmdesc",
222 .type
= QEMU_OPT_BOOL
,
223 .help
= "Set on to disable self-describing migration",
225 .name
= "aes-key-wrap",
226 .type
= QEMU_OPT_BOOL
,
227 .help
= "enable/disable AES key wrapping using the CPACF wrapping key",
229 .name
= "dea-key-wrap",
230 .type
= QEMU_OPT_BOOL
,
231 .help
= "enable/disable DEA key wrapping using the CPACF wrapping key",
234 .type
= QEMU_OPT_STRING
,
235 .help
= "Up to 8 chars in set of [A-Za-z0-9. ](lower case chars"
236 " converted to upper case) to pass to machine"
237 " loader, boot manager, and guest kernel",
239 { /* End of list */ }
243 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
247 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
248 CommandLineOptionInfo
*info
;
251 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
252 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
253 info
= g_malloc0(sizeof(*info
));
254 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
255 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
256 info
->parameters
= get_drive_infolist();
257 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
258 info
->parameters
= query_option_descs(machine_opts
.desc
);
261 query_option_descs(vm_config_groups
[i
]->desc
);
263 entry
= g_malloc0(sizeof(*entry
));
265 entry
->next
= conf_list
;
270 if (conf_list
== NULL
) {
271 error_setg(errp
, "invalid option name: %s", option
);
277 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
279 return find_list(vm_config_groups
, group
, errp
);
282 void qemu_add_drive_opts(QemuOptsList
*list
)
286 entries
= ARRAY_SIZE(drive_config_groups
);
287 entries
--; /* keep list NULL terminated */
288 for (i
= 0; i
< entries
; i
++) {
289 if (drive_config_groups
[i
] == NULL
) {
290 drive_config_groups
[i
] = list
;
294 fprintf(stderr
, "ran out of space in drive_config_groups");
298 void qemu_add_opts(QemuOptsList
*list
)
302 entries
= ARRAY_SIZE(vm_config_groups
);
303 entries
--; /* keep list NULL terminated */
304 for (i
= 0; i
< entries
; i
++) {
305 if (vm_config_groups
[i
] == NULL
) {
306 vm_config_groups
[i
] = list
;
310 fprintf(stderr
, "ran out of space in vm_config_groups");
314 int qemu_set_option(const char *str
)
316 Error
*local_err
= NULL
;
317 char group
[64], id
[64], arg
[64];
322 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
323 if (rc
< 3 || str
[offset
] != '=') {
324 error_report("can't parse: \"%s\"", str
);
328 list
= qemu_find_opts(group
);
333 opts
= qemu_opts_find(list
, id
);
335 error_report("there is no %s \"%s\" defined",
340 qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
);
342 error_report_err(local_err
);
348 struct ConfigWriteData
{
353 static int config_write_opt(void *opaque
, const char *name
, const char *value
,
356 struct ConfigWriteData
*data
= opaque
;
358 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
362 static int config_write_opts(void *opaque
, QemuOpts
*opts
, Error
**errp
)
364 struct ConfigWriteData
*data
= opaque
;
365 const char *id
= qemu_opts_id(opts
);
368 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
370 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
372 qemu_opt_foreach(opts
, config_write_opt
, data
, NULL
);
373 fprintf(data
->fp
, "\n");
377 void qemu_config_write(FILE *fp
)
379 struct ConfigWriteData data
= { .fp
= fp
};
380 QemuOptsList
**lists
= vm_config_groups
;
383 fprintf(fp
, "# qemu config file\n\n");
384 for (i
= 0; lists
[i
] != NULL
; i
++) {
385 data
.list
= lists
[i
];
386 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, NULL
);
390 /* Returns number of config groups on success, -errno on error */
391 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
393 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
395 QemuOptsList
*list
= NULL
;
396 Error
*local_err
= NULL
;
397 QemuOpts
*opts
= NULL
;
398 int res
= -EINVAL
, lno
= 0;
402 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
403 loc_set_file(fname
, ++lno
);
404 if (line
[0] == '\n') {
405 /* skip empty lines */
408 if (line
[0] == '#') {
412 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
414 list
= find_list(lists
, group
, &local_err
);
416 error_report_err(local_err
);
419 opts
= qemu_opts_create(list
, id
, 1, NULL
);
423 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
424 /* group without id */
425 list
= find_list(lists
, group
, &local_err
);
427 error_report_err(local_err
);
430 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
435 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
436 sscanf(line
, " %63s = \"\"", arg
) == 1) {
439 error_report("no group defined");
442 qemu_opt_set(opts
, arg
, value
, &local_err
);
444 error_report_err(local_err
);
449 error_report("parse error");
453 error_report("error reading file");
462 int qemu_read_config_file(const char *filename
)
464 FILE *f
= fopen(filename
, "r");
471 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
476 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
482 Error
*local_err
= NULL
;
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, &local_err
);
496 error_propagate(errp
, local_err
);
500 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
502 error_propagate(errp
, local_err
);
506 enum_size
= qdict_size(subqdict
);
507 if (enum_size
< orig_size
&& enum_size
) {
508 error_setg(errp
, "Unknown option '%s' for [%s]",
509 qdict_first(subqdict
)->key
, opts
->name
);
514 /* Multiple, enumerated sections */
515 QListEntry
*list_entry
;
518 /* Not required anymore */
519 qemu_opts_del(subopts
);
521 qdict_array_split(subqdict
, &list
);
522 if (qdict_size(subqdict
)) {
523 error_setg(errp
, "Unused option '%s' for [%s]",
524 qdict_first(subqdict
)->key
, opts
->name
);
528 QLIST_FOREACH_ENTRY(list
, list_entry
) {
529 QDict
*section
= qobject_to_qdict(qlist_entry_obj(list_entry
));
533 error_setg(errp
, "[%s] section (index %u) does not consist of "
534 "keys", opts
->name
, i
);
538 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
539 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
542 error_propagate(errp
, local_err
);
546 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
548 error_propagate(errp
, local_err
);
549 qemu_opts_del(subopts
);
553 if (qdict_size(section
)) {
554 error_setg(errp
, "[%s] section doesn't support the option '%s'",
555 opts
->name
, qdict_first(section
)->key
);
556 qemu_opts_del(subopts
);
567 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
571 Error
*local_err
= NULL
;
573 for (i
= 0; lists
[i
]; i
++) {
574 config_parse_qdict_section(options
, lists
[i
], &local_err
);
576 error_propagate(errp
, local_err
);