xen_sxpr.c 86.0 KB
Newer Older
1 2 3
/*
 * xen_sxpr.c: Xen SEXPR parsing functions
 *
4
 * Copyright (C) 2010-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2011 Univention GmbH
 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29
 *
 * Author: Anthony Liguori <aliguori@us.ibm.com>
 * Author: Daniel Veillard <veillard@redhat.com>
 * Author: Markus Groß <gross@univention.de>
 */

#include <config.h>

#include "internal.h"
30
#include "virerror.h"
31
#include "virconf.h"
32
#include "viralloc.h"
33
#include "verify.h"
34
#include "viruuid.h"
35
#include "virlog.h"
36 37 38
#include "count-one-bits.h"
#include "xenxs_private.h"
#include "xen_sxpr.h"
39
#include "virstoragefile.h"
40
#include "virstring.h"
41

J
Jim Fehlig 已提交
42
VIR_LOG_INIT("xenconfig.xen_sxpr");
43

P
Philipp Hahn 已提交
44
/* Get a domain id from a S-expression string */
45
int xenGetDomIdFromSxprString(const char *sexpr, int xendConfigVersion, int *id)
46 47
{
    struct sexpr *root = string2sexpr(sexpr);
48 49 50
    int ret;

    *id = -1;
51 52 53 54

    if (!root)
        return -1;

55
    ret = xenGetDomIdFromSxpr(root, xendConfigVersion, id);
56
    sexpr_free(root);
57
    return ret;
58 59
}

P
Philipp Hahn 已提交
60
/* Get a domain id from a S-expression */
61
int xenGetDomIdFromSxpr(const struct sexpr *root, int xendConfigVersion, int *id)
62 63
{
    const char * tmp = sexpr_node(root, "domain/domid");
64
    if (tmp == NULL && xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) { /* domid was mandatory */
65 66
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing id"));
67
        return -1;
68
    } else {
69 70
        *id = tmp ? sexpr_int(root, "domain/domid") : -1;
        return 0;
71 72 73 74 75
    }
}

/*****************************************************************
 ******
P
Philipp Hahn 已提交
76
 ****** Parsing of S-Expression into virDomainDef objects
77 78 79 80
 ******
 *****************************************************************/

/**
P
Philipp Hahn 已提交
81
 * xenParseSxprOS:
82 83
 * @node: the root of the parsed S-Expression
 * @def: the domain config
P
Philipp Hahn 已提交
84
 * @hvm: true or 1 if node contains HVM S-Expression
85 86 87 88 89 90
 *
 * Parse the xend sexp for description of os and append it to buf.
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
M
Markus Groß 已提交
91 92 93
xenParseSxprOS(const struct sexpr *node,
               virDomainDefPtr def,
               int hvm)
94 95
{
    if (hvm) {
96
        if (VIR_ALLOC(def->os.loader) < 0)
97
            goto error;
98 99 100 101
        if (sexpr_node_copy(node, "domain/image/hvm/loader", &def->os.loader->path) < 0)
            goto error;
        if (def->os.loader->path == NULL) {
            if (sexpr_node_copy(node, "domain/image/hvm/kernel", &def->os.loader->path) < 0)
102
                goto error;
103

104
            if (def->os.loader->path == NULL) {
105 106
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("domain information incomplete, missing HVM loader"));
107
                return -1;
108 109 110
            }
        } else {
            if (sexpr_node_copy(node, "domain/image/hvm/kernel", &def->os.kernel) < 0)
111
                goto error;
112
            if (sexpr_node_copy(node, "domain/image/hvm/ramdisk", &def->os.initrd) < 0)
113
                goto error;
114
            if (sexpr_node_copy(node, "domain/image/hvm/args", &def->os.cmdline) < 0)
115
                goto error;
116
            if (sexpr_node_copy(node, "domain/image/hvm/root", &def->os.root) < 0)
117
                goto error;
118 119 120
        }
    } else {
        if (sexpr_node_copy(node, "domain/image/linux/kernel", &def->os.kernel) < 0)
121
            goto error;
122
        if (sexpr_node_copy(node, "domain/image/linux/ramdisk", &def->os.initrd) < 0)
123
            goto error;
124
        if (sexpr_node_copy(node, "domain/image/linux/args", &def->os.cmdline) < 0)
125
            goto error;
126
        if (sexpr_node_copy(node, "domain/image/linux/root", &def->os.root) < 0)
127
            goto error;
128 129 130 131 132
    }

    /* If HVM kenrel == loader, then old xend, so kill off kernel */
    if (hvm &&
        def->os.kernel &&
133
        STREQ(def->os.kernel, def->os.loader->path)) {
134 135
        VIR_FREE(def->os.kernel);
    }
136 137 138 139 140 141
    /* Drop kernel argument that has no value */
    if (hvm &&
        def->os.kernel && *def->os.kernel == '\0' &&
        def->os.loader) {
        VIR_FREE(def->os.kernel);
    }
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    if (!def->os.kernel &&
        hvm) {
        const char *boot = sexpr_node(node, "domain/image/hvm/boot");
        if ((boot != NULL) && (boot[0] != 0)) {
            while (*boot &&
                   def->os.nBootDevs < VIR_DOMAIN_BOOT_LAST) {
                if (*boot == 'a')
                    def->os.bootDevs[def->os.nBootDevs++] = VIR_DOMAIN_BOOT_FLOPPY;
                else if (*boot == 'c')
                    def->os.bootDevs[def->os.nBootDevs++] = VIR_DOMAIN_BOOT_DISK;
                else if (*boot == 'd')
                    def->os.bootDevs[def->os.nBootDevs++] = VIR_DOMAIN_BOOT_CDROM;
                else if (*boot == 'n')
                    def->os.bootDevs[def->os.nBootDevs++] = VIR_DOMAIN_BOOT_NET;
                boot++;
            }
        }
    }

    if (!hvm &&
        !def->os.kernel &&
        !def->os.bootloader) {
165 166
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing kernel & bootloader"));
167 168 169 170 171
        return -1;
    }

    return 0;

172
 error:
173 174 175
    return -1;
}

P
Philipp Hahn 已提交
176 177 178 179 180 181 182 183 184 185

/**
  * xenParseSxprChar:
  * @value: A string describing a character device.
  * @tty: the console pty path
  *
  * Parse the xend S-expression for description of a character device.
  *
  * Returns a character device object or NULL in case of failure.
  */
186
virDomainChrDefPtr
M
Markus Groß 已提交
187 188
xenParseSxprChar(const char *value,
                 const char *tty)
189 190 191 192 193
{
    const char *prefix;
    char *tmp;
    virDomainChrDefPtr def;

M
Michal Novotny 已提交
194
    if (!(def = virDomainChrDefNew()))
195 196 197 198 199 200
        return NULL;

    prefix = value;

    if (value[0] == '/') {
        def->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
201 202
        if (VIR_STRDUP(def->source.data.file.path, value) < 0)
            goto error;
203 204 205 206 207 208 209 210 211 212 213
    } else {
        if ((tmp = strchr(value, ':')) != NULL) {
            *tmp = '\0';
            value = tmp + 1;
        }

        if (STRPREFIX(prefix, "telnet")) {
            def->source.type = VIR_DOMAIN_CHR_TYPE_TCP;
            def->source.data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
        } else {
            if ((def->source.type = virDomainChrTypeFromString(prefix)) < 0) {
214 215
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown chr device type '%s'"), prefix);
216 217 218 219 220 221 222
                goto error;
            }
        }
    }

    switch (def->source.type) {
    case VIR_DOMAIN_CHR_TYPE_PTY:
223 224
        if (VIR_STRDUP(def->source.data.file.path, tty) < 0)
            goto error;
225 226 227 228
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
229 230
        if (VIR_STRDUP(def->source.data.file.path, value) < 0)
            goto error;
231 232 233 234 235 236 237 238
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
    {
        const char *offset = strchr(value, ':');
        const char *offset2;

        if (offset == NULL) {
239 240
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("malformed char device string"));
241 242 243 244
            goto error;
        }

        if (offset != value &&
245 246
            VIR_STRNDUP(def->source.data.tcp.host, value, offset - value) < 0)
            goto error;
247 248

        offset2 = strchr(offset, ',');
249 250
        offset++;
        if (VIR_STRNDUP(def->source.data.tcp.service, offset,
251
                        offset2 ? offset2 - offset : -1) < 0)
252
            goto error;
253 254 255 256 257 258 259 260 261 262 263 264

        if (offset2 && strstr(offset2, ",server"))
            def->source.data.tcp.listen = true;
    }
    break;

    case VIR_DOMAIN_CHR_TYPE_UDP:
    {
        const char *offset = strchr(value, ':');
        const char *offset2, *offset3;

        if (offset == NULL) {
265 266
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("malformed char device string"));
267 268 269 270
            goto error;
        }

        if (offset != value &&
271 272
            VIR_STRNDUP(def->source.data.udp.connectHost, value, offset - value) < 0)
            goto error;
273 274 275

        offset2 = strchr(offset, '@');
        if (offset2 != NULL) {
276 277 278
            if (VIR_STRNDUP(def->source.data.udp.connectService,
                            offset + 1, offset2 - offset - 1) < 0)
                goto error;
279 280 281

            offset3 = strchr(offset2, ':');
            if (offset3 == NULL) {
282 283
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("malformed char device string"));
284 285 286 287
                goto error;
            }

            if (offset3 > (offset2 + 1) &&
288 289 290
                VIR_STRNDUP(def->source.data.udp.bindHost,
                            offset2 + 1, offset3 - offset2 - 1) < 0)
                goto error;
291

292 293
            if (VIR_STRDUP(def->source.data.udp.bindService, offset3 + 1) < 0)
                goto error;
294
        } else {
295 296
            if (VIR_STRDUP(def->source.data.udp.connectService, offset + 1) < 0)
                goto error;
297 298 299 300 301 302 303
        }
    }
    break;

    case VIR_DOMAIN_CHR_TYPE_UNIX:
    {
        const char *offset = strchr(value, ',');
304
        if (VIR_STRNDUP(def->source.data.nix.path, value,
305
                        offset ? offset - value : -1) < 0)
306
            goto error;
307 308 309 310 311 312 313 314 315 316

        if (offset != NULL &&
            strstr(offset, ",server") != NULL)
            def->source.data.nix.listen = true;
    }
    break;
    }

    return def;

317
 error:
318 319 320 321
    virDomainChrDefFree(def);
    return NULL;
}

P
Philipp Hahn 已提交
322

323
/**
P
Philipp Hahn 已提交
324 325 326 327
 * xenParseSxprDisks:
 * @def: the domain config
 * @root: root S-expression
 * @hvm: true or 1 if node contains HVM S-Expression
328 329
 * @xendConfigVersion: version of xend
 *
P
Philipp Hahn 已提交
330
 * This parses out block devices from the domain S-expression
331 332 333 334
 *
 * Returns 0 if successful or -1 if failed.
 */
static int
M
Markus Groß 已提交
335 336 337 338
xenParseSxprDisks(virDomainDefPtr def,
                  const struct sexpr *root,
                  int hvm,
                  int xendConfigVersion)
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
    const struct sexpr *cur, *node;
    virDomainDiskDefPtr disk = NULL;

    for (cur = root; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        node = cur->u.s.car;
        /* Normally disks are in a (device (vbd ...)) block
           but blktap disks ended up in a differently named
           (device (tap ....)) block.... */
        if (sexpr_lookup(node, "device/vbd") ||
            sexpr_lookup(node, "device/tap") ||
            sexpr_lookup(node, "device/tap2")) {
            char *offset;
            const char *src = NULL;
            const char *dst = NULL;
            const char *mode = NULL;
P
Philipp Hahn 已提交
355
            const char *bootable = NULL;
356 357 358 359 360 361

            /* Again dealing with (vbd...) vs (tap ...) differences */
            if (sexpr_lookup(node, "device/vbd")) {
                src = sexpr_node(node, "device/vbd/uname");
                dst = sexpr_node(node, "device/vbd/dev");
                mode = sexpr_node(node, "device/vbd/mode");
P
Philipp Hahn 已提交
362
                bootable = sexpr_node(node, "device/vbd/bootable");
363 364 365 366
            } else if (sexpr_lookup(node, "device/tap2")) {
                src = sexpr_node(node, "device/tap2/uname");
                dst = sexpr_node(node, "device/tap2/dev");
                mode = sexpr_node(node, "device/tap2/mode");
P
Philipp Hahn 已提交
367
                bootable = sexpr_node(node, "device/tap2/bootable");
368 369 370 371
            } else {
                src = sexpr_node(node, "device/tap/uname");
                dst = sexpr_node(node, "device/tap/dev");
                mode = sexpr_node(node, "device/tap/mode");
P
Philipp Hahn 已提交
372
                bootable = sexpr_node(node, "device/tap/bootable");
373 374
            }

375
            if (!(disk = virDomainDiskDefNew(NULL)))
376
                goto error;
377 378

            if (dst == NULL) {
379 380
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("domain information incomplete, vbd has no dev"));
381 382 383 384 385 386 387 388 389
                goto error;
            }

            if (src == NULL) {
                /* There is a case without the uname to the CD-ROM device */
                offset = strchr(dst, ':');
                if (!offset ||
                    !hvm ||
                    STRNEQ(offset, ":cdrom")) {
390 391
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("domain information incomplete, vbd has no src"));
392 393 394 395 396 397 398
                    goto error;
                }
            }

            if (src != NULL) {
                offset = strchr(src, ':');
                if (!offset) {
399 400
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("cannot parse vbd filename, missing driver name"));
401 402 403
                    goto error;
                }

P
Philipp Hahn 已提交
404 405
                if (sexpr_lookup(node, "device/tap2") &&
                    STRPREFIX(src, "tap:")) {
406
                    if (virDomainDiskSetDriver(disk, "tap2") < 0)
407
                        goto error;
P
Philipp Hahn 已提交
408
                } else {
409 410
                    char *tmp;
                    if (VIR_STRNDUP(tmp, src, offset - src) < 0)
411
                        goto error;
412 413
                    if (virDomainDiskSetDriver(disk, tmp) < 0) {
                        VIR_FREE(tmp);
P
Philipp Hahn 已提交
414 415
                        goto error;
                    }
416
                    VIR_FREE(tmp);
417 418 419 420
                }

                src = offset + 1;

421 422
                if (STREQ(virDomainDiskGetDriver(disk), "tap") ||
                    STREQ(virDomainDiskGetDriver(disk), "tap2")) {
423 424
                    char *driverType = NULL;

425 426
                    offset = strchr(src, ':');
                    if (!offset) {
427 428
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       "%s", _("cannot parse vbd filename, missing driver type"));
429 430 431
                        goto error;
                    }

432 433
                    if (VIR_STRNDUP(driverType, src, offset - src) < 0)
                        goto error;
434
                    if (STREQ(driverType, "aio"))
435
                        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
436
                    else
437 438
                        virDomainDiskSetFormat(disk,
                                               virStorageFileFormatTypeFromString(driverType));
439
                    VIR_FREE(driverType);
440
                    if (virDomainDiskGetFormat(disk) <= 0) {
441
                        virReportError(VIR_ERR_INTERNAL_ERROR,
442
                                       _("Unknown driver type %s"), src);
443 444 445 446 447 448 449 450 451
                        goto error;
                    }

                    src = offset + 1;
                    /* Its possible to use blktap driver for block devs
                       too, but kinda pointless because blkback is better,
                       so we assume common case here. If blktap becomes
                       omnipotent, we can revisit this, perhaps stat()'ing
                       the src file in question */
E
Eric Blake 已提交
452
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
453
                } else if (STREQ(virDomainDiskGetDriver(disk), "phy")) {
E
Eric Blake 已提交
454
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
455
                } else if (STREQ(virDomainDiskGetDriver(disk), "file")) {
E
Eric Blake 已提交
456
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
457 458 459 460 461
                }
            } else {
                /* No CDROM media so can't really tell. We'll just
                   call if a FILE for now and update when media
                   is inserted later */
E
Eric Blake 已提交
462
                virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
463 464
            }

465
            if (STREQLEN(dst, "ioemu:", 6))
466 467 468 469
                dst += 6;

            disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
            /* New style disk config from Xen >= 3.0.3 */
470
            if (xendConfigVersion >= XEND_CONFIG_VERSION_3_0_3) {
471 472
                offset = strrchr(dst, ':');
                if (offset) {
473
                    if (STREQ(offset, ":cdrom")) {
474
                        disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
475
                    } else if (STREQ(offset, ":disk")) {
476 477 478 479 480 481 482 483
                        /* The default anyway */
                    } else {
                        /* Unknown, lets pretend its a disk too */
                    }
                    offset[0] = '\0';
                }
            }

484 485
            if (VIR_STRDUP(disk->dst, dst) < 0)
                goto error;
486
            if (virDomainDiskSetSource(disk, src) < 0)
487
                goto error;
488 489 490 491 492 493 494 495 496 497 498 499

            if (STRPREFIX(disk->dst, "xvd"))
                disk->bus = VIR_DOMAIN_DISK_BUS_XEN;
            else if (STRPREFIX(disk->dst, "hd"))
                disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
            else if (STRPREFIX(disk->dst, "sd"))
                disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
            else
                disk->bus = VIR_DOMAIN_DISK_BUS_IDE;

            if (mode &&
                strchr(mode, 'r'))
500
                disk->src->readonly = true;
501 502
            if (mode &&
                strchr(mode, '!'))
503
                disk->src->shared = true;
504 505

            if (VIR_REALLOC_N(def->disks, def->ndisks+1) < 0)
506
                goto error;
507

P
Philipp Hahn 已提交
508 509 510 511 512 513 514
            /* re-order disks if there is a bootable device */
            if (STREQ_NULLABLE(bootable, "1")) {
                def->disks[def->ndisks++] = def->disks[0];
                def->disks[0] = disk;
            } else {
                def->disks[def->ndisks++] = disk;
            }
515 516 517 518 519 520
            disk = NULL;
        }
    }

    return 0;

521
 error:
522 523 524 525 526
    virDomainDiskDefFree(disk);
    return -1;
}


P
Philipp Hahn 已提交
527 528 529 530 531 532 533 534 535
/**
 * xenParseSxprNets:
 * @def: the domain config
 * @root: root S-expression
 *
 * This parses out network devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
536
static int
M
Markus Groß 已提交
537 538
xenParseSxprNets(virDomainDefPtr def,
                 const struct sexpr *root)
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
{
    virDomainNetDefPtr net = NULL;
    const struct sexpr *cur, *node;
    const char *tmp;
    int vif_index = 0;

    for (cur = root; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        node = cur->u.s.car;
        if (sexpr_lookup(node, "device/vif")) {
            const char *tmp2, *model, *type;
            tmp2 = sexpr_node(node, "device/vif/script");
            tmp = sexpr_node(node, "device/vif/bridge");
            model = sexpr_node(node, "device/vif/model");
            type = sexpr_node(node, "device/vif/type");

            if (VIR_ALLOC(net) < 0)
555
                goto cleanup;
556 557 558 559 560 561

            if (tmp != NULL ||
                (tmp2 != NULL && STREQ(tmp2, DEFAULT_VIF_SCRIPT))) {
                net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
                /* XXX virtual network reverse resolve */

562 563 564 565 566
                if (VIR_STRDUP(net->data.bridge.brname, tmp) < 0)
                    goto cleanup;
                if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE &&
                    VIR_STRDUP(net->script, tmp2) < 0)
                    goto cleanup;
567
                tmp = sexpr_node(node, "device/vif/ip");
568
                if (tmp && virDomainNetAppendIpAddress(net, tmp, AF_UNSPEC, 0) < 0)
569
                    goto cleanup;
570 571
            } else {
                net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
572 573
                if (VIR_STRDUP(net->script, tmp2) < 0)
                    goto cleanup;
574
                tmp = sexpr_node(node, "device/vif/ip");
575
                if (tmp && virDomainNetAppendIpAddress(net, tmp, AF_UNSPEC, 0) < 0)
576
                    goto cleanup;
577 578 579
            }

            tmp = sexpr_node(node, "device/vif/vifname");
580 581 582 583
            /* If vifname is specified in xend config, include it in net
             * definition regardless of domain state.  If vifname is not
             * specified, only generate one if domain is active (id != -1). */
            if (tmp) {
584 585
                if (VIR_STRDUP(net->ifname, tmp) < 0)
                    goto cleanup;
586 587
            } else if (def->id != -1) {
                if (virAsprintf(&net->ifname, "vif%d.%d", def->id, vif_index) < 0)
588
                    goto cleanup;
589 590 591 592
            }

            tmp = sexpr_node(node, "device/vif/mac");
            if (tmp) {
593
                if (virMacAddrParse(tmp, &net->mac) < 0) {
594 595
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("malformed mac address '%s'"), tmp);
596 597 598 599
                    goto cleanup;
                }
            }

600 601
            if (VIR_STRDUP(net->model, model) < 0)
                goto cleanup;
602

603 604 605
            if (!model && type && STREQ(type, "netfront") &&
                VIR_STRDUP(net->model, "netfront") < 0)
                goto cleanup;
606

607
            if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
608
                goto cleanup;
609 610 611 612 613 614 615

            vif_index++;
        }
    }

    return 0;

616
 cleanup:
617 618 619 620 621
    virDomainNetDefFree(net);
    return -1;
}


P
Philipp Hahn 已提交
622 623 624 625 626 627 628 629 630
/**
 * xenParseSxprSound:
 * @def: the domain config
 * @str: comma separated list of sound models
 *
 * This parses out sound devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
631
int
M
Markus Groß 已提交
632 633
xenParseSxprSound(virDomainDefPtr def,
                  const char *str)
634 635
{
    if (STREQ(str, "all")) {
636
        size_t i;
637 638

        /*
639
         * Special compatibility code for Xen with a bogus
640 641
         * sound=all in config.
         *
E
Eric Blake 已提交
642
         * NB deliberately, don't include all possible
643 644 645 646 647 648 649 650 651 652
         * sound models anymore, just the 2 that were
         * historically present in Xen's QEMU.
         *
         * ie just es1370 + sb16.
         *
         * Hence use of MODEL_ES1370 + 1, instead of MODEL_LAST
         */

        if (VIR_ALLOC_N(def->sounds,
                        VIR_DOMAIN_SOUND_MODEL_ES1370 + 1) < 0)
653
            goto error;
654 655


656
        for (i = 0; i < (VIR_DOMAIN_SOUND_MODEL_ES1370 + 1); i++) {
657 658
            virDomainSoundDefPtr sound;
            if (VIR_ALLOC(sound) < 0)
659
                goto error;
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
            sound->model = i;
            def->sounds[def->nsounds++] = sound;
        }
    } else {
        char model[10];
        const char *offset = str, *offset2;

        do {
            int len;
            virDomainSoundDefPtr sound;
            offset2 = strchr(offset, ',');
            if (offset2)
                len = (offset2 - offset);
            else
                len = strlen(offset);
            if (virStrncpy(model, offset, len, sizeof(model)) == NULL) {
676 677 678
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Sound model %s too big for destination"),
                               offset);
679 680 681 682
                goto error;
            }

            if (VIR_ALLOC(sound) < 0)
683
                goto error;
684 685 686 687 688 689

            if ((sound->model = virDomainSoundModelTypeFromString(model)) < 0) {
                VIR_FREE(sound);
                goto error;
            }

690
            if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, sound) < 0) {
691
                virDomainSoundDefFree(sound);
692
                goto error;
693 694 695 696 697 698 699 700
            }

            offset = offset2 ? offset2 + 1 : NULL;
        } while (offset);
    }

    return 0;

701
 error:
702 703 704 705
    return -1;
}


P
Philipp Hahn 已提交
706 707 708 709 710 711 712 713 714
/**
 * xenParseSxprUSB:
 * @def: the domain config
 * @root: root S-expression
 *
 * This parses out USB devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
715
static int
M
Markus Groß 已提交
716 717
xenParseSxprUSB(virDomainDefPtr def,
                const struct sexpr *root)
718 719 720 721 722 723 724 725 726 727
{
    struct sexpr *cur, *node;
    const char *tmp;

    for (cur = sexpr_lookup(root, "domain/image/hvm"); cur && cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        node = cur->u.s.car;
        if (sexpr_lookup(node, "usbdevice")) {
            tmp = sexpr_node(node, "usbdevice");
            if (tmp && *tmp) {
                if (STREQ(tmp, "tablet") ||
728 729
                    STREQ(tmp, "mouse") ||
                    STREQ(tmp, "keyboard")) {
730 731
                    virDomainInputDefPtr input;
                    if (VIR_ALLOC(input) < 0)
732
                        goto error;
733 734 735
                    input->bus = VIR_DOMAIN_INPUT_BUS_USB;
                    if (STREQ(tmp, "tablet"))
                        input->type = VIR_DOMAIN_INPUT_TYPE_TABLET;
736
                    else if (STREQ(tmp, "mouse"))
737
                        input->type = VIR_DOMAIN_INPUT_TYPE_MOUSE;
738 739
                    else
                        input->type = VIR_DOMAIN_INPUT_TYPE_KBD;
740

741
                    if (VIR_APPEND_ELEMENT(def->inputs, def->ninputs, input) < 0) {
742
                        VIR_FREE(input);
743
                        goto error;
744 745 746 747 748 749 750 751 752
                    }
                } else {
                    /* XXX Handle other non-input USB devices later */
                }
            }
        }
    }
    return 0;

753
 error:
754 755 756
    return -1;
}

P
Philipp Hahn 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769

/*
 * xenParseSxprGraphicsOld:
 * @def: the domain config
 * @root: root S-expression
 * @hvm: true or 1 if root contains HVM S-Expression
 * @xendConfigVersion: version of xend
 * @vncport: VNC port number
 *
 * This parses out VNC devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
770
static int
M
Markus Groß 已提交
771 772 773 774
xenParseSxprGraphicsOld(virDomainDefPtr def,
                        const struct sexpr *root,
                        int hvm,
                        int xendConfigVersion, int vncport)
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
{
    const char *tmp;
    virDomainGraphicsDefPtr graphics = NULL;

    if ((tmp = sexpr_fmt_node(root, "domain/image/%s/vnc", hvm ? "hvm" : "linux")) &&
        tmp[0] == '1') {
        /* Graphics device (HVM, or old (pre-3.0.4) style PV VNC config) */
        int port;
        const char *listenAddr = sexpr_fmt_node(root, "domain/image/%s/vnclisten", hvm ? "hvm" : "linux");
        const char *vncPasswd = sexpr_fmt_node(root, "domain/image/%s/vncpasswd", hvm ? "hvm" : "linux");
        const char *keymap = sexpr_fmt_node(root, "domain/image/%s/keymap", hvm ? "hvm" : "linux");
        const char *unused = sexpr_fmt_node(root, "domain/image/%s/vncunused", hvm ? "hvm" : "linux");

        port = vncport;

        if (VIR_ALLOC(graphics) < 0)
791
            goto error;
792 793 794 795 796

        graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
        /* For Xen >= 3.0.3, don't generate a fixed port mapping
         * because it will almost certainly be wrong ! Just leave
         * it as -1 which lets caller see that the VNC server isn't
J
Ján Tomko 已提交
797
         * present yet. Subsequent dumps of the XML will eventually
798 799
         * find the port in XenStore once VNC server has started
         */
800
        if (port == -1 && xendConfigVersion < XEND_CONFIG_VERSION_3_0_3)
801 802 803
            port = 5900 + def->id;

        if ((unused && STREQ(unused, "1")) || port == -1)
804
            graphics->data.vnc.autoport = true;
805 806 807
        graphics->data.vnc.port = port;

        if (listenAddr &&
808 809
            virDomainGraphicsListenSetAddress(graphics, 0, listenAddr, -1, true))
            goto error;
810

811 812
        if (VIR_STRDUP(graphics->data.vnc.auth.passwd, vncPasswd) < 0)
            goto error;
813

814 815
        if (VIR_STRDUP(graphics->data.vnc.keymap, keymap) < 0)
            goto error;
816 817

        if (VIR_ALLOC_N(def->graphics, 1) < 0)
818
            goto error;
819 820 821 822 823 824 825 826 827 828
        def->graphics[0] = graphics;
        def->ngraphics = 1;
        graphics = NULL;
    } else if ((tmp = sexpr_fmt_node(root, "domain/image/%s/sdl", hvm ? "hvm" : "linux")) &&
               tmp[0] == '1') {
        /* Graphics device (HVM, or old (pre-3.0.4) style PV sdl config) */
        const char *display = sexpr_fmt_node(root, "domain/image/%s/display", hvm ? "hvm" : "linux");
        const char *xauth = sexpr_fmt_node(root, "domain/image/%s/xauthority", hvm ? "hvm" : "linux");

        if (VIR_ALLOC(graphics) < 0)
829
            goto error;
830 831

        graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
832 833 834 835
        if (VIR_STRDUP(graphics->data.sdl.display, display) < 0)
            goto error;
        if (VIR_STRDUP(graphics->data.sdl.xauth, xauth) < 0)
            goto error;
836 837

        if (VIR_ALLOC_N(def->graphics, 1) < 0)
838
            goto error;
839 840 841 842 843 844 845
        def->graphics[0] = graphics;
        def->ngraphics = 1;
        graphics = NULL;
    }

    return 0;

846
 error:
847 848 849 850 851
    virDomainGraphicsDefFree(graphics);
    return -1;
}


P
Philipp Hahn 已提交
852 853 854 855 856 857 858 859 860 861
/*
 * xenParseSxprGraphicsNew:
 * @def: the domain config
 * @root: root S-expression
 * @vncport: VNC port number
 *
 * This parses out VNC devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
862
static int
M
Markus Groß 已提交
863 864
xenParseSxprGraphicsNew(virDomainDefPtr def,
                        const struct sexpr *root, int vncport)
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
{
    virDomainGraphicsDefPtr graphics = NULL;
    const struct sexpr *cur, *node;
    const char *tmp;

    /* append network devices and framebuffer */
    for (cur = root; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        node = cur->u.s.car;
        if (sexpr_lookup(node, "device/vfb")) {
            /* New style graphics config for PV guests in >= 3.0.4,
             * or for HVM guests in >= 3.0.5 */
            if (sexpr_node(node, "device/vfb/type")) {
                tmp = sexpr_node(node, "device/vfb/type");
            } else if (sexpr_node(node, "device/vfb/vnc")) {
                tmp = "vnc";
            } else if (sexpr_node(node, "device/vfb/sdl")) {
                tmp = "sdl";
            } else {
                tmp = "unknown";
            }

            if (VIR_ALLOC(graphics) < 0)
887
                goto error;
888 889

            if ((graphics->type = virDomainGraphicsTypeFromString(tmp)) < 0) {
890 891
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown graphics type '%s'"), tmp);
892 893 894 895 896 897
                goto error;
            }

            if (graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                const char *display = sexpr_node(node, "device/vfb/display");
                const char *xauth = sexpr_node(node, "device/vfb/xauthority");
898 899 900 901
                if (VIR_STRDUP(graphics->data.sdl.display, display) < 0)
                    goto error;
                if (VIR_STRDUP(graphics->data.sdl.xauth, xauth) < 0)
                    goto error;
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
            } else {
                int port;
                const char *listenAddr = sexpr_node(node, "device/vfb/vnclisten");
                const char *vncPasswd = sexpr_node(node, "device/vfb/vncpasswd");
                const char *keymap = sexpr_node(node, "device/vfb/keymap");
                const char *unused = sexpr_node(node, "device/vfb/vncunused");

                port = vncport;

                /* Didn't find port entry in xenstore */
                if (port == -1) {
                    const char *str = sexpr_node(node, "device/vfb/vncdisplay");
                    int val;
                    if (str != NULL && virStrToLong_i(str, NULL, 0, &val) == 0)
                        port = val;
                }

                if ((unused && STREQ(unused, "1")) || port == -1)
920
                    graphics->data.vnc.autoport = true;
921 922 923 924 925 926

                if (port >= 0 && port < 5900)
                    port += 5900;
                graphics->data.vnc.port = port;

                if (listenAddr &&
927 928
                    virDomainGraphicsListenSetAddress(graphics, 0, listenAddr, -1, true))
                    goto error;
929

930 931
                if (VIR_STRDUP(graphics->data.vnc.auth.passwd, vncPasswd) < 0)
                    goto error;
932

933 934
                if (VIR_STRDUP(graphics->data.vnc.keymap, keymap) < 0)
                    goto error;
935 936 937
            }

            if (VIR_ALLOC_N(def->graphics, 1) < 0)
938
                goto error;
939 940 941 942 943 944 945 946 947
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
            break;
        }
    }

    return 0;

948
 error:
949 950 951 952
    virDomainGraphicsDefFree(graphics);
    return -1;
}

P
Philipp Hahn 已提交
953

954
/**
P
Philipp Hahn 已提交
955 956
 * xenParseSxprPCI:
 * @def: the domain config
957 958
 * @root: root sexpr
 *
P
Philipp Hahn 已提交
959
 * This parses out PCI devices from the domain sexpr
960 961 962 963
 *
 * Returns 0 if successful or -1 if failed.
 */
static int
M
Markus Groß 已提交
964 965
xenParseSxprPCI(virDomainDefPtr def,
                const struct sexpr *root)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
{
    const struct sexpr *cur, *tmp = NULL, *node;
    virDomainHostdevDefPtr dev = NULL;

    /*
     * With the (domain ...) block we have the following odd setup
     *
     * (device
     *    (pci
     *       (dev (domain 0x0000) (bus 0x00) (slot 0x1b) (func 0x0))
     *       (dev (domain 0x0000) (bus 0x00) (slot 0x13) (func 0x0))
     *    )
     * )
     *
     * Normally there is one (device ...) block per device, but in
E
Eric Blake 已提交
981
     * weird world of Xen PCI, once (device ...) covers multiple
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
     * devices.
     */

    for (cur = root; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        node = cur->u.s.car;
        if ((tmp = sexpr_lookup(node, "device/pci")) != NULL)
            break;
    }

    if (!tmp)
        return 0;

    for (cur = tmp; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
        const char *domain = NULL;
        const char *bus = NULL;
        const char *slot = NULL;
        const char *func = NULL;
        int domainID;
        int busID;
        int slotID;
        int funcID;

        node = cur->u.s.car;
        if (!sexpr_lookup(node, "dev"))
            continue;

        if (!(domain = sexpr_node(node, "dev/domain"))) {
1009 1010
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI domain"));
1011 1012 1013
            goto error;
        }
        if (!(bus = sexpr_node(node, "dev/bus"))) {
1014 1015
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI bus"));
1016 1017 1018
            goto error;
        }
        if (!(slot = sexpr_node(node, "dev/slot"))) {
1019 1020
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI slot"));
1021 1022 1023
            goto error;
        }
        if (!(func = sexpr_node(node, "dev/func"))) {
1024 1025
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI func"));
1026 1027 1028 1029
            goto error;
        }

        if (virStrToLong_i(domain, NULL, 0, &domainID) < 0) {
1030 1031
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI domain '%s'"), domain);
1032 1033 1034
            goto error;
        }
        if (virStrToLong_i(bus, NULL, 0, &busID) < 0) {
1035 1036
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI bus '%s'"), bus);
1037 1038 1039
            goto error;
        }
        if (virStrToLong_i(slot, NULL, 0, &slotID) < 0) {
1040 1041
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI slot '%s'"), slot);
1042 1043 1044
            goto error;
        }
        if (virStrToLong_i(func, NULL, 0, &funcID) < 0) {
1045 1046
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI func '%s'"), func);
1047 1048 1049
            goto error;
        }

1050 1051
        if (!(dev = virDomainHostdevDefAlloc()))
           goto error;
1052 1053

        dev->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
1054
        dev->managed = false;
1055
        dev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
1056 1057 1058 1059
        dev->source.subsys.u.pci.addr.domain = domainID;
        dev->source.subsys.u.pci.addr.bus = busID;
        dev->source.subsys.u.pci.addr.slot = slotID;
        dev->source.subsys.u.pci.addr.function = funcID;
1060

1061
        if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, dev) < 0)
1062
            goto error;
1063 1064 1065 1066
    }

    return 0;

1067
 error:
1068 1069 1070 1071 1072 1073
    virDomainHostdevDefFree(dev);
    return -1;
}


/**
M
Markus Groß 已提交
1074
 * xenParseSxpr:
1075 1076 1077
 * @root: the root of the parsed S-Expression
 * @xendConfigVersion: version of xend
 * @cpus: set of cpus the domain may be pinned to
P
Philipp Hahn 已提交
1078 1079
 * @tty: the console pty path
 * @vncport: VNC port number
1080
 *
P
Philipp Hahn 已提交
1081 1082
 * Parse the xend S-expression description and turn it into a virDomainDefPtr
 * representing these settings as closely as is practical.
1083
 *
P
Philipp Hahn 已提交
1084 1085
 * Returns the domain config or NULL in case of error.
 *         The caller must free() the returned value.
1086 1087
 */
virDomainDefPtr
M
Markus Groß 已提交
1088 1089 1090
xenParseSxpr(const struct sexpr *root,
             int xendConfigVersion,
             const char *cpus, char *tty, int vncport)
1091 1092 1093
{
    const char *tmp;
    virDomainDefPtr def;
P
Philipp Hahn 已提交
1094
    int hvm = 0, vmlocaltime;
1095

1096
    if (!(def = virDomainDefNew()))
1097
        goto error;
1098 1099

    tmp = sexpr_node(root, "domain/domid");
1100
    if (tmp == NULL && xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) { /* domid was mandatory */
1101 1102
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing id"));
1103 1104 1105 1106 1107 1108 1109 1110 1111
        goto error;
    }
    def->virtType = VIR_DOMAIN_VIRT_XEN;
    if (tmp)
        def->id = sexpr_int(root, "domain/domid");
    else
        def->id = -1;

    if (sexpr_node_copy(root, "domain/name", &def->name) < 0)
1112
        goto error;
1113
    if (def->name == NULL) {
1114 1115
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing name"));
1116 1117 1118 1119 1120
        goto error;
    }

    tmp = sexpr_node(root, "domain/uuid");
    if (tmp == NULL) {
1121 1122
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing name"));
1123 1124
        goto error;
    }
1125 1126
    if (virUUIDParse(tmp, def->uuid) < 0)
        goto error;
1127 1128

    if (sexpr_node_copy(root, "domain/description", &def->description) < 0)
1129
        goto error;
1130 1131 1132 1133 1134

    hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0;
    if (!hvm) {
        if (sexpr_node_copy(root, "domain/bootloader",
                            &def->os.bootloader) < 0)
1135
            goto error;
1136 1137 1138

        if (!def->os.bootloader &&
            sexpr_has(root, "domain/bootloader") &&
1139 1140
            VIR_STRDUP(def->os.bootloader, "") < 0)
            goto error;
1141 1142 1143 1144

        if (def->os.bootloader &&
            sexpr_node_copy(root, "domain/bootloader_args",
                            &def->os.bootloaderArgs) < 0)
1145
            goto error;
1146 1147
    }

1148
    def->os.type = (hvm ? VIR_DOMAIN_OSTYPE_HVM : VIR_DOMAIN_OSTYPE_LINUX);
1149 1150 1151

    if (def->id != 0) {
        if (sexpr_lookup(root, "domain/image")) {
M
Markus Groß 已提交
1152
            if (xenParseSxprOS(root, def, hvm) < 0)
1153 1154 1155 1156
                goto error;
        }
    }

1157
    virDomainDefSetMemoryTotal(def, (sexpr_u64(root, "domain/maxmem") << 10));
1158
    def->mem.cur_balloon = (sexpr_u64(root, "domain/memory") << 10);
1159 1160 1161

    if (def->mem.cur_balloon > virDomainDefGetMemoryActual(def))
        def->mem.cur_balloon = virDomainDefGetMemoryActual(def);
1162 1163

    if (cpus != NULL) {
H
Hu Tao 已提交
1164
        if (virBitmapParse(cpus, 0, &def->cpumask,
1165
                           VIR_DOMAIN_CPUMASK_LEN) < 0)
1166
            goto error;
1167 1168 1169 1170 1171 1172 1173

        if (virBitmapIsAllClear(def->cpumask)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Invalid value of 'cpumask': %s"),
                           cpus);
            goto error;
        }
1174 1175
    }

1176 1177
    if (virDomainDefSetVcpusMax(def, sexpr_int(root, "domain/vcpus")) < 0)
        goto error;
1178 1179 1180 1181 1182 1183 1184
    def->vcpus = count_one_bits_l(sexpr_u64(root, "domain/vcpu_avail"));
    if (!def->vcpus || def->maxvcpus < def->vcpus)
        def->vcpus = def->maxvcpus;

    tmp = sexpr_node(root, "domain/on_poweroff");
    if (tmp != NULL) {
        if ((def->onPoweroff = virDomainLifecycleTypeFromString(tmp)) < 0) {
1185 1186
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1187 1188
            goto error;
        }
1189
    } else {
1190
        def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
1191
    }
1192 1193 1194 1195

    tmp = sexpr_node(root, "domain/on_reboot");
    if (tmp != NULL) {
        if ((def->onReboot = virDomainLifecycleTypeFromString(tmp)) < 0) {
1196 1197
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1198 1199
            goto error;
        }
1200
    } else {
1201
        def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
1202
    }
1203 1204 1205 1206

    tmp = sexpr_node(root, "domain/on_crash");
    if (tmp != NULL) {
        if ((def->onCrash = virDomainLifecycleCrashTypeFromString(tmp)) < 0) {
1207 1208
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1209 1210
            goto error;
        }
1211
    } else {
1212
        def->onCrash = VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY;
1213
    }
1214 1215 1216

    if (hvm) {
        if (sexpr_int(root, "domain/image/hvm/acpi"))
J
Ján Tomko 已提交
1217
            def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_TRISTATE_SWITCH_ON;
1218
        if (sexpr_int(root, "domain/image/hvm/apic"))
J
Ján Tomko 已提交
1219
            def->features[VIR_DOMAIN_FEATURE_APIC] = VIR_TRISTATE_SWITCH_ON;
1220
        if (sexpr_int(root, "domain/image/hvm/pae"))
J
Ján Tomko 已提交
1221
            def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_TRISTATE_SWITCH_ON;
1222
        if (sexpr_int(root, "domain/image/hvm/hap"))
J
Ján Tomko 已提交
1223
            def->features[VIR_DOMAIN_FEATURE_HAP] = VIR_TRISTATE_SWITCH_ON;
1224
        if (sexpr_int(root, "domain/image/hvm/viridian"))
J
Ján Tomko 已提交
1225
            def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_TRISTATE_SWITCH_ON;
P
Philipp Hahn 已提交
1226
    }
1227

P
Philipp Hahn 已提交
1228 1229 1230 1231 1232 1233
    /* 12aaf4a2486b (3.0.3) added a second low-priority 'localtime' setting */
    vmlocaltime = sexpr_int(root, "domain/localtime");
    if (hvm) {
        const char *value = sexpr_node(root, "domain/image/hvm/localtime");
        if (value) {
            if (virStrToLong_i(value, NULL, 0, &vmlocaltime) < 0) {
1234 1235
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown localtime offset %s"), value);
P
Philipp Hahn 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
                goto error;
            }
        }
        /* only managed HVM domains since 3.1.0 have persistent rtc_timeoffset */
        if (xendConfigVersion < XEND_CONFIG_VERSION_3_1_0) {
            if (vmlocaltime)
                def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME;
            else
                def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
            def->clock.data.utc_reset = true;
        } else {
            int rtc_offset;
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
            rtc_offset =  sexpr_int(root, "domain/image/hvm/rtc_timeoffset");
            def->clock.data.variable.adjustment = rtc_offset;
            def->clock.data.variable.basis = vmlocaltime ?
                VIR_DOMAIN_CLOCK_BASIS_LOCALTIME :
                VIR_DOMAIN_CLOCK_BASIS_UTC;
        }
1255 1256 1257 1258 1259

        if (sexpr_lookup(root, "domain/image/hvm/hpet")) {
            virDomainTimerDefPtr timer;

            if (VIR_ALLOC_N(def->clock.timers, 1) < 0 ||
1260
                VIR_ALLOC(timer) < 0)
1261 1262 1263 1264 1265
                goto error;

            timer->name = VIR_DOMAIN_TIMER_NAME_HPET;
            timer->present = sexpr_int(root, "domain/image/hvm/hpet");
            timer->tickpolicy = -1;
1266 1267
            timer->mode = -1;
            timer->track = -1;
1268 1269 1270 1271

            def->clock.ntimers = 1;
            def->clock.timers[0] = timer;
        }
P
Philipp Hahn 已提交
1272 1273 1274 1275
    } else {
        const char *value = sexpr_node(root, "domain/image/linux/localtime");
        if (value) {
            if (virStrToLong_i(value, NULL, 0, &vmlocaltime) < 0) {
1276 1277
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown localtime offset %s"), value);
P
Philipp Hahn 已提交
1278 1279 1280 1281 1282
                goto error;
            }
        }
        /* PV domains do not have an emulated RTC and the offset is fixed. */
        if (vmlocaltime)
1283
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME;
P
Philipp Hahn 已提交
1284 1285 1286 1287
        else
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
        def->clock.data.utc_reset = true;
    } /* !hvm */
1288 1289 1290 1291 1292

    if (sexpr_node_copy(root, hvm ?
                        "domain/image/hvm/device_model" :
                        "domain/image/linux/device_model",
                        &def->emulator) < 0)
1293
        goto error;
1294 1295

    /* append block devices */
M
Markus Groß 已提交
1296
    if (xenParseSxprDisks(def, root, hvm, xendConfigVersion) < 0)
1297 1298
        goto error;

M
Markus Groß 已提交
1299
    if (xenParseSxprNets(def, root) < 0)
1300 1301
        goto error;

M
Markus Groß 已提交
1302
    if (xenParseSxprPCI(def, root) < 0)
1303 1304 1305
        goto error;

    /* New style graphics device config */
M
Markus Groß 已提交
1306
    if (xenParseSxprGraphicsNew(def, root, vncport) < 0)
1307 1308 1309 1310
        goto error;

    /* Graphics device (HVM <= 3.0.4, or PV <= 3.0.3) vnc config */
    if ((def->ngraphics == 0) &&
M
Markus Groß 已提交
1311
        xenParseSxprGraphicsOld(def, root, hvm, xendConfigVersion,
1312 1313 1314 1315 1316 1317
                                      vncport) < 0)
        goto error;


    /* Old style cdrom config from Xen <= 3.0.2 */
    if (hvm &&
1318
        xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
1319 1320 1321
        tmp = sexpr_node(root, "domain/image/hvm/cdrom");
        if ((tmp != NULL) && (tmp[0] != 0)) {
            virDomainDiskDefPtr disk;
1322
            if (!(disk = virDomainDiskDefNew(NULL)))
1323
                goto error;
1324
            if (virDomainDiskSetSource(disk, tmp) < 0) {
1325
                virDomainDiskDefFree(disk);
1326
                goto error;
1327
            }
E
Eric Blake 已提交
1328
            virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
1329
            disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
1330
            if (VIR_STRDUP(disk->dst, "hdc") < 0) {
1331
                virDomainDiskDefFree(disk);
1332
                goto error;
1333
            }
1334
            if (virDomainDiskSetDriver(disk, "file") < 0) {
1335
                virDomainDiskDefFree(disk);
1336
                goto error;
1337 1338
            }
            disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
1339
            disk->src->readonly = true;
1340

1341
            if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) {
1342
                virDomainDiskDefFree(disk);
1343
                goto error;
1344 1345 1346 1347 1348 1349 1350 1351
            }
        }
    }


    /* Floppy disk config */
    if (hvm) {
        const char *const fds[] = { "fda", "fdb" };
1352
        size_t i;
1353
        for (i = 0; i < ARRAY_CARDINALITY(fds); i++) {
1354 1355 1356
            tmp = sexpr_fmt_node(root, "domain/image/hvm/%s", fds[i]);
            if ((tmp != NULL) && (tmp[0] != 0)) {
                virDomainDiskDefPtr disk;
1357
                if (!(disk = virDomainDiskDefNew(NULL)))
1358
                    goto error;
1359
                if (virDomainDiskSetSource(disk, tmp) < 0) {
1360
                    virDomainDiskDefFree(disk);
1361
                    goto error;
1362
                }
E
Eric Blake 已提交
1363
                virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
1364
                disk->device = VIR_DOMAIN_DISK_DEVICE_FLOPPY;
1365
                if (VIR_STRDUP(disk->dst, fds[i]) < 0) {
1366
                    virDomainDiskDefFree(disk);
1367
                    goto error;
1368
                }
1369
                if (virDomainDiskSetSource(disk, "file") < 0) {
1370
                    virDomainDiskDefFree(disk);
1371
                    goto error;
1372 1373 1374
                }
                disk->bus = VIR_DOMAIN_DISK_BUS_FDC;

1375
                if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) {
1376
                    virDomainDiskDefFree(disk);
1377
                    goto error;
1378 1379 1380 1381 1382 1383 1384
                }
            }
        }
    }

    /* in case of HVM we have USB device emulation */
    if (hvm &&
M
Markus Groß 已提交
1385
        xenParseSxprUSB(def, root) < 0)
1386 1387 1388 1389
        goto error;

    /* Character device config */
    if (hvm) {
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        const struct sexpr *serial_root;
        bool have_multiple_serials = false;

        serial_root = sexpr_lookup(root, "domain/image/hvm/serial");
        if (serial_root) {
            const struct sexpr *cur, *node, *cur2;
            int ports_skipped = 0;

            for (cur = serial_root; cur->kind == SEXPR_CONS; cur = cur->u.s.cdr) {
                node = cur->u.s.car;

                for (cur2 = node; cur2->kind == SEXPR_CONS; cur2 = cur2->u.s.cdr) {
                    tmp = cur2->u.s.car->u.value;

                    if (tmp && STRNEQ(tmp, "none")) {
                        virDomainChrDefPtr chr;
                        if ((chr = xenParseSxprChar(tmp, tty)) == NULL)
                            goto error;
1408 1409 1410
                        chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                        chr->target.port = def->nserials + ports_skipped;
                        if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
1411
                            virDomainChrDefFree(chr);
1412
                            goto error;
1413 1414 1415 1416 1417 1418 1419
                        }
                    }
                    else
                        ports_skipped++;

                    have_multiple_serials = true;
                }
1420 1421
            }
        }
1422 1423 1424 1425 1426 1427 1428

        if (!have_multiple_serials) {
            tmp = sexpr_node(root, "domain/image/hvm/serial");
            if (tmp && STRNEQ(tmp, "none")) {
                virDomainChrDefPtr chr;
                if ((chr = xenParseSxprChar(tmp, tty)) == NULL)
                    goto error;
1429 1430 1431
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = 0;
                if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
1432
                    virDomainChrDefFree(chr);
1433
                    goto error;
1434 1435 1436 1437
                }
            }
        }

1438 1439 1440 1441
        tmp = sexpr_node(root, "domain/image/hvm/parallel");
        if (tmp && STRNEQ(tmp, "none")) {
            virDomainChrDefPtr chr;
            /* XXX does XenD stuff parallel port tty info into xenstore somewhere ? */
M
Markus Groß 已提交
1442
            if ((chr = xenParseSxprChar(tmp, NULL)) == NULL)
1443
                goto error;
1444 1445 1446
            chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
            chr->target.port = 0;
            if (VIR_APPEND_ELEMENT(def->parallels, def->nparallels, chr) < 0) {
1447
                virDomainChrDefFree(chr);
1448
                goto error;
1449 1450
            }
        }
1451
    } else if (def->id != 0) {
1452
        if (VIR_ALLOC_N(def->consoles, 1) < 0)
1453
            goto error;
1454
        def->nconsoles = 1;
1455
        /* Fake a paravirt console, since that's not in the sexpr */
1456
        if (!(def->consoles[0] = xenParseSxprChar("pty", tty)))
1457
            goto error;
1458 1459 1460
        def->consoles[0]->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_CONSOLE;
        def->consoles[0]->target.port = 0;
        def->consoles[0]->targetType = VIR_DOMAIN_CHR_CONSOLE_TARGET_TYPE_XEN;
1461 1462 1463 1464 1465 1466 1467 1468
    }
    VIR_FREE(tty);


    /* Sound device config */
    if (hvm &&
        (tmp = sexpr_node(root, "domain/image/hvm/soundhw")) != NULL &&
        *tmp) {
M
Markus Groß 已提交
1469
        if (xenParseSxprSound(def, tmp) < 0)
1470 1471 1472 1473 1474
            goto error;
    }

    return def;

1475
 error:
1476 1477 1478 1479 1480
    VIR_FREE(tty);
    virDomainDefFree(def);
    return NULL;
}

P
Philipp Hahn 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494

/**
 * xenParseSxprString:
 * @sexpr: the root of the parsed S-Expression
 * @xendConfigVersion: version of xend
 * @tty: the console pty path
 * @vncport: VNC port number
 *
 * Parse the xend S-expression description and turn it into a virDomainDefPtr
 * representing these settings as closely as is practical.
 *
 * Returns the domain config or NULL in case of error.
 *         The caller must free() the returned value.
 */
1495
virDomainDefPtr
M
Markus Groß 已提交
1496
xenParseSxprString(const char *sexpr,
1497 1498 1499 1500 1501
                   int xendConfigVersion,
                   char *tty,
                   int vncport,
                   virCapsPtr caps,
                   virDomainXMLOptionPtr xmlopt)
1502 1503 1504 1505 1506 1507 1508
{
    struct sexpr *root = string2sexpr(sexpr);
    virDomainDefPtr def;

    if (!root)
        return NULL;

1509 1510
    if (!(def = xenParseSxpr(root, xendConfigVersion, NULL, tty, vncport)))
        goto cleanup;
1511

1512 1513 1514 1515 1516 1517 1518
    if (virDomainDefPostParse(def, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE,
                              xmlopt) < 0) {
        virDomainDefFree(def);
        def = NULL;
    }

 cleanup:
1519 1520 1521
    sexpr_free(root);

    return def;
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
}

/************************************************************************
 *                                                                      *
 * Converter functions to go from the XML tree to an S-Expr for Xen     *
 *                                                                      *
 ************************************************************************/


/**
P
Philipp Hahn 已提交
1532 1533 1534
 * xenFormatSxprGraphicsNew:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
1535
 *
P
Philipp Hahn 已提交
1536 1537
 * Convert the graphics part of the domain description into a S-expression
 * in buf. (HVM > 3.0.4 or PV > 3.0.3)
1538 1539 1540 1541
 *
 * Returns 0 in case of success, -1 in case of error
 */
static int
M
Markus Groß 已提交
1542 1543
xenFormatSxprGraphicsNew(virDomainGraphicsDefPtr def,
                         virBufferPtr buf)
1544
{
1545 1546
    const char *listenAddr;

1547 1548
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_SDL &&
        def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
1549 1550 1551
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected graphics type %d"),
                       def->type);
1552 1553 1554 1555 1556 1557 1558 1559 1560
        return -1;
    }

    virBufferAddLit(buf, "(device (vkbd))");
    virBufferAddLit(buf, "(device (vfb ");

    if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
        virBufferAddLit(buf, "(type sdl)");
        if (def->data.sdl.display)
1561
            virBufferAsprintf(buf, "(display '%s')", def->data.sdl.display);
1562
        if (def->data.sdl.xauth)
1563
            virBufferAsprintf(buf, "(xauthority '%s')", def->data.sdl.xauth);
1564 1565 1566 1567 1568 1569
    } else if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
        virBufferAddLit(buf, "(type vnc)");
        if (def->data.vnc.autoport) {
            virBufferAddLit(buf, "(vncunused 1)");
        } else {
            virBufferAddLit(buf, "(vncunused 0)");
1570
            virBufferAsprintf(buf, "(vncdisplay %d)", def->data.vnc.port-5900);
1571 1572
        }

1573 1574 1575
        listenAddr = virDomainGraphicsListenGetAddress(def, 0);
        if (listenAddr)
            virBufferAsprintf(buf, "(vnclisten '%s')", listenAddr);
1576
        if (def->data.vnc.auth.passwd)
1577
            virBufferAsprintf(buf, "(vncpasswd '%s')", def->data.vnc.auth.passwd);
1578
        if (def->data.vnc.keymap)
1579
            virBufferAsprintf(buf, "(keymap '%s')", def->data.vnc.keymap);
1580 1581 1582 1583 1584 1585 1586 1587
    }

    virBufferAddLit(buf, "))");

    return 0;
}


P
Philipp Hahn 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
/**
 * xenFormatSxprGraphicsOld:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 * @xendConfigVersion: version of xend
 *
 * Convert the graphics part of the domain description into a S-expression
 * in buf. (HVM <= 3.0.4 or PV <= 3.0.3)
 *
 * Returns 0 in case of success, -1 in case of error
 */
1599
static int
M
Markus Groß 已提交
1600 1601 1602
xenFormatSxprGraphicsOld(virDomainGraphicsDefPtr def,
                         virBufferPtr buf,
                         int xendConfigVersion)
1603
{
1604 1605
    const char *listenAddr;

1606 1607
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_SDL &&
        def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
1608 1609 1610
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected graphics type %d"),
                       def->type);
1611 1612 1613 1614 1615 1616
        return -1;
    }

    if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
        virBufferAddLit(buf, "(sdl 1)");
        if (def->data.sdl.display)
1617
            virBufferAsprintf(buf, "(display '%s')", def->data.sdl.display);
1618
        if (def->data.sdl.xauth)
1619
            virBufferAsprintf(buf, "(xauthority '%s')", def->data.sdl.xauth);
1620 1621
    } else if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
        virBufferAddLit(buf, "(vnc 1)");
1622
        if (xendConfigVersion >= XEND_CONFIG_VERSION_3_0_3) {
1623 1624 1625 1626
            if (def->data.vnc.autoport) {
                virBufferAddLit(buf, "(vncunused 1)");
            } else {
                virBufferAddLit(buf, "(vncunused 0)");
1627
                virBufferAsprintf(buf, "(vncdisplay %d)", def->data.vnc.port-5900);
1628 1629
            }

1630 1631 1632
            listenAddr = virDomainGraphicsListenGetAddress(def, 0);
            if (listenAddr)
                virBufferAsprintf(buf, "(vnclisten '%s')", listenAddr);
1633
            if (def->data.vnc.auth.passwd)
1634
                virBufferAsprintf(buf, "(vncpasswd '%s')", def->data.vnc.auth.passwd);
1635
            if (def->data.vnc.keymap)
1636
                virBufferAsprintf(buf, "(keymap '%s')", def->data.vnc.keymap);
1637 1638 1639 1640 1641 1642 1643

        }
    }

    return 0;
}

P
Philipp Hahn 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

/**
 * xenFormatSxprChr:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 *
 * Convert the character device part of the domain config into a S-expression
 * in buf.
 *
 * Returns 0 in case of success, -1 in case of error
 */
1655
int
M
Markus Groß 已提交
1656 1657
xenFormatSxprChr(virDomainChrDefPtr def,
                 virBufferPtr buf)
1658 1659 1660 1661
{
    const char *type = virDomainChrTypeToString(def->source.type);

    if (!type) {
1662 1663
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected chr device type"));
1664 1665 1666 1667 1668 1669 1670 1671
        return -1;
    }

    switch (def->source.type) {
    case VIR_DOMAIN_CHR_TYPE_NULL:
    case VIR_DOMAIN_CHR_TYPE_STDIO:
    case VIR_DOMAIN_CHR_TYPE_VC:
    case VIR_DOMAIN_CHR_TYPE_PTY:
1672
        virBufferAdd(buf, type, -1);
1673 1674 1675 1676
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
1677
        virBufferAsprintf(buf, "%s:", type);
1678 1679 1680 1681 1682 1683 1684 1685
        virBufferEscapeSexpr(buf, "%s", def->source.data.file.path);
        break;

    case VIR_DOMAIN_CHR_TYPE_DEV:
        virBufferEscapeSexpr(buf, "%s", def->source.data.file.path);
        break;

    case VIR_DOMAIN_CHR_TYPE_TCP:
1686
        virBufferAsprintf(buf, "%s:%s:%s%s",
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
                          (def->source.data.tcp.protocol
                           == VIR_DOMAIN_CHR_TCP_PROTOCOL_RAW ?
                           "tcp" : "telnet"),
                          (def->source.data.tcp.host ?
                           def->source.data.tcp.host : ""),
                          (def->source.data.tcp.service ?
                           def->source.data.tcp.service : ""),
                          (def->source.data.tcp.listen ?
                           ",server,nowait" : ""));
        break;

    case VIR_DOMAIN_CHR_TYPE_UDP:
1699
        virBufferAsprintf(buf, "%s:%s:%s@%s:%s", type,
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
                          (def->source.data.udp.connectHost ?
                           def->source.data.udp.connectHost : ""),
                          (def->source.data.udp.connectService ?
                           def->source.data.udp.connectService : ""),
                          (def->source.data.udp.bindHost ?
                           def->source.data.udp.bindHost : ""),
                          (def->source.data.udp.bindService ?
                           def->source.data.udp.bindService : ""));
        break;

    case VIR_DOMAIN_CHR_TYPE_UNIX:
1711
        virBufferAsprintf(buf, "%s:", type);
1712 1713 1714 1715
        virBufferEscapeSexpr(buf, "%s", def->source.data.nix.path);
        if (def->source.data.nix.listen)
            virBufferAddLit(buf, ",server,nowait");
        break;
1716 1717

    default:
1718 1719
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unsupported chr device type '%s'"), type);
1720
        return -1;
1721 1722
    }

1723
    if (virBufferCheckError(buf) < 0)
1724 1725 1726 1727 1728 1729 1730
        return -1;

    return 0;
}


/**
P
Philipp Hahn 已提交
1731 1732 1733 1734
 * xenFormatSxprDisk:
 * @node: node containing the disk description
 * @buf: a buffer for the result S-expression
 * @hvm: true or 1 if domain is HVM
1735
 * @xendConfigVersion: xend configuration file format
P
Philipp Hahn 已提交
1736
 * @isAttach: create expression for device attach (1).
1737
 *
P
Philipp Hahn 已提交
1738
 * Convert the disk device part of the domain config into a S-expresssion in buf.
1739 1740 1741 1742
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1743
xenFormatSxprDisk(virDomainDiskDefPtr def,
M
Markus Groß 已提交
1744 1745 1746 1747
                  virBufferPtr buf,
                  int hvm,
                  int xendConfigVersion,
                  int isAttach)
1748
{
1749 1750 1751
    const char *src = virDomainDiskGetSource(def);
    const char *driver = virDomainDiskGetDriver(def);

1752 1753 1754 1755 1756 1757
    /* Xend (all versions) put the floppy device config
     * under the hvm (image (os)) block
     */
    if (hvm &&
        def->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY) {
        if (isAttach) {
1758
            virReportError(VIR_ERR_INVALID_ARG,
1759
                           _("Cannot directly attach floppy %s"), src);
1760 1761 1762 1763 1764 1765 1766 1767
            return -1;
        }
        return 0;
    }

    /* Xend <= 3.0.2 doesn't include cdrom config here */
    if (hvm &&
        def->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
1768
        xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
1769
        if (isAttach) {
1770
            virReportError(VIR_ERR_INVALID_ARG,
1771
                           _("Cannot directly attach CDROM %s"), src);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
            return -1;
        }
        return 0;
    }

    if (!isAttach)
        virBufferAddLit(buf, "(device ");

    /* Normally disks are in a (device (vbd ...)) block
     * but blktap disks ended up in a differently named
     * (device (tap ....)) block.... */
1783
    if (STREQ_NULLABLE(driver, "tap")) {
1784
        virBufferAddLit(buf, "(tap ");
1785
    } else if (STREQ_NULLABLE(driver, "tap2")) {
1786 1787 1788 1789 1790 1791 1792
        virBufferAddLit(buf, "(tap2 ");
    } else {
        virBufferAddLit(buf, "(vbd ");
    }

    if (hvm) {
        /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */
1793
        if (xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
1794 1795 1796 1797
            virBufferEscapeSexpr(buf, "(dev 'ioemu:%s')", def->dst);
        } else {
            /* But newer does not */
            virBufferEscapeSexpr(buf, "(dev '%s:", def->dst);
1798
            virBufferAsprintf(buf, "%s')",
1799 1800 1801 1802 1803 1804 1805 1806 1807
                              def->device == VIR_DOMAIN_DISK_DEVICE_CDROM ?
                              "cdrom" : "disk");
        }
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
        virBufferEscapeSexpr(buf, "(dev '%s:cdrom')", def->dst);
    } else {
        virBufferEscapeSexpr(buf, "(dev '%s')", def->dst);
    }

1808 1809 1810 1811
    if (src) {
        if (driver) {
            if (STREQ(driver, "tap") ||
                STREQ(driver, "tap2")) {
1812
                const char *type;
1813
                int format = virDomainDiskGetFormat(def);
1814

1815
                if (!format || format == VIR_STORAGE_FILE_RAW)
1816
                    type = "aio";
1817
                else
1818 1819
                    type = virStorageFileFormatTypeToString(format);
                virBufferEscapeSexpr(buf, "(uname '%s:", driver);
1820
                virBufferEscapeSexpr(buf, "%s:", type);
1821
                virBufferEscapeSexpr(buf, "%s')", src);
1822
            } else {
1823 1824
                virBufferEscapeSexpr(buf, "(uname '%s:", driver);
                virBufferEscapeSexpr(buf, "%s')", src);
1825 1826
            }
        } else {
1827 1828
            int type = virDomainDiskGetType(def);

E
Eric Blake 已提交
1829
            if (type == VIR_STORAGE_TYPE_FILE) {
1830
                virBufferEscapeSexpr(buf, "(uname 'file:%s')", src);
E
Eric Blake 已提交
1831
            } else if (type == VIR_STORAGE_TYPE_BLOCK) {
1832 1833
                if (src[0] == '/')
                    virBufferEscapeSexpr(buf, "(uname 'phy:%s')", src);
1834 1835
                else
                    virBufferEscapeSexpr(buf, "(uname 'phy:/dev/%s')",
1836
                                         src);
1837
            } else {
1838 1839
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported disk type %s"),
E
Eric Blake 已提交
1840
                               virStorageTypeToString(type));
1841 1842 1843 1844 1845
                return -1;
            }
        }
    }

1846
    if (def->src->readonly)
1847
        virBufferAddLit(buf, "(mode 'r')");
1848
    else if (def->src->shared)
1849 1850 1851
        virBufferAddLit(buf, "(mode 'w!')");
    else
        virBufferAddLit(buf, "(mode 'w')");
1852
    if (def->transient) {
1853
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1854
                       _("transient disks not supported yet"));
1855 1856
        return -1;
    }
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866

    if (!isAttach)
        virBufferAddLit(buf, ")");

    virBufferAddLit(buf, ")");

    return 0;
}

/**
P
Philipp Hahn 已提交
1867 1868 1869 1870 1871
 * xenFormatSxprNet:
 * @conn: connection
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 * @hvm: true or 1 if domain is HVM
1872
 * @xendConfigVersion: xend configuration file format
P
Philipp Hahn 已提交
1873
 * @isAttach: create expression for device attach (1).
1874
 *
P
Philipp Hahn 已提交
1875
 * Convert the interface description of the domain config into a S-expression in buf.
1876 1877 1878 1879 1880 1881 1882
 * This is a temporary interface as the S-Expr interface
 * will be replaced by XML-RPC in the future. However the XML format should
 * stay valid over time.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
M
Markus Groß 已提交
1883 1884 1885 1886 1887 1888
xenFormatSxprNet(virConnectPtr conn,
                 virDomainNetDefPtr def,
                 virBufferPtr buf,
                 int hvm,
                 int xendConfigVersion,
                 int isAttach)
1889 1890
{
    const char *script = DEFAULT_VIF_SCRIPT;
1891
    char macaddr[VIR_MAC_STRING_BUFLEN];
1892 1893 1894 1895

    if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
        def->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
        def->type != VIR_DOMAIN_NET_TYPE_ETHERNET) {
1896 1897
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network type %d"), def->type);
1898 1899
        return -1;
    }
1900 1901 1902
    if (def->script &&
        def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
        def->type != VIR_DOMAIN_NET_TYPE_ETHERNET) {
1903 1904 1905
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("scripts are not supported on interfaces of type %s"),
                       virDomainNetTypeToString(def->type));
1906 1907
        return -1;
    }
1908 1909 1910 1911 1912 1913

    if (!isAttach)
        virBufferAddLit(buf, "(device ");

    virBufferAddLit(buf, "(vif ");

1914
    virBufferAsprintf(buf, "(mac '%s')", virMacAddrFormat(&def->mac, macaddr));
1915 1916 1917 1918

    switch (def->type) {
    case VIR_DOMAIN_NET_TYPE_BRIDGE:
        virBufferEscapeSexpr(buf, "(bridge '%s')", def->data.bridge.brname);
1919 1920
        if (def->script)
            script = def->script;
1921 1922

        virBufferEscapeSexpr(buf, "(script '%s')", script);
1923
        if (def->nips == 1) {
1924 1925 1926
            char *ipStr = virSocketAddrFormat(&def->ips[0]->address);
            virBufferEscapeSexpr(buf, "(ip '%s')", ipStr);
            VIR_FREE(ipStr);
1927 1928 1929 1930
        } else if (def->nips > 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Driver does not support setting multiple IP addresses"));
            return -1;
1931
        }
1932 1933 1934 1935 1936 1937 1938 1939 1940
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
    {
        virNetworkPtr network =
            virNetworkLookupByName(conn, def->data.network.name);
        char *bridge;

        if (!network) {
1941 1942
            virReportError(VIR_ERR_NO_NETWORK, "%s",
                           def->data.network.name);
1943 1944 1945 1946
            return -1;
        }

        bridge = virNetworkGetBridgeName(network);
1947
        virObjectUnref(network);
1948
        if (!bridge) {
1949 1950 1951
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("network %s is not active"),
                           def->data.network.name);
1952 1953 1954 1955 1956 1957 1958 1959 1960
            return -1;
        }
        virBufferEscapeSexpr(buf, "(bridge '%s')", bridge);
        virBufferEscapeSexpr(buf, "(script '%s')", script);
        VIR_FREE(bridge);
    }
    break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
1961
        if (def->script)
1962
            virBufferEscapeSexpr(buf, "(script '%s')",
1963
                                 def->script);
1964
        if (def->nips == 1) {
1965 1966 1967
            char *ipStr = virSocketAddrFormat(&def->ips[0]->address);
            virBufferEscapeSexpr(buf, "(ip '%s')", ipStr);
            VIR_FREE(ipStr);
1968 1969 1970 1971
        } else if (def->nips > 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Driver does not support setting multiple IP addresses"));
            return -1;
1972
        }
1973 1974
        break;

M
Michele Paolino 已提交
1975
    case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
1976 1977 1978 1979
    case VIR_DOMAIN_NET_TYPE_USER:
    case VIR_DOMAIN_NET_TYPE_SERVER:
    case VIR_DOMAIN_NET_TYPE_CLIENT:
    case VIR_DOMAIN_NET_TYPE_MCAST:
1980
    case VIR_DOMAIN_NET_TYPE_UDP:
1981 1982
    case VIR_DOMAIN_NET_TYPE_INTERNAL:
    case VIR_DOMAIN_NET_TYPE_DIRECT:
1983
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
    case VIR_DOMAIN_NET_TYPE_LAST:
        break;
    }

    if (def->ifname != NULL &&
        !STRPREFIX(def->ifname, "vif"))
        virBufferEscapeSexpr(buf, "(vifname '%s')", def->ifname);

    if (!hvm) {
        if (def->model != NULL)
            virBufferEscapeSexpr(buf, "(model '%s')", def->model);
1995
    } else {
1996 1997
        if (def->model != NULL && STREQ(def->model, "netfront")) {
            virBufferAddLit(buf, "(type netfront)");
1998
        } else {
1999
            if (def->model != NULL)
2000 2001 2002 2003 2004
                virBufferEscapeSexpr(buf, "(model '%s')", def->model);
            /*
             * apparently (type ioemu) breaks paravirt drivers on HVM so skip
             * this from XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU
             */
2005
            if (xendConfigVersion <= XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU)
2006 2007
                virBufferAddLit(buf, "(type ioemu)");
        }
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
    }

    if (!isAttach)
        virBufferAddLit(buf, ")");

    virBufferAddLit(buf, ")");

    return 0;
}


P
Philipp Hahn 已提交
2019 2020 2021 2022 2023 2024 2025 2026 2027
/**
 * xenFormatSxprPCI:
 * @def: the device config
 * @buf: a buffer for the result S-expression
 *
 * Convert a single PCI device part of the domain config into a S-expresssion in buf.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
2028
static void
M
Markus Groß 已提交
2029 2030
xenFormatSxprPCI(virDomainHostdevDefPtr def,
                 virBufferPtr buf)
2031
{
2032
    virBufferAsprintf(buf, "(dev (domain 0x%04x)(bus 0x%02x)(slot 0x%02x)(func 0x%x))",
2033 2034 2035 2036
                      def->source.subsys.u.pci.addr.domain,
                      def->source.subsys.u.pci.addr.bus,
                      def->source.subsys.u.pci.addr.slot,
                      def->source.subsys.u.pci.addr.function);
2037 2038
}

P
Philipp Hahn 已提交
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049

/**
 * xenFormatSxprOnePCI:
 * @def: the device config
 * @buf: a buffer for the result S-expression
 * @detach: create expression for device detach (1).
 *
 * Convert a single PCI device part of the domain config into a S-expresssion in buf.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
2050
int
M
Markus Groß 已提交
2051 2052 2053
xenFormatSxprOnePCI(virDomainHostdevDefPtr def,
                    virBufferPtr buf,
                    int detach)
2054 2055
{
    if (def->managed) {
2056 2057
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("managed PCI devices not supported with XenD"));
2058 2059 2060 2061
        return -1;
    }

    virBufferAddLit(buf, "(pci ");
M
Markus Groß 已提交
2062
    xenFormatSxprPCI(def, buf);
2063 2064 2065 2066 2067 2068 2069 2070 2071
    if (detach)
        virBufferAddLit(buf, "(state 'Closing')");
    else
        virBufferAddLit(buf, "(state 'Initialising')");
    virBufferAddLit(buf, ")");

    return 0;
}

P
Philipp Hahn 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081

/**
 * xenFormatSxprAllPCI:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 *
 * Convert all PCI device parts of the domain config into a S-expresssion in buf.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
2082
static int
M
Markus Groß 已提交
2083 2084
xenFormatSxprAllPCI(virDomainDefPtr def,
                    virBufferPtr buf)
2085 2086
{
    int hasPCI = 0;
2087
    size_t i;
2088

2089
    for (i = 0; i < def->nhostdevs; i++)
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            def->hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
            hasPCI = 1;

    if (!hasPCI)
        return 0;

    /*
     * With the (domain ...) block we have the following odd setup
     *
     * (device
     *    (pci
     *       (dev (domain 0x0000) (bus 0x00) (slot 0x1b) (func 0x0))
     *       (dev (domain 0x0000) (bus 0x00) (slot 0x13) (func 0x0))
     *    )
     * )
     *
     * Normally there is one (device ...) block per device, but in the
     * weird world of Xen PCI, one (device ...) covers multiple devices.
     */

    virBufferAddLit(buf, "(device (pci ");
2112
    for (i = 0; i < def->nhostdevs; i++) {
2113 2114 2115
        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            def->hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
            if (def->hostdevs[i]->managed) {
2116 2117
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("managed PCI devices not supported with XenD"));
2118 2119 2120
                return -1;
            }

M
Markus Groß 已提交
2121
            xenFormatSxprPCI(def->hostdevs[i], buf);
2122 2123 2124 2125 2126 2127 2128
        }
    }
    virBufferAddLit(buf, "))");

    return 0;
}

P
Philipp Hahn 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138

/**
 * xenFormatSxprSound:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 *
 * Convert all sound device parts of the domain config into S-expression in buf.
 *
 * Returns 0 if successful or -1 if failed.
 */
2139
int
M
Markus Groß 已提交
2140 2141
xenFormatSxprSound(virDomainDefPtr def,
                   virBufferPtr buf)
2142 2143
{
    const char *str;
2144
    size_t i;
2145

2146
    for (i = 0; i < def->nsounds; i++) {
2147
        if (!(str = virDomainSoundModelTypeToString(def->sounds[i]->model))) {
2148 2149 2150
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected sound model %d"),
                           def->sounds[i]->model);
2151 2152 2153 2154 2155 2156 2157
            return -1;
        }
        if (i)
            virBufferAddChar(buf, ',');
        virBufferEscapeSexpr(buf, "%s", str);
    }

2158
    if (virBufferCheckError(buf) < 0)
2159 2160 2161 2162 2163 2164
        return -1;

    return 0;
}


P
Philipp Hahn 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173
/**
 * xenFormatSxprInput:
 * @input: the input config
 * @buf: a buffer for the result S-expression
 *
 * Convert all input device parts of the domain config into S-expression in buf.
 *
 * Returns 0 if successful or -1 if failed.
 */
2174
static int
M
Markus Groß 已提交
2175 2176
xenFormatSxprInput(virDomainInputDefPtr input,
                   virBufferPtr buf)
2177 2178 2179 2180 2181
{
    if (input->bus != VIR_DOMAIN_INPUT_BUS_USB)
        return 0;

    if (input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
2182 2183
        input->type != VIR_DOMAIN_INPUT_TYPE_TABLET &&
        input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
2184 2185
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected input type %d"), input->type);
2186 2187 2188
        return -1;
    }

2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    switch (input->type) {
        case VIR_DOMAIN_INPUT_TYPE_MOUSE:
            virBufferAsprintf(buf, "(usbdevice %s)", "mouse");
            break;
        case VIR_DOMAIN_INPUT_TYPE_TABLET:
            virBufferAsprintf(buf, "(usbdevice %s)", "tablet");
            break;
        case VIR_DOMAIN_INPUT_TYPE_KBD:
            virBufferAsprintf(buf, "(usbdevice %s)", "keyboard");
            break;
    }
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209

    return 0;
}


/* Computing the vcpu_avail bitmask works because MAX_VIRT_CPUS is
   either 32, or 64 on a platform where long is big enough.  */
verify(MAX_VIRT_CPUS <= sizeof(1UL) * CHAR_BIT);

/**
M
Markus Groß 已提交
2210
 * xenFormatSxpr:
2211 2212 2213 2214
 * @conn: pointer to the hypervisor connection
 * @def: domain config definition
 * @xendConfigVersion: xend configuration file format
 *
P
Philipp Hahn 已提交
2215
 * Generate an S-expression representing the domain configuration.
2216 2217 2218 2219 2220
 *
 * Returns the 0 terminated S-Expr string or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
M
Markus Groß 已提交
2221 2222 2223
xenFormatSxpr(virConnectPtr conn,
              virDomainDefPtr def,
              int xendConfigVersion)
2224 2225 2226 2227 2228
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    const char *tmp;
    char *bufout;
2229 2230
    int hvm = 0, vmlocaltime = -1;
    size_t i;
P
Philipp Hahn 已提交
2231
    bool in_image = false;
2232

2233
    VIR_DEBUG("Formatting domain sexpr");
2234 2235 2236

    virBufferAddLit(&buf, "(vm ");
    virBufferEscapeSexpr(&buf, "(name '%s')", def->name);
2237
    virBufferAsprintf(&buf, "(memory %llu)(maxmem %llu)",
2238
                      VIR_DIV_UP(def->mem.cur_balloon, 1024),
2239
                      VIR_DIV_UP(virDomainDefGetMemoryActual(def), 1024));
2240
    virBufferAsprintf(&buf, "(vcpus %u)", def->maxvcpus);
2241 2242
    /* Computing the vcpu_avail bitmask works because MAX_VIRT_CPUS is
       either 32, or 64 on a platform where long is big enough.  */
2243
    if (virDomainDefHasVcpusOffline(def))
2244
        virBufferAsprintf(&buf, "(vcpu_avail %lu)", (1UL << def->vcpus) - 1);
2245 2246

    if (def->cpumask) {
H
Hu Tao 已提交
2247
        char *ranges = virBitmapFormat(def->cpumask);
2248 2249 2250 2251 2252 2253 2254
        if (ranges == NULL)
            goto error;
        virBufferEscapeSexpr(&buf, "(cpus '%s')", ranges);
        VIR_FREE(ranges);
    }

    virUUIDFormat(def->uuid, uuidstr);
2255
    virBufferAsprintf(&buf, "(uuid '%s')", uuidstr);
2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270

    if (def->description)
        virBufferEscapeSexpr(&buf, "(description '%s')", def->description);

    if (def->os.bootloader) {
        if (def->os.bootloader[0])
            virBufferEscapeSexpr(&buf, "(bootloader '%s')", def->os.bootloader);
        else
            virBufferAddLit(&buf, "(bootloader)");

        if (def->os.bootloaderArgs)
            virBufferEscapeSexpr(&buf, "(bootloader_args '%s')", def->os.bootloaderArgs);
    }

    if (!(tmp = virDomainLifecycleTypeToString(def->onPoweroff))) {
2271 2272
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onPoweroff);
2273 2274
        goto error;
    }
2275
    virBufferAsprintf(&buf, "(on_poweroff '%s')", tmp);
2276 2277

    if (!(tmp = virDomainLifecycleTypeToString(def->onReboot))) {
2278 2279
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onReboot);
2280 2281
        goto error;
    }
2282
    virBufferAsprintf(&buf, "(on_reboot '%s')", tmp);
2283 2284

    if (!(tmp = virDomainLifecycleCrashTypeToString(def->onCrash))) {
2285 2286
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onCrash);
2287 2288
        goto error;
    }
2289
    virBufferAsprintf(&buf, "(on_crash '%s')", tmp);
2290

2291
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
P
Philipp Hahn 已提交
2292
        hvm = 1;
2293 2294 2295 2296 2297 2298

    if (!def->os.bootloader) {
        if (hvm)
            virBufferAddLit(&buf, "(image (hvm ");
        else
            virBufferAddLit(&buf, "(image (linux ");
P
Philipp Hahn 已提交
2299
        in_image = true;
2300 2301 2302

        if (hvm &&
            def->os.loader == NULL) {
2303
            virReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
2304
                           "%s", _("no HVM domain loader"));
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
            goto error;
        }

        if (def->os.kernel)
            virBufferEscapeSexpr(&buf, "(kernel '%s')", def->os.kernel);
        if (def->os.initrd)
            virBufferEscapeSexpr(&buf, "(ramdisk '%s')", def->os.initrd);
        if (def->os.root)
            virBufferEscapeSexpr(&buf, "(root '%s')", def->os.root);
        if (def->os.cmdline)
            virBufferEscapeSexpr(&buf, "(args '%s')", def->os.cmdline);

        if (hvm) {
            char bootorder[VIR_DOMAIN_BOOT_LAST+1];
            if (def->os.kernel)
2320
                virBufferEscapeSexpr(&buf, "(loader '%s')", def->os.loader->path);
2321
            else
2322
                virBufferEscapeSexpr(&buf, "(kernel '%s')", def->os.loader->path);
2323

2324
            virBufferAsprintf(&buf, "(vcpus %u)", def->maxvcpus);
2325
            if (virDomainDefHasVcpusOffline(def))
2326
                virBufferAsprintf(&buf, "(vcpu_avail %lu)",
2327 2328
                                  (1UL << def->vcpus) - 1);

2329
            for (i = 0; i < def->os.nBootDevs; i++) {
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
                switch (def->os.bootDevs[i]) {
                case VIR_DOMAIN_BOOT_FLOPPY:
                    bootorder[i] = 'a';
                    break;
                default:
                case VIR_DOMAIN_BOOT_DISK:
                    bootorder[i] = 'c';
                    break;
                case VIR_DOMAIN_BOOT_CDROM:
                    bootorder[i] = 'd';
                    break;
                case VIR_DOMAIN_BOOT_NET:
                    bootorder[i] = 'n';
                    break;
                }
            }
            if (def->os.nBootDevs == 0) {
                bootorder[0] = 'c';
                bootorder[1] = '\0';
            } else {
                bootorder[def->os.nBootDevs] = '\0';
            }
2352
            virBufferAsprintf(&buf, "(boot %s)", bootorder);
2353 2354

            /* some disk devices are defined here */
2355
            for (i = 0; i < def->ndisks; i++) {
2356 2357
                const char *src = virDomainDiskGetSource(def->disks[i]);

2358 2359 2360
                switch (def->disks[i]->device) {
                case VIR_DOMAIN_DISK_DEVICE_CDROM:
                    /* Only xend <= 3.0.2 wants cdrom config here */
2361
                    if (xendConfigVersion != XEND_CONFIG_VERSION_3_0_2)
2362
                        break;
2363
                    if (STRNEQ(def->disks[i]->dst, "hdc") || !src)
2364 2365
                        break;

2366
                    virBufferEscapeSexpr(&buf, "(cdrom '%s')", src);
2367 2368 2369 2370 2371
                    break;

                case VIR_DOMAIN_DISK_DEVICE_FLOPPY:
                    /* all xend versions define floppies here */
                    virBufferEscapeSexpr(&buf, "(%s ", def->disks[i]->dst);
2372
                    virBufferEscapeSexpr(&buf, "'%s')", src);
2373 2374 2375 2376 2377 2378 2379
                    break;

                default:
                    break;
                }
            }

J
Ján Tomko 已提交
2380
            if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_TRISTATE_SWITCH_ON)
2381
                virBufferAddLit(&buf, "(acpi 1)");
J
Ján Tomko 已提交
2382
            if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_TRISTATE_SWITCH_ON)
2383
                virBufferAddLit(&buf, "(apic 1)");
J
Ján Tomko 已提交
2384
            if (def->features[VIR_DOMAIN_FEATURE_PAE] == VIR_TRISTATE_SWITCH_ON)
2385
                virBufferAddLit(&buf, "(pae 1)");
J
Ján Tomko 已提交
2386
            if (def->features[VIR_DOMAIN_FEATURE_HAP] == VIR_TRISTATE_SWITCH_ON)
2387
                virBufferAddLit(&buf, "(hap 1)");
J
Ján Tomko 已提交
2388
            if (def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] == VIR_TRISTATE_SWITCH_ON)
2389
                virBufferAddLit(&buf, "(viridian 1)");
2390 2391 2392

            virBufferAddLit(&buf, "(usb 1)");

2393
            for (i = 0; i < def->ninputs; i++)
M
Markus Groß 已提交
2394
                if (xenFormatSxprInput(def->inputs[i], &buf) < 0)
2395 2396 2397 2398
                    goto error;

            if (def->parallels) {
                virBufferAddLit(&buf, "(parallel ");
M
Markus Groß 已提交
2399
                if (xenFormatSxprChr(def->parallels[0], &buf) < 0)
2400 2401 2402 2403 2404 2405
                    goto error;
                virBufferAddLit(&buf, ")");
            } else {
                virBufferAddLit(&buf, "(parallel none)");
            }
            if (def->serials) {
2406
                if ((def->nserials > 1) || (def->serials[0]->target.port != 0)) {
2407 2408
                    int maxport = -1, port;
                    size_t j = 0;
2409 2410 2411 2412 2413 2414

                    virBufferAddLit(&buf, "(serial (");
                    for (i = 0; i < def->nserials; i++)
                        if (def->serials[i]->target.port > maxport)
                            maxport = def->serials[i]->target.port;

2415
                    for (port = 0; port <= maxport; port++) {
2416 2417
                        virDomainChrDefPtr chr = NULL;

2418
                        if (port)
2419 2420
                            virBufferAddLit(&buf, " ");
                        for (j = 0; j < def->nserials; j++) {
2421
                            if (def->serials[j]->target.port == port) {
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
                                chr = def->serials[j];
                                break;
                            }
                        }
                        if (chr) {
                            if (xenFormatSxprChr(chr, &buf) < 0)
                                goto error;
                        } else {
                            virBufferAddLit(&buf, "none");
                        }
                    }
                    virBufferAddLit(&buf, "))");
2434
                } else {
2435 2436 2437 2438 2439
                    virBufferAddLit(&buf, "(serial ");
                    if (xenFormatSxprChr(def->serials[0], &buf) < 0)
                        goto error;
                    virBufferAddLit(&buf, ")");
                }
2440 2441 2442 2443 2444 2445
            } else {
                virBufferAddLit(&buf, "(serial none)");
            }

            if (def->sounds) {
                virBufferAddLit(&buf, "(soundhw '");
M
Markus Groß 已提交
2446
                if (xenFormatSxprSound(def, &buf) < 0)
2447 2448 2449
                    goto error;
                virBufferAddLit(&buf, "')");
            }
P
Philipp Hahn 已提交
2450
        } /* hvm */
2451 2452

        /* get the device emulation model */
2453
        if (def->emulator && (hvm || xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4))
2454 2455
            virBufferEscapeSexpr(&buf, "(device_model '%s')", def->emulator);

2456 2457 2458 2459 2460 2461 2462 2463 2464
        /* look for HPET in order to override the hypervisor/xend default */
        for (i = 0; i < def->clock.ntimers; i++) {
            if (def->clock.timers[i]->name == VIR_DOMAIN_TIMER_NAME_HPET &&
                def->clock.timers[i]->present != -1) {
                virBufferAsprintf(&buf, "(hpet %d)",
                                  def->clock.timers[i]->present);
                break;
            }
        }
2465

2466 2467
        /* PV graphics for xen <= 3.0.4, or HVM graphics */
        if (hvm || (xendConfigVersion < XEND_CONFIG_MIN_VERS_PVFB_NEWCONF)) {
2468
            if ((def->ngraphics == 1) &&
M
Markus Groß 已提交
2469 2470
                xenFormatSxprGraphicsOld(def->graphics[0],
                                         &buf, xendConfigVersion) < 0)
2471 2472
                goto error;
        }
2473 2474 2475
    } else {
        /* PV domains accept kernel cmdline args */
        if (def->os.cmdline) {
P
Philipp Hahn 已提交
2476 2477
            virBufferEscapeSexpr(&buf, "(image (linux (args '%s')", def->os.cmdline);
            in_image = true;
2478
        }
P
Philipp Hahn 已提交
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
    } /* os.bootloader */


    if (xendConfigVersion < XEND_CONFIG_VERSION_3_1_0) {
        /* <3.1: UTC and LOCALTIME */
        switch (def->clock.offset) {
        case VIR_DOMAIN_CLOCK_OFFSET_UTC:
            vmlocaltime = 0;
            break;
        case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
            vmlocaltime = 1;
            break;
        default:
2492 2493 2494
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
            goto error;
        }
    } else {
        if (!in_image) {
            if (hvm)
                virBufferAddLit(&buf, "(image (hvm ");
            else
                virBufferAddLit(&buf, "(image (linux ");
            in_image = true;
        }
        if (hvm) {
            /* >=3.1 HV: VARIABLE */
            int rtc_timeoffset;
            switch (def->clock.offset) {
            case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE:
                vmlocaltime = (int)def->clock.data.variable.basis;
                rtc_timeoffset = def->clock.data.variable.adjustment;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_UTC:
                if (def->clock.data.utc_reset) {
2515
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2516
                                   _("unsupported clock adjustment='reset'"));
P
Philipp Hahn 已提交
2517 2518 2519 2520 2521 2522 2523
                    goto error;
                }
                vmlocaltime = 0;
                rtc_timeoffset = 0;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
                if (def->clock.data.utc_reset) {
2524
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
2525
                                   _("unsupported clock adjustment='reset'"));
P
Philipp Hahn 已提交
2526 2527 2528 2529 2530 2531
                    goto error;
                }
                vmlocaltime = 1;
                rtc_timeoffset = 0;
                break;
            default:
2532 2533 2534
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
                goto error;
            }
            virBufferAsprintf(&buf, "(rtc_timeoffset %d)", rtc_timeoffset);
        } else {
            /* >=3.1 PV: UTC and LOCALTIME */
            switch (def->clock.offset) {
            case VIR_DOMAIN_CLOCK_OFFSET_UTC:
                vmlocaltime = 0;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
                vmlocaltime = 1;
                break;
            default:
2548 2549 2550
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
2551 2552 2553 2554 2555
                goto error;
            }
        } /* !hvm */
        /* default post-XenD-3.1 location: */
        virBufferAsprintf(&buf, "(localtime %d)", vmlocaltime);
2556
    }
P
Philipp Hahn 已提交
2557 2558 2559 2560 2561 2562 2563 2564
    if (in_image) {
        /* closes (image(hvm|linux */
        virBufferAddLit(&buf, "))");
        in_image = false;
    }
    /* pre-XenD-3.1 and compatibility location */
    virBufferAsprintf(&buf, "(localtime %d)", vmlocaltime);

2565

2566
    for (i = 0; i < def->ndisks; i++)
2567
        if (xenFormatSxprDisk(def->disks[i],
M
Markus Groß 已提交
2568
                              &buf, hvm, xendConfigVersion, 0) < 0)
2569 2570
            goto error;

2571
    for (i = 0; i < def->nnets; i++)
M
Markus Groß 已提交
2572 2573
        if (xenFormatSxprNet(conn, def->nets[i],
                             &buf, hvm, xendConfigVersion, 0) < 0)
2574 2575
            goto error;

M
Markus Groß 已提交
2576
    if (xenFormatSxprAllPCI(def, &buf) < 0)
2577 2578
        goto error;

2579 2580
    /* New style PV graphics config xen >= 3.0.4 */
    if (!hvm && (xendConfigVersion >= XEND_CONFIG_MIN_VERS_PVFB_NEWCONF)) {
2581
        if ((def->ngraphics == 1) &&
M
Markus Groß 已提交
2582
            xenFormatSxprGraphicsNew(def->graphics[0], &buf) < 0)
2583 2584 2585 2586 2587
            goto error;
    }

    virBufferAddLit(&buf, ")"); /* closes (vm */

2588
    if (virBufferCheckError(&buf) < 0)
2589 2590 2591 2592 2593 2594
        goto error;

    bufout = virBufferContentAndReset(&buf);
    VIR_DEBUG("Formatted sexpr: \n%s", bufout);
    return bufout;

2595
 error:
2596 2597 2598
    virBufferFreeAndReset(&buf);
    return NULL;
}