xen_xm.c 67.0 KB
Newer Older
1 2 3
/*
 * xen_xm.c: Xen XM parsing functions
 *
4
 * Copyright (C) 2006-2007, 2009-2010, 2012-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2011 Univention GmbH
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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: Daniel P. Berrange <berrange@redhat.com>
 * Author: Markus Groß <gross@univention.de>
 */

#include <config.h>

#include "internal.h"
29
#include "virerror.h"
30
#include "virconf.h"
31
#include "viralloc.h"
32
#include "verify.h"
33
#include "viruuid.h"
34
#include "virsexpr.h"
35 36 37 38
#include "count-one-bits.h"
#include "xenxs_private.h"
#include "xen_xm.h"
#include "xen_sxpr.h"
M
Michal Novotny 已提交
39
#include "domain_conf.h"
40
#include "virstoragefile.h"
41
#include "virstring.h"
42

43
/* Convenience method to grab a long int from the config file object */
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static int xenXMConfigGetBool(virConfPtr conf,
                              const char *name,
                              int *value,
                              int def) {
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

    if (val->type == VIR_CONF_LONG) {
        *value = val->l ? 1 : 0;
    } else if (val->type == VIR_CONF_STRING) {
        *value = STREQ(val->str, "1") ? 1 : 0;
    } else {
61 62
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
63 64 65 66 67 68 69 70 71 72
        return -1;
    }
    return 0;
}


/* Convenience method to grab a int from the config file object */
static int xenXMConfigGetULong(virConfPtr conf,
                               const char *name,
                               unsigned long *value,
73
                               unsigned long def) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

    if (val->type == VIR_CONF_LONG) {
        *value = val->l;
    } else if (val->type == VIR_CONF_STRING) {
        char *ret;
        *value = strtol(val->str, &ret, 10);
        if (ret == val->str) {
88 89
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("config value %s was malformed"), name);
90 91 92
            return -1;
        }
    } else {
93 94
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
95 96 97 98 99 100
        return -1;
    }
    return 0;
}


101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/* Convenience method to grab a int from the config file object */
static int xenXMConfigGetULongLong(virConfPtr conf,
                                   const char *name,
                                   unsigned long long *value,
                                   unsigned long long def) {
    virConfValuePtr val;

    *value = 0;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

    if (val->type == VIR_CONF_LONG) {
        *value = val->l;
    } else if (val->type == VIR_CONF_STRING) {
        char *ret;
        *value = strtoll(val->str, &ret, 10);
        if (ret == val->str) {
120 121
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("config value %s was malformed"), name);
122 123 124
            return -1;
        }
    } else {
125 126
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
127 128 129 130 131 132
        return -1;
    }
    return 0;
}


133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* Convenience method to grab a string from the config file object */
static int xenXMConfigGetString(virConfPtr conf,
                                const char *name,
                                const char **value,
                                const char *def) {
    virConfValuePtr val;

    *value = NULL;
    if (!(val = virConfGetValue(conf, name))) {
        *value = def;
        return 0;
    }

    if (val->type != VIR_CONF_STRING) {
147 148
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        return -1;
    }
    if (!val->str)
        *value = def;
    else
        *value = val->str;
    return 0;
}

static int xenXMConfigCopyStringInternal(virConfPtr conf,
                                         const char *name,
                                         char **value,
                                         int allowMissing) {
    virConfValuePtr val;

    *value = NULL;
    if (!(val = virConfGetValue(conf, name))) {
        if (allowMissing)
            return 0;
168 169
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was missing"), name);
170 171 172 173
        return -1;
    }

    if (val->type != VIR_CONF_STRING) {
174 175
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was not a string"), name);
176 177 178 179 180
        return -1;
    }
    if (!val->str) {
        if (allowMissing)
            return 0;
181 182
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was missing"), name);
183 184 185
        return -1;
    }

186
    return VIR_STRDUP(*value, val->str);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
}


static int xenXMConfigCopyString(virConfPtr conf,
                                 const char *name,
                                 char **value) {
    return xenXMConfigCopyStringInternal(conf, name, value, 0);
}

static int xenXMConfigCopyStringOpt(virConfPtr conf,
                                    const char *name,
                                    char **value) {
    return xenXMConfigCopyStringInternal(conf, name, value, 1);
}


/* Convenience method to grab a string UUID from the config file object */
static int xenXMConfigGetUUID(virConfPtr conf, const char *name, unsigned char *uuid) {
    virConfValuePtr val;
206 207

    if (!uuid || !name || !conf) {
208
        virReportError(VIR_ERR_INVALID_ARG, "%s",
209
                       _("Arguments must be non null"));
210 211 212
        return -1;
    }

213
    if (!(val = virConfGetValue(conf, name))) {
214 215 216 217 218 219 220
        if (virUUIDGenerate(uuid)) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           "%s", _("Failed to generate UUID"));
            return -1;
        } else {
            return 0;
        }
221 222
    }

223
    if (val->type != VIR_CONF_STRING) {
224 225
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("config value %s not a string"), name);
226 227 228 229
        return -1;
    }

    if (!val->str) {
230 231
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("%s can't be empty"), name);
232 233
        return -1;
    }
234

235
    if (virUUIDParse(val->str, uuid) < 0) {
236 237
        virReportError(VIR_ERR_CONF_SYNTAX,
                       _("%s not parseable"), val->str);
238 239
        return -1;
    }
240

241
    return 0;
242 243 244 245 246 247 248 249
}

#define MAX_VFB 1024
/*
 * Turn a config record into a lump of XML describing the
 * domain, suitable for later feeding for virDomainCreateXML
 */
virDomainDefPtr
M
Markus Groß 已提交
250
xenParseXM(virConfPtr conf, int xendConfigVersion,
251 252 253 254 255 256 257 258 259 260
                       virCapsPtr caps) {
    const char *str;
    int hvm = 0;
    int val;
    virConfValuePtr list;
    virDomainDefPtr def = NULL;
    virDomainDiskDefPtr disk = NULL;
    virDomainNetDefPtr net = NULL;
    virDomainGraphicsDefPtr graphics = NULL;
    virDomainHostdevDefPtr hostdev = NULL;
261
    size_t i;
262
    const char *defaultMachine;
263 264
    int vmlocaltime = 0;
    unsigned long count;
265
    char *script = NULL;
266
    char *listenAddr = NULL;
267

268
    if (VIR_ALLOC(def) < 0)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        return NULL;

    def->virtType = VIR_DOMAIN_VIRT_XEN;
    def->id = -1;

    if (xenXMConfigCopyString(conf, "name", &def->name) < 0)
        goto cleanup;
    if (xenXMConfigGetUUID(conf, "uuid", def->uuid) < 0)
        goto cleanup;


    if ((xenXMConfigGetString(conf, "builder", &str, "linux") == 0) &&
        STREQ(str, "hvm"))
        hvm = 1;

284 285
    if (VIR_STRDUP(def->os.type, hvm ? "hvm" : "xen") < 0)
        goto cleanup;
286

287 288 289 290 291
    def->os.arch =
        virCapabilitiesDefaultGuestArch(caps,
                                        def->os.type,
                                        virDomainVirtTypeToString(def->virtType));
    if (!def->os.arch) {
292 293 294
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no supported architecture for os type '%s'"),
                       def->os.type);
295 296 297 298 299 300 301 302
        goto cleanup;
    }

    defaultMachine = virCapabilitiesDefaultGuestMachine(caps,
                                                        def->os.type,
                                                        def->os.arch,
                                                        virDomainVirtTypeToString(def->virtType));
    if (defaultMachine != NULL) {
303 304
        if (VIR_STRDUP(def->os.machine, defaultMachine) < 0)
            goto cleanup;
305 306 307 308 309 310 311 312 313 314
    }

    if (hvm) {
        const char *boot;
        if (xenXMConfigCopyString(conf, "kernel", &def->os.loader) < 0)
            goto cleanup;

        if (xenXMConfigGetString(conf, "boot", &boot, "c") < 0)
            goto cleanup;

315
        for (i = 0; i < VIR_DOMAIN_BOOT_LAST && boot[i]; i++) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
            switch (*boot) {
            case 'a':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_FLOPPY;
                break;
            case 'd':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_CDROM;
                break;
            case 'n':
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_NET;
                break;
            case 'c':
            default:
                def->os.bootDevs[i] = VIR_DOMAIN_BOOT_DISK;
                break;
            }
            def->os.nBootDevs++;
        }
    } else {
        if (xenXMConfigCopyStringOpt(conf, "bootloader", &def->os.bootloader) < 0)
            goto cleanup;
        if (xenXMConfigCopyStringOpt(conf, "bootargs", &def->os.bootloaderArgs) < 0)
            goto cleanup;

        if (xenXMConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
            goto cleanup;
        if (xenXMConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
            goto cleanup;
        if (xenXMConfigCopyStringOpt(conf, "extra", &def->os.cmdline) < 0)
            goto cleanup;
    }

347 348
    if (xenXMConfigGetULongLong(conf, "memory", &def->mem.cur_balloon,
                                MIN_XEN_GUEST_SIZE * 2) < 0)
349 350
        goto cleanup;

351 352
    if (xenXMConfigGetULongLong(conf, "maxmem", &def->mem.max_balloon,
                                def->mem.cur_balloon) < 0)
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        goto cleanup;

    def->mem.cur_balloon *= 1024;
    def->mem.max_balloon *= 1024;

    if (xenXMConfigGetULong(conf, "vcpus", &count, 1) < 0 ||
        MAX_VIRT_CPUS < count)
        goto cleanup;
    def->maxvcpus = count;
    if (xenXMConfigGetULong(conf, "vcpu_avail", &count, -1) < 0)
        goto cleanup;
    def->vcpus = MIN(count_one_bits_l(count), def->maxvcpus);

    if (xenXMConfigGetString(conf, "cpus", &str, NULL) < 0)
        goto cleanup;
H
Hu Tao 已提交
368
    if (str && (virBitmapParse(str, 0, &def->cpumask, 4096) < 0))
369 370 371 372 373
            goto cleanup;

    if (xenXMConfigGetString(conf, "on_poweroff", &str, "destroy") < 0)
        goto cleanup;
    if ((def->onPoweroff = virDomainLifecycleTypeFromString(str)) < 0) {
374 375
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_poweroff"), str);
376 377 378 379 380 381
        goto cleanup;
    }

    if (xenXMConfigGetString(conf, "on_reboot", &str, "restart") < 0)
        goto cleanup;
    if ((def->onReboot = virDomainLifecycleTypeFromString(str)) < 0) {
382 383
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_reboot"), str);
384 385 386 387 388 389
        goto cleanup;
    }

    if (xenXMConfigGetString(conf, "on_crash", &str, "restart") < 0)
        goto cleanup;
    if ((def->onCrash = virDomainLifecycleCrashTypeFromString(str)) < 0) {
390 391
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_crash"), str);
392 393 394 395 396 397 398 399 400
        goto cleanup;
    }



    if (hvm) {
        if (xenXMConfigGetBool(conf, "pae", &val, 0) < 0)
            goto cleanup;
        else if (val)
401
            def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_DOMAIN_FEATURE_STATE_ON;
402 403 404
        if (xenXMConfigGetBool(conf, "acpi", &val, 0) < 0)
            goto cleanup;
        else if (val)
405
            def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_DOMAIN_FEATURE_STATE_ON;
406 407 408
        if (xenXMConfigGetBool(conf, "apic", &val, 0) < 0)
            goto cleanup;
        else if (val)
409
            def->features[VIR_DOMAIN_FEATURE_APIC] = VIR_DOMAIN_FEATURE_STATE_ON;
410 411 412
        if (xenXMConfigGetBool(conf, "hap", &val, 0) < 0)
            goto cleanup;
        else if (val)
413
            def->features[VIR_DOMAIN_FEATURE_HAP] = VIR_DOMAIN_FEATURE_STATE_ON;
414 415 416
        if (xenXMConfigGetBool(conf, "viridian", &val, 0) < 0)
            goto cleanup;
        else if (val)
417
            def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_DOMAIN_FEATURE_STATE_ON;
418 419 420 421 422 423 424

        if (xenXMConfigGetBool(conf, "hpet", &val, -1) < 0)
            goto cleanup;
        else if (val != -1) {
            virDomainTimerDefPtr timer;

            if (VIR_ALLOC_N(def->clock.timers, 1) < 0 ||
425
                VIR_ALLOC(timer) < 0)
426 427 428 429 430 431 432 433 434
                goto cleanup;

            timer->name = VIR_DOMAIN_TIMER_NAME_HPET;
            timer->present = val;
            timer->tickpolicy = -1;

            def->clock.ntimers = 1;
            def->clock.timers[0] = timer;
        }
435 436 437 438
    }
    if (xenXMConfigGetBool(conf, "localtime", &vmlocaltime, 0) < 0)
        goto cleanup;

P
Philipp Hahn 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    if (hvm) {
        /* only managed HVM domains since 3.1.0 have persistent rtc_timeoffset */
        if (xendConfigVersion < XEND_CONFIG_VERSION_3_1_0) {
            if (vmlocaltime)
                def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME;
            else
                def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_UTC;
            def->clock.data.utc_reset = true;
        } else {
            unsigned long rtc_timeoffset;
            def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
            if (xenXMConfigGetULong(conf, "rtc_timeoffset", &rtc_timeoffset, 0) < 0)
                goto cleanup;
            def->clock.data.variable.adjustment = (int)rtc_timeoffset;
            def->clock.data.variable.basis = vmlocaltime ?
                VIR_DOMAIN_CLOCK_BASIS_LOCALTIME :
                VIR_DOMAIN_CLOCK_BASIS_UTC;
        }
    } else {
        /* PV domains do not have an emulated RTC and the offset is fixed. */
        def->clock.offset = vmlocaltime ?
            VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME :
            VIR_DOMAIN_CLOCK_OFFSET_UTC;
        def->clock.data.utc_reset = true;
    } /* !hvm */
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

    if (xenXMConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
        goto cleanup;

    list = virConfGetValue(conf, "disk");
    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char *head;
            char *offset;
            char *tmp;

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skipdisk;
            head = list->str;

            if (VIR_ALLOC(disk) < 0)
481
                goto cleanup;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

            /*
             * Disks have 3 components, SOURCE,DEST-DEVICE,MODE
             * eg, phy:/dev/HostVG/XenGuest1,xvda,w
             * The SOURCE is usually prefixed with a driver type,
             * and optionally driver sub-type
             * The DEST-DEVICE is optionally post-fixed with disk type
             */

            /* Extract the source file path*/
            if (!(offset = strchr(head, ',')))
                goto skipdisk;

            if (offset == head) {
                disk->src = NULL; /* No source file given, eg CDROM with no media */
            } else {
                if (VIR_ALLOC_N(disk->src, (offset - head) + 1) < 0)
499
                    goto cleanup;
500 501
                if (virStrncpy(disk->src, head, offset - head,
                               (offset - head) + 1) == NULL) {
502 503 504
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("Source file %s too big for destination"),
                                   head);
505 506 507 508 509 510 511 512 513 514 515 516 517
                    goto cleanup;
                }
            }
            head = offset + 1;

            /* Remove legacy ioemu: junk */
            if (STRPREFIX(head, "ioemu:"))
                head = head + 6;

            /* Extract the dest device name */
            if (!(offset = strchr(head, ',')))
                goto skipdisk;
            if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
518
                goto cleanup;
519 520
            if (virStrncpy(disk->dst, head, offset - head,
                           (offset - head) + 1) == NULL) {
521 522
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Dest file %s too big for destination"), head);
523 524 525 526 527 528 529 530 531 532
                goto cleanup;
            }
            head = offset + 1;


            /* Extract source driver type */
            if (disk->src) {
                /* The main type  phy:, file:, tap: ... */
                if ((tmp = strchr(disk->src, ':')) != NULL) {
                    if (VIR_ALLOC_N(disk->driverName, (tmp - disk->src) + 1) < 0)
533
                        goto cleanup;
534 535 536
                    if (virStrncpy(disk->driverName, disk->src,
                                   (tmp - disk->src),
                                   (tmp - disk->src) + 1) == NULL) {
537 538 539
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Driver name %s too big for destination"),
                                       disk->src);
540 541 542 543 544 545 546 547 548 549 550
                        goto cleanup;
                    }

                    /* Strip the prefix we found off the source file name */
                    memmove(disk->src, disk->src+(tmp-disk->src)+1,
                            strlen(disk->src)-(tmp-disk->src));
                }

                /* And the sub-type for tap:XXX: type */
                if (disk->driverName &&
                    STREQ(disk->driverName, "tap")) {
551 552
                    char *driverType;

553 554
                    if (!(tmp = strchr(disk->src, ':')))
                        goto skipdisk;
555

556 557
                    if (VIR_STRNDUP(driverType, disk->src, tmp - disk->src) < 0)
                        goto cleanup;
558 559 560 561 562 563 564
                    if (STREQ(driverType, "aio"))
                        disk->format = VIR_STORAGE_FILE_RAW;
                    else
                        disk->format =
                            virStorageFileFormatTypeFromString(driverType);
                    VIR_FREE(driverType);
                    if (disk->format <= 0) {
565
                        virReportError(VIR_ERR_INTERNAL_ERROR,
566
                                       _("Unknown driver type %s"),
567
                                       disk->src);
568 569 570 571 572 573 574 575 576 577 578
                        goto cleanup;
                    }

                    /* Strip the prefix we found off the source file name */
                    memmove(disk->src, disk->src+(tmp-disk->src)+1,
                            strlen(disk->src)-(tmp-disk->src));
                }
            }

            /* No source, or driver name, so fix to phy: */
            if (!disk->driverName &&
579 580
                VIR_STRDUP(disk->driverName, "phy") < 0)
                goto cleanup;
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604


            /* phy: type indicates a block device */
            disk->type = STREQ(disk->driverName, "phy") ?
                VIR_DOMAIN_DISK_TYPE_BLOCK : VIR_DOMAIN_DISK_TYPE_FILE;

            /* Check for a :cdrom/:disk postfix */
            disk->device = VIR_DOMAIN_DISK_DEVICE_DISK;
            if ((tmp = strchr(disk->dst, ':')) != NULL) {
                if (STREQ(tmp, ":cdrom"))
                    disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
                tmp[0] = '\0';
            }

            if (STRPREFIX(disk->dst, "xvd") || !hvm) {
                disk->bus = VIR_DOMAIN_DISK_BUS_XEN;
            } else if (STRPREFIX(disk->dst, "sd")) {
                disk->bus = VIR_DOMAIN_DISK_BUS_SCSI;
            } else {
                disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
            }

            if (STREQ(head, "r") ||
                STREQ(head, "ro"))
605
                disk->readonly = true;
606 607
            else if ((STREQ(head, "w!")) ||
                     (STREQ(head, "!")))
608
                disk->shared = true;
609 610 611

            /* Maintain list in sorted order according to target device name */
            if (VIR_REALLOC_N(def->disks, def->ndisks+1) < 0)
612
                goto cleanup;
613 614 615 616 617 618 619 620 621
            def->disks[def->ndisks++] = disk;
            disk = NULL;

            skipdisk:
            list = list->next;
            virDomainDiskDefFree(disk);
        }
    }

622
    if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
623 624 625 626
        if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0)
            goto cleanup;
        if (str) {
            if (VIR_ALLOC(disk) < 0)
627
                goto cleanup;
628 629 630

            disk->type = VIR_DOMAIN_DISK_TYPE_FILE;
            disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
631 632 633 634 635 636
            if (VIR_STRDUP(disk->driverName, "file") < 0)
                goto cleanup;
            if (VIR_STRDUP(disk->src, str) < 0)
                goto cleanup;
            if (VIR_STRDUP(disk->dst, "hdc") < 0)
                goto cleanup;
637
            disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
638
            disk->readonly = true;
639 640

            if (VIR_REALLOC_N(def->disks, def->ndisks+1) < 0)
641
                goto cleanup;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
            def->disks[def->ndisks++] = disk;
            disk = NULL;
        }
    }

    list = virConfGetValue(conf, "vif");
    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char model[10];
            char type[10];
            char ip[16];
            char mac[18];
            char bridge[50];
            char vifname[50];
            char *key;

            bridge[0] = '\0';
            mac[0] = '\0';
            ip[0] = '\0';
            model[0] = '\0';
            type[0] = '\0';
            vifname[0] = '\0';

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skipnic;

            key = list->str;
            while (key) {
                char *data;
                char *nextkey = strchr(key, ',');

                if (!(data = strchr(key, '=')))
                    goto skipnic;
                data++;

                if (STRPREFIX(key, "mac=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(mac) - 1;
                    if (virStrncpy(mac, data, len, sizeof(mac)) == NULL) {
681 682 683
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("MAC address %s too big for destination"),
                                       data);
684 685 686 687 688
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "bridge=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(bridge) - 1;
                    if (virStrncpy(bridge, data, len, sizeof(bridge)) == NULL) {
689 690 691
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Bridge %s too big for destination"),
                                       data);
692 693 694
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "script=")) {
695 696
                    int len = nextkey ? (nextkey - data) : strlen(data);
                    VIR_FREE(script);
697 698
                    if (VIR_STRNDUP(script, data, len) < 0)
                        goto cleanup;
699 700 701
                } else if (STRPREFIX(key, "model=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(model) - 1;
                    if (virStrncpy(model, data, len, sizeof(model)) == NULL) {
702 703
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Model %s too big for destination"), data);
704 705 706 707 708
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "type=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(type) - 1;
                    if (virStrncpy(type, data, len, sizeof(type)) == NULL) {
709 710
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Type %s too big for destination"), data);
711 712 713 714 715
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "vifname=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(vifname) - 1;
                    if (virStrncpy(vifname, data, len, sizeof(vifname)) == NULL) {
716 717 718
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Vifname %s too big for destination"),
                                       data);
719 720 721 722 723
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "ip=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(ip) - 1;
                    if (virStrncpy(ip, data, len, sizeof(ip)) == NULL) {
724 725
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("IP %s too big for destination"), data);
726 727 728 729 730 731 732 733 734 735 736 737
                        goto skipnic;
                    }
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }

            if (VIR_ALLOC(net) < 0)
738
                goto cleanup;
739 740

            if (mac[0]) {
741
                if (virMacAddrParse(mac, &net->mac) < 0) {
742 743
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("malformed mac address '%s'"), mac);
744 745 746 747
                    goto cleanup;
                }
            }

748 749
            if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") ||
                STREQ_NULLABLE(script, "vif-vnic")) {
750 751 752 753 754 755
                net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
            } else {
                net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            }

            if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
756 757 758 759
                if (bridge[0] && VIR_STRDUP(net->data.bridge.brname, bridge) < 0)
                    goto cleanup;
                if (ip[0] && VIR_STRDUP(net->data.bridge.ipaddr, ip) < 0)
                    goto cleanup;
760
            } else {
761 762
                if (ip[0] && VIR_STRDUP(net->data.ethernet.ipaddr, ip) < 0)
                    goto cleanup;
763 764
            }

765
            if (script && script[0] &&
766 767
                VIR_STRDUP(net->script, script) < 0)
                goto cleanup;
768

769
            if (model[0] &&
770
                VIR_STRDUP(net->model, model) < 0)
771
                goto cleanup;
772

773 774 775
            if (!model[0] && type[0] && STREQ(type, "netfront") &&
                VIR_STRDUP(net->model, "netfront") < 0)
                goto cleanup;
776 777

            if (vifname[0] &&
778 779
                VIR_STRDUP(net->ifname, vifname) < 0)
                goto cleanup;
780 781

            if (VIR_REALLOC_N(def->nets, def->nnets+1) < 0)
782
                goto cleanup;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
            def->nets[def->nnets++] = net;
            net = NULL;

        skipnic:
            list = list->next;
            virDomainNetDefFree(net);
        }
    }

    list = virConfGetValue(conf, "pci");
    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char domain[5];
            char bus[3];
            char slot[3];
            char func[2];
            char *key, *nextkey;
            int domainID;
            int busID;
            int slotID;
            int funcID;

            domain[0] = bus[0] = slot[0] = func[0] = '\0';

            if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                goto skippci;

            /* pci=['0000:00:1b.0','0000:00:13.0'] */
            if (!(key = list->str))
                goto skippci;
            if (!(nextkey = strchr(key, ':')))
                goto skippci;

            if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) == NULL) {
818 819
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Domain %s too big for destination"), key);
820 821 822 823 824 825 826 827
                goto skippci;
            }

            key = nextkey + 1;
            if (!(nextkey = strchr(key, ':')))
                goto skippci;

            if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) == NULL) {
828 829
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Bus %s too big for destination"), key);
830 831 832 833 834 835 836 837
                goto skippci;
            }

            key = nextkey + 1;
            if (!(nextkey = strchr(key, '.')))
                goto skippci;

            if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) == NULL) {
838 839
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Slot %s too big for destination"), key);
840 841 842 843 844 845 846 847
                goto skippci;
            }

            key = nextkey + 1;
            if (strlen(key) != 1)
                goto skippci;

            if (virStrncpy(func, key, 1, sizeof(func)) == NULL) {
848 849
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Function %s too big for destination"), key);
850 851 852 853 854 855 856 857 858 859 860 861
                goto skippci;
            }

            if (virStrToLong_i(domain, NULL, 16, &domainID) < 0)
                goto skippci;
            if (virStrToLong_i(bus, NULL, 16, &busID) < 0)
                goto skippci;
            if (virStrToLong_i(slot, NULL, 16, &slotID) < 0)
                goto skippci;
            if (virStrToLong_i(func, NULL, 16, &funcID) < 0)
                goto skippci;

862 863
            if (!(hostdev = virDomainHostdevDefAlloc()))
               goto cleanup;
864

865
            hostdev->managed = false;
866
            hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
867 868 869 870
            hostdev->source.subsys.u.pci.addr.domain = domainID;
            hostdev->source.subsys.u.pci.addr.bus = busID;
            hostdev->source.subsys.u.pci.addr.slot = slotID;
            hostdev->source.subsys.u.pci.addr.function = funcID;
871

872 873
            if (VIR_REALLOC_N(def->hostdevs, def->nhostdevs+1) < 0) {
                virDomainHostdevDefFree(hostdev);
874
                goto cleanup;
875
            }
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
            def->hostdevs[def->nhostdevs++] = hostdev;
            hostdev = NULL;

        skippci:
            list = list->next;
        }
    }

    if (hvm) {
        if (xenXMConfigGetString(conf, "usbdevice", &str, NULL) < 0)
            goto cleanup;
        if (str &&
            (STREQ(str, "tablet") ||
             STREQ(str, "mouse"))) {
            virDomainInputDefPtr input;
            if (VIR_ALLOC(input) < 0)
892
                goto cleanup;
893 894 895 896 897 898
            input->bus = VIR_DOMAIN_INPUT_BUS_USB;
            input->type = STREQ(str, "tablet") ?
                VIR_DOMAIN_INPUT_TYPE_TABLET :
                VIR_DOMAIN_INPUT_TYPE_MOUSE;
            if (VIR_ALLOC_N(def->inputs, 1) < 0) {
                virDomainInputDefFree(input);
899
                goto cleanup;
900 901 902 903 904 905 906
            }
            def->inputs[0] = input;
            def->ninputs = 1;
        }
    }

    /* HVM guests, or old PV guests use this config format */
907
    if (hvm || xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
908 909 910 911 912
        if (xenXMConfigGetBool(conf, "vnc", &val, 0) < 0)
            goto cleanup;

        if (val) {
            if (VIR_ALLOC(graphics) < 0)
913
                goto cleanup;
914 915 916 917 918 919 920 921 922 923 924
            graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;
            if (xenXMConfigGetBool(conf, "vncunused", &val, 1) < 0)
                goto cleanup;
            graphics->data.vnc.autoport = val ? 1 : 0;

            if (!graphics->data.vnc.autoport) {
                unsigned long vncdisplay;
                if (xenXMConfigGetULong(conf, "vncdisplay", &vncdisplay, 0) < 0)
                    goto cleanup;
                graphics->data.vnc.port = (int)vncdisplay + 5900;
            }
925 926

            if (xenXMConfigCopyStringOpt(conf, "vnclisten", &listenAddr) < 0)
927
                goto cleanup;
928 929 930 931 932 933 934
            if (listenAddr &&
                virDomainGraphicsListenSetAddress(graphics, 0, listenAddr,
                                                  -1, true) < 0) {
               goto cleanup;
            }
            VIR_FREE(listenAddr);

935 936 937 938 939 940
            if (xenXMConfigCopyStringOpt(conf, "vncpasswd", &graphics->data.vnc.auth.passwd) < 0)
                goto cleanup;
            if (xenXMConfigCopyStringOpt(conf, "keymap", &graphics->data.vnc.keymap) < 0)
                goto cleanup;

            if (VIR_ALLOC_N(def->graphics, 1) < 0)
941
                goto cleanup;
942 943 944 945 946 947 948 949
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
        } else {
            if (xenXMConfigGetBool(conf, "sdl", &val, 0) < 0)
                goto cleanup;
            if (val) {
                if (VIR_ALLOC(graphics) < 0)
950
                    goto cleanup;
951 952 953 954 955 956
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
                if (xenXMConfigCopyStringOpt(conf, "display", &graphics->data.sdl.display) < 0)
                    goto cleanup;
                if (xenXMConfigCopyStringOpt(conf, "xauthority", &graphics->data.sdl.xauth) < 0)
                    goto cleanup;
                if (VIR_ALLOC_N(def->graphics, 1) < 0)
957
                    goto cleanup;
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
                def->graphics[0] = graphics;
                def->ngraphics = 1;
                graphics = NULL;
            }
        }
    }

    if (!hvm && def->graphics == NULL) { /* New PV guests use this format */
        list = virConfGetValue(conf, "vfb");
        if (list && list->type == VIR_CONF_LIST &&
            list->list && list->list->type == VIR_CONF_STRING &&
            list->list->str) {
            char vfb[MAX_VFB];
            char *key = vfb;

            if (virStrcpyStatic(vfb, list->list->str) == NULL) {
974 975 976
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VFB %s too big for destination"),
                               list->list->str);
977 978 979 980
                goto cleanup;
            }

            if (VIR_ALLOC(graphics) < 0)
981
                goto cleanup;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

            if (strstr(key, "type=sdl"))
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_SDL;
            else
                graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_VNC;

            while (key) {
                char *nextkey = strchr(key, ',');
                char *end = nextkey;
                if (nextkey) {
                    *end = '\0';
                    nextkey++;
                }

                if (!strchr(key, '='))
                    break;

                if (graphics->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
                    if (STRPREFIX(key, "vncunused=")) {
                        if (STREQ(key + 10, "1"))
1002
                            graphics->data.vnc.autoport = true;
1003
                    } else if (STRPREFIX(key, "vnclisten=")) {
1004 1005 1006
                        if (virDomainGraphicsListenSetAddress(graphics, 0, key+10,
                                                              -1, true) < 0)
                            goto cleanup;
1007
                    } else if (STRPREFIX(key, "vncpasswd=")) {
1008 1009
                        if (VIR_STRDUP(graphics->data.vnc.auth.passwd, key + 10) < 0)
                            goto cleanup;
1010
                    } else if (STRPREFIX(key, "keymap=")) {
1011 1012
                        if (VIR_STRDUP(graphics->data.vnc.keymap, key + 7) < 0)
                            goto cleanup;
1013 1014 1015 1016 1017
                    } else if (STRPREFIX(key, "vncdisplay=")) {
                        graphics->data.vnc.port = strtol(key+11, NULL, 10) + 5900;
                    }
                } else {
                    if (STRPREFIX(key, "display=")) {
1018 1019
                        if (VIR_STRDUP(graphics->data.sdl.display, key + 8) < 0)
                            goto cleanup;
1020
                    } else if (STRPREFIX(key, "xauthority=")) {
1021 1022
                        if (VIR_STRDUP(graphics->data.sdl.xauth, key + 11) < 0)
                            goto cleanup;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
                    }
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }
            if (VIR_ALLOC_N(def->graphics, 1) < 0)
1033
                goto cleanup;
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
        }
    }

    if (hvm) {
        virDomainChrDefPtr chr = NULL;

        if (xenXMConfigGetString(conf, "parallel", &str, NULL) < 0)
            goto cleanup;
        if (str && STRNEQ(str, "none") &&
M
Markus Groß 已提交
1046
            !(chr = xenParseSxprChar(str, NULL)))
1047 1048 1049 1050 1051
            goto cleanup;

        if (chr) {
            if (VIR_ALLOC_N(def->parallels, 1) < 0) {
                virDomainChrDefFree(chr);
1052
                goto cleanup;
1053 1054
            }
            chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
M
Michal Novotny 已提交
1055
            chr->target.port = 0;
1056 1057 1058 1059 1060
            def->parallels[0] = chr;
            def->nparallels++;
            chr = NULL;
        }

1061 1062 1063 1064
        /* Try to get the list of values to support multiple serial ports */
        list = virConfGetValue(conf, "serial");
        if (list && list->type == VIR_CONF_LIST) {
            int portnum = -1;
1065

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
            list = list->list;
            while (list) {
                char *port = NULL;

                if ((list->type != VIR_CONF_STRING) || (list->str == NULL))
                    goto cleanup;

                port = list->str;
                portnum++;
                if (STREQ(port, "none")) {
                    list = list->next;
                    continue;
                }

                if (!(chr = xenParseSxprChar(port, NULL)))
                    goto cleanup;

1083 1084
                if (VIR_REALLOC_N(def->serials, def->nserials+1) < 0) {
                    virDomainChrDefFree(chr);
1085
                    goto cleanup;
1086
                }
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105

                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = portnum;

                def->serials[def->nserials++] = chr;
                chr = NULL;

                list = list->next;
            }
        } else {
            /* If domain is not using multiple serial ports we parse data old way */
            if (xenXMConfigGetString(conf, "serial", &str, NULL) < 0)
                goto cleanup;
            if (str && STRNEQ(str, "none") &&
                !(chr = xenParseSxprChar(str, NULL)))
                goto cleanup;
            if (chr) {
                if (VIR_ALLOC_N(def->serials, 1) < 0) {
                    virDomainChrDefFree(chr);
1106
                    goto cleanup;
1107 1108
                }
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
M
Michal Novotny 已提交
1109
                chr->target.port = 0;
1110 1111
                def->serials[0] = chr;
                def->nserials++;
1112 1113 1114
            }
        }
    } else {
1115
        if (VIR_ALLOC_N(def->consoles, 1) < 0)
1116
            goto cleanup;
1117
        def->nconsoles = 1;
1118
        if (!(def->consoles[0] = xenParseSxprChar("pty", NULL)))
1119
            goto cleanup;
1120 1121 1122
        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;
1123 1124 1125 1126 1127 1128 1129
    }

    if (hvm) {
        if (xenXMConfigGetString(conf, "soundhw", &str, NULL) < 0)
            goto cleanup;

        if (str &&
M
Markus Groß 已提交
1130
            xenParseSxprSound(def, str) < 0)
1131 1132 1133
            goto cleanup;
    }

1134
    VIR_FREE(script);
1135 1136 1137 1138 1139 1140 1141
    return def;

cleanup:
    virDomainGraphicsDefFree(graphics);
    virDomainNetDefFree(net);
    virDomainDiskDefFree(disk);
    virDomainDefFree(def);
1142
    VIR_FREE(script);
1143
    VIR_FREE(listenAddr);
1144 1145
    return NULL;
}
1146 1147 1148


static
1149
int xenXMConfigSetInt(virConfPtr conf, const char *setting, long long l) {
1150 1151
    virConfValuePtr value = NULL;

1152
    if ((long) l != l) {
1153 1154
        virReportError(VIR_ERR_OVERFLOW, _("failed to store %lld to %s"),
                       l, setting);
1155 1156
        return -1;
    }
1157
    if (VIR_ALLOC(value) < 0)
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
        return -1;

    value->type = VIR_CONF_LONG;
    value->next = NULL;
    value->l = l;

    return virConfSetValue(conf, setting, value);
}


static
int xenXMConfigSetString(virConfPtr conf, const char *setting, const char *str) {
    virConfValuePtr value = NULL;

1172
    if (VIR_ALLOC(value) < 0)
1173 1174 1175 1176
        return -1;

    value->type = VIR_CONF_STRING;
    value->next = NULL;
1177
    if (VIR_STRDUP(value->str, str) < 0) {
1178 1179 1180 1181 1182 1183 1184 1185
        VIR_FREE(value);
        return -1;
    }

    return virConfSetValue(conf, setting, value);
}


M
Markus Groß 已提交
1186
static int xenFormatXMDisk(virConfValuePtr list,
1187 1188 1189 1190 1191 1192 1193
                                       virDomainDiskDefPtr disk,
                                       int hvm,
                                       int xendConfigVersion)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;

1194
    if (disk->src) {
1195 1196 1197 1198
        if (disk->format) {
            const char *type;

            if (disk->format == VIR_STORAGE_FILE_RAW)
1199
                type = "aio";
1200 1201
            else
                type = virStorageFileFormatTypeToString(disk->format);
1202
            virBufferAsprintf(&buf, "%s:", disk->driverName);
1203
            if (STREQ(disk->driverName, "tap"))
1204
                virBufferAsprintf(&buf, "%s:", type);
1205 1206 1207 1208 1209 1210 1211 1212 1213
        } else {
            switch (disk->type) {
            case VIR_DOMAIN_DISK_TYPE_FILE:
                virBufferAddLit(&buf, "file:");
                break;
            case VIR_DOMAIN_DISK_TYPE_BLOCK:
                virBufferAddLit(&buf, "phy:");
                break;
            default:
1214 1215 1216
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unsupported disk type %s"),
                               virDomainDiskTypeToString(disk->type));
1217 1218 1219
                goto cleanup;
            }
        }
1220
        virBufferAdd(&buf, disk->src, -1);
1221 1222
    }
    virBufferAddLit(&buf, ",");
1223
    if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2)
1224 1225
        virBufferAddLit(&buf, "ioemu:");

1226
    virBufferAdd(&buf, disk->dst, -1);
1227 1228 1229 1230 1231 1232 1233 1234 1235
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
        virBufferAddLit(&buf, ":cdrom");

    if (disk->readonly)
        virBufferAddLit(&buf, ",r");
    else if (disk->shared)
        virBufferAddLit(&buf, ",!");
    else
        virBufferAddLit(&buf, ",w");
1236
    if (disk->transient) {
1237
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1238
                       _("transient disks not supported yet"));
1239 1240
        return -1;
    }
1241 1242 1243 1244 1245 1246

    if (virBufferError(&buf)) {
        virReportOOMError();
        goto cleanup;
    }

1247
    if (VIR_ALLOC(val) < 0)
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
        goto cleanup;

    val->type = VIR_CONF_STRING;
    val->str = virBufferContentAndReset(&buf);
    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = val;
    else
        list->list = val;

    return 0;

cleanup:
    virBufferFreeAndReset(&buf);
    return -1;
}

1267 1268 1269 1270 1271 1272 1273 1274 1275
static int xenFormatXMSerial(virConfValuePtr list,
                             virDomainChrDefPtr serial)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
    int ret;

    if (serial) {
        ret = xenFormatSxprChr(serial, &buf);
1276
        if (ret < 0)
1277 1278 1279 1280 1281 1282 1283 1284 1285
            goto cleanup;
    } else {
        virBufferAddLit(&buf, "none");
    }
    if (virBufferError(&buf)) {
        virReportOOMError();
        goto cleanup;
    }

1286
    if (VIR_ALLOC(val) < 0)
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
        goto cleanup;

    val->type = VIR_CONF_STRING;
    val->str = virBufferContentAndReset(&buf);
    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = val;
    else
        list->list = val;

    return 0;

cleanup:
    virBufferFreeAndReset(&buf);
    return -1;
}

M
Markus Groß 已提交
1306
static int xenFormatXMNet(virConnectPtr conn,
1307 1308 1309 1310 1311 1312
                                      virConfValuePtr list,
                                      virDomainNetDefPtr net,
                                      int hvm, int xendConfigVersion)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
1313
    char macaddr[VIR_MAC_STRING_BUFLEN];
1314

1315
    virBufferAsprintf(&buf, "mac=%s", virMacAddrFormat(&net->mac, macaddr));
1316 1317 1318

    switch (net->type) {
    case VIR_DOMAIN_NET_TYPE_BRIDGE:
1319
        virBufferAsprintf(&buf, ",bridge=%s", net->data.bridge.brname);
1320
        if (net->data.bridge.ipaddr)
1321 1322
            virBufferAsprintf(&buf, ",ip=%s", net->data.bridge.ipaddr);
        virBufferAsprintf(&buf, ",script=%s", DEFAULT_VIF_SCRIPT);
1323 1324 1325
        break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
1326 1327
        if (net->script)
            virBufferAsprintf(&buf, ",script=%s", net->script);
1328
        if (net->data.ethernet.ipaddr)
1329
            virBufferAsprintf(&buf, ",ip=%s", net->data.ethernet.ipaddr);
1330 1331 1332 1333 1334 1335 1336
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
    {
        virNetworkPtr network = virNetworkLookupByName(conn, net->data.network.name);
        char *bridge;
        if (!network) {
1337 1338
            virReportError(VIR_ERR_NO_NETWORK, "%s",
                           net->data.network.name);
1339 1340 1341 1342 1343
            return -1;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (!bridge) {
1344 1345 1346
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("network %s is not active"),
                           net->data.network.name);
1347 1348 1349
            return -1;
        }

1350 1351
        virBufferAsprintf(&buf, ",bridge=%s", bridge);
        virBufferAsprintf(&buf, ",script=%s", DEFAULT_VIF_SCRIPT);
1352 1353 1354 1355
    }
    break;

    default:
1356 1357 1358
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network type %d"),
                       net->type);
1359 1360 1361 1362 1363
        goto cleanup;
    }

    if (!hvm) {
        if (net->model != NULL)
1364
            virBufferAsprintf(&buf, ",model=%s", net->model);
1365 1366
    }
    else {
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
        if (net->model != NULL && STREQ(net->model, "netfront")) {
            virBufferAddLit(&buf, ",type=netfront");
        }
        else {
            if (net->model != NULL)
                virBufferAsprintf(&buf, ",model=%s", net->model);

            /*
             * apparently type ioemu breaks paravirt drivers on HVM so skip this
             * from XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU
             */
            if (xendConfigVersion <= XEND_CONFIG_MAX_VERS_NET_TYPE_IOEMU)
                virBufferAddLit(&buf, ",type=ioemu");
        }
1381 1382 1383
    }

    if (net->ifname)
1384
        virBufferAsprintf(&buf, ",vifname=%s",
1385 1386 1387 1388 1389 1390 1391
                          net->ifname);

    if (virBufferError(&buf)) {
        virReportOOMError();
        goto cleanup;
    }

1392
    if (VIR_ALLOC(val) < 0)
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
        goto cleanup;

    val->type = VIR_CONF_STRING;
    val->str = virBufferContentAndReset(&buf);
    tmp = list->list;
    while (tmp && tmp->next)
        tmp = tmp->next;
    if (tmp)
        tmp->next = val;
    else
        list->list = val;

    return 0;

cleanup:
    virBufferFreeAndReset(&buf);
    return -1;
}



static int
M
Markus Groß 已提交
1415
xenFormatXMPCI(virConfPtr conf,
1416 1417 1418 1419 1420
                           virDomainDefPtr def)
{

    virConfValuePtr pciVal = NULL;
    int hasPCI = 0;
1421
    size_t i;
1422

1423
    for (i = 0; i < def->nhostdevs; i++)
1424 1425 1426 1427 1428 1429 1430
        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;

1431
    if (VIR_ALLOC(pciVal) < 0)
1432 1433 1434 1435 1436
        return -1;

    pciVal->type = VIR_CONF_LIST;
    pciVal->list = NULL;

1437
    for (i = 0; i < def->nhostdevs; i++) {
1438 1439 1440 1441 1442 1443
        if (def->hostdevs[i]->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
            def->hostdevs[i]->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
            virConfValuePtr val, tmp;
            char *buf;

            if (virAsprintf(&buf, "%04x:%02x:%02x.%x",
1444 1445 1446
                            def->hostdevs[i]->source.subsys.u.pci.addr.domain,
                            def->hostdevs[i]->source.subsys.u.pci.addr.bus,
                            def->hostdevs[i]->source.subsys.u.pci.addr.slot,
1447
                            def->hostdevs[i]->source.subsys.u.pci.addr.function) < 0)
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
                goto error;

            if (VIR_ALLOC(val) < 0) {
                VIR_FREE(buf);
                goto error;
            }
            val->type = VIR_CONF_STRING;
            val->str = buf;
            tmp = pciVal->list;
            while (tmp && tmp->next)
                tmp = tmp->next;
            if (tmp)
                tmp->next = val;
            else
                pciVal->list = val;
        }
    }

    if (pciVal->list != NULL) {
        int ret = virConfSetValue(conf, "pci", pciVal);
        pciVal = NULL;
        if (ret < 0)
            return -1;
    }
    VIR_FREE(pciVal);

    return 0;

error:
    virConfFreeValue(pciVal);
    return -1;
}


/* 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ß 已提交
1486
virConfPtr xenFormatXM(virConnectPtr conn,
1487 1488 1489
                                   virDomainDefPtr def,
                                   int xendConfigVersion) {
    virConfPtr conf = NULL;
1490 1491
    int hvm = 0, vmlocaltime = 0;
    size_t i;
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    char *cpus = NULL;
    const char *lifecycle;
    char uuid[VIR_UUID_STRING_BUFLEN];
    virConfValuePtr diskVal = NULL;
    virConfValuePtr netVal = NULL;

    if (!(conf = virConfNew()))
        goto cleanup;


    if (xenXMConfigSetString(conf, "name", def->name) < 0)
1503
        goto cleanup;
1504 1505 1506

    virUUIDFormat(def->uuid, uuid);
    if (xenXMConfigSetString(conf, "uuid", uuid) < 0)
1507
        goto cleanup;
1508

1509 1510
    if (xenXMConfigSetInt(conf, "maxmem",
                          VIR_DIV_UP(def->mem.max_balloon, 1024)) < 0)
1511
        goto cleanup;
1512

1513 1514
    if (xenXMConfigSetInt(conf, "memory",
                          VIR_DIV_UP(def->mem.cur_balloon, 1024)) < 0)
1515
        goto cleanup;
1516 1517

    if (xenXMConfigSetInt(conf, "vcpus", def->maxvcpus) < 0)
1518
        goto cleanup;
1519 1520 1521 1522
    /* Computing the vcpu_avail bitmask works because MAX_VIRT_CPUS is
       either 32, or 64 on a platform where long is big enough.  */
    if (def->vcpus < def->maxvcpus &&
        xenXMConfigSetInt(conf, "vcpu_avail", (1UL << def->vcpus) - 1) < 0)
1523
        goto cleanup;
1524 1525

    if ((def->cpumask != NULL) &&
H
Hu Tao 已提交
1526
        ((cpus = virBitmapFormat(def->cpumask)) == NULL)) {
1527
        goto cleanup;
H
Hu Tao 已提交
1528
    }
1529 1530 1531

    if (cpus &&
        xenXMConfigSetString(conf, "cpus", cpus) < 0)
1532
        goto cleanup;
1533 1534 1535 1536 1537 1538 1539
    VIR_FREE(cpus);

    hvm = STREQ(def->os.type, "hvm") ? 1 : 0;

    if (hvm) {
        char boot[VIR_DOMAIN_BOOT_LAST+1];
        if (xenXMConfigSetString(conf, "builder", "hvm") < 0)
1540
            goto cleanup;
1541 1542 1543

        if (def->os.loader &&
            xenXMConfigSetString(conf, "kernel", def->os.loader) < 0)
1544
            goto cleanup;
1545

1546
        for (i = 0; i < def->os.nBootDevs; i++) {
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
            switch (def->os.bootDevs[i]) {
            case VIR_DOMAIN_BOOT_FLOPPY:
                boot[i] = 'a';
                break;
            case VIR_DOMAIN_BOOT_CDROM:
                boot[i] = 'd';
                break;
            case VIR_DOMAIN_BOOT_NET:
                boot[i] = 'n';
                break;
            case VIR_DOMAIN_BOOT_DISK:
            default:
                boot[i] = 'c';
                break;
            }
        }
        if (!def->os.nBootDevs) {
            boot[0] = 'c';
            boot[1] = '\0';
        } else {
            boot[def->os.nBootDevs] = '\0';
        }

        if (xenXMConfigSetString(conf, "boot", boot) < 0)
1571
            goto cleanup;
1572 1573

        if (xenXMConfigSetInt(conf, "pae",
1574 1575
                              (def->features[VIR_DOMAIN_FEATURE_PAE] ==
                               VIR_DOMAIN_FEATURE_STATE_ON) ? 1 : 0) < 0)
1576
            goto cleanup;
1577 1578

        if (xenXMConfigSetInt(conf, "acpi",
1579 1580
                              (def->features[VIR_DOMAIN_FEATURE_ACPI] ==
                               VIR_DOMAIN_FEATURE_STATE_ON) ? 1 : 0) < 0)
1581
            goto cleanup;
1582 1583

        if (xenXMConfigSetInt(conf, "apic",
1584 1585
                              (def->features[VIR_DOMAIN_FEATURE_APIC] ==
                               VIR_DOMAIN_FEATURE_STATE_ON) ? 1 : 0) < 0)
1586
            goto cleanup;
1587

1588
        if (xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4) {
1589
            if (xenXMConfigSetInt(conf, "hap",
1590 1591
                                  (def->features[VIR_DOMAIN_FEATURE_HAP] ==
                                   VIR_DOMAIN_FEATURE_STATE_ON) ? 1 : 0) < 0)
1592
                goto cleanup;
1593

1594
            if (xenXMConfigSetInt(conf, "viridian",
1595 1596
                                  (def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] ==
                                   VIR_DOMAIN_FEATURE_STATE_ON) ? 1 : 0) < 0)
1597
                goto cleanup;
1598 1599
        }

1600 1601 1602 1603
        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 &&
                xenXMConfigSetInt(conf, "hpet", def->clock.timers[i]->present) < 0)
1604
                goto cleanup;
1605 1606
        }

1607
        if (xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
1608
            for (i = 0; i < def->ndisks; i++) {
1609 1610 1611 1612 1613 1614
                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
                    def->disks[i]->dst &&
                    STREQ(def->disks[i]->dst, "hdc") &&
                    def->disks[i]->src) {
                    if (xenXMConfigSetString(conf, "cdrom",
                                             def->disks[i]->src) < 0)
1615
                        goto cleanup;
1616 1617 1618 1619 1620 1621 1622 1623 1624
                    break;
                }
            }
        }

        /* XXX floppy disks */
    } else {
        if (def->os.bootloader &&
            xenXMConfigSetString(conf, "bootloader", def->os.bootloader) < 0)
1625
            goto cleanup;
1626 1627
        if (def->os.bootloaderArgs &&
            xenXMConfigSetString(conf, "bootargs", def->os.bootloaderArgs) < 0)
1628
            goto cleanup;
1629 1630
        if (def->os.kernel &&
            xenXMConfigSetString(conf, "kernel", def->os.kernel) < 0)
1631
            goto cleanup;
1632 1633
        if (def->os.initrd &&
            xenXMConfigSetString(conf, "ramdisk", def->os.initrd) < 0)
1634
            goto cleanup;
1635 1636
        if (def->os.cmdline &&
            xenXMConfigSetString(conf, "extra", def->os.cmdline) < 0)
1637
            goto cleanup;
P
Philipp Hahn 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
    } /* !hvm */


    if (xendConfigVersion < XEND_CONFIG_VERSION_3_1_0) {
        /* <3.1: UTC and LOCALTIME */
        switch (def->clock.offset) {
        case VIR_DOMAIN_CLOCK_OFFSET_UTC:
            vmlocaltime = 0;
            break;
        case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
            vmlocaltime = 1;
            break;
        default:
1651 1652 1653
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
            goto cleanup;
        }
    } else {
        if (hvm) {
            /* >=3.1 HV: VARIABLE */
            int rtc_timeoffset;
            switch (def->clock.offset) {
            case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE:
                vmlocaltime = (int)def->clock.data.variable.basis;
                rtc_timeoffset = def->clock.data.variable.adjustment;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_UTC:
                if (def->clock.data.utc_reset) {
1667
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1668
                                   _("unsupported clock adjustment='reset'"));
P
Philipp Hahn 已提交
1669 1670 1671 1672 1673 1674 1675
                    goto cleanup;
                }
                vmlocaltime = 0;
                rtc_timeoffset = 0;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
                if (def->clock.data.utc_reset) {
1676
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1677
                                   _("unsupported clock adjustment='reset'"));
P
Philipp Hahn 已提交
1678 1679 1680 1681 1682 1683
                    goto cleanup;
                }
                vmlocaltime = 1;
                rtc_timeoffset = 0;
                break;
            default:
1684 1685 1686
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
1687 1688 1689
                goto cleanup;
            }
            if (xenXMConfigSetInt(conf, "rtc_timeoffset", rtc_timeoffset) < 0)
1690
                goto cleanup;
P
Philipp Hahn 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
        } else {
            /* >=3.1 PV: UTC and LOCALTIME */
            switch (def->clock.offset) {
            case VIR_DOMAIN_CLOCK_OFFSET_UTC:
                vmlocaltime = 0;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
                vmlocaltime = 1;
                break;
            default:
1701 1702 1703
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
P
Philipp Hahn 已提交
1704 1705 1706
                goto cleanup;
            }
        } /* !hvm */
1707
    }
P
Philipp Hahn 已提交
1708
    if (xenXMConfigSetInt(conf, "localtime", vmlocaltime) < 0)
1709
        goto cleanup;
P
Philipp Hahn 已提交
1710

1711 1712

    if (!(lifecycle = virDomainLifecycleTypeToString(def->onPoweroff))) {
1713 1714
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onPoweroff);
1715 1716 1717
        goto cleanup;
    }
    if (xenXMConfigSetString(conf, "on_poweroff", lifecycle) < 0)
1718
        goto cleanup;
1719 1720 1721


    if (!(lifecycle = virDomainLifecycleTypeToString(def->onReboot))) {
1722 1723
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onReboot);
1724 1725 1726
        goto cleanup;
    }
    if (xenXMConfigSetString(conf, "on_reboot", lifecycle) < 0)
1727
        goto cleanup;
1728 1729 1730


    if (!(lifecycle = virDomainLifecycleCrashTypeToString(def->onCrash))) {
1731 1732
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onCrash);
1733 1734 1735
        goto cleanup;
    }
    if (xenXMConfigSetString(conf, "on_crash", lifecycle) < 0)
1736
        goto cleanup;
1737 1738 1739 1740 1741 1742



    if (hvm) {
        if (def->emulator &&
            xenXMConfigSetString(conf, "device_model", def->emulator) < 0)
1743
            goto cleanup;
1744

1745
        for (i = 0; i < def->ninputs; i++) {
1746 1747
            if (def->inputs[i]->bus == VIR_DOMAIN_INPUT_BUS_USB) {
                if (xenXMConfigSetInt(conf, "usb", 1) < 0)
1748
                    goto cleanup;
1749 1750 1751
                if (xenXMConfigSetString(conf, "usbdevice",
                                         def->inputs[i]->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ?
                                         "mouse" : "tablet") < 0)
1752
                    goto cleanup;
1753 1754 1755 1756 1757 1758
                break;
            }
        }
    }

    if (def->ngraphics == 1) {
1759
        if (hvm || (xendConfigVersion < XEND_CONFIG_MIN_VERS_PVFB_NEWCONF)) {
1760 1761
            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                if (xenXMConfigSetInt(conf, "sdl", 1) < 0)
1762
                    goto cleanup;
1763
                if (xenXMConfigSetInt(conf, "vnc", 0) < 0)
1764
                    goto cleanup;
1765 1766 1767
                if (def->graphics[0]->data.sdl.display &&
                    xenXMConfigSetString(conf, "display",
                                     def->graphics[0]->data.sdl.display) < 0)
1768
                    goto cleanup;
1769 1770 1771
                if (def->graphics[0]->data.sdl.xauth &&
                    xenXMConfigSetString(conf, "xauthority",
                                         def->graphics[0]->data.sdl.xauth) < 0)
1772
                    goto cleanup;
1773
            } else {
1774 1775
                const char *listenAddr;

1776
                if (xenXMConfigSetInt(conf, "sdl", 0) < 0)
1777
                    goto cleanup;
1778
                if (xenXMConfigSetInt(conf, "vnc", 1) < 0)
1779
                    goto cleanup;
1780 1781
                if (xenXMConfigSetInt(conf, "vncunused",
                              def->graphics[0]->data.vnc.autoport ? 1 : 0) < 0)
1782
                    goto cleanup;
1783 1784 1785
                if (!def->graphics[0]->data.vnc.autoport &&
                    xenXMConfigSetInt(conf, "vncdisplay",
                                  def->graphics[0]->data.vnc.port - 5900) < 0)
1786
                    goto cleanup;
1787 1788 1789
                listenAddr = virDomainGraphicsListenGetAddress(def->graphics[0], 0);
                if (listenAddr &&
                    xenXMConfigSetString(conf, "vnclisten", listenAddr) < 0)
1790
                    goto cleanup;
1791 1792 1793
                if (def->graphics[0]->data.vnc.auth.passwd &&
                    xenXMConfigSetString(conf, "vncpasswd",
                                        def->graphics[0]->data.vnc.auth.passwd) < 0)
1794
                    goto cleanup;
1795 1796 1797
                if (def->graphics[0]->data.vnc.keymap &&
                    xenXMConfigSetString(conf, "keymap",
                                        def->graphics[0]->data.vnc.keymap) < 0)
1798
                    goto cleanup;
1799 1800 1801 1802 1803 1804 1805 1806
            }
        } else {
            virConfValuePtr vfb, disp;
            char *vfbstr = NULL;
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                virBufferAddLit(&buf, "type=sdl");
                if (def->graphics[0]->data.sdl.display)
1807
                    virBufferAsprintf(&buf, ",display=%s",
1808 1809
                                      def->graphics[0]->data.sdl.display);
                if (def->graphics[0]->data.sdl.xauth)
1810
                    virBufferAsprintf(&buf, ",xauthority=%s",
1811 1812
                                      def->graphics[0]->data.sdl.xauth);
            } else {
1813 1814 1815
                const char *listenAddr
                    = virDomainGraphicsListenGetAddress(def->graphics[0], 0);

1816
                virBufferAddLit(&buf, "type=vnc");
1817
                virBufferAsprintf(&buf, ",vncunused=%d",
1818 1819
                                  def->graphics[0]->data.vnc.autoport ? 1 : 0);
                if (!def->graphics[0]->data.vnc.autoport)
1820
                    virBufferAsprintf(&buf, ",vncdisplay=%d",
1821
                                      def->graphics[0]->data.vnc.port - 5900);
1822 1823
                if (listenAddr)
                    virBufferAsprintf(&buf, ",vnclisten=%s", listenAddr);
1824
                if (def->graphics[0]->data.vnc.auth.passwd)
1825
                    virBufferAsprintf(&buf, ",vncpasswd=%s",
1826 1827
                                      def->graphics[0]->data.vnc.auth.passwd);
                if (def->graphics[0]->data.vnc.keymap)
1828
                    virBufferAsprintf(&buf, ",keymap=%s",
1829 1830 1831 1832
                                      def->graphics[0]->data.vnc.keymap);
            }
            if (virBufferError(&buf)) {
                virBufferFreeAndReset(&buf);
1833 1834
                virReportOOMError();
                goto cleanup;
1835 1836 1837 1838 1839 1840
            }

            vfbstr = virBufferContentAndReset(&buf);

            if (VIR_ALLOC(vfb) < 0) {
                VIR_FREE(vfbstr);
1841
                goto cleanup;
1842 1843 1844 1845 1846
            }

            if (VIR_ALLOC(disp) < 0) {
                VIR_FREE(vfb);
                VIR_FREE(vfbstr);
1847
                goto cleanup;
1848 1849 1850 1851 1852 1853 1854 1855
            }

            vfb->type = VIR_CONF_LIST;
            vfb->list = disp;
            disp->type = VIR_CONF_STRING;
            disp->str = vfbstr;

            if (virConfSetValue(conf, "vfb", vfb) < 0)
1856
                goto cleanup;
1857 1858 1859 1860 1861
        }
    }

    /* analyze of the devices */
    if (VIR_ALLOC(diskVal) < 0)
1862
        goto cleanup;
1863 1864 1865
    diskVal->type = VIR_CONF_LIST;
    diskVal->list = NULL;

1866
    for (i = 0; i < def->ndisks; i++) {
1867
        if (xendConfigVersion == XEND_CONFIG_VERSION_3_0_2 &&
1868 1869 1870 1871 1872 1873 1874 1875
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
            def->disks[i]->dst &&
            STREQ(def->disks[i]->dst, "hdc")) {
            continue;
        }
        if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_FLOPPY)
            continue;

M
Markus Groß 已提交
1876 1877
        if (xenFormatXMDisk(diskVal, def->disks[i],
                            hvm, xendConfigVersion) < 0)
1878 1879 1880 1881 1882 1883
            goto cleanup;
    }
    if (diskVal->list != NULL) {
        int ret = virConfSetValue(conf, "disk", diskVal);
        diskVal = NULL;
        if (ret < 0)
1884
            goto cleanup;
1885 1886 1887 1888
    }
    VIR_FREE(diskVal);

    if (VIR_ALLOC(netVal) < 0)
1889
        goto cleanup;
1890 1891 1892
    netVal->type = VIR_CONF_LIST;
    netVal->list = NULL;

1893
    for (i = 0; i < def->nnets; i++) {
M
Markus Groß 已提交
1894 1895
        if (xenFormatXMNet(conn, netVal,def->nets[i],
                           hvm, xendConfigVersion) < 0)
1896 1897 1898 1899 1900 1901
            goto cleanup;
    }
    if (netVal->list != NULL) {
        int ret = virConfSetValue(conf, "vif", netVal);
        netVal = NULL;
        if (ret < 0)
1902
            goto cleanup;
1903 1904 1905
    }
    VIR_FREE(netVal);

M
Markus Groß 已提交
1906
    if (xenFormatXMPCI(conf, def) < 0)
1907 1908 1909 1910 1911 1912 1913 1914
        goto cleanup;

    if (hvm) {
        if (def->nparallels) {
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            char *str;
            int ret;

M
Markus Groß 已提交
1915
            ret = xenFormatSxprChr(def->parallels[0], &buf);
1916 1917 1918 1919 1920
            str = virBufferContentAndReset(&buf);
            if (ret == 0)
                ret = xenXMConfigSetString(conf, "parallel", str);
            VIR_FREE(str);
            if (ret < 0)
1921
                goto cleanup;
1922 1923
        } else {
            if (xenXMConfigSetString(conf, "parallel", "none") < 0)
1924
                goto cleanup;
1925 1926 1927
        }

        if (def->nserials) {
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
            if ((def->nserials == 1) && (def->serials[0]->target.port == 0)) {
                virBuffer buf = VIR_BUFFER_INITIALIZER;
                char *str;
                int ret;

                ret = xenFormatSxprChr(def->serials[0], &buf);
                str = virBufferContentAndReset(&buf);
                if (ret == 0)
                    ret = xenXMConfigSetString(conf, "serial", str);
                VIR_FREE(str);
                if (ret < 0)
1939
                    goto cleanup;
1940
            } else {
1941 1942
                size_t j = 0;
                int maxport = -1, port;
1943
                virConfValuePtr serialVal = NULL;
1944

1945
                if (VIR_ALLOC(serialVal) < 0)
1946
                    goto cleanup;
1947 1948 1949 1950 1951 1952 1953
                serialVal->type = VIR_CONF_LIST;
                serialVal->list = NULL;

                for (i = 0; i < def->nserials; i++)
                    if (def->serials[i]->target.port > maxport)
                        maxport = def->serials[i]->target.port;

1954
                for (port = 0; port <= maxport; port++) {
1955 1956
                    virDomainChrDefPtr chr = NULL;
                    for (j = 0; j < def->nserials; j++) {
1957
                        if (def->serials[j]->target.port == port) {
1958 1959 1960 1961
                            chr = def->serials[j];
                            break;
                        }
                    }
1962 1963
                    if (xenFormatXMSerial(serialVal, chr) < 0) {
                        virConfFreeValue(serialVal);
1964
                        goto cleanup;
1965
                    }
1966 1967 1968 1969 1970 1971
                }

                if (serialVal->list != NULL) {
                    int ret = virConfSetValue(conf, "serial", serialVal);
                    serialVal = NULL;
                    if (ret < 0)
1972
                        goto cleanup;
1973 1974 1975
                }
                VIR_FREE(serialVal);
            }
1976 1977
        } else {
            if (xenXMConfigSetString(conf, "serial", "none") < 0)
1978
                goto cleanup;
1979 1980 1981 1982 1983 1984
        }


        if (def->sounds) {
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            char *str = NULL;
M
Markus Groß 已提交
1985
            int ret = xenFormatSxprSound(def, &buf);
1986 1987 1988 1989 1990 1991
            str = virBufferContentAndReset(&buf);
            if (ret == 0)
                ret = xenXMConfigSetString(conf, "soundhw", str);

            VIR_FREE(str);
            if (ret < 0)
1992
                goto cleanup;
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
        }
    }

    return conf;

cleanup:
    virConfFreeValue(diskVal);
    virConfFreeValue(netVal);
    VIR_FREE(cpus);
    if (conf)
        virConfFree(conf);
2004
    return NULL;
2005
}