xen_sxpr.c 80.6 KB
Newer Older
1 2 3
/*
 * xen_sxpr.c: Xen SEXPR parsing functions
 *
4
 * Copyright (C) 2010-2016 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
 *
 * Author: Anthony Liguori <aliguori@us.ibm.com>
 * Author: Daniel Veillard <veillard@redhat.com>
 * Author: Markus Groß <gross@univention.de>
 */

#include <config.h>

29 30
#include <regex.h>

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

J
Jim Fehlig 已提交
44 45
#define VIR_FROM_THIS VIR_FROM_SEXPR

J
Jim Fehlig 已提交
46
VIR_LOG_INIT("xenconfig.xen_sxpr");
47

P
Philipp Hahn 已提交
48
/* Get a domain id from a S-expression string */
49
int xenGetDomIdFromSxprString(const char *sexpr, int *id)
50 51
{
    struct sexpr *root = string2sexpr(sexpr);
52 53 54
    int ret;

    *id = -1;
55 56 57 58

    if (!root)
        return -1;

59
    ret = xenGetDomIdFromSxpr(root, id);
60
    sexpr_free(root);
61
    return ret;
62 63
}

P
Philipp Hahn 已提交
64
/* Get a domain id from a S-expression */
65
int xenGetDomIdFromSxpr(const struct sexpr *root, int *id)
66 67
{
    const char * tmp = sexpr_node(root, "domain/domid");
68 69 70

    *id = tmp ? sexpr_int(root, "domain/domid") : -1;
    return 0;
71 72 73 74
}

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

/**
P
Philipp Hahn 已提交
80
 * xenParseSxprOS:
81 82
 * @node: the root of the parsed S-Expression
 * @def: the domain config
P
Philipp Hahn 已提交
83
 * @hvm: true or 1 if node contains HVM S-Expression
84 85 86 87 88 89
 *
 * 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ß 已提交
90 91 92
xenParseSxprOS(const struct sexpr *node,
               virDomainDefPtr def,
               int hvm)
93 94
{
    if (hvm) {
95
        if (VIR_ALLOC(def->os.loader) < 0)
96
            goto error;
97 98 99 100
        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)
101
                goto error;
102

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

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

    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) {
164 165
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing kernel & bootloader"));
166 167 168 169 170
        return -1;
    }

    return 0;

171
 error:
172 173 174
    return -1;
}

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

/**
  * 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.
  */
185
virDomainChrDefPtr
M
Markus Groß 已提交
186 187
xenParseSxprChar(const char *value,
                 const char *tty)
188 189 190 191 192
{
    const char *prefix;
    char *tmp;
    virDomainChrDefPtr def;

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

    prefix = value;

    if (value[0] == '/') {
        def->source.type = VIR_DOMAIN_CHR_TYPE_DEV;
200 201
        if (VIR_STRDUP(def->source.data.file.path, value) < 0)
            goto error;
202 203 204 205 206 207 208 209 210 211 212
    } 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) {
213 214
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown chr device type '%s'"), prefix);
215 216 217 218 219 220 221
                goto error;
            }
        }
    }

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

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

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

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

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

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

        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) {
264 265
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("malformed char device string"));
266 267 268 269
            goto error;
        }

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

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

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

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

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

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

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

    return def;

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

P
Philipp Hahn 已提交
321

322 323 324 325 326 327 328 329
static const char *vif_bytes_per_sec_re = "^[0-9]+[GMK]?[Bb]/s$";

int
xenParseSxprVifRate(const char *rate, unsigned long long *kbytes_per_sec)
{
    char *trate = NULL;
    char *p;
    regex_t rec;
330
    int err;
331 332 333 334 335 336 337 338 339 340 341
    char *suffix;
    unsigned long long tmp;
    int ret = -1;

    if (VIR_STRDUP(trate, rate) < 0)
        return -1;

    p = strchr(trate, '@');
    if (p != NULL)
        *p = 0;

342 343 344 345 346 347 348 349 350 351
    err = regcomp(&rec, vif_bytes_per_sec_re, REG_EXTENDED|REG_NOSUB);
    if (err != 0) {
        char error[100];
        regerror(err, &rec, error, sizeof(error));
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to compile regular expression '%s': %s"),
                       vif_bytes_per_sec_re, error);
        goto cleanup;
    }

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    if (regexec(&rec, trate, 0, NULL, 0)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid rate '%s' specified"), rate);
        goto cleanup;
    }

    if (virStrToLong_ull(rate, &suffix, 10, &tmp)) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to parse rate '%s'"), rate);
        goto cleanup;
    }

    if (*suffix == 'G')
       tmp *= 1024 * 1024;
    else if (*suffix == 'M')
       tmp *= 1024;

    if (*suffix == 'b' || *(suffix + 1) == 'b')
       tmp /= 8;

    *kbytes_per_sec = tmp;
    ret = 0;

 cleanup:
    regfree(&rec);
    VIR_FREE(trate);
    return ret;
}


382
/**
P
Philipp Hahn 已提交
383 384 385 386
 * xenParseSxprDisks:
 * @def: the domain config
 * @root: root S-expression
 * @hvm: true or 1 if node contains HVM S-Expression
387
 *
P
Philipp Hahn 已提交
388
 * This parses out block devices from the domain S-expression
389 390 391 392
 *
 * Returns 0 if successful or -1 if failed.
 */
static int
M
Markus Groß 已提交
393 394
xenParseSxprDisks(virDomainDefPtr def,
                  const struct sexpr *root,
395
                  int hvm)
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
{
    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 已提交
412
            const char *bootable = NULL;
413 414 415 416 417 418

            /* 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 已提交
419
                bootable = sexpr_node(node, "device/vbd/bootable");
420 421 422 423
            } 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 已提交
424
                bootable = sexpr_node(node, "device/tap2/bootable");
425 426 427 428
            } 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 已提交
429
                bootable = sexpr_node(node, "device/tap/bootable");
430 431
            }

432
            if (!(disk = virDomainDiskDefNew(NULL)))
433
                goto error;
434 435

            if (dst == NULL) {
436 437
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               "%s", _("domain information incomplete, vbd has no dev"));
438 439 440 441 442 443 444 445 446
                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")) {
447 448
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("domain information incomplete, vbd has no src"));
449 450 451 452 453 454 455
                    goto error;
                }
            }

            if (src != NULL) {
                offset = strchr(src, ':');
                if (!offset) {
456 457
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   "%s", _("cannot parse vbd filename, missing driver name"));
458 459 460
                    goto error;
                }

P
Philipp Hahn 已提交
461 462
                if (sexpr_lookup(node, "device/tap2") &&
                    STRPREFIX(src, "tap:")) {
463
                    if (virDomainDiskSetDriver(disk, "tap2") < 0)
464
                        goto error;
P
Philipp Hahn 已提交
465
                } else {
466 467
                    char *tmp;
                    if (VIR_STRNDUP(tmp, src, offset - src) < 0)
468
                        goto error;
469 470
                    if (virDomainDiskSetDriver(disk, tmp) < 0) {
                        VIR_FREE(tmp);
P
Philipp Hahn 已提交
471 472
                        goto error;
                    }
473
                    VIR_FREE(tmp);
474 475 476 477
                }

                src = offset + 1;

478 479
                if (STREQ(virDomainDiskGetDriver(disk), "tap") ||
                    STREQ(virDomainDiskGetDriver(disk), "tap2")) {
480 481
                    char *driverType = NULL;

482 483
                    offset = strchr(src, ':');
                    if (!offset) {
484 485
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       "%s", _("cannot parse vbd filename, missing driver type"));
486 487 488
                        goto error;
                    }

489 490
                    if (VIR_STRNDUP(driverType, src, offset - src) < 0)
                        goto error;
491
                    if (STREQ(driverType, "aio"))
492
                        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
493
                    else
494 495
                        virDomainDiskSetFormat(disk,
                                               virStorageFileFormatTypeFromString(driverType));
496
                    VIR_FREE(driverType);
497
                    if (virDomainDiskGetFormat(disk) <= 0) {
498
                        virReportError(VIR_ERR_INTERNAL_ERROR,
499
                                       _("Unknown driver type %s"), src);
500 501 502 503 504 505 506 507 508
                        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 已提交
509
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
510
                } else if (STREQ(virDomainDiskGetDriver(disk), "phy")) {
E
Eric Blake 已提交
511
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_BLOCK);
512
                } else if (STREQ(virDomainDiskGetDriver(disk), "file")) {
E
Eric Blake 已提交
513
                    virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
514 515 516 517 518
                }
            } 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 已提交
519
                virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
520 521
            }

522
            if (STREQLEN(dst, "ioemu:", 6))
523 524 525
                dst += 6;

            disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
526 527 528 529 530 531 532 533
            offset = strrchr(dst, ':');
            if (offset) {
                if (STREQ(offset, ":cdrom")) {
                    disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
                } else if (STREQ(offset, ":disk")) {
                    /* The default anyway */
                } else {
                    /* Unknown, lets pretend its a disk too */
534
                }
535
                offset[0] = '\0';
536 537
            }

538 539
            if (VIR_STRDUP(disk->dst, dst) < 0)
                goto error;
540
            if (virDomainDiskSetSource(disk, src) < 0)
541
                goto error;
542 543 544 545 546 547 548 549 550 551 552 553

            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'))
554
                disk->src->readonly = true;
555 556
            if (mode &&
                strchr(mode, '!'))
557
                disk->src->shared = true;
558 559

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

P
Philipp Hahn 已提交
562 563 564 565 566 567 568
            /* 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;
            }
569 570 571 572 573 574
            disk = NULL;
        }
    }

    return 0;

575
 error:
576 577 578 579 580
    virDomainDiskDefFree(disk);
    return -1;
}


P
Philipp Hahn 已提交
581 582 583 584 585 586 587 588 589
/**
 * 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.
 */
590
static int
M
Markus Groß 已提交
591 592
xenParseSxprNets(virDomainDefPtr def,
                 const struct sexpr *root)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
    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)
609
                goto cleanup;
610 611 612 613 614 615

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

616 617 618 619 620
                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;
621
                tmp = sexpr_node(node, "device/vif/ip");
622
                if (tmp && virDomainNetAppendIpAddress(net, tmp, AF_UNSPEC, 0) < 0)
623
                    goto cleanup;
624 625
            } else {
                net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
626 627
                if (VIR_STRDUP(net->script, tmp2) < 0)
                    goto cleanup;
628
                tmp = sexpr_node(node, "device/vif/ip");
629
                if (tmp && virDomainNetAppendIpAddress(net, tmp, AF_UNSPEC, 0) < 0)
630
                    goto cleanup;
631 632 633
            }

            tmp = sexpr_node(node, "device/vif/vifname");
634 635 636 637
            /* 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) {
638 639
                if (VIR_STRDUP(net->ifname, tmp) < 0)
                    goto cleanup;
640 641
            } else if (def->id != -1) {
                if (virAsprintf(&net->ifname, "vif%d.%d", def->id, vif_index) < 0)
642
                    goto cleanup;
643 644 645 646
            }

            tmp = sexpr_node(node, "device/vif/mac");
            if (tmp) {
647
                if (virMacAddrParse(tmp, &net->mac) < 0) {
648 649
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("malformed mac address '%s'"), tmp);
650 651 652 653
                    goto cleanup;
                }
            }

654 655
            if (VIR_STRDUP(net->model, model) < 0)
                goto cleanup;
656

657 658 659
            if (!model && type && STREQ(type, "netfront") &&
                VIR_STRDUP(net->model, "netfront") < 0)
                goto cleanup;
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
            tmp = sexpr_node(node, "device/vif/rate");
            if (tmp) {
                virNetDevBandwidthPtr bandwidth;
                unsigned long long kbytes_per_sec;

                if (xenParseSxprVifRate(tmp, &kbytes_per_sec) < 0)
                    goto cleanup;

                if (VIR_ALLOC(bandwidth) < 0)
                    goto cleanup;
                if (VIR_ALLOC(bandwidth->out) < 0) {
                    VIR_FREE(bandwidth);
                    goto cleanup;
                }

                bandwidth->out->average = kbytes_per_sec;
                net->bandwidth = bandwidth;
            }

680
            if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
681
                goto cleanup;
682 683 684 685 686 687 688

            vif_index++;
        }
    }

    return 0;

689
 cleanup:
690 691 692 693 694
    virDomainNetDefFree(net);
    return -1;
}


P
Philipp Hahn 已提交
695 696 697 698 699 700 701 702 703
/**
 * 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.
 */
704
int
M
Markus Groß 已提交
705 706
xenParseSxprSound(virDomainDefPtr def,
                  const char *str)
707 708
{
    if (STREQ(str, "all")) {
709
        size_t i;
710 711

        /*
712
         * Special compatibility code for Xen with a bogus
713 714
         * sound=all in config.
         *
E
Eric Blake 已提交
715
         * NB deliberately, don't include all possible
716 717 718 719 720 721 722 723 724 725
         * 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)
726
            goto error;
727 728


729
        for (i = 0; i < (VIR_DOMAIN_SOUND_MODEL_ES1370 + 1); i++) {
730 731
            virDomainSoundDefPtr sound;
            if (VIR_ALLOC(sound) < 0)
732
                goto error;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
            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) {
749 750 751
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Sound model %s too big for destination"),
                               offset);
752 753 754 755
                goto error;
            }

            if (VIR_ALLOC(sound) < 0)
756
                goto error;
757 758 759 760 761 762

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

763
            if (VIR_APPEND_ELEMENT(def->sounds, def->nsounds, sound) < 0) {
764
                virDomainSoundDefFree(sound);
765
                goto error;
766 767 768 769 770 771 772 773
            }

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

    return 0;

774
 error:
775 776 777 778
    return -1;
}


P
Philipp Hahn 已提交
779 780 781 782 783 784 785 786 787
/**
 * 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.
 */
788
static int
M
Markus Groß 已提交
789 790
xenParseSxprUSB(virDomainDefPtr def,
                const struct sexpr *root)
791 792 793 794 795 796 797 798 799 800
{
    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") ||
801 802
                    STREQ(tmp, "mouse") ||
                    STREQ(tmp, "keyboard")) {
803 804
                    virDomainInputDefPtr input;
                    if (VIR_ALLOC(input) < 0)
805
                        goto error;
806 807 808
                    input->bus = VIR_DOMAIN_INPUT_BUS_USB;
                    if (STREQ(tmp, "tablet"))
                        input->type = VIR_DOMAIN_INPUT_TYPE_TABLET;
809
                    else if (STREQ(tmp, "mouse"))
810
                        input->type = VIR_DOMAIN_INPUT_TYPE_MOUSE;
811 812
                    else
                        input->type = VIR_DOMAIN_INPUT_TYPE_KBD;
813

814
                    if (VIR_APPEND_ELEMENT(def->inputs, def->ninputs, input) < 0) {
815
                        VIR_FREE(input);
816
                        goto error;
817 818 819 820 821 822 823 824 825
                    }
                } else {
                    /* XXX Handle other non-input USB devices later */
                }
            }
        }
    }
    return 0;

826
 error:
827 828 829
    return -1;
}

P
Philipp Hahn 已提交
830 831 832 833 834 835 836 837 838 839 840 841

/*
 * xenParseSxprGraphicsOld:
 * @def: the domain config
 * @root: root S-expression
 * @hvm: true or 1 if root contains HVM S-Expression
 * @vncport: VNC port number
 *
 * This parses out VNC devices from the domain S-expression
 *
 * Returns 0 if successful or -1 if failed.
 */
842
static int
M
Markus Groß 已提交
843 844 845
xenParseSxprGraphicsOld(virDomainDefPtr def,
                        const struct sexpr *root,
                        int hvm,
846
                        int vncport)
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
{
    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)
863
            goto error;
864 865 866

        graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
        if ((unused && STREQ(unused, "1")) || port == -1)
867
            graphics->data.vnc.autoport = true;
868 869 870
        graphics->data.vnc.port = port;

        if (listenAddr &&
871
            virDomainGraphicsListenAppendAddress(graphics, listenAddr) < 0)
872
            goto error;
873

874 875
        if (VIR_STRDUP(graphics->data.vnc.auth.passwd, vncPasswd) < 0)
            goto error;
876

877 878
        if (VIR_STRDUP(graphics->data.vnc.keymap, keymap) < 0)
            goto error;
879 880

        if (VIR_ALLOC_N(def->graphics, 1) < 0)
881
            goto error;
882 883 884 885 886 887 888 889 890 891
        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)
892
            goto error;
893 894

        graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
895 896 897 898
        if (VIR_STRDUP(graphics->data.sdl.display, display) < 0)
            goto error;
        if (VIR_STRDUP(graphics->data.sdl.xauth, xauth) < 0)
            goto error;
899 900

        if (VIR_ALLOC_N(def->graphics, 1) < 0)
901
            goto error;
902 903 904 905 906 907 908
        def->graphics[0] = graphics;
        def->ngraphics = 1;
        graphics = NULL;
    }

    return 0;

909
 error:
910 911 912 913 914
    virDomainGraphicsDefFree(graphics);
    return -1;
}


P
Philipp Hahn 已提交
915 916 917 918 919 920 921 922 923 924
/*
 * 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.
 */
925
static int
M
Markus Groß 已提交
926 927
xenParseSxprGraphicsNew(virDomainDefPtr def,
                        const struct sexpr *root, int vncport)
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
{
    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)
950
                goto error;
951 952

            if ((graphics->type = virDomainGraphicsTypeFromString(tmp)) < 0) {
953 954
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown graphics type '%s'"), tmp);
955 956 957 958 959 960
                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");
961 962 963 964
                if (VIR_STRDUP(graphics->data.sdl.display, display) < 0)
                    goto error;
                if (VIR_STRDUP(graphics->data.sdl.xauth, xauth) < 0)
                    goto error;
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
            } 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)
983
                    graphics->data.vnc.autoport = true;
984 985 986 987 988 989

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

                if (listenAddr &&
990
                    virDomainGraphicsListenAppendAddress(graphics, listenAddr) < 0)
991
                    goto error;
992

993 994
                if (VIR_STRDUP(graphics->data.vnc.auth.passwd, vncPasswd) < 0)
                    goto error;
995

996 997
                if (VIR_STRDUP(graphics->data.vnc.keymap, keymap) < 0)
                    goto error;
998 999 1000
            }

            if (VIR_ALLOC_N(def->graphics, 1) < 0)
1001
                goto error;
1002 1003 1004 1005 1006 1007 1008 1009 1010
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
            break;
        }
    }

    return 0;

1011
 error:
1012 1013 1014 1015
    virDomainGraphicsDefFree(graphics);
    return -1;
}

P
Philipp Hahn 已提交
1016

1017
/**
P
Philipp Hahn 已提交
1018 1019
 * xenParseSxprPCI:
 * @def: the domain config
1020 1021
 * @root: root sexpr
 *
P
Philipp Hahn 已提交
1022
 * This parses out PCI devices from the domain sexpr
1023 1024 1025 1026
 *
 * Returns 0 if successful or -1 if failed.
 */
static int
M
Markus Groß 已提交
1027 1028
xenParseSxprPCI(virDomainDefPtr def,
                const struct sexpr *root)
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
{
    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 已提交
1044
     * weird world of Xen PCI, once (device ...) covers multiple
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
     * 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"))) {
1072 1073
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI domain"));
1074 1075 1076
            goto error;
        }
        if (!(bus = sexpr_node(node, "dev/bus"))) {
1077 1078
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI bus"));
1079 1080 1081
            goto error;
        }
        if (!(slot = sexpr_node(node, "dev/slot"))) {
1082 1083
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI slot"));
1084 1085 1086
            goto error;
        }
        if (!(func = sexpr_node(node, "dev/func"))) {
1087 1088
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("missing PCI func"));
1089 1090 1091 1092
            goto error;
        }

        if (virStrToLong_i(domain, NULL, 0, &domainID) < 0) {
1093 1094
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI domain '%s'"), domain);
1095 1096 1097
            goto error;
        }
        if (virStrToLong_i(bus, NULL, 0, &busID) < 0) {
1098 1099
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI bus '%s'"), bus);
1100 1101 1102
            goto error;
        }
        if (virStrToLong_i(slot, NULL, 0, &slotID) < 0) {
1103 1104
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI slot '%s'"), slot);
1105 1106 1107
            goto error;
        }
        if (virStrToLong_i(func, NULL, 0, &funcID) < 0) {
1108 1109
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("cannot parse PCI func '%s'"), func);
1110 1111 1112
            goto error;
        }

1113
        if (!(dev = virDomainHostdevDefAlloc(NULL)))
1114
           goto error;
1115 1116

        dev->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
1117
        dev->managed = false;
1118
        dev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
1119 1120 1121 1122
        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;
1123

1124
        if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, dev) < 0)
1125
            goto error;
1126 1127 1128 1129
    }

    return 0;

1130
 error:
1131 1132 1133 1134 1135 1136
    virDomainHostdevDefFree(dev);
    return -1;
}


/**
M
Markus Groß 已提交
1137
 * xenParseSxpr:
1138 1139
 * @root: the root of the parsed S-Expression
 * @cpus: set of cpus the domain may be pinned to
P
Philipp Hahn 已提交
1140 1141
 * @tty: the console pty path
 * @vncport: VNC port number
1142
 *
P
Philipp Hahn 已提交
1143 1144
 * Parse the xend S-expression description and turn it into a virDomainDefPtr
 * representing these settings as closely as is practical.
1145
 *
P
Philipp Hahn 已提交
1146 1147
 * Returns the domain config or NULL in case of error.
 *         The caller must free() the returned value.
1148 1149
 */
virDomainDefPtr
M
Markus Groß 已提交
1150
xenParseSxpr(const struct sexpr *root,
1151 1152 1153 1154 1155
             const char *cpus,
             char *tty,
             int vncport,
             virCapsPtr caps,
             virDomainXMLOptionPtr xmlopt)
1156 1157 1158
{
    const char *tmp;
    virDomainDefPtr def;
P
Philipp Hahn 已提交
1159
    int hvm = 0, vmlocaltime;
1160
    unsigned int vcpus;
1161

1162
    if (!(def = virDomainDefNew()))
1163
        goto error;
1164 1165 1166 1167 1168 1169 1170 1171 1172

    tmp = sexpr_node(root, "domain/domid");
    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)
1173
        goto error;
1174
    if (def->name == NULL) {
1175 1176
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing name"));
1177 1178 1179 1180 1181
        goto error;
    }

    tmp = sexpr_node(root, "domain/uuid");
    if (tmp == NULL) {
1182 1183
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("domain information incomplete, missing name"));
1184 1185
        goto error;
    }
1186 1187
    if (virUUIDParse(tmp, def->uuid) < 0)
        goto error;
1188 1189

    if (sexpr_node_copy(root, "domain/description", &def->description) < 0)
1190
        goto error;
1191 1192 1193 1194 1195

    hvm = sexpr_lookup(root, "domain/image/hvm") ? 1 : 0;
    if (!hvm) {
        if (sexpr_node_copy(root, "domain/bootloader",
                            &def->os.bootloader) < 0)
1196
            goto error;
1197 1198 1199

        if (!def->os.bootloader &&
            sexpr_has(root, "domain/bootloader") &&
1200 1201
            VIR_STRDUP(def->os.bootloader, "") < 0)
            goto error;
1202 1203 1204 1205

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

1209
    def->os.type = (hvm ? VIR_DOMAIN_OSTYPE_HVM : VIR_DOMAIN_OSTYPE_LINUX);
1210 1211 1212

    if (def->id != 0) {
        if (sexpr_lookup(root, "domain/image")) {
M
Markus Groß 已提交
1213
            if (xenParseSxprOS(root, def, hvm) < 0)
1214 1215 1216 1217
                goto error;
        }
    }

1218
    virDomainDefSetMemoryTotal(def, (sexpr_u64(root, "domain/maxmem") << 10));
1219
    def->mem.cur_balloon = (sexpr_u64(root, "domain/memory") << 10);
1220 1221 1222

    if (def->mem.cur_balloon > virDomainDefGetMemoryActual(def))
        def->mem.cur_balloon = virDomainDefGetMemoryActual(def);
1223 1224

    if (cpus != NULL) {
H
Hu Tao 已提交
1225
        if (virBitmapParse(cpus, 0, &def->cpumask,
1226
                           VIR_DOMAIN_CPUMASK_LEN) < 0)
1227
            goto error;
1228 1229 1230 1231 1232 1233 1234

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

1237 1238
    if (virDomainDefSetVcpusMax(def, sexpr_int(root, "domain/vcpus")) < 0)
        goto error;
1239 1240 1241 1242 1243 1244 1245

    vcpus = count_one_bits_l(sexpr_u64(root, "domain/vcpu_avail"));
    if (!vcpus || virDomainDefGetVcpusMax(def) < vcpus)
        vcpus = virDomainDefGetVcpusMax(def);

    if (virDomainDefSetVcpus(def, vcpus) < 0)
        goto error;
1246 1247 1248 1249

    tmp = sexpr_node(root, "domain/on_poweroff");
    if (tmp != NULL) {
        if ((def->onPoweroff = virDomainLifecycleTypeFromString(tmp)) < 0) {
1250 1251
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1252 1253
            goto error;
        }
1254
    } else {
1255
        def->onPoweroff = VIR_DOMAIN_LIFECYCLE_DESTROY;
1256
    }
1257 1258 1259 1260

    tmp = sexpr_node(root, "domain/on_reboot");
    if (tmp != NULL) {
        if ((def->onReboot = virDomainLifecycleTypeFromString(tmp)) < 0) {
1261 1262
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1263 1264
            goto error;
        }
1265
    } else {
1266
        def->onReboot = VIR_DOMAIN_LIFECYCLE_RESTART;
1267
    }
1268 1269 1270 1271

    tmp = sexpr_node(root, "domain/on_crash");
    if (tmp != NULL) {
        if ((def->onCrash = virDomainLifecycleCrashTypeFromString(tmp)) < 0) {
1272 1273
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unknown lifecycle type %s"), tmp);
1274 1275
            goto error;
        }
1276
    } else {
1277
        def->onCrash = VIR_DOMAIN_LIFECYCLE_CRASH_DESTROY;
1278
    }
1279 1280 1281

    if (hvm) {
        if (sexpr_int(root, "domain/image/hvm/acpi"))
J
Ján Tomko 已提交
1282
            def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_TRISTATE_SWITCH_ON;
1283
        if (sexpr_int(root, "domain/image/hvm/apic"))
J
Ján Tomko 已提交
1284
            def->features[VIR_DOMAIN_FEATURE_APIC] = VIR_TRISTATE_SWITCH_ON;
1285
        if (sexpr_int(root, "domain/image/hvm/pae"))
J
Ján Tomko 已提交
1286
            def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_TRISTATE_SWITCH_ON;
1287
        if (sexpr_int(root, "domain/image/hvm/hap"))
J
Ján Tomko 已提交
1288
            def->features[VIR_DOMAIN_FEATURE_HAP] = VIR_TRISTATE_SWITCH_ON;
1289
        if (sexpr_int(root, "domain/image/hvm/viridian"))
J
Ján Tomko 已提交
1290
            def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_TRISTATE_SWITCH_ON;
P
Philipp Hahn 已提交
1291
    }
1292

P
Philipp Hahn 已提交
1293 1294 1295 1296
    /* 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");
1297 1298
        int rtc_offset;

P
Philipp Hahn 已提交
1299 1300
        if (value) {
            if (virStrToLong_i(value, NULL, 0, &vmlocaltime) < 0) {
1301 1302
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown localtime offset %s"), value);
P
Philipp Hahn 已提交
1303 1304 1305
                goto error;
            }
        }
1306 1307 1308 1309 1310 1311
        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;
1312 1313 1314 1315 1316

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

            if (VIR_ALLOC_N(def->clock.timers, 1) < 0 ||
1317
                VIR_ALLOC(timer) < 0)
1318 1319 1320 1321 1322
                goto error;

            timer->name = VIR_DOMAIN_TIMER_NAME_HPET;
            timer->present = sexpr_int(root, "domain/image/hvm/hpet");
            timer->tickpolicy = -1;
1323 1324
            timer->mode = -1;
            timer->track = -1;
1325 1326 1327 1328

            def->clock.ntimers = 1;
            def->clock.timers[0] = timer;
        }
P
Philipp Hahn 已提交
1329 1330 1331 1332
    } else {
        const char *value = sexpr_node(root, "domain/image/linux/localtime");
        if (value) {
            if (virStrToLong_i(value, NULL, 0, &vmlocaltime) < 0) {
1333 1334
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unknown localtime offset %s"), value);
P
Philipp Hahn 已提交
1335 1336 1337 1338 1339
                goto error;
            }
        }
        /* PV domains do not have an emulated RTC and the offset is fixed. */
        if (vmlocaltime)
1340
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME;
P
Philipp Hahn 已提交
1341 1342 1343 1344
        else
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
        def->clock.data.utc_reset = true;
    } /* !hvm */
1345 1346 1347 1348 1349

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

    /* append block devices */
1353
    if (xenParseSxprDisks(def, root, hvm) < 0)
1354 1355
        goto error;

M
Markus Groß 已提交
1356
    if (xenParseSxprNets(def, root) < 0)
1357 1358
        goto error;

M
Markus Groß 已提交
1359
    if (xenParseSxprPCI(def, root) < 0)
1360 1361 1362
        goto error;

    /* New style graphics device config */
M
Markus Groß 已提交
1363
    if (xenParseSxprGraphicsNew(def, root, vncport) < 0)
1364 1365 1366 1367
        goto error;

    /* Graphics device (HVM <= 3.0.4, or PV <= 3.0.3) vnc config */
    if ((def->ngraphics == 0) &&
1368
        xenParseSxprGraphicsOld(def, root, hvm, vncport) < 0)
1369 1370 1371 1372
        goto error;

    /* in case of HVM we have USB device emulation */
    if (hvm &&
M
Markus Groß 已提交
1373
        xenParseSxprUSB(def, root) < 0)
1374 1375 1376 1377
        goto error;

    /* Character device config */
    if (hvm) {
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
        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;
1396 1397 1398
                        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) {
1399
                            virDomainChrDefFree(chr);
1400
                            goto error;
1401 1402 1403 1404 1405 1406 1407
                        }
                    }
                    else
                        ports_skipped++;

                    have_multiple_serials = true;
                }
1408 1409
            }
        }
1410 1411 1412 1413 1414 1415 1416

        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;
1417 1418 1419
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = 0;
                if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
1420
                    virDomainChrDefFree(chr);
1421
                    goto error;
1422 1423 1424 1425
                }
            }
        }

1426 1427 1428 1429
        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ß 已提交
1430
            if ((chr = xenParseSxprChar(tmp, NULL)) == NULL)
1431
                goto error;
1432 1433 1434
            chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
            chr->target.port = 0;
            if (VIR_APPEND_ELEMENT(def->parallels, def->nparallels, chr) < 0) {
1435
                virDomainChrDefFree(chr);
1436
                goto error;
1437 1438
            }
        }
1439
    } else if (def->id != 0) {
1440
        if (VIR_ALLOC_N(def->consoles, 1) < 0)
1441
            goto error;
1442
        def->nconsoles = 1;
1443
        /* Fake a paravirt console, since that's not in the sexpr */
1444
        if (!(def->consoles[0] = xenParseSxprChar("pty", tty)))
1445
            goto error;
1446 1447 1448
        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;
1449 1450 1451 1452 1453 1454 1455 1456
    }
    VIR_FREE(tty);


    /* Sound device config */
    if (hvm &&
        (tmp = sexpr_node(root, "domain/image/hvm/soundhw")) != NULL &&
        *tmp) {
M
Markus Groß 已提交
1457
        if (xenParseSxprSound(def, tmp) < 0)
1458 1459 1460
            goto error;
    }

1461 1462 1463 1464
    if (virDomainDefPostParse(def, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE,
                              xmlopt) < 0)
        goto error;

1465 1466
    return def;

1467
 error:
1468 1469 1470 1471 1472
    VIR_FREE(tty);
    virDomainDefFree(def);
    return NULL;
}

P
Philipp Hahn 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

/**
 * xenParseSxprString:
 * @sexpr: the root of the parsed S-Expression
 * @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.
 */
1486
virDomainDefPtr
M
Markus Groß 已提交
1487
xenParseSxprString(const char *sexpr,
1488 1489 1490 1491
                   char *tty,
                   int vncport,
                   virCapsPtr caps,
                   virDomainXMLOptionPtr xmlopt)
1492 1493 1494 1495 1496 1497 1498
{
    struct sexpr *root = string2sexpr(sexpr);
    virDomainDefPtr def;

    if (!root)
        return NULL;

1499
    def = xenParseSxpr(root, NULL, tty, vncport, caps, xmlopt);
1500 1501 1502
    sexpr_free(root);

    return def;
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
}

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


/**
P
Philipp Hahn 已提交
1513 1514 1515
 * xenFormatSxprGraphicsNew:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
1516
 *
P
Philipp Hahn 已提交
1517 1518
 * Convert the graphics part of the domain description into a S-expression
 * in buf. (HVM > 3.0.4 or PV > 3.0.3)
1519 1520 1521 1522
 *
 * Returns 0 in case of success, -1 in case of error
 */
static int
M
Markus Groß 已提交
1523 1524
xenFormatSxprGraphicsNew(virDomainGraphicsDefPtr def,
                         virBufferPtr buf)
1525
{
P
Pavel Hrdina 已提交
1526
    virDomainGraphicsListenDefPtr gListen;
1527

1528 1529
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_SDL &&
        def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
1530 1531 1532
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected graphics type %d"),
                       def->type);
1533 1534 1535 1536 1537 1538 1539 1540 1541
        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)
1542
            virBufferAsprintf(buf, "(display '%s')", def->data.sdl.display);
1543
        if (def->data.sdl.xauth)
1544
            virBufferAsprintf(buf, "(xauthority '%s')", def->data.sdl.xauth);
1545 1546 1547 1548 1549 1550
    } 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)");
1551
            virBufferAsprintf(buf, "(vncdisplay %d)", def->data.vnc.port-5900);
1552 1553
        }

P
Pavel Hrdina 已提交
1554 1555 1556
        if ((gListen = virDomainGraphicsGetListen(def, 0)) &&
            gListen->address)
            virBufferAsprintf(buf, "(vnclisten '%s')", gListen->address);
1557
        if (def->data.vnc.auth.passwd)
1558
            virBufferAsprintf(buf, "(vncpasswd '%s')", def->data.vnc.auth.passwd);
1559
        if (def->data.vnc.keymap)
1560
            virBufferAsprintf(buf, "(keymap '%s')", def->data.vnc.keymap);
1561 1562 1563 1564 1565 1566 1567 1568
    }

    virBufferAddLit(buf, "))");

    return 0;
}


P
Philipp Hahn 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
/**
 * xenFormatSxprGraphicsOld:
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 *
 * 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
 */
1579
static int
1580
xenFormatSxprGraphicsOld(virDomainGraphicsDefPtr def, virBufferPtr buf)
1581
{
P
Pavel Hrdina 已提交
1582
    virDomainGraphicsListenDefPtr gListen;
1583

1584 1585
    if (def->type != VIR_DOMAIN_GRAPHICS_TYPE_SDL &&
        def->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
1586 1587 1588
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected graphics type %d"),
                       def->type);
1589 1590 1591 1592 1593 1594
        return -1;
    }

    if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
        virBufferAddLit(buf, "(sdl 1)");
        if (def->data.sdl.display)
1595
            virBufferAsprintf(buf, "(display '%s')", def->data.sdl.display);
1596
        if (def->data.sdl.xauth)
1597
            virBufferAsprintf(buf, "(xauthority '%s')", def->data.sdl.xauth);
1598 1599
    } else if (def->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
        virBufferAddLit(buf, "(vnc 1)");
1600 1601 1602 1603 1604
        if (def->data.vnc.autoport) {
            virBufferAddLit(buf, "(vncunused 1)");
        } else {
            virBufferAddLit(buf, "(vncunused 0)");
            virBufferAsprintf(buf, "(vncdisplay %d)", def->data.vnc.port-5900);
1605
        }
1606

P
Pavel Hrdina 已提交
1607 1608 1609
        if ((gListen = virDomainGraphicsGetListen(def, 0)) &&
            gListen->address)
            virBufferAsprintf(buf, "(vnclisten '%s')", gListen->address);
1610 1611 1612 1613
        if (def->data.vnc.auth.passwd)
            virBufferAsprintf(buf, "(vncpasswd '%s')", def->data.vnc.auth.passwd);
        if (def->data.vnc.keymap)
            virBufferAsprintf(buf, "(keymap '%s')", def->data.vnc.keymap);
1614 1615 1616 1617 1618
    }

    return 0;
}

P
Philipp Hahn 已提交
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

/**
 * 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
 */
1630
int
M
Markus Groß 已提交
1631 1632
xenFormatSxprChr(virDomainChrDefPtr def,
                 virBufferPtr buf)
1633 1634 1635 1636
{
    const char *type = virDomainChrTypeToString(def->source.type);

    if (!type) {
1637 1638
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("unexpected chr device type"));
1639 1640 1641 1642 1643 1644 1645 1646
        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:
1647
        virBufferAdd(buf, type, -1);
1648 1649 1650 1651
        break;

    case VIR_DOMAIN_CHR_TYPE_FILE:
    case VIR_DOMAIN_CHR_TYPE_PIPE:
1652
        virBufferAsprintf(buf, "%s:", type);
1653 1654 1655 1656 1657 1658 1659 1660
        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:
1661
        virBufferAsprintf(buf, "%s:%s:%s%s",
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
                          (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:
1674
        virBufferAsprintf(buf, "%s:%s:%s@%s:%s", type,
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
                          (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:
1686
        virBufferAsprintf(buf, "%s:", type);
1687 1688 1689 1690
        virBufferEscapeSexpr(buf, "%s", def->source.data.nix.path);
        if (def->source.data.nix.listen)
            virBufferAddLit(buf, ",server,nowait");
        break;
1691 1692

    default:
1693 1694
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("unsupported chr device type '%s'"), type);
1695
        return -1;
1696 1697
    }

1698
    if (virBufferCheckError(buf) < 0)
1699 1700 1701 1702 1703 1704 1705
        return -1;

    return 0;
}


/**
P
Philipp Hahn 已提交
1706 1707 1708 1709 1710
 * xenFormatSxprDisk:
 * @node: node containing the disk description
 * @buf: a buffer for the result S-expression
 * @hvm: true or 1 if domain is HVM
 * @isAttach: create expression for device attach (1).
1711
 *
P
Philipp Hahn 已提交
1712
 * Convert the disk device part of the domain config into a S-expresssion in buf.
1713 1714 1715 1716
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1717
xenFormatSxprDisk(virDomainDiskDefPtr def,
M
Markus Groß 已提交
1718 1719 1720
                  virBufferPtr buf,
                  int hvm,
                  int isAttach)
1721
{
1722 1723 1724
    const char *src = virDomainDiskGetSource(def);
    const char *driver = virDomainDiskGetDriver(def);

1725 1726 1727 1728 1729 1730
    /* 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) {
1731
            virReportError(VIR_ERR_INVALID_ARG,
1732
                           _("Cannot directly attach floppy %s"), src);
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
            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.... */
1744
    if (STREQ_NULLABLE(driver, "tap")) {
1745
        virBufferAddLit(buf, "(tap ");
1746
    } else if (STREQ_NULLABLE(driver, "tap2")) {
1747 1748 1749 1750 1751 1752
        virBufferAddLit(buf, "(tap2 ");
    } else {
        virBufferAddLit(buf, "(vbd ");
    }

    if (hvm) {
1753 1754 1755 1756
        virBufferEscapeSexpr(buf, "(dev '%s:", def->dst);
        virBufferAsprintf(buf, "%s')",
                          def->device == VIR_DOMAIN_DISK_DEVICE_CDROM ?
                          "cdrom" : "disk");
1757 1758 1759 1760 1761 1762
    } else if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
        virBufferEscapeSexpr(buf, "(dev '%s:cdrom')", def->dst);
    } else {
        virBufferEscapeSexpr(buf, "(dev '%s')", def->dst);
    }

1763 1764 1765 1766
    if (src) {
        if (driver) {
            if (STREQ(driver, "tap") ||
                STREQ(driver, "tap2")) {
1767
                const char *type;
1768
                int format = virDomainDiskGetFormat(def);
1769

1770
                if (!format || format == VIR_STORAGE_FILE_RAW)
1771
                    type = "aio";
1772
                else
1773 1774
                    type = virStorageFileFormatTypeToString(format);
                virBufferEscapeSexpr(buf, "(uname '%s:", driver);
1775
                virBufferEscapeSexpr(buf, "%s:", type);
1776
                virBufferEscapeSexpr(buf, "%s')", src);
1777
            } else {
1778 1779
                virBufferEscapeSexpr(buf, "(uname '%s:", driver);
                virBufferEscapeSexpr(buf, "%s')", src);
1780 1781
            }
        } else {
1782 1783
            int type = virDomainDiskGetType(def);

E
Eric Blake 已提交
1784
            if (type == VIR_STORAGE_TYPE_FILE) {
1785
                virBufferEscapeSexpr(buf, "(uname 'file:%s')", src);
E
Eric Blake 已提交
1786
            } else if (type == VIR_STORAGE_TYPE_BLOCK) {
1787 1788
                if (src[0] == '/')
                    virBufferEscapeSexpr(buf, "(uname 'phy:%s')", src);
1789 1790
                else
                    virBufferEscapeSexpr(buf, "(uname 'phy:/dev/%s')",
1791
                                         src);
1792
            } else {
1793 1794
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported disk type %s"),
E
Eric Blake 已提交
1795
                               virStorageTypeToString(type));
1796 1797 1798 1799 1800
                return -1;
            }
        }
    }

1801
    if (def->src->readonly)
1802
        virBufferAddLit(buf, "(mode 'r')");
1803
    else if (def->src->shared)
1804 1805 1806
        virBufferAddLit(buf, "(mode 'w!')");
    else
        virBufferAddLit(buf, "(mode 'w')");
1807
    if (def->transient) {
1808
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1809
                       _("transient disks not supported yet"));
1810 1811
        return -1;
    }
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821

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

    virBufferAddLit(buf, ")");

    return 0;
}

/**
P
Philipp Hahn 已提交
1822 1823 1824 1825 1826 1827
 * xenFormatSxprNet:
 * @conn: connection
 * @def: the domain config
 * @buf: a buffer for the result S-expression
 * @hvm: true or 1 if domain is HVM
 * @isAttach: create expression for device attach (1).
1828
 *
P
Philipp Hahn 已提交
1829
 * Convert the interface description of the domain config into a S-expression in buf.
1830 1831 1832 1833 1834 1835 1836
 * 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ß 已提交
1837 1838 1839 1840 1841
xenFormatSxprNet(virConnectPtr conn,
                 virDomainNetDefPtr def,
                 virBufferPtr buf,
                 int hvm,
                 int isAttach)
1842 1843
{
    const char *script = DEFAULT_VIF_SCRIPT;
1844
    char macaddr[VIR_MAC_STRING_BUFLEN];
1845 1846 1847 1848

    if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
        def->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
        def->type != VIR_DOMAIN_NET_TYPE_ETHERNET) {
1849 1850
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network type %d"), def->type);
1851 1852
        return -1;
    }
1853 1854 1855
    if (def->script &&
        def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
        def->type != VIR_DOMAIN_NET_TYPE_ETHERNET) {
1856 1857 1858
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("scripts are not supported on interfaces of type %s"),
                       virDomainNetTypeToString(def->type));
1859 1860
        return -1;
    }
1861 1862 1863 1864 1865 1866

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

    virBufferAddLit(buf, "(vif ");

1867
    virBufferAsprintf(buf, "(mac '%s')", virMacAddrFormat(&def->mac, macaddr));
1868

1869 1870 1871
    if (def->bandwidth && def->bandwidth->out && def->bandwidth->out->average)
        virBufferAsprintf(buf, "(rate '%lluKB/s')", def->bandwidth->out->average);

1872 1873 1874
    switch (def->type) {
    case VIR_DOMAIN_NET_TYPE_BRIDGE:
        virBufferEscapeSexpr(buf, "(bridge '%s')", def->data.bridge.brname);
1875 1876
        if (def->script)
            script = def->script;
1877 1878

        virBufferEscapeSexpr(buf, "(script '%s')", script);
1879
        if (def->nips == 1) {
1880 1881 1882
            char *ipStr = virSocketAddrFormat(&def->ips[0]->address);
            virBufferEscapeSexpr(buf, "(ip '%s')", ipStr);
            VIR_FREE(ipStr);
1883 1884 1885 1886
        } else if (def->nips > 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Driver does not support setting multiple IP addresses"));
            return -1;
1887
        }
1888 1889 1890 1891 1892 1893 1894 1895 1896
        break;

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

        if (!network) {
1897 1898
            virReportError(VIR_ERR_NO_NETWORK, "%s",
                           def->data.network.name);
1899 1900 1901 1902
            return -1;
        }

        bridge = virNetworkGetBridgeName(network);
1903
        virObjectUnref(network);
1904
        if (!bridge) {
1905 1906 1907
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("network %s is not active"),
                           def->data.network.name);
1908 1909 1910 1911 1912 1913 1914 1915 1916
            return -1;
        }
        virBufferEscapeSexpr(buf, "(bridge '%s')", bridge);
        virBufferEscapeSexpr(buf, "(script '%s')", script);
        VIR_FREE(bridge);
    }
    break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
1917
        if (def->script)
1918
            virBufferEscapeSexpr(buf, "(script '%s')",
1919
                                 def->script);
1920
        if (def->nips == 1) {
1921 1922 1923
            char *ipStr = virSocketAddrFormat(&def->ips[0]->address);
            virBufferEscapeSexpr(buf, "(ip '%s')", ipStr);
            VIR_FREE(ipStr);
1924 1925 1926 1927
        } else if (def->nips > 1) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Driver does not support setting multiple IP addresses"));
            return -1;
1928
        }
1929 1930
        break;

M
Michele Paolino 已提交
1931
    case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
1932 1933 1934 1935
    case VIR_DOMAIN_NET_TYPE_USER:
    case VIR_DOMAIN_NET_TYPE_SERVER:
    case VIR_DOMAIN_NET_TYPE_CLIENT:
    case VIR_DOMAIN_NET_TYPE_MCAST:
1936
    case VIR_DOMAIN_NET_TYPE_UDP:
1937 1938
    case VIR_DOMAIN_NET_TYPE_INTERNAL:
    case VIR_DOMAIN_NET_TYPE_DIRECT:
1939
    case VIR_DOMAIN_NET_TYPE_HOSTDEV:
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
    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);
1951
    } else {
1952 1953
        if (def->model != NULL && STREQ(def->model, "netfront")) {
            virBufferAddLit(buf, "(type netfront)");
1954
        } else {
1955
            if (def->model != NULL)
1956 1957
                virBufferEscapeSexpr(buf, "(model '%s')", def->model);
        }
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
    }

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

    virBufferAddLit(buf, ")");

    return 0;
}


P
Philipp Hahn 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977
/**
 * 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.
 */
1978
static void
M
Markus Groß 已提交
1979 1980
xenFormatSxprPCI(virDomainHostdevDefPtr def,
                 virBufferPtr buf)
1981
{
1982
    virBufferAsprintf(buf, "(dev (domain 0x%04x)(bus 0x%02x)(slot 0x%02x)(func 0x%x))",
1983 1984 1985 1986
                      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);
1987 1988
}

P
Philipp Hahn 已提交
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999

/**
 * 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.
 */
2000
int
M
Markus Groß 已提交
2001 2002 2003
xenFormatSxprOnePCI(virDomainHostdevDefPtr def,
                    virBufferPtr buf,
                    int detach)
2004 2005
{
    if (def->managed) {
2006 2007
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("managed PCI devices not supported with XenD"));
2008 2009 2010 2011
        return -1;
    }

    virBufferAddLit(buf, "(pci ");
M
Markus Groß 已提交
2012
    xenFormatSxprPCI(def, buf);
2013 2014 2015 2016 2017 2018 2019 2020 2021
    if (detach)
        virBufferAddLit(buf, "(state 'Closing')");
    else
        virBufferAddLit(buf, "(state 'Initialising')");
    virBufferAddLit(buf, ")");

    return 0;
}

P
Philipp Hahn 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031

/**
 * 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.
 */
2032
static int
M
Markus Groß 已提交
2033 2034
xenFormatSxprAllPCI(virDomainDefPtr def,
                    virBufferPtr buf)
2035 2036
{
    int hasPCI = 0;
2037
    size_t i;
2038

2039
    for (i = 0; i < def->nhostdevs; i++)
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
        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 ");
2062
    for (i = 0; i < def->nhostdevs; i++) {
2063 2064 2065
        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) {
2066 2067
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("managed PCI devices not supported with XenD"));
2068 2069 2070
                return -1;
            }

M
Markus Groß 已提交
2071
            xenFormatSxprPCI(def->hostdevs[i], buf);
2072 2073 2074 2075 2076 2077 2078
        }
    }
    virBufferAddLit(buf, "))");

    return 0;
}

P
Philipp Hahn 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088

/**
 * 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.
 */
2089
int
M
Markus Groß 已提交
2090 2091
xenFormatSxprSound(virDomainDefPtr def,
                   virBufferPtr buf)
2092 2093
{
    const char *str;
2094
    size_t i;
2095

2096
    for (i = 0; i < def->nsounds; i++) {
2097
        if (!(str = virDomainSoundModelTypeToString(def->sounds[i]->model))) {
2098 2099 2100
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("unexpected sound model %d"),
                           def->sounds[i]->model);
2101 2102 2103 2104 2105 2106 2107
            return -1;
        }
        if (i)
            virBufferAddChar(buf, ',');
        virBufferEscapeSexpr(buf, "%s", str);
    }

2108
    if (virBufferCheckError(buf) < 0)
2109 2110 2111 2112 2113 2114
        return -1;

    return 0;
}


P
Philipp Hahn 已提交
2115 2116 2117 2118 2119 2120 2121 2122 2123
/**
 * 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.
 */
2124
static int
M
Markus Groß 已提交
2125 2126
xenFormatSxprInput(virDomainInputDefPtr input,
                   virBufferPtr buf)
2127 2128 2129 2130 2131
{
    if (input->bus != VIR_DOMAIN_INPUT_BUS_USB)
        return 0;

    if (input->type != VIR_DOMAIN_INPUT_TYPE_MOUSE &&
2132 2133
        input->type != VIR_DOMAIN_INPUT_TYPE_TABLET &&
        input->type != VIR_DOMAIN_INPUT_TYPE_KBD) {
2134 2135
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected input type %d"), input->type);
2136 2137 2138
        return -1;
    }

2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
    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;
    }
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159

    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ß 已提交
2160
 * xenFormatSxpr:
2161 2162 2163
 * @conn: pointer to the hypervisor connection
 * @def: domain config definition
 *
P
Philipp Hahn 已提交
2164
 * Generate an S-expression representing the domain configuration.
2165 2166 2167 2168 2169
 *
 * Returns the 0 terminated S-Expr string or NULL in case of error.
 *         the caller must free() the returned value.
 */
char *
2170
xenFormatSxpr(virConnectPtr conn, virDomainDefPtr def)
2171 2172 2173 2174 2175
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char uuidstr[VIR_UUID_STRING_BUFLEN];
    const char *tmp;
    char *bufout;
2176 2177
    int hvm = 0, vmlocaltime = -1;
    size_t i;
P
Philipp Hahn 已提交
2178
    bool in_image = false;
2179

2180
    VIR_DEBUG("Formatting domain sexpr");
2181 2182 2183

    virBufferAddLit(&buf, "(vm ");
    virBufferEscapeSexpr(&buf, "(name '%s')", def->name);
2184
    virBufferAsprintf(&buf, "(memory %llu)(maxmem %llu)",
2185
                      VIR_DIV_UP(def->mem.cur_balloon, 1024),
2186
                      VIR_DIV_UP(virDomainDefGetMemoryActual(def), 1024));
2187
    virBufferAsprintf(&buf, "(vcpus %u)", virDomainDefGetVcpusMax(def));
2188 2189
    /* Computing the vcpu_avail bitmask works because MAX_VIRT_CPUS is
       either 32, or 64 on a platform where long is big enough.  */
2190
    if (virDomainDefHasVcpusOffline(def))
2191 2192
        virBufferAsprintf(&buf, "(vcpu_avail %lu)",
                          (1UL << virDomainDefGetVcpus(def)) - 1);
2193 2194

    if (def->cpumask) {
H
Hu Tao 已提交
2195
        char *ranges = virBitmapFormat(def->cpumask);
2196 2197 2198 2199 2200 2201 2202
        if (ranges == NULL)
            goto error;
        virBufferEscapeSexpr(&buf, "(cpus '%s')", ranges);
        VIR_FREE(ranges);
    }

    virUUIDFormat(def->uuid, uuidstr);
2203
    virBufferAsprintf(&buf, "(uuid '%s')", uuidstr);
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218

    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))) {
2219 2220
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onPoweroff);
2221 2222
        goto error;
    }
2223
    virBufferAsprintf(&buf, "(on_poweroff '%s')", tmp);
2224 2225

    if (!(tmp = virDomainLifecycleTypeToString(def->onReboot))) {
2226 2227
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onReboot);
2228 2229
        goto error;
    }
2230
    virBufferAsprintf(&buf, "(on_reboot '%s')", tmp);
2231 2232

    if (!(tmp = virDomainLifecycleCrashTypeToString(def->onCrash))) {
2233 2234
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle value %d"), def->onCrash);
2235 2236
        goto error;
    }
2237
    virBufferAsprintf(&buf, "(on_crash '%s')", tmp);
2238

2239
    if (def->os.type == VIR_DOMAIN_OSTYPE_HVM)
P
Philipp Hahn 已提交
2240
        hvm = 1;
2241 2242 2243 2244 2245 2246

    if (!def->os.bootloader) {
        if (hvm)
            virBufferAddLit(&buf, "(image (hvm ");
        else
            virBufferAddLit(&buf, "(image (linux ");
P
Philipp Hahn 已提交
2247
        in_image = true;
2248 2249 2250

        if (hvm &&
            def->os.loader == NULL) {
2251
            virReportError(VIR_ERR_INTERNAL_ERROR,
E
Eric Blake 已提交
2252
                           "%s", _("no HVM domain loader"));
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
            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)
2268
                virBufferEscapeSexpr(&buf, "(loader '%s')", def->os.loader->path);
2269
            else
2270
                virBufferEscapeSexpr(&buf, "(kernel '%s')", def->os.loader->path);
2271

2272
            virBufferAsprintf(&buf, "(vcpus %u)", virDomainDefGetVcpusMax(def));
2273
            if (virDomainDefHasVcpusOffline(def))
2274
                virBufferAsprintf(&buf, "(vcpu_avail %lu)",
2275
                                  (1UL << virDomainDefGetVcpus(def)) - 1);
2276

2277
            for (i = 0; i < def->os.nBootDevs; i++) {
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
                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';
            }
2300
            virBufferAsprintf(&buf, "(boot %s)", bootorder);
2301

J
Ján Tomko 已提交
2302
            if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_TRISTATE_SWITCH_ON)
2303
                virBufferAddLit(&buf, "(acpi 1)");
J
Ján Tomko 已提交
2304
            if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_TRISTATE_SWITCH_ON)
2305
                virBufferAddLit(&buf, "(apic 1)");
J
Ján Tomko 已提交
2306
            if (def->features[VIR_DOMAIN_FEATURE_PAE] == VIR_TRISTATE_SWITCH_ON)
2307
                virBufferAddLit(&buf, "(pae 1)");
J
Ján Tomko 已提交
2308
            if (def->features[VIR_DOMAIN_FEATURE_HAP] == VIR_TRISTATE_SWITCH_ON)
2309
                virBufferAddLit(&buf, "(hap 1)");
J
Ján Tomko 已提交
2310
            if (def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] == VIR_TRISTATE_SWITCH_ON)
2311
                virBufferAddLit(&buf, "(viridian 1)");
2312 2313 2314

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

2315
            for (i = 0; i < def->ninputs; i++)
M
Markus Groß 已提交
2316
                if (xenFormatSxprInput(def->inputs[i], &buf) < 0)
2317 2318 2319 2320
                    goto error;

            if (def->parallels) {
                virBufferAddLit(&buf, "(parallel ");
M
Markus Groß 已提交
2321
                if (xenFormatSxprChr(def->parallels[0], &buf) < 0)
2322 2323 2324 2325 2326 2327
                    goto error;
                virBufferAddLit(&buf, ")");
            } else {
                virBufferAddLit(&buf, "(parallel none)");
            }
            if (def->serials) {
2328
                if ((def->nserials > 1) || (def->serials[0]->target.port != 0)) {
2329 2330
                    int maxport = -1, port;
                    size_t j = 0;
2331 2332 2333 2334 2335 2336

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

2337
                    for (port = 0; port <= maxport; port++) {
2338 2339
                        virDomainChrDefPtr chr = NULL;

2340
                        if (port)
2341 2342
                            virBufferAddLit(&buf, " ");
                        for (j = 0; j < def->nserials; j++) {
2343
                            if (def->serials[j]->target.port == port) {
2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
                                chr = def->serials[j];
                                break;
                            }
                        }
                        if (chr) {
                            if (xenFormatSxprChr(chr, &buf) < 0)
                                goto error;
                        } else {
                            virBufferAddLit(&buf, "none");
                        }
                    }
                    virBufferAddLit(&buf, "))");
2356
                } else {
2357 2358 2359 2360 2361
                    virBufferAddLit(&buf, "(serial ");
                    if (xenFormatSxprChr(def->serials[0], &buf) < 0)
                        goto error;
                    virBufferAddLit(&buf, ")");
                }
2362 2363 2364 2365 2366 2367
            } else {
                virBufferAddLit(&buf, "(serial none)");
            }

            if (def->sounds) {
                virBufferAddLit(&buf, "(soundhw '");
M
Markus Groß 已提交
2368
                if (xenFormatSxprSound(def, &buf) < 0)
2369 2370 2371
                    goto error;
                virBufferAddLit(&buf, "')");
            }
P
Philipp Hahn 已提交
2372
        } /* hvm */
2373 2374

        /* get the device emulation model */
2375
        if (def->emulator && hvm)
2376 2377
            virBufferEscapeSexpr(&buf, "(device_model '%s')", def->emulator);

2378 2379 2380 2381 2382 2383 2384 2385 2386
        /* 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;
            }
        }
2387

2388
        /* PV graphics for xen <= 3.0.4, or HVM graphics */
2389
        if (hvm) {
2390
            if ((def->ngraphics == 1) &&
2391
                xenFormatSxprGraphicsOld(def->graphics[0], &buf) < 0)
2392 2393
                goto error;
        }
2394 2395 2396
    } else {
        /* PV domains accept kernel cmdline args */
        if (def->os.cmdline) {
P
Philipp Hahn 已提交
2397 2398
            virBufferEscapeSexpr(&buf, "(image (linux (args '%s')", def->os.cmdline);
            in_image = true;
2399
        }
P
Philipp Hahn 已提交
2400 2401
    } /* os.bootloader */

2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
    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;
P
Philipp Hahn 已提交
2412
        switch (def->clock.offset) {
2413 2414 2415 2416
        case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE:
            vmlocaltime = (int)def->clock.data.variable.basis;
            rtc_timeoffset = def->clock.data.variable.adjustment;
            break;
P
Philipp Hahn 已提交
2417
        case VIR_DOMAIN_CLOCK_OFFSET_UTC:
2418 2419 2420 2421 2422
            if (def->clock.data.utc_reset) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("unsupported clock adjustment='reset'"));
                goto error;
            }
P
Philipp Hahn 已提交
2423
            vmlocaltime = 0;
2424
            rtc_timeoffset = 0;
P
Philipp Hahn 已提交
2425 2426
            break;
        case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
2427 2428 2429 2430 2431
            if (def->clock.data.utc_reset) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("unsupported clock adjustment='reset'"));
                goto error;
            }
P
Philipp Hahn 已提交
2432
            vmlocaltime = 1;
2433
            rtc_timeoffset = 0;
P
Philipp Hahn 已提交
2434 2435
            break;
        default:
2436 2437 2438
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
2439 2440
            goto error;
        }
2441
        virBufferAsprintf(&buf, "(rtc_timeoffset %d)", rtc_timeoffset);
P
Philipp Hahn 已提交
2442
    } else {
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
        /* >=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:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
            goto error;
P
Philipp Hahn 已提交
2456
        }
2457 2458 2459 2460
    } /* !hvm */
    /* default post-XenD-3.1 location: */
    virBufferAsprintf(&buf, "(localtime %d)", vmlocaltime);

P
Philipp Hahn 已提交
2461 2462 2463 2464 2465 2466 2467 2468
    if (in_image) {
        /* closes (image(hvm|linux */
        virBufferAddLit(&buf, "))");
        in_image = false;
    }
    /* pre-XenD-3.1 and compatibility location */
    virBufferAsprintf(&buf, "(localtime %d)", vmlocaltime);

2469

2470
    for (i = 0; i < def->ndisks; i++)
2471
        if (xenFormatSxprDisk(def->disks[i], &buf, hvm, 0) < 0)
2472 2473
            goto error;

2474
    for (i = 0; i < def->nnets; i++)
2475
        if (xenFormatSxprNet(conn, def->nets[i], &buf, hvm, 0) < 0)
2476 2477
            goto error;

M
Markus Groß 已提交
2478
    if (xenFormatSxprAllPCI(def, &buf) < 0)
2479 2480
        goto error;

2481
    /* New style PV graphics config xen >= 3.0.4 */
2482
    if (!hvm) {
2483
        if ((def->ngraphics == 1) &&
M
Markus Groß 已提交
2484
            xenFormatSxprGraphicsNew(def->graphics[0], &buf) < 0)
2485 2486 2487 2488 2489
            goto error;
    }

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

2490
    if (virBufferCheckError(&buf) < 0)
2491 2492 2493 2494 2495 2496
        goto error;

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

2497
 error:
2498 2499 2500
    virBufferFreeAndReset(&buf);
    return NULL;
}