1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
5 #include "qapi/qmp/qerror.h"
7 #include "qapi/error.h"
8 #include "qmp-commands.h"
9 #include "hw/i386/pc.h"
11 static QemuOptsList
*vm_config_groups
[32];
12 static QemuOptsList
*drive_config_groups
[4];
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
;
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 { /* End of list */ }
230 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
234 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
235 CommandLineOptionInfo
*info
;
238 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
239 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
240 info
= g_malloc0(sizeof(*info
));
241 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
242 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
243 info
->parameters
= get_drive_infolist();
244 } else if (!strcmp("machine", vm_config_groups
[i
]->name
)) {
245 info
->parameters
= query_option_descs(machine_opts
.desc
);
248 query_option_descs(vm_config_groups
[i
]->desc
);
250 entry
= g_malloc0(sizeof(*entry
));
252 entry
->next
= conf_list
;
257 if (conf_list
== NULL
) {
258 error_setg(errp
, "invalid option name: %s", option
);
264 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
266 return find_list(vm_config_groups
, group
, errp
);
269 void qemu_add_drive_opts(QemuOptsList
*list
)
273 entries
= ARRAY_SIZE(drive_config_groups
);
274 entries
--; /* keep list NULL terminated */
275 for (i
= 0; i
< entries
; i
++) {
276 if (drive_config_groups
[i
] == NULL
) {
277 drive_config_groups
[i
] = list
;
281 fprintf(stderr
, "ran out of space in drive_config_groups");
285 void qemu_add_opts(QemuOptsList
*list
)
289 entries
= ARRAY_SIZE(vm_config_groups
);
290 entries
--; /* keep list NULL terminated */
291 for (i
= 0; i
< entries
; i
++) {
292 if (vm_config_groups
[i
] == NULL
) {
293 vm_config_groups
[i
] = list
;
297 fprintf(stderr
, "ran out of space in vm_config_groups");
301 int qemu_set_option(const char *str
)
303 Error
*local_err
= NULL
;
304 char group
[64], id
[64], arg
[64];
309 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
310 if (rc
< 3 || str
[offset
] != '=') {
311 error_report("can't parse: \"%s\"", str
);
315 list
= qemu_find_opts(group
);
320 opts
= qemu_opts_find(list
, id
);
322 error_report("there is no %s \"%s\" defined",
327 qemu_opt_set(opts
, arg
, str
+ offset
+ 1, &local_err
);
329 error_report_err(local_err
);
335 struct ConfigWriteData
{
340 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
342 struct ConfigWriteData
*data
= opaque
;
344 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
348 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
350 struct ConfigWriteData
*data
= opaque
;
351 const char *id
= qemu_opts_id(opts
);
354 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
356 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
358 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
359 fprintf(data
->fp
, "\n");
363 void qemu_config_write(FILE *fp
)
365 struct ConfigWriteData data
= { .fp
= fp
};
366 QemuOptsList
**lists
= vm_config_groups
;
369 fprintf(fp
, "# qemu config file\n\n");
370 for (i
= 0; lists
[i
] != NULL
; i
++) {
371 data
.list
= lists
[i
];
372 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
376 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
378 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
380 QemuOptsList
*list
= NULL
;
381 Error
*local_err
= NULL
;
382 QemuOpts
*opts
= NULL
;
383 int res
= -1, lno
= 0;
386 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
387 loc_set_file(fname
, ++lno
);
388 if (line
[0] == '\n') {
389 /* skip empty lines */
392 if (line
[0] == '#') {
396 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
398 list
= find_list(lists
, group
, &local_err
);
400 error_report_err(local_err
);
403 opts
= qemu_opts_create(list
, id
, 1, NULL
);
406 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
407 /* group without id */
408 list
= find_list(lists
, group
, &local_err
);
410 error_report_err(local_err
);
413 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
416 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
419 error_report("no group defined");
422 qemu_opt_set(opts
, arg
, value
, &local_err
);
424 error_report_err(local_err
);
429 error_report("parse error");
433 error_report("error reading file");
442 int qemu_read_config_file(const char *filename
)
444 FILE *f
= fopen(filename
, "r");
451 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
461 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
467 Error
*local_err
= NULL
;
468 size_t orig_size
, enum_size
;
471 prefix
= g_strdup_printf("%s.", opts
->name
);
472 qdict_extract_subqdict(options
, &subqdict
, prefix
);
474 orig_size
= qdict_size(subqdict
);
479 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
481 error_propagate(errp
, local_err
);
485 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
487 error_propagate(errp
, local_err
);
491 enum_size
= qdict_size(subqdict
);
492 if (enum_size
< orig_size
&& enum_size
) {
493 error_setg(errp
, "Unknown option '%s' for [%s]",
494 qdict_first(subqdict
)->key
, opts
->name
);
499 /* Multiple, enumerated sections */
500 QListEntry
*list_entry
;
503 /* Not required anymore */
504 qemu_opts_del(subopts
);
506 qdict_array_split(subqdict
, &list
);
507 if (qdict_size(subqdict
)) {
508 error_setg(errp
, "Unused option '%s' for [%s]",
509 qdict_first(subqdict
)->key
, opts
->name
);
513 QLIST_FOREACH_ENTRY(list
, list_entry
) {
514 QDict
*section
= qobject_to_qdict(qlist_entry_obj(list_entry
));
518 error_setg(errp
, "[%s] section (index %u) does not consist of "
519 "keys", opts
->name
, i
);
523 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
524 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
527 error_propagate(errp
, local_err
);
531 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
533 error_propagate(errp
, local_err
);
534 qemu_opts_del(subopts
);
538 if (qdict_size(section
)) {
539 error_setg(errp
, "[%s] section doesn't support the option '%s'",
540 opts
->name
, qdict_first(section
)->key
);
541 qemu_opts_del(subopts
);
552 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
556 Error
*local_err
= NULL
;
558 for (i
= 0; lists
[i
]; i
++) {
559 config_parse_qdict_section(options
, lists
[i
], &local_err
);
561 error_propagate(errp
, local_err
);