qemu/util/qemu-config.c
<<
>>
Prefs
   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"
  11
  12static QemuOptsList *vm_config_groups[48];
  13static QemuOptsList *drive_config_groups[5];
  14
  15static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
  16                               Error **errp)
  17{
  18    int i;
  19
  20    qemu_load_module_for_opts(group);
  21    for (i = 0; lists[i] != NULL; i++) {
  22        if (strcmp(lists[i]->name, group) == 0)
  23            break;
  24    }
  25    if (lists[i] == NULL) {
  26        error_setg(errp, "There is no option group '%s'", group);
  27    }
  28    return lists[i];
  29}
  30
  31QemuOptsList *qemu_find_opts(const char *group)
  32{
  33    QemuOptsList *ret;
  34    Error *local_err = NULL;
  35
  36    ret = find_list(vm_config_groups, group, &local_err);
  37    if (local_err) {
  38        error_report_err(local_err);
  39    }
  40
  41    return ret;
  42}
  43
  44QemuOpts *qemu_find_opts_singleton(const char *group)
  45{
  46    QemuOptsList *list;
  47    QemuOpts *opts;
  48
  49    list = qemu_find_opts(group);
  50    assert(list);
  51    opts = qemu_opts_find(list, NULL);
  52    if (!opts) {
  53        opts = qemu_opts_create(list, NULL, 0, &error_abort);
  54    }
  55    return opts;
  56}
  57
  58static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
  59{
  60    CommandLineParameterInfoList *param_list = NULL;
  61    CommandLineParameterInfo *info;
  62    int i;
  63
  64    for (i = 0; desc[i].name != NULL; i++) {
  65        info = g_malloc0(sizeof(*info));
  66        info->name = g_strdup(desc[i].name);
  67
  68        switch (desc[i].type) {
  69        case QEMU_OPT_STRING:
  70            info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
  71            break;
  72        case QEMU_OPT_BOOL:
  73            info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
  74            break;
  75        case QEMU_OPT_NUMBER:
  76            info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
  77            break;
  78        case QEMU_OPT_SIZE:
  79            info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
  80            break;
  81        }
  82
  83        if (desc[i].help) {
  84            info->has_help = true;
  85            info->help = g_strdup(desc[i].help);
  86        }
  87        if (desc[i].def_value_str) {
  88            info->has_q_default = true;
  89            info->q_default = g_strdup(desc[i].def_value_str);
  90        }
  91
  92        QAPI_LIST_PREPEND(param_list, info);
  93    }
  94
  95    return param_list;
  96}
  97
  98/* remove repeated entry from the info list */
  99static void cleanup_infolist(CommandLineParameterInfoList *head)
 100{
 101    CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
 102
 103    cur = head;
 104    while (cur->next) {
 105        pre_entry = head;
 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);
 112                break;
 113            }
 114            pre_entry = pre_entry->next;
 115        }
 116        cur = cur->next;
 117    }
 118}
 119
 120/* merge the description items of two parameter infolists */
 121static void connect_infolist(CommandLineParameterInfoList *head,
 122                             CommandLineParameterInfoList *new)
 123{
 124    CommandLineParameterInfoList *cur;
 125
 126    cur = head;
 127    while (cur->next) {
 128        cur = cur->next;
 129    }
 130    cur->next = new;
 131}
 132
 133/* access all the local QemuOptsLists for drive option */
 134static CommandLineParameterInfoList *get_drive_infolist(void)
 135{
 136    CommandLineParameterInfoList *head = NULL, *cur;
 137    int i;
 138
 139    for (i = 0; drive_config_groups[i] != NULL; i++) {
 140        if (!head) {
 141            head = query_option_descs(drive_config_groups[i]->desc);
 142        } else {
 143            cur = query_option_descs(drive_config_groups[i]->desc);
 144            connect_infolist(head, cur);
 145        }
 146    }
 147    cleanup_infolist(head);
 148
 149    return head;
 150}
 151
 152/* restore machine options that are now machine's properties */
 153static QemuOptsList machine_opts = {
 154    .merge_lists = true,
 155    .head = QTAILQ_HEAD_INITIALIZER(machine_opts.head),
 156    .desc = {
 157        {
 158            .name = "type",
 159            .type = QEMU_OPT_STRING,
 160            .help = "emulated machine"
 161        },{
 162            .name = "accel",
 163            .type = QEMU_OPT_STRING,
 164            .help = "accelerator list",
 165        },{
 166            .name = "kernel_irqchip",
 167            .type = QEMU_OPT_BOOL,
 168            .help = "use KVM in-kernel irqchip",
 169        },{
 170            .name = "kvm_shadow_mem",
 171            .type = QEMU_OPT_SIZE,
 172            .help = "KVM shadow MMU size",
 173        },{
 174            .name = "kernel",
 175            .type = QEMU_OPT_STRING,
 176            .help = "Linux kernel image file",
 177        },{
 178            .name = "initrd",
 179            .type = QEMU_OPT_STRING,
 180            .help = "Linux initial ramdisk file",
 181        },{
 182            .name = "append",
 183            .type = QEMU_OPT_STRING,
 184            .help = "Linux kernel command line",
 185        },{
 186            .name = "dtb",
 187            .type = QEMU_OPT_STRING,
 188            .help = "Linux kernel device tree file",
 189        },{
 190            .name = "dumpdtb",
 191            .type = QEMU_OPT_STRING,
 192            .help = "Dump current dtb to a file and quit",
 193        },{
 194            .name = "phandle_start",
 195            .type = QEMU_OPT_NUMBER,
 196            .help = "The first phandle ID we may generate dynamically",
 197        },{
 198            .name = "dt_compatible",
 199            .type = QEMU_OPT_STRING,
 200            .help = "Overrides the \"compatible\" property of the dt root node",
 201        },{
 202            .name = "dump-guest-core",
 203            .type = QEMU_OPT_BOOL,
 204            .help = "Include guest memory in  a core dump",
 205        },{
 206            .name = "mem-merge",
 207            .type = QEMU_OPT_BOOL,
 208            .help = "enable/disable memory merge support",
 209        },{
 210            .name = "usb",
 211            .type = QEMU_OPT_BOOL,
 212            .help = "Set on/off to enable/disable usb",
 213        },{
 214            .name = "firmware",
 215            .type = QEMU_OPT_STRING,
 216            .help = "firmware image",
 217        },{
 218            .name = "iommu",
 219            .type = QEMU_OPT_BOOL,
 220            .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
 221        },{
 222            .name = "suppress-vmdesc",
 223            .type = QEMU_OPT_BOOL,
 224            .help = "Set on to disable self-describing migration",
 225        },{
 226            .name = "aes-key-wrap",
 227            .type = QEMU_OPT_BOOL,
 228            .help = "enable/disable AES key wrapping using the CPACF wrapping key",
 229        },{
 230            .name = "dea-key-wrap",
 231            .type = QEMU_OPT_BOOL,
 232            .help = "enable/disable DEA key wrapping using the CPACF wrapping key",
 233        },{
 234            .name = "loadparm",
 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",
 239        },
 240        { /* End of list */ }
 241    }
 242};
 243
 244CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
 245                                                          const char *option,
 246                                                          Error **errp)
 247{
 248    CommandLineOptionInfoList *conf_list = NULL;
 249    CommandLineOptionInfo *info;
 250    int i;
 251
 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 {
 259                info->parameters =
 260                    query_option_descs(vm_config_groups[i]->desc);
 261            }
 262            QAPI_LIST_PREPEND(conf_list, info);
 263        }
 264    }
 265
 266    if (!has_option || !strcmp(option, "machine")) {
 267        info = g_malloc0(sizeof(*info));
 268        info->option = g_strdup("machine");
 269        info->parameters = query_option_descs(machine_opts.desc);
 270        QAPI_LIST_PREPEND(conf_list, info);
 271    }
 272
 273    if (conf_list == NULL) {
 274        error_setg(errp, "invalid option name: %s", option);
 275    }
 276
 277    return conf_list;
 278}
 279
 280QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
 281{
 282    return find_list(vm_config_groups, group, errp);
 283}
 284
 285void qemu_add_drive_opts(QemuOptsList *list)
 286{
 287    int entries, i;
 288
 289    entries = ARRAY_SIZE(drive_config_groups);
 290    entries--; /* keep list NULL terminated */
 291    for (i = 0; i < entries; i++) {
 292        if (drive_config_groups[i] == NULL) {
 293            drive_config_groups[i] = list;
 294            return;
 295        }
 296    }
 297    fprintf(stderr, "ran out of space in drive_config_groups");
 298    abort();
 299}
 300
 301void qemu_add_opts(QemuOptsList *list)
 302{
 303    int entries, i;
 304
 305    entries = ARRAY_SIZE(vm_config_groups);
 306    entries--; /* keep list NULL terminated */
 307    for (i = 0; i < entries; i++) {
 308        if (vm_config_groups[i] == NULL) {
 309            vm_config_groups[i] = list;
 310            return;
 311        }
 312    }
 313    fprintf(stderr, "ran out of space in vm_config_groups");
 314    abort();
 315}
 316
 317struct ConfigWriteData {
 318    QemuOptsList *list;
 319    FILE *fp;
 320};
 321
 322static int config_write_opt(void *opaque, const char *name, const char *value,
 323                            Error **errp)
 324{
 325    struct ConfigWriteData *data = opaque;
 326
 327    fprintf(data->fp, "  %s = \"%s\"\n", name, value);
 328    return 0;
 329}
 330
 331static int config_write_opts(void *opaque, QemuOpts *opts, Error **errp)
 332{
 333    struct ConfigWriteData *data = opaque;
 334    const char *id = qemu_opts_id(opts);
 335
 336    if (id) {
 337        fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
 338    } else {
 339        fprintf(data->fp, "[%s]\n", data->list->name);
 340    }
 341    qemu_opt_foreach(opts, config_write_opt, data, NULL);
 342    fprintf(data->fp, "\n");
 343    return 0;
 344}
 345
 346void qemu_config_write(FILE *fp)
 347{
 348    struct ConfigWriteData data = { .fp = fp };
 349    QemuOptsList **lists = vm_config_groups;
 350    int i;
 351
 352    fprintf(fp, "# qemu config file\n\n");
 353    for (i = 0; lists[i] != NULL; i++) {
 354        data.list = lists[i];
 355        qemu_opts_foreach(data.list, config_write_opts, &data, NULL);
 356    }
 357}
 358
 359/* Returns number of config groups on success, -errno on error */
 360static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
 361                               const char *fname, Error **errp)
 362{
 363    char line[1024], prev_group[64], group[64], arg[64], value[1024];
 364    Location loc;
 365    Error *local_err = NULL;
 366    QDict *qdict = NULL;
 367    int res = -EINVAL, lno = 0;
 368    int count = 0;
 369
 370    loc_push_none(&loc);
 371    while (fgets(line, sizeof(line), fp) != NULL) {
 372        ++lno;
 373        if (line[0] == '\n') {
 374            /* skip empty lines */
 375            continue;
 376        }
 377        if (line[0] == '#') {
 378            /* comment */
 379            continue;
 380        }
 381        if (line[0] == '[') {
 382            QDict *prev = qdict;
 383            if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
 384                qdict = qdict_new();
 385                qdict_put_str(qdict, "id", value);
 386                count++;
 387            } else if (sscanf(line, "[%63[^]]]", group) == 1) {
 388                qdict = qdict_new();
 389                count++;
 390            }
 391            if (qdict != prev) {
 392                if (prev) {
 393                    cb(prev_group, prev, opaque, &local_err);
 394                    qobject_unref(prev);
 395                    if (local_err) {
 396                        error_propagate(errp, local_err);
 397                        goto out;
 398                    }
 399                }
 400                strcpy(prev_group, group);
 401                continue;
 402            }
 403        }
 404        loc_set_file(fname, lno);
 405        value[0] = '\0';
 406        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
 407            sscanf(line, " %63s = \"\"", arg) == 1) {
 408            /* arg = value */
 409            if (qdict == NULL) {
 410                error_setg(errp, "no group defined");
 411                goto out;
 412            }
 413            qdict_put_str(qdict, arg, value);
 414            continue;
 415        }
 416        error_setg(errp, "parse error");
 417        goto out;
 418    }
 419    if (ferror(fp)) {
 420        loc_pop(&loc);
 421        error_setg_errno(errp, errno, "Cannot read config file");
 422        goto out_no_loc;
 423    }
 424    res = count;
 425    if (qdict) {
 426        cb(group, qdict, opaque, errp);
 427    }
 428out:
 429    loc_pop(&loc);
 430out_no_loc:
 431    qobject_unref(qdict);
 432    return res;
 433}
 434
 435void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
 436{
 437    QemuOptsList **lists = opaque;
 438    QemuOptsList *list;
 439
 440    list = find_list(lists, group, errp);
 441    if (!list) {
 442        return;
 443    }
 444
 445    qemu_opts_from_qdict(list, qdict, errp);
 446}
 447
 448int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
 449{
 450    return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
 451}
 452
 453int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
 454{
 455    FILE *f = fopen(filename, "r");
 456    int ret;
 457
 458    if (f == NULL) {
 459        error_setg_file_open(errp, errno, filename);
 460        return -errno;
 461    }
 462
 463    ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
 464    fclose(f);
 465    return ret;
 466}
 467
 468static void config_parse_qdict_section(QDict *options, QemuOptsList *opts,
 469                                       Error **errp)
 470{
 471    QemuOpts *subopts;
 472    QDict *subqdict;
 473    QList *list = NULL;
 474    size_t orig_size, enum_size;
 475    char *prefix;
 476
 477    prefix = g_strdup_printf("%s.", opts->name);
 478    qdict_extract_subqdict(options, &subqdict, prefix);
 479    g_free(prefix);
 480    orig_size = qdict_size(subqdict);
 481    if (!orig_size) {
 482        goto out;
 483    }
 484
 485    subopts = qemu_opts_create(opts, NULL, 0, errp);
 486    if (!subopts) {
 487        goto out;
 488    }
 489
 490    if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
 491        goto out;
 492    }
 493
 494    enum_size = qdict_size(subqdict);
 495    if (enum_size < orig_size && enum_size) {
 496        error_setg(errp, "Unknown option '%s' for [%s]",
 497                   qdict_first(subqdict)->key, opts->name);
 498        goto out;
 499    }
 500
 501    if (enum_size) {
 502        /* Multiple, enumerated sections */
 503        QListEntry *list_entry;
 504        unsigned i = 0;
 505
 506        /* Not required anymore */
 507        qemu_opts_del(subopts);
 508
 509        qdict_array_split(subqdict, &list);
 510        if (qdict_size(subqdict)) {
 511            error_setg(errp, "Unused option '%s' for [%s]",
 512                       qdict_first(subqdict)->key, opts->name);
 513            goto out;
 514        }
 515
 516        QLIST_FOREACH_ENTRY(list, list_entry) {
 517            QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
 518            char *opt_name;
 519
 520            if (!section) {
 521                error_setg(errp, "[%s] section (index %u) does not consist of "
 522                           "keys", opts->name, i);
 523                goto out;
 524            }
 525
 526            opt_name = g_strdup_printf("%s.%u", opts->name, i++);
 527            subopts = qemu_opts_create(opts, opt_name, 1, errp);
 528            g_free(opt_name);
 529            if (!subopts) {
 530                goto out;
 531            }
 532
 533            if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
 534                qemu_opts_del(subopts);
 535                goto out;
 536            }
 537
 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);
 542                goto out;
 543            }
 544        }
 545    }
 546
 547out:
 548    qobject_unref(subqdict);
 549    qobject_unref(list);
 550}
 551
 552void qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
 553                             Error **errp)
 554{
 555    int i;
 556    Error *local_err = NULL;
 557
 558    for (i = 0; lists[i]; i++) {
 559        config_parse_qdict_section(options, lists[i], &local_err);
 560        if (local_err) {
 561            error_propagate(errp, local_err);
 562            return;
 563        }
 564    }
 565}
 566