qemu-config.c 13.2 KB
Newer Older
G
Gerd Hoffmann 已提交
1
#include "qemu-common.h"
2
#include "qemu-error.h"
G
Gerd Hoffmann 已提交
3 4
#include "qemu-option.h"
#include "qemu-config.h"
G
Gerd Hoffmann 已提交
5
#include "sysemu.h"
6
#include "hw/qdev.h"
G
Gerd Hoffmann 已提交
7 8 9

QemuOptsList qemu_drive_opts = {
    .name = "drive",
B
Blue Swirl 已提交
10
    .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
G
Gerd Hoffmann 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    .desc = {
        {
            .name = "bus",
            .type = QEMU_OPT_NUMBER,
            .help = "bus number",
        },{
            .name = "unit",
            .type = QEMU_OPT_NUMBER,
            .help = "unit number (i.e. lun for scsi)",
        },{
            .name = "if",
            .type = QEMU_OPT_STRING,
            .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
        },{
            .name = "index",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "cyls",
            .type = QEMU_OPT_NUMBER,
            .help = "number of cylinders (ide disk geometry)",
        },{
            .name = "heads",
            .type = QEMU_OPT_NUMBER,
            .help = "number of heads (ide disk geometry)",
        },{
            .name = "secs",
            .type = QEMU_OPT_NUMBER,
            .help = "number of sectors (ide disk geometry)",
        },{
            .name = "trans",
            .type = QEMU_OPT_STRING,
            .help = "chs translation (auto, lba. none)",
        },{
            .name = "media",
            .type = QEMU_OPT_STRING,
            .help = "media type (disk, cdrom)",
        },{
            .name = "snapshot",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "file",
            .type = QEMU_OPT_STRING,
            .help = "disk image",
        },{
            .name = "cache",
            .type = QEMU_OPT_STRING,
57
            .help = "host cache usage (none, writeback, writethrough, unsafe)",
58 59 60 61
        },{
            .name = "aio",
            .type = QEMU_OPT_STRING,
            .help = "host AIO implementation (threads, native)",
G
Gerd Hoffmann 已提交
62 63 64 65 66 67 68
        },{
            .name = "format",
            .type = QEMU_OPT_STRING,
            .help = "disk format (raw, qcow2, ...)",
        },{
            .name = "serial",
            .type = QEMU_OPT_STRING,
K
Kevin Wolf 已提交
69 70 71
        },{
            .name = "rerror",
            .type = QEMU_OPT_STRING,
G
Gerd Hoffmann 已提交
72 73 74 75 76 77 78
        },{
            .name = "werror",
            .type = QEMU_OPT_STRING,
        },{
            .name = "addr",
            .type = QEMU_OPT_STRING,
            .help = "pci address (virtio only)",
79 80 81
        },{
            .name = "readonly",
            .type = QEMU_OPT_BOOL,
G
Gerd Hoffmann 已提交
82 83 84 85 86
        },
        { /* end if list */ }
    },
};

87 88
QemuOptsList qemu_chardev_opts = {
    .name = "chardev",
89
    .implied_opt_name = "backend",
B
Blue Swirl 已提交
90
    .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
91
    .desc = {
92 93 94 95 96 97
        {
            .name = "backend",
            .type = QEMU_OPT_STRING,
        },{
            .name = "path",
            .type = QEMU_OPT_STRING,
98 99 100 101 102 103
        },{
            .name = "host",
            .type = QEMU_OPT_STRING,
        },{
            .name = "port",
            .type = QEMU_OPT_STRING,
G
Gerd Hoffmann 已提交
104 105 106 107 108 109
        },{
            .name = "localaddr",
            .type = QEMU_OPT_STRING,
        },{
            .name = "localport",
            .type = QEMU_OPT_STRING,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        },{
            .name = "to",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "ipv4",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "ipv6",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "wait",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "server",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "delay",
            .type = QEMU_OPT_BOOL,
        },{
            .name = "telnet",
            .type = QEMU_OPT_BOOL,
G
Gerd Hoffmann 已提交
131 132 133 134 135 136 137 138 139 140 141 142
        },{
            .name = "width",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "height",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "cols",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "rows",
            .type = QEMU_OPT_NUMBER,
G
Gerd Hoffmann 已提交
143 144 145
        },{
            .name = "mux",
            .type = QEMU_OPT_BOOL,
146 147 148
        },{
            .name = "signal",
            .type = QEMU_OPT_BOOL,
149
        },
150 151 152 153
        { /* end if list */ }
    },
};

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#ifdef CONFIG_LINUX
QemuOptsList qemu_fsdev_opts = {
    .name = "fsdev",
    .implied_opt_name = "fstype",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
    .desc = {
        {
            .name = "fstype",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "path",
            .type = QEMU_OPT_STRING,
        },
        { /*End of list */ }
    },
};
#endif

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
#ifdef CONFIG_LINUX
QemuOptsList qemu_virtfs_opts = {
    .name = "virtfs",
    .implied_opt_name = "fstype",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
    .desc = {
        {
            .name = "fstype",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "path",
            .type = QEMU_OPT_STRING,
        }, {
            .name = "mount_tag",
            .type = QEMU_OPT_STRING,
        },

        { /*End of list */ }
    },
};
#endif

G
Gerd Hoffmann 已提交
194 195
QemuOptsList qemu_device_opts = {
    .name = "device",
196
    .implied_opt_name = "driver",
B
Blue Swirl 已提交
197
    .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
G
Gerd Hoffmann 已提交
198 199 200 201 202 203 204 205 206 207
    .desc = {
        /*
         * no elements => accept any
         * sanity checking will happen later
         * when setting device properties
         */
        { /* end if list */ }
    },
};

M
Mark McLoughlin 已提交
208 209
QemuOptsList qemu_netdev_opts = {
    .name = "netdev",
210
    .implied_opt_name = "type",
M
Mark McLoughlin 已提交
211 212 213 214 215 216 217 218 219 220
    .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
    .desc = {
        /*
         * no elements => accept any params
         * validation will happen later
         */
        { /* end of list */ }
    },
};

M
Mark McLoughlin 已提交
221 222
QemuOptsList qemu_net_opts = {
    .name = "net",
223
    .implied_opt_name = "type",
M
Mark McLoughlin 已提交
224 225 226 227 228 229 230 231 232 233
    .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
    .desc = {
        /*
         * no elements => accept any params
         * validation will happen later
         */
        { /* end of list */ }
    },
};

J
Jan Kiszka 已提交
234 235 236 237 238 239 240
QemuOptsList qemu_rtc_opts = {
    .name = "rtc",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
    .desc = {
        {
            .name = "base",
            .type = QEMU_OPT_STRING,
J
Jan Kiszka 已提交
241 242 243
        },{
            .name = "clock",
            .type = QEMU_OPT_STRING,
J
Jan Kiszka 已提交
244 245 246 247 248 249 250 251 252 253
#ifdef TARGET_I386
        },{
            .name = "driftfix",
            .type = QEMU_OPT_STRING,
#endif
        },
        { /* end if list */ }
    },
};

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
QemuOptsList qemu_global_opts = {
    .name = "global",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
    .desc = {
        {
            .name = "driver",
            .type = QEMU_OPT_STRING,
        },{
            .name = "property",
            .type = QEMU_OPT_STRING,
        },{
            .name = "value",
            .type = QEMU_OPT_STRING,
        },
        { /* end if list */ }
    },
};

272 273
QemuOptsList qemu_mon_opts = {
    .name = "mon",
274
    .implied_opt_name = "chardev",
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
    .desc = {
        {
            .name = "mode",
            .type = QEMU_OPT_STRING,
        },{
            .name = "chardev",
            .type = QEMU_OPT_STRING,
        },{
            .name = "default",
            .type = QEMU_OPT_BOOL,
        },
        { /* end if list */ }
    },
};

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
QemuOptsList qemu_cpudef_opts = {
    .name = "cpudef",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
    .desc = {
        {
            .name = "name",
            .type = QEMU_OPT_STRING,
        },{
            .name = "level",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "vendor",
            .type = QEMU_OPT_STRING,
        },{
            .name = "family",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "model",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "stepping",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "feature_edx",      /* cpuid 0000_0001.edx */
            .type = QEMU_OPT_STRING,
        },{
            .name = "feature_ecx",      /* cpuid 0000_0001.ecx */
            .type = QEMU_OPT_STRING,
        },{
            .name = "extfeature_edx",   /* cpuid 8000_0001.edx */
            .type = QEMU_OPT_STRING,
        },{
            .name = "extfeature_ecx",   /* cpuid 8000_0001.ecx */
            .type = QEMU_OPT_STRING,
        },{
            .name = "xlevel",
            .type = QEMU_OPT_NUMBER,
        },{
            .name = "model_id",
            .type = QEMU_OPT_STRING,
        },{
            .name = "vendor_override",
            .type = QEMU_OPT_NUMBER,
        },
        { /* end of list */ }
    },
};

339
static QemuOptsList *vm_config_groups[] = {
G
Gerd Hoffmann 已提交
340
    &qemu_drive_opts,
341
    &qemu_chardev_opts,
G
Gerd Hoffmann 已提交
342
    &qemu_device_opts,
M
Mark McLoughlin 已提交
343
    &qemu_netdev_opts,
M
Mark McLoughlin 已提交
344
    &qemu_net_opts,
J
Jan Kiszka 已提交
345
    &qemu_rtc_opts,
346
    &qemu_global_opts,
347
    &qemu_mon_opts,
348
    &qemu_cpudef_opts,
G
Gerd Hoffmann 已提交
349 350 351
    NULL,
};

352
static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
G
Gerd Hoffmann 已提交
353 354 355 356 357 358 359 360
{
    int i;

    for (i = 0; lists[i] != NULL; i++) {
        if (strcmp(lists[i]->name, group) == 0)
            break;
    }
    if (lists[i] == NULL) {
361
        error_report("there is no option group \"%s\"", group);
G
Gerd Hoffmann 已提交
362 363 364 365
    }
    return lists[i];
}

366 367 368 369 370
QemuOptsList *qemu_find_opts(const char *group)
{
    return find_list(vm_config_groups, group);
}

G
Gerd Hoffmann 已提交
371 372 373
int qemu_set_option(const char *str)
{
    char group[64], id[64], arg[64];
G
Gerd Hoffmann 已提交
374
    QemuOptsList *list;
G
Gerd Hoffmann 已提交
375
    QemuOpts *opts;
G
Gerd Hoffmann 已提交
376
    int rc, offset;
G
Gerd Hoffmann 已提交
377 378 379

    rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
    if (rc < 3 || str[offset] != '=') {
380
        error_report("can't parse: \"%s\"", str);
G
Gerd Hoffmann 已提交
381 382 383
        return -1;
    }

384
    list = qemu_find_opts(group);
G
Gerd Hoffmann 已提交
385
    if (list == NULL) {
G
Gerd Hoffmann 已提交
386 387 388
        return -1;
    }

G
Gerd Hoffmann 已提交
389
    opts = qemu_opts_find(list, id);
G
Gerd Hoffmann 已提交
390
    if (!opts) {
391 392
        error_report("there is no %s \"%s\" defined",
                     list->name, id);
G
Gerd Hoffmann 已提交
393 394 395
        return -1;
    }

M
Mark McLoughlin 已提交
396
    if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
G
Gerd Hoffmann 已提交
397 398 399 400 401
        return -1;
    }
    return 0;
}

402 403 404 405 406 407 408 409
int qemu_global_option(const char *str)
{
    char driver[64], property[64];
    QemuOpts *opts;
    int rc, offset;

    rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
    if (rc < 2 || str[offset] != '=') {
410
        error_report("can't parse: \"%s\"", str);
411 412 413 414 415 416 417 418 419 420
        return -1;
    }

    opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
    qemu_opt_set(opts, "driver", driver);
    qemu_opt_set(opts, "property", property);
    qemu_opt_set(opts, "value", str+offset+1);
    return 0;
}

G
Gerd Hoffmann 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
struct ConfigWriteData {
    QemuOptsList *list;
    FILE *fp;
};

static int config_write_opt(const char *name, const char *value, void *opaque)
{
    struct ConfigWriteData *data = opaque;

    fprintf(data->fp, "  %s = \"%s\"\n", name, value);
    return 0;
}

static int config_write_opts(QemuOpts *opts, void *opaque)
{
    struct ConfigWriteData *data = opaque;
    const char *id = qemu_opts_id(opts);

    if (id) {
        fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
    } else {
        fprintf(data->fp, "[%s]\n", data->list->name);
    }
    qemu_opt_foreach(opts, config_write_opt, data, 0);
    fprintf(data->fp, "\n");
    return 0;
}

void qemu_config_write(FILE *fp)
{
    struct ConfigWriteData data = { .fp = fp };
452
    QemuOptsList **lists = vm_config_groups;
G
Gerd Hoffmann 已提交
453 454 455 456 457 458 459 460
    int i;

    fprintf(fp, "# qemu config file\n\n");
    for (i = 0; lists[i] != NULL; i++) {
        data.list = lists[i];
        qemu_opts_foreach(data.list, config_write_opts, &data, 0);
    }
}
G
Gerd Hoffmann 已提交
461

462
int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
G
Gerd Hoffmann 已提交
463 464
{
    char line[1024], group[64], id[64], arg[64], value[1024];
465
    Location loc;
G
Gerd Hoffmann 已提交
466 467
    QemuOptsList *list = NULL;
    QemuOpts *opts = NULL;
468
    int res = -1, lno = 0;
G
Gerd Hoffmann 已提交
469

470
    loc_push_none(&loc);
G
Gerd Hoffmann 已提交
471
    while (fgets(line, sizeof(line), fp) != NULL) {
472
        loc_set_file(fname, ++lno);
G
Gerd Hoffmann 已提交
473 474 475 476 477 478 479 480 481 482
        if (line[0] == '\n') {
            /* skip empty lines */
            continue;
        }
        if (line[0] == '#') {
            /* comment */
            continue;
        }
        if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
            /* group with id */
483
            list = find_list(lists, group);
G
Gerd Hoffmann 已提交
484
            if (list == NULL)
485
                goto out;
G
Gerd Hoffmann 已提交
486 487 488 489 490
            opts = qemu_opts_create(list, id, 1);
            continue;
        }
        if (sscanf(line, "[%63[^]]]", group) == 1) {
            /* group without id */
491
            list = find_list(lists, group);
G
Gerd Hoffmann 已提交
492
            if (list == NULL)
493
                goto out;
G
Gerd Hoffmann 已提交
494 495 496 497 498 499
            opts = qemu_opts_create(list, NULL, 0);
            continue;
        }
        if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
            /* arg = value */
            if (opts == NULL) {
500 501
                error_report("no group defined");
                goto out;
G
Gerd Hoffmann 已提交
502 503
            }
            if (qemu_opt_set(opts, arg, value) != 0) {
504
                goto out;
G
Gerd Hoffmann 已提交
505 506 507
            }
            continue;
        }
508 509
        error_report("parse error");
        goto out;
G
Gerd Hoffmann 已提交
510
    }
511 512 513 514
    if (ferror(fp)) {
        error_report("error reading file");
        goto out;
    }
515 516 517 518
    res = 0;
out:
    loc_pop(&loc);
    return res;
G
Gerd Hoffmann 已提交
519
}
520 521 522 523

int qemu_read_config_file(const char *filename)
{
    FILE *f = fopen(filename, "r");
524 525
    int ret;

526 527 528 529
    if (f == NULL) {
        return -errno;
    }

530
    ret = qemu_config_parse(f, vm_config_groups, filename);
531 532
    fclose(f);

533 534 535 536 537
    if (ret == 0) {
        return 0;
    } else {
        return -EINVAL;
    }
538
}