xen_xm.c 73.7 KB
Newer Older
1 2 3
/*
 * xen_xm.c: Xen XM parsing functions
 *
4
 * Copyright (C) 2006-2007, 2009-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2011 Univention GmbH
 * Copyright (C) 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
static int xenXMConfigGetBool(virConfPtr conf,
                              const char *name,
                              int *value,
47 48
                              int def)
{
49 50 51 52 53 54 55 56 57 58 59 60 61
    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 {
62 63
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
64 65 66 67 68 69 70 71 72 73
        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,
74 75
                               unsigned long def)
{
76 77 78 79 80 81 82 83 84 85 86
    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) {
87
        if (virStrToLong_ul(val->str, NULL, 10, value) < 0) {
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
/* Convenience method to grab a int from the config file object */
static int xenXMConfigGetULongLong(virConfPtr conf,
                                   const char *name,
                                   unsigned long long *value,
105 106
                                   unsigned long long def)
{
107 108 109 110 111 112 113 114 115 116 117
    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) {
118
        if (virStrToLong_ull(val->str, NULL, 10, value) < 0) {
119 120
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("config value %s was malformed"), name);
121 122 123
            return -1;
        }
    } else {
124 125
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("config value %s was malformed"), name);
126 127 128 129 130 131
        return -1;
    }
    return 0;
}


132 133 134 135
/* Convenience method to grab a string from the config file object */
static int xenXMConfigGetString(virConfPtr conf,
                                const char *name,
                                const char **value,
136 137
                                const char *def)
{
138 139 140 141 142 143 144 145 146
    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
        return -1;
    }
    if (!val->str)
        *value = def;
    else
        *value = val->str;
    return 0;
}

static int xenXMConfigCopyStringInternal(virConfPtr conf,
                                         const char *name,
                                         char **value,
161 162
                                         int allowMissing)
{
163 164 165 166 167 168
    virConfValuePtr val;

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

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

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


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 */
205 206 207
static int
xenXMConfigGetUUID(virConfPtr conf, const char *name, unsigned char *uuid)
{
208
    virConfValuePtr val;
209 210

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

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

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

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

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

244
    return 0;
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

static int
xenParseXMMem(virConfPtr conf, virDomainDefPtr def)
{
    if (xenXMConfigGetULongLong(conf, "memory", &def->mem.cur_balloon,
                                MIN_XEN_GUEST_SIZE * 2) < 0)
        return -1;

    if (xenXMConfigGetULongLong(conf, "maxmem", &def->mem.max_balloon,
                                def->mem.cur_balloon) < 0)
        return -1;

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

    return 0;
}


266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static int
xenParseXMTimeOffset(virConfPtr conf, virDomainDefPtr def,
                     int xendConfigVersion)
{
    int vmlocaltime;

    if (xenXMConfigGetBool(conf, "localtime", &vmlocaltime, 0) < 0)
        return -1;

    if (STREQ(def->os.type, "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)
                return -1;

            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 */

    return 0;
}


306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static int
xenParseXMEventsActions(virConfPtr conf, virDomainDefPtr def)
{
    const char *str = NULL;

    if (xenXMConfigGetString(conf, "on_poweroff", &str, "destroy") < 0)
        return -1;

    if ((def->onPoweroff = virDomainLifecycleTypeFromString(str)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_poweroff"), str);
        return -1;
    }

    if (xenXMConfigGetString(conf, "on_reboot", &str, "restart") < 0)
        return -1;

    if ((def->onReboot = virDomainLifecycleTypeFromString(str)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_reboot"), str);
        return -1;
    }

    if (xenXMConfigGetString(conf, "on_crash", &str, "restart") < 0)
        return -1;

    if ((def->onCrash = virDomainLifecycleCrashTypeFromString(str)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected value %s for on_crash"), str);
        return -1;
    }

    return 0;
}


342 343 344 345 346 347 348 349 350 351 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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
static int
xenParseXMPCI(virConfPtr conf, virDomainDefPtr def)
{
    virConfValuePtr list = virConfGetValue(conf, "pci");
    virDomainHostdevDefPtr hostdev = NULL;

    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) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Domain %s too big for destination"), key);
                goto skippci;
            }

            key = nextkey + 1;
            if (!(nextkey = strchr(key, ':')))
                goto skippci;
            if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) == NULL) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Bus %s too big for destination"), key);
                goto skippci;
            }

            key = nextkey + 1;
            if (!(nextkey = strchr(key, '.')))
                goto skippci;
            if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) == NULL) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Slot %s too big for destination"), key);
                goto skippci;
            }

            key = nextkey + 1;
            if (strlen(key) != 1)
                goto skippci;
            if (virStrncpy(func, key, 1, sizeof(func)) == NULL) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Function %s too big for destination"), key);
                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;
            if (!(hostdev = virDomainHostdevDefAlloc()))
               return -1;

            hostdev->managed = false;
            hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI;
            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;

            if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) {
                virDomainHostdevDefFree(hostdev);
                return -1;
            }

        skippci:
            list = list->next;
        }
    }

    return 0;
}


435 436 437 438 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
static int
xenParseXMCPUFeatures(virConfPtr conf, virDomainDefPtr def)
{
    unsigned long count = 0;
    const char *str = NULL;
    int val = 0;

    if (xenXMConfigGetULong(conf, "vcpus", &count, 1) < 0 ||
        MAX_VIRT_CPUS < count)
        return -1;

    def->maxvcpus = count;
    if (xenXMConfigGetULong(conf, "vcpu_avail", &count, -1) < 0)
        return -1;

    def->vcpus = MIN(count_one_bits_l(count), def->maxvcpus);
    if (xenXMConfigGetString(conf, "cpus", &str, NULL) < 0)
        return -1;

    if (str && (virBitmapParse(str, 0, &def->cpumask, 4096) < 0))
        return -1;

    if (STREQ(def->os.type, "hvm")) {
        if (xenXMConfigGetBool(conf, "pae", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_PAE] = VIR_TRISTATE_SWITCH_ON;
        if (xenXMConfigGetBool(conf, "acpi", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_ACPI] = VIR_TRISTATE_SWITCH_ON;
        if (xenXMConfigGetBool(conf, "apic", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_APIC] = VIR_TRISTATE_SWITCH_ON;
        if (xenXMConfigGetBool(conf, "hap", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_HAP] = VIR_TRISTATE_SWITCH_ON;
        if (xenXMConfigGetBool(conf, "viridian", &val, 0) < 0)
            return -1;

        else if (val)
            def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] = VIR_TRISTATE_SWITCH_ON;

        if (xenXMConfigGetBool(conf, "hpet", &val, -1) < 0)
            return -1;

        else if (val != -1) {
            virDomainTimerDefPtr timer;

            if (VIR_ALLOC_N(def->clock.timers, 1) < 0 ||
                VIR_ALLOC(timer) < 0)
                return -1;

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

            def->clock.ntimers = 1;
            def->clock.timers[0] = timer;
        }
    }

    return 0;
}


507 508
static int
xenParseXMDisk(virConfPtr conf, virDomainDefPtr def, int xendConfigVersion)
509
{
510
    const char *str = NULL;
511
    virDomainDiskDefPtr disk = NULL;
512 513
    int hvm = STREQ(def->os.type, "hvm");
    virConfValuePtr list = virConfGetValue(conf, "disk");
514 515 516 517 518 519 520

    if (list && list->type == VIR_CONF_LIST) {
        list = list->list;
        while (list) {
            char *head;
            char *offset;
            char *tmp;
521
            const char *src;
522 523 524 525

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

526
            head = list->str;
527
            if (!(disk = virDomainDiskDefNew()))
528
                return -1;
529 530 531 532 533 534 535 536 537 538 539 540 541 542

            /*
             * 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) {
543 544
                /* No source file given, eg CDROM with no media */
                ignore_value(virDomainDiskSetSource(disk, NULL));
545
            } else {
546
                if (VIR_STRNDUP(tmp, head, offset - head) < 0)
547
                    goto cleanup;
548

549 550
                if (virDomainDiskSetSource(disk, tmp) < 0) {
                    VIR_FREE(tmp);
551 552
                    goto cleanup;
                }
553
                VIR_FREE(tmp);
554 555
            }

556
            head = offset + 1;
557 558 559 560 561 562 563
            /* Remove legacy ioemu: junk */
            if (STRPREFIX(head, "ioemu:"))
                head = head + 6;

            /* Extract the dest device name */
            if (!(offset = strchr(head, ',')))
                goto skipdisk;
564

565
            if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0)
566
                goto cleanup;
567

568 569
            if (virStrncpy(disk->dst, head, offset - head,
                           (offset - head) + 1) == NULL) {
570 571
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Dest file %s too big for destination"), head);
572 573 574
                goto cleanup;
            }

575
            head = offset + 1;
576
            /* Extract source driver type */
577 578 579
            src = virDomainDiskGetSource(disk);
            if (src) {
                size_t len;
580
                /* The main type  phy:, file:, tap: ... */
581 582 583
                if ((tmp = strchr(src, ':')) != NULL) {
                    len = tmp - src;
                    if (VIR_STRNDUP(tmp, src, len) < 0)
584
                        goto cleanup;
585

586 587
                    if (virDomainDiskSetDriver(disk, tmp) < 0) {
                        VIR_FREE(tmp);
588 589
                        goto cleanup;
                    }
590
                    VIR_FREE(tmp);
591 592

                    /* Strip the prefix we found off the source file name */
593 594
                    if (virDomainDiskSetSource(disk, src + len + 1) < 0)
                        goto cleanup;
595

596
                    src = virDomainDiskGetSource(disk);
597 598 599
                }

                /* And the sub-type for tap:XXX: type */
600
                if (STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap")) {
601 602
                    char *driverType;

603
                    if (!(tmp = strchr(src, ':')))
604
                        goto skipdisk;
605
                    len = tmp - src;
606

607
                    if (VIR_STRNDUP(driverType, src, len) < 0)
608
                        goto cleanup;
609

610
                    if (STREQ(driverType, "aio"))
611
                        virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW);
612
                    else
613 614
                        virDomainDiskSetFormat(disk,
                                               virStorageFileFormatTypeFromString(driverType));
615
                    VIR_FREE(driverType);
616
                    if (virDomainDiskGetFormat(disk) <= 0) {
617
                        virReportError(VIR_ERR_INTERNAL_ERROR,
618
                                       _("Unknown driver type %s"),
619
                                       src);
620 621 622 623
                        goto cleanup;
                    }

                    /* Strip the prefix we found off the source file name */
624 625 626
                    if (virDomainDiskSetSource(disk, src + len + 1) < 0)
                        goto cleanup;
                    src = virDomainDiskGetSource(disk);
627 628 629 630
                }
            }

            /* No source, or driver name, so fix to phy: */
631 632
            if (!virDomainDiskGetDriver(disk) &&
                virDomainDiskSetDriver(disk, "phy") < 0)
633
                goto cleanup;
634 635

            /* phy: type indicates a block device */
636 637
            virDomainDiskSetType(disk,
                                 STREQ(virDomainDiskGetDriver(disk), "phy") ?
E
Eric Blake 已提交
638 639
                                 VIR_STORAGE_TYPE_BLOCK :
                                 VIR_STORAGE_TYPE_FILE);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

            /* 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"))
659
                disk->src->readonly = true;
660 661
            else if ((STREQ(head, "w!")) ||
                     (STREQ(head, "!")))
662
                disk->src->shared = true;
663 664

            /* Maintain list in sorted order according to target device name */
665
            if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
666
                goto cleanup;
667 668 669 670 671 672 673

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

674
    if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
675 676 677
        if (xenXMConfigGetString(conf, "cdrom", &str, NULL) < 0)
            goto cleanup;
        if (str) {
678
            if (!(disk = virDomainDiskDefNew()))
679
                goto cleanup;
680

E
Eric Blake 已提交
681
            virDomainDiskSetType(disk, VIR_STORAGE_TYPE_FILE);
682
            disk->device = VIR_DOMAIN_DISK_DEVICE_CDROM;
683
            if (virDomainDiskSetDriver(disk, "file") < 0)
684
                goto cleanup;
685
            if (virDomainDiskSetSource(disk, str) < 0)
686 687 688
                goto cleanup;
            if (VIR_STRDUP(disk->dst, "hdc") < 0)
                goto cleanup;
689
            disk->bus = VIR_DOMAIN_DISK_BUS_IDE;
690
            disk->src->readonly = true;
691

692
            if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0)
693
                goto cleanup;
694 695 696
        }
    }

697 698 699 700 701 702 703 704 705 706
    return 0;

 cleanup:
    virDomainDiskDefFree(disk);
    return -1;
}


#define MAX_VFB 1024

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 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 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
static int
xenParseXMVfb(virConfPtr conf, virDomainDefPtr def, int xendConfigVersion)
{
    int val;
    char *listenAddr = NULL;
    int hvm = STREQ(def->os.type, "hvm");
    virConfValuePtr list;
    virDomainGraphicsDefPtr graphics = NULL;

    if (hvm || xendConfigVersion < XEND_CONFIG_VERSION_3_0_4) {
        if (xenXMConfigGetBool(conf, "vnc", &val, 0) < 0)
            goto cleanup;
        if (val) {
            if (VIR_ALLOC(graphics) < 0)
                goto cleanup;
            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;
            }

            if (xenXMConfigCopyStringOpt(conf, "vnclisten", &listenAddr) < 0)
                goto cleanup;
            if (listenAddr &&
                virDomainGraphicsListenSetAddress(graphics, 0, listenAddr,
                                                  -1, true) < 0) {
               goto cleanup;
            }

            VIR_FREE(listenAddr);
            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)
                goto cleanup;
            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)
                    goto cleanup;
                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)
                    goto cleanup;
                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) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("VFB %s too big for destination"),
                               list->list->str);
                goto cleanup;
            }

            if (VIR_ALLOC(graphics) < 0)
                goto cleanup;
            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"))
                            graphics->data.vnc.autoport = true;
                    } else if (STRPREFIX(key, "vnclisten=")) {
                        if (virDomainGraphicsListenSetAddress(graphics, 0, key+10,
                                                              -1, true) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "vncpasswd=")) {
                        if (VIR_STRDUP(graphics->data.vnc.auth.passwd, key + 10) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "keymap=")) {
                        if (VIR_STRDUP(graphics->data.vnc.keymap, key + 7) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "vncdisplay=")) {
                        if (virStrToLong_i(key + 11, NULL, 10,
                                           &graphics->data.vnc.port) < 0) {
                            virReportError(VIR_ERR_INTERNAL_ERROR,
                                           _("invalid vncdisplay value '%s'"),
                                           key + 11);
                            goto cleanup;
                        }
                        graphics->data.vnc.port += 5900;
                    }
                } else {
                    if (STRPREFIX(key, "display=")) {
                        if (VIR_STRDUP(graphics->data.sdl.display, key + 8) < 0)
                            goto cleanup;
                    } else if (STRPREFIX(key, "xauthority=")) {
                        if (VIR_STRDUP(graphics->data.sdl.xauth, key + 11) < 0)
                            goto cleanup;
                    }
                }

                while (nextkey && (nextkey[0] == ',' ||
                                   nextkey[0] == ' ' ||
                                   nextkey[0] == '\t'))
                    nextkey++;
                key = nextkey;
            }
            if (VIR_ALLOC_N(def->graphics, 1) < 0)
                goto cleanup;
            def->graphics[0] = graphics;
            def->ngraphics = 1;
            graphics = NULL;
        }
    }

    return 0;

 cleanup:
    virDomainGraphicsDefFree(graphics);
    VIR_FREE(listenAddr);
    return -1;
}


859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
static int
xenParseXMCharDev(virConfPtr conf, virDomainDefPtr def)
{
    const char *str;
    virConfValuePtr value = NULL;
    virDomainChrDefPtr chr = NULL;

    if (STREQ(def->os.type, "hvm")) {
        if (xenXMConfigGetString(conf, "parallel", &str, NULL) < 0)
            goto cleanup;
        if (str && STRNEQ(str, "none") &&
            !(chr = xenParseSxprChar(str, NULL)))
            goto cleanup;
        if (chr) {
            if (VIR_ALLOC_N(def->parallels, 1) < 0) {
                goto cleanup;
            }

            chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_PARALLEL;
            chr->target.port = 0;
            def->parallels[0] = chr;
            def->nparallels++;
            chr = NULL;
        }

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

            value = value->list;
            while (value) {
                char *port = NULL;

                if ((value->type != VIR_CONF_STRING) || (value->str == NULL))
                    goto cleanup;
                port = value->str;
                portnum++;
                if (STREQ(port, "none")) {
                    value = value->next;
                    continue;
                }

                if (!(chr = xenParseSxprChar(port, NULL)))
                    goto cleanup;
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = portnum;
                if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) {
                    goto cleanup;
                }

                value = value->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);
                    goto cleanup;
                }
                chr->deviceType = VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL;
                chr->target.port = 0;
                def->serials[0] = chr;
                def->nserials++;
            }
        }
    } else {
        if (VIR_ALLOC_N(def->consoles, 1) < 0)
            goto cleanup;
        def->nconsoles = 1;
        if (!(def->consoles[0] = xenParseSxprChar("pty", NULL)))
            goto cleanup;
        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;
    }

    return 0;

 cleanup:
    virDomainChrDefFree(chr);
    return -1;
}


949 950
static int
xenParseXMVif(virConfPtr conf, virDomainDefPtr def)
951 952
{
    char *script = NULL;
953 954
    virDomainNetDefPtr net = NULL;
    virConfValuePtr list = virConfGetValue(conf, "vif");
955

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
    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) {
989 990 991
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("MAC address %s too big for destination"),
                                       data);
992 993 994 995 996
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "bridge=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(bridge) - 1;
                    if (virStrncpy(bridge, data, len, sizeof(bridge)) == NULL) {
997 998 999
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Bridge %s too big for destination"),
                                       data);
1000 1001 1002
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "script=")) {
1003 1004
                    int len = nextkey ? (nextkey - data) : strlen(data);
                    VIR_FREE(script);
1005 1006
                    if (VIR_STRNDUP(script, data, len) < 0)
                        goto cleanup;
1007 1008 1009
                } else if (STRPREFIX(key, "model=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(model) - 1;
                    if (virStrncpy(model, data, len, sizeof(model)) == NULL) {
1010
                        virReportError(VIR_ERR_INTERNAL_ERROR,
1011 1012
                                       _("Model %s too big for destination"),
                                       data);
1013 1014 1015 1016 1017
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "type=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(type) - 1;
                    if (virStrncpy(type, data, len, sizeof(type)) == NULL) {
1018
                        virReportError(VIR_ERR_INTERNAL_ERROR,
1019 1020
                                       _("Type %s too big for destination"),
                                       data);
1021 1022 1023 1024 1025
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "vifname=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(vifname) - 1;
                    if (virStrncpy(vifname, data, len, sizeof(vifname)) == NULL) {
1026 1027 1028
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("Vifname %s too big for destination"),
                                       data);
1029 1030 1031 1032 1033
                        goto skipnic;
                    }
                } else if (STRPREFIX(key, "ip=")) {
                    int len = nextkey ? (nextkey - data) : sizeof(ip) - 1;
                    if (virStrncpy(ip, data, len, sizeof(ip)) == NULL) {
1034 1035
                        virReportError(VIR_ERR_INTERNAL_ERROR,
                                       _("IP %s too big for destination"), data);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
                        goto skipnic;
                    }
                }

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

            if (VIR_ALLOC(net) < 0)
1048
                goto cleanup;
1049 1050

            if (mac[0]) {
1051
                if (virMacAddrParse(mac, &net->mac) < 0) {
1052 1053
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("malformed mac address '%s'"), mac);
1054 1055 1056 1057
                    goto cleanup;
                }
            }

1058 1059
            if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") ||
                STREQ_NULLABLE(script, "vif-vnic")) {
1060 1061 1062 1063 1064 1065
                net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
            } else {
                net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
            }

            if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
1066 1067 1068 1069
                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;
1070
            } else {
1071 1072
                if (ip[0] && VIR_STRDUP(net->data.ethernet.ipaddr, ip) < 0)
                    goto cleanup;
1073 1074
            }

1075
            if (script && script[0] &&
1076 1077
                VIR_STRDUP(net->script, script) < 0)
                goto cleanup;
1078

1079
            if (model[0] &&
1080
                VIR_STRDUP(net->model, model) < 0)
1081
                goto cleanup;
1082

1083 1084 1085
            if (!model[0] && type[0] && STREQ(type, "netfront") &&
                VIR_STRDUP(net->model, "netfront") < 0)
                goto cleanup;
1086 1087

            if (vifname[0] &&
1088 1089
                VIR_STRDUP(net->ifname, vifname) < 0)
                goto cleanup;
1090

1091
            if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0)
1092
                goto cleanup;
1093 1094 1095 1096

        skipnic:
            list = list->next;
            virDomainNetDefFree(net);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
            VIR_FREE(script);
        }
    }

    return 0;

 cleanup:
    VIR_FREE(script);
    return -1;
}


1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
static int
xenParseXMEmulatedDevices(virConfPtr conf, virDomainDefPtr def)
{
    const char *str;

    if (STREQ(def->os.type, "hvm")) {
        if (xenXMConfigGetString(conf, "soundhw", &str, NULL) < 0)
            return -1;

        if (str &&
            xenParseSxprSound(def, str) < 0)
            return -1;

        if (xenXMConfigGetString(conf, "usbdevice", &str, NULL) < 0)
            return -1;

        if (str &&
            (STREQ(str, "tablet") ||
             STREQ(str, "mouse") ||
             STREQ(str, "keyboard"))) {
            virDomainInputDefPtr input;
            if (VIR_ALLOC(input) < 0)
                return -1;

            input->bus = VIR_DOMAIN_INPUT_BUS_USB;
            if (STREQ(str, "mouse"))
                input->type = VIR_DOMAIN_INPUT_TYPE_MOUSE;
            else if (STREQ(str, "tablet"))
                input->type = VIR_DOMAIN_INPUT_TYPE_TABLET;
            else if (STREQ(str, "keyboard"))
                input->type = VIR_DOMAIN_INPUT_TYPE_KBD;
            if (VIR_ALLOC_N(def->inputs, 1) < 0) {
                virDomainInputDefFree(input);
                return -1;

            }
            def->inputs[0] = input;
            def->ninputs = 1;
        }
    }

    return 0;
}


1154 1155
static int
xenParseXMGeneralMeta(virConfPtr conf, virDomainDefPtr def, virCapsPtr caps)
1156
{
1157
    const char *defaultMachine;
1158 1159 1160 1161
    const char *str;
    int hvm = 0;

    if (xenXMConfigCopyString(conf, "name", &def->name) < 0)
1162
        return -1;
1163

1164 1165
    if (xenXMConfigGetUUID(conf, "uuid", def->uuid) < 0)
        return -1;
1166 1167 1168 1169 1170 1171

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

    if (VIR_STRDUP(def->os.type, hvm ? "hvm" : "xen") < 0)
1172
        return -1;
1173 1174 1175 1176 1177 1178 1179 1180 1181

    def->os.arch =
        virCapabilitiesDefaultGuestArch(caps,
                                        def->os.type,
                                        virDomainVirtTypeToString(def->virtType));
    if (!def->os.arch) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("no supported architecture for os type '%s'"),
                       def->os.type);
1182
        return -1;
1183 1184 1185 1186 1187 1188 1189 1190
    }

    defaultMachine = virCapabilitiesDefaultGuestMachine(caps,
                                                        def->os.type,
                                                        def->os.arch,
                                                        virDomainVirtTypeToString(def->virtType));
    if (defaultMachine != NULL) {
        if (VIR_STRDUP(def->os.machine, defaultMachine) < 0)
1191
            return -1;
1192 1193
    }

1194 1195 1196 1197
    return 0;
}


1198 1199
static int
xenParseXMOS(virConfPtr conf, virDomainDefPtr def)
1200 1201 1202
{
    size_t i;

1203 1204
    if (xenXMConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
        return -1;
1205 1206

    if (STREQ(def->os.type, "hvm")) {
1207
        const char *boot;
1208 1209 1210

       if (xenXMConfigCopyString(conf, "kernel", &def->os.loader) < 0)
            return -1;
1211 1212

        if (xenXMConfigGetString(conf, "boot", &boot, "c") < 0)
1213
            return -1;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

        for (i = 0; i < VIR_DOMAIN_BOOT_LAST && boot[i]; i++) {
            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 {
        const char *extra, *root;

        if (xenXMConfigCopyStringOpt(conf, "bootloader", &def->os.bootloader) < 0)
1237
            return -1;
1238
        if (xenXMConfigCopyStringOpt(conf, "bootargs", &def->os.bootloaderArgs) < 0)
1239
            return -1;
1240 1241

        if (xenXMConfigCopyStringOpt(conf, "kernel", &def->os.kernel) < 0)
1242 1243
            return -1;

1244
        if (xenXMConfigCopyStringOpt(conf, "ramdisk", &def->os.initrd) < 0)
1245 1246
            return -1;

1247
        if (xenXMConfigGetString(conf, "extra", &extra, NULL) < 0)
1248 1249
            return -1;

1250
        if (xenXMConfigGetString(conf, "root", &root, NULL) < 0)
1251
            return -1;
1252 1253 1254

        if (root) {
            if (virAsprintf(&def->os.cmdline, "root=%s %s", root, extra) < 0)
1255
                return -1;
1256 1257
        } else {
            if (VIR_STRDUP(def->os.cmdline, extra) < 0)
1258
                return -1;
1259 1260 1261
        }
    }

1262 1263 1264 1265
    return 0;
}


1266 1267 1268 1269 1270
int
xenParseConfigCommon(virConfPtr conf,
                     virDomainDefPtr def,
                     virCapsPtr caps,
                     int xendConfigVersion)
1271 1272
{
    if (xenParseXMGeneralMeta(conf, def, caps) < 0)
1273
        return -1;
1274 1275

    if (xenParseXMOS(conf, def) < 0)
1276
        return -1;
1277

1278
    if (xenParseXMMem(conf, def) < 0)
1279
        return -1;
1280 1281

    if (xenParseXMEventsActions(conf, def) < 0)
1282
        return -1;
1283 1284

    if (xenParseXMCPUFeatures(conf, def) < 0)
1285
        return -1;
1286 1287

    if (xenParseXMTimeOffset(conf, def, xendConfigVersion) < 0)
1288
        return -1;
1289 1290

    if (xenXMConfigCopyStringOpt(conf, "device_model", &def->emulator) < 0)
1291
        return -1;
1292 1293

    if (xenParseXMVif(conf, def) < 0)
1294
        return -1;
1295

1296
    if (xenParseXMPCI(conf, def) < 0)
1297
        return -1;
1298

1299
    if (xenParseXMEmulatedDevices(conf, def) < 0)
1300
        return -1;
1301

1302
    if (xenParseXMVfb(conf, def, xendConfigVersion) < 0)
1303
        return -1;
1304

1305
    if (xenParseXMCharDev(conf, def) < 0)
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
        return -1;

    return 0;
}


/*
 * Turn a config record into a lump of XML describing the
 * domain, suitable for later feeding for virDomainCreateXML
 */
virDomainDefPtr
xenParseXM(virConfPtr conf,
           int xendConfigVersion,
           virCapsPtr caps)
{
    virDomainDefPtr def = NULL;

    if (VIR_ALLOC(def) < 0)
        return NULL;

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

    if (xenParseConfigCommon(conf, def, caps, xendConfigVersion) < 0)
1330
        goto cleanup;
1331

1332 1333 1334
    if (xenParseXMDisk(conf, def, xendConfigVersion) < 0)
         goto cleanup;

1335 1336
    return def;

1337
 cleanup:
1338 1339 1340
    virDomainDefFree(def);
    return NULL;
}
1341 1342 1343


static
1344 1345
int xenXMConfigSetInt(virConfPtr conf, const char *setting, long long l)
{
1346 1347
    virConfValuePtr value = NULL;

1348
    if ((long) l != l) {
1349 1350
        virReportError(VIR_ERR_OVERFLOW, _("failed to store %lld to %s"),
                       l, setting);
1351 1352
        return -1;
    }
1353
    if (VIR_ALLOC(value) < 0)
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
        return -1;

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

    return virConfSetValue(conf, setting, value);
}


1364 1365 1366
static int
xenXMConfigSetString(virConfPtr conf, const char *setting, const char *str)
{
1367 1368
    virConfValuePtr value = NULL;

1369
    if (VIR_ALLOC(value) < 0)
1370 1371 1372 1373
        return -1;

    value->type = VIR_CONF_STRING;
    value->next = NULL;
1374
    if (VIR_STRDUP(value->str, str) < 0) {
1375 1376 1377 1378 1379 1380 1381 1382
        VIR_FREE(value);
        return -1;
    }

    return virConfSetValue(conf, setting, value);
}


1383 1384 1385 1386 1387
static int
xenFormatXMDisk(virConfValuePtr list,
                virDomainDiskDefPtr disk,
                int hvm,
                int xendConfigVersion)
1388 1389 1390
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
1391 1392 1393
    const char *src = virDomainDiskGetSource(disk);
    int format = virDomainDiskGetFormat(disk);
    const char *driver = virDomainDiskGetDriver(disk);
1394

1395 1396
    if (src) {
        if (format) {
1397 1398
            const char *type;

1399
            if (format == VIR_STORAGE_FILE_RAW)
1400
                type = "aio";
1401
            else
1402 1403 1404
                type = virStorageFileFormatTypeToString(format);
            virBufferAsprintf(&buf, "%s:", driver);
            if (STREQ(driver, "tap"))
1405
                virBufferAsprintf(&buf, "%s:", type);
1406
        } else {
1407
            switch (virDomainDiskGetType(disk)) {
E
Eric Blake 已提交
1408
            case VIR_STORAGE_TYPE_FILE:
1409 1410
                virBufferAddLit(&buf, "file:");
                break;
E
Eric Blake 已提交
1411
            case VIR_STORAGE_TYPE_BLOCK:
1412 1413 1414
                virBufferAddLit(&buf, "phy:");
                break;
            default:
1415 1416
                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("unsupported disk type %s"),
E
Eric Blake 已提交
1417
                               virStorageTypeToString(virDomainDiskGetType(disk)));
1418 1419 1420
                goto cleanup;
            }
        }
1421
        virBufferAdd(&buf, src, -1);
1422 1423
    }
    virBufferAddLit(&buf, ",");
1424
    if (hvm && xendConfigVersion == XEND_CONFIG_VERSION_3_0_2)
1425 1426
        virBufferAddLit(&buf, "ioemu:");

1427
    virBufferAdd(&buf, disk->dst, -1);
1428 1429 1430
    if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
        virBufferAddLit(&buf, ":cdrom");

1431
    if (disk->src->readonly)
1432
        virBufferAddLit(&buf, ",r");
1433
    else if (disk->src->shared)
1434 1435 1436
        virBufferAddLit(&buf, ",!");
    else
        virBufferAddLit(&buf, ",w");
1437
    if (disk->transient) {
1438
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
1439
                       _("transient disks not supported yet"));
1440 1441
        return -1;
    }
1442

1443
    if (virBufferCheckError(&buf) < 0)
1444 1445
        goto cleanup;

1446
    if (VIR_ALLOC(val) < 0)
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
        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;

1461
 cleanup:
1462 1463 1464 1465
    virBufferFreeAndReset(&buf);
    return -1;
}

1466 1467 1468 1469 1470 1471 1472 1473 1474
static int xenFormatXMSerial(virConfValuePtr list,
                             virDomainChrDefPtr serial)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
    int ret;

    if (serial) {
        ret = xenFormatSxprChr(serial, &buf);
1475
        if (ret < 0)
1476 1477 1478 1479
            goto cleanup;
    } else {
        virBufferAddLit(&buf, "none");
    }
1480
    if (virBufferCheckError(&buf) < 0)
1481 1482
        goto cleanup;

1483
    if (VIR_ALLOC(val) < 0)
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
        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;

1498
 cleanup:
1499 1500 1501 1502
    virBufferFreeAndReset(&buf);
    return -1;
}

M
Markus Groß 已提交
1503
static int xenFormatXMNet(virConnectPtr conn,
1504 1505 1506 1507 1508 1509
                                      virConfValuePtr list,
                                      virDomainNetDefPtr net,
                                      int hvm, int xendConfigVersion)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virConfValuePtr val, tmp;
1510
    char macaddr[VIR_MAC_STRING_BUFLEN];
1511

1512
    virBufferAsprintf(&buf, "mac=%s", virMacAddrFormat(&net->mac, macaddr));
1513 1514 1515

    switch (net->type) {
    case VIR_DOMAIN_NET_TYPE_BRIDGE:
1516
        virBufferAsprintf(&buf, ",bridge=%s", net->data.bridge.brname);
1517
        if (net->data.bridge.ipaddr)
1518 1519
            virBufferAsprintf(&buf, ",ip=%s", net->data.bridge.ipaddr);
        virBufferAsprintf(&buf, ",script=%s", DEFAULT_VIF_SCRIPT);
1520 1521 1522
        break;

    case VIR_DOMAIN_NET_TYPE_ETHERNET:
1523 1524
        if (net->script)
            virBufferAsprintf(&buf, ",script=%s", net->script);
1525
        if (net->data.ethernet.ipaddr)
1526
            virBufferAsprintf(&buf, ",ip=%s", net->data.ethernet.ipaddr);
1527 1528 1529 1530 1531 1532 1533
        break;

    case VIR_DOMAIN_NET_TYPE_NETWORK:
    {
        virNetworkPtr network = virNetworkLookupByName(conn, net->data.network.name);
        char *bridge;
        if (!network) {
1534 1535
            virReportError(VIR_ERR_NO_NETWORK, "%s",
                           net->data.network.name);
1536 1537 1538 1539 1540
            return -1;
        }
        bridge = virNetworkGetBridgeName(network);
        virNetworkFree(network);
        if (!bridge) {
1541 1542 1543
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("network %s is not active"),
                           net->data.network.name);
1544 1545 1546
            return -1;
        }

1547 1548
        virBufferAsprintf(&buf, ",bridge=%s", bridge);
        virBufferAsprintf(&buf, ",script=%s", DEFAULT_VIF_SCRIPT);
1549 1550 1551 1552
    }
    break;

    default:
1553 1554 1555
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network type %d"),
                       net->type);
1556 1557 1558 1559 1560
        goto cleanup;
    }

    if (!hvm) {
        if (net->model != NULL)
1561
            virBufferAsprintf(&buf, ",model=%s", net->model);
1562 1563
    }
    else {
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
        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");
        }
1578 1579 1580
    }

    if (net->ifname)
1581
        virBufferAsprintf(&buf, ",vifname=%s",
1582 1583
                          net->ifname);

1584
    if (virBufferCheckError(&buf) < 0)
1585 1586
        goto cleanup;

1587
    if (VIR_ALLOC(val) < 0)
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
        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;

1602
 cleanup:
1603 1604 1605 1606 1607 1608 1609
    virBufferFreeAndReset(&buf);
    return -1;
}



static int
M
Markus Groß 已提交
1610
xenFormatXMPCI(virConfPtr conf,
1611 1612 1613 1614 1615
                           virDomainDefPtr def)
{

    virConfValuePtr pciVal = NULL;
    int hasPCI = 0;
1616
    size_t i;
1617

1618
    for (i = 0; i < def->nhostdevs; i++)
1619 1620 1621 1622 1623 1624 1625
        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;

1626
    if (VIR_ALLOC(pciVal) < 0)
1627 1628 1629 1630 1631
        return -1;

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

1632
    for (i = 0; i < def->nhostdevs; i++) {
1633 1634 1635 1636 1637 1638
        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",
1639 1640 1641
                            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,
1642
                            def->hostdevs[i]->source.subsys.u.pci.addr.function) < 0)
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
                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;

1671
 error:
1672 1673 1674 1675 1676
    virConfFreeValue(pciVal);
    return -1;
}


1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
static int
xenFormatXMGeneralMeta(virConfPtr conf, virDomainDefPtr def)
{
    char uuid[VIR_UUID_STRING_BUFLEN];

    if (xenXMConfigSetString(conf, "name", def->name) < 0)
        return -1;

    virUUIDFormat(def->uuid, uuid);
    if (xenXMConfigSetString(conf, "uuid", uuid) < 0)
        return -1;

    return 0;
}


1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
static int
xenFormatXMMem(virConfPtr conf, virDomainDefPtr def)
{
    if (xenXMConfigSetInt(conf, "maxmem",
                          VIR_DIV_UP(def->mem.max_balloon, 1024)) < 0)
        return -1;

    if (xenXMConfigSetInt(conf, "memory",
                          VIR_DIV_UP(def->mem.cur_balloon, 1024)) < 0)
        return -1;

    return 0;
}


1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
static int
xenFormatXMTimeOffset(virConfPtr conf,
                      virDomainDefPtr def,
                      int xendConfigVersion)
{
    int vmlocaltime;

    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:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("unsupported clock offset='%s'"),
                           virDomainClockOffsetTypeToString(def->clock.offset));
            return -1;
        }

    } else {
        if (STREQ(def->os.type, "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) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("unsupported clock adjustment='reset'"));
                    return -1;
                }
                vmlocaltime = 0;
                rtc_timeoffset = 0;
                break;
            case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
                if (def->clock.data.utc_reset) {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("unsupported clock adjustment='reset'"));
                    return -1;
                }
                vmlocaltime = 1;
                rtc_timeoffset = 0;
                break;
            default:
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
                return -1;
            }
            if (xenXMConfigSetInt(conf, "rtc_timeoffset", rtc_timeoffset) < 0)
                return -1;

        } 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:
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                               _("unsupported clock offset='%s'"),
                               virDomainClockOffsetTypeToString(def->clock.offset));
                return -1;
            }
        } /* !hvm */
    }

    if (xenXMConfigSetInt(conf, "localtime", vmlocaltime) < 0)
        return -1;

    return 0;
}


1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
static int
xenFormatXMEventActions(virConfPtr conf, virDomainDefPtr def)
{
    const char *lifecycle = NULL;

    if (!(lifecycle = virDomainLifecycleTypeToString(def->onPoweroff))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onPoweroff);
        return -1;
    }
    if (xenXMConfigSetString(conf, "on_poweroff", lifecycle) < 0)
        return -1;


    if (!(lifecycle = virDomainLifecycleTypeToString(def->onReboot))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onReboot);
        return -1;
    }
    if (xenXMConfigSetString(conf, "on_reboot", lifecycle) < 0)
        return -1;


    if (!(lifecycle = virDomainLifecycleCrashTypeToString(def->onCrash))) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected lifecycle action %d"), def->onCrash);
        return -1;
    }
    if (xenXMConfigSetString(conf, "on_crash", lifecycle) < 0)
        return -1;

    return 0;
}


1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
static int
xenFormatXMCharDev(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;

    if (STREQ(def->os.type, "hvm")) {
        if (def->nparallels) {
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            char *str;
            int ret;

            ret = xenFormatSxprChr(def->parallels[0], &buf);
            str = virBufferContentAndReset(&buf);
            if (ret == 0)
                ret = xenXMConfigSetString(conf, "parallel", str);
            VIR_FREE(str);
            if (ret < 0)
                return -1;
        } else {
            if (xenXMConfigSetString(conf, "parallel", "none") < 0)
                return -1;
        }

        if (def->nserials) {
            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)
                    return -1;
            } else {
                size_t j = 0;
                int maxport = -1, port;
                virConfValuePtr serialVal = NULL;

                if (VIR_ALLOC(serialVal) < 0)
                    return -1;

                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;

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

                    for (j = 0; j < def->nserials; j++) {
                        if (def->serials[j]->target.port == port) {
                            chr = def->serials[j];
                            break;
                        }
                    }

                    if (xenFormatXMSerial(serialVal, chr) < 0) {
                        VIR_FREE(serialVal);
                        return -1;
                    }
                }

                if (serialVal->list != NULL) {
                    int ret = virConfSetValue(conf, "serial", serialVal);

                    serialVal = NULL;
                    if (ret < 0)
                        return -1;
                }
                VIR_FREE(serialVal);
            }
        } else {
            if (xenXMConfigSetString(conf, "serial", "none") < 0)
                return -1;
        }
    }

    return 0;
}


1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
static int
xenFormatXMDisks(virConfPtr conf, virDomainDefPtr def, int xendConfigVersion)
{
    virConfValuePtr diskVal = NULL;
    size_t i = 0;
    int hvm = STREQ(def->os.type, "hvm");

    if (VIR_ALLOC(diskVal) < 0)
        goto cleanup;

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

    for (i = 0; i < def->ndisks; i++) {
        if (xendConfigVersion == XEND_CONFIG_VERSION_3_0_2 &&
            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;

        if (xenFormatXMDisk(diskVal, def->disks[i],
                            hvm, xendConfigVersion) < 0)
            goto cleanup;
    }

    if (diskVal->list != NULL) {
        int ret = virConfSetValue(conf, "disk", diskVal);
        diskVal = NULL;
        if (ret < 0)
            goto cleanup;
    }
    VIR_FREE(diskVal);

    return 0;

 cleanup:
    virConfFreeValue(diskVal);
    return -1;
}


1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
static int
xenFormatXMCPUAllocation(virConfPtr conf, virDomainDefPtr def)
{
    int ret = -1;
    char *cpus = NULL;

    if (xenXMConfigSetInt(conf, "vcpus", def->maxvcpus) < 0)
        goto cleanup;

    /* 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)
        goto cleanup;

    if ((def->cpumask != NULL) &&
        ((cpus = virBitmapFormat(def->cpumask)) == NULL)) {
        goto cleanup;
    }

    if (cpus &&
        xenXMConfigSetString(conf, "cpus", cpus) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    VIR_FREE(cpus);
    return ret;
}


static int
xenFormatXMCPUFeatures(virConfPtr conf,
                       virDomainDefPtr def,
                       int xendConfigVersion)
{
    size_t i;

    if (STREQ(def->os.type, "hvm")) {
        if (xenXMConfigSetInt(conf, "pae",
                              (def->features[VIR_DOMAIN_FEATURE_PAE] ==
                               VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
            return -1;

        if (xenXMConfigSetInt(conf, "acpi",
                              (def->features[VIR_DOMAIN_FEATURE_ACPI] ==
                               VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
            return -1;

        if (xenXMConfigSetInt(conf, "apic",
                              (def->features[VIR_DOMAIN_FEATURE_APIC] ==
                               VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
            return -1;

        if (xendConfigVersion >= XEND_CONFIG_VERSION_3_0_4) {
            if (xenXMConfigSetInt(conf, "hap",
                                  (def->features[VIR_DOMAIN_FEATURE_HAP] ==
                                   VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
                return -1;

            if (xenXMConfigSetInt(conf, "viridian",
                                  (def->features[VIR_DOMAIN_FEATURE_VIRIDIAN] ==
                                   VIR_TRISTATE_SWITCH_ON) ? 1 : 0) < 0)
                return -1;
        }

        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)
                return -1;
        }
    }

    return 0;
}


2038 2039 2040 2041 2042 2043
static int
xenFormatXMEmulator(virConfPtr conf, virDomainDefPtr def)
{
    if (def->emulator &&
        xenXMConfigSetString(conf, "device_model", def->emulator) < 0)
        return -1;
2044

2045 2046 2047 2048 2049 2050 2051 2052
    return 0;
}


static int
xenFormatXMCDROM(virConfPtr conf,
                 virDomainDefPtr def,
                 int xendConfigVersion)
2053
{
2054
    size_t i;
2055

2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
    if (STREQ(def->os.type, "hvm")) {
        if (xendConfigVersion == XEND_CONFIG_VERSION_3_0_2) {
            for (i = 0; i < def->ndisks; i++) {
                if (def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM &&
                    def->disks[i]->dst &&
                    STREQ(def->disks[i]->dst, "hdc") &&
                    virDomainDiskGetSource(def->disks[i])) {
                    if (xenXMConfigSetString(conf, "cdrom",
                                             virDomainDiskGetSource(def->disks[i])) < 0)
                        return -1;
                    break;
                }
            }
        }
    }
2071

2072 2073
    return 0;
}
2074 2075


2076 2077 2078 2079
static int
xenFormatXMOS(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;
2080

2081
    if (STREQ(def->os.type, "hvm")) {
2082 2083
        char boot[VIR_DOMAIN_BOOT_LAST+1];
        if (xenXMConfigSetString(conf, "builder", "hvm") < 0)
2084
            return -1;
2085 2086 2087

        if (def->os.loader &&
            xenXMConfigSetString(conf, "kernel", def->os.loader) < 0)
2088
            return -1;
2089

2090
        for (i = 0; i < def->os.nBootDevs; i++) {
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
            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;
            }
        }
2107

2108 2109 2110 2111 2112 2113 2114 2115
        if (!def->os.nBootDevs) {
            boot[0] = 'c';
            boot[1] = '\0';
        } else {
            boot[def->os.nBootDevs] = '\0';
        }

        if (xenXMConfigSetString(conf, "boot", boot) < 0)
2116
            return -1;
2117 2118 2119 2120

        /* XXX floppy disks */
    } else {
        if (def->os.bootloader &&
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
             xenXMConfigSetString(conf, "bootloader", def->os.bootloader) < 0)
            return -1;

         if (def->os.bootloaderArgs &&
             xenXMConfigSetString(conf, "bootargs", def->os.bootloaderArgs) < 0)
            return -1;

         if (def->os.kernel &&
             xenXMConfigSetString(conf, "kernel", def->os.kernel) < 0)
            return -1;

         if (def->os.initrd &&
             xenXMConfigSetString(conf, "ramdisk", def->os.initrd) < 0)
            return -1;

         if (def->os.cmdline &&
             xenXMConfigSetString(conf, "extra", def->os.cmdline) < 0)
            return -1;
     } /* !hvm */

    return 0;
}


2145 2146 2147 2148
static int
xenFormatXMVfb(virConfPtr conf,
               virDomainDefPtr def,
               int xendConfigVersion)
2149 2150
{
    int hvm = STREQ(def->os.type, "hvm") ? 1 : 0;
2151 2152

    if (def->ngraphics == 1) {
2153
        if (hvm || (xendConfigVersion < XEND_CONFIG_MIN_VERS_PVFB_NEWCONF)) {
2154 2155
            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                if (xenXMConfigSetInt(conf, "sdl", 1) < 0)
2156 2157
                    return -1;

2158
                if (xenXMConfigSetInt(conf, "vnc", 0) < 0)
2159 2160
                    return -1;

2161 2162 2163
                if (def->graphics[0]->data.sdl.display &&
                    xenXMConfigSetString(conf, "display",
                                     def->graphics[0]->data.sdl.display) < 0)
2164 2165
                    return -1;

2166 2167 2168
                if (def->graphics[0]->data.sdl.xauth &&
                    xenXMConfigSetString(conf, "xauthority",
                                         def->graphics[0]->data.sdl.xauth) < 0)
2169
                    return -1;
2170
            } else {
2171 2172
                const char *listenAddr;

2173
                if (xenXMConfigSetInt(conf, "sdl", 0) < 0)
2174 2175
                    return -1;

2176
                if (xenXMConfigSetInt(conf, "vnc", 1) < 0)
2177 2178
                    return -1;

2179 2180
                if (xenXMConfigSetInt(conf, "vncunused",
                              def->graphics[0]->data.vnc.autoport ? 1 : 0) < 0)
2181 2182
                    return -1;

2183 2184 2185
                if (!def->graphics[0]->data.vnc.autoport &&
                    xenXMConfigSetInt(conf, "vncdisplay",
                                  def->graphics[0]->data.vnc.port - 5900) < 0)
2186 2187
                    return -1;

2188 2189 2190
                listenAddr = virDomainGraphicsListenGetAddress(def->graphics[0], 0);
                if (listenAddr &&
                    xenXMConfigSetString(conf, "vnclisten", listenAddr) < 0)
2191 2192
                    return -1;

2193 2194 2195
                if (def->graphics[0]->data.vnc.auth.passwd &&
                    xenXMConfigSetString(conf, "vncpasswd",
                                        def->graphics[0]->data.vnc.auth.passwd) < 0)
2196 2197
                    return -1;

2198 2199 2200
                if (def->graphics[0]->data.vnc.keymap &&
                    xenXMConfigSetString(conf, "keymap",
                                        def->graphics[0]->data.vnc.keymap) < 0)
2201
                    return -1;
2202 2203 2204 2205 2206
            }
        } else {
            virConfValuePtr vfb, disp;
            char *vfbstr = NULL;
            virBuffer buf = VIR_BUFFER_INITIALIZER;
2207

2208 2209 2210
            if (def->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_SDL) {
                virBufferAddLit(&buf, "type=sdl");
                if (def->graphics[0]->data.sdl.display)
2211
                    virBufferAsprintf(&buf, ",display=%s",
2212 2213
                                      def->graphics[0]->data.sdl.display);
                if (def->graphics[0]->data.sdl.xauth)
2214
                    virBufferAsprintf(&buf, ",xauthority=%s",
2215 2216
                                      def->graphics[0]->data.sdl.xauth);
            } else {
2217 2218 2219
                const char *listenAddr
                    = virDomainGraphicsListenGetAddress(def->graphics[0], 0);

2220
                virBufferAddLit(&buf, "type=vnc");
2221
                virBufferAsprintf(&buf, ",vncunused=%d",
2222 2223
                                  def->graphics[0]->data.vnc.autoport ? 1 : 0);
                if (!def->graphics[0]->data.vnc.autoport)
2224
                    virBufferAsprintf(&buf, ",vncdisplay=%d",
2225
                                      def->graphics[0]->data.vnc.port - 5900);
2226 2227
                if (listenAddr)
                    virBufferAsprintf(&buf, ",vnclisten=%s", listenAddr);
2228
                if (def->graphics[0]->data.vnc.auth.passwd)
2229
                    virBufferAsprintf(&buf, ",vncpasswd=%s",
2230 2231
                                      def->graphics[0]->data.vnc.auth.passwd);
                if (def->graphics[0]->data.vnc.keymap)
2232
                    virBufferAsprintf(&buf, ",keymap=%s",
2233 2234
                                      def->graphics[0]->data.vnc.keymap);
            }
2235
            if (virBufferCheckError(&buf) < 0)
2236
                return -1;
2237 2238 2239 2240 2241

            vfbstr = virBufferContentAndReset(&buf);

            if (VIR_ALLOC(vfb) < 0) {
                VIR_FREE(vfbstr);
2242
                return -1;
2243 2244 2245 2246 2247
            }

            if (VIR_ALLOC(disp) < 0) {
                VIR_FREE(vfb);
                VIR_FREE(vfbstr);
2248
                return -1;
2249 2250 2251 2252 2253 2254 2255 2256
            }

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

            if (virConfSetValue(conf, "vfb", vfb) < 0)
2257
                return -1;
2258 2259 2260
        }
    }

2261 2262 2263 2264
    return 0;
}


2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
static int
xenFormatXMSound(virConfPtr conf, virDomainDefPtr def)
{
    if (STREQ(def->os.type, "hvm")) {
        if (def->sounds) {
            virBuffer buf = VIR_BUFFER_INITIALIZER;
            char *str = NULL;
            int ret = xenFormatSxprSound(def, &buf);

            str = virBufferContentAndReset(&buf);
            if (ret == 0)
                ret = xenXMConfigSetString(conf, "soundhw", str);

            VIR_FREE(str);
            if (ret < 0)
                return -1;
        }
    }

    return 0;
}


static int
xenFormatXMInputDevs(virConfPtr conf, virDomainDefPtr def)
{
    size_t i;

    if (STREQ(def->os.type, "hvm")) {
        for (i = 0; i < def->ninputs; i++) {
            if (def->inputs[i]->bus == VIR_DOMAIN_INPUT_BUS_USB) {
                if (xenXMConfigSetInt(conf, "usb", 1) < 0)
                    return -1;

                switch (def->inputs[i]->type) {
                    case VIR_DOMAIN_INPUT_TYPE_MOUSE:
                        if (xenXMConfigSetString(conf, "usbdevice", "mouse") < 0)
                            return -1;

                        break;
                    case VIR_DOMAIN_INPUT_TYPE_TABLET:
                        if (xenXMConfigSetString(conf, "usbdevice", "tablet") < 0)
                            return -1;

                        break;
                    case VIR_DOMAIN_INPUT_TYPE_KBD:
                        if (xenXMConfigSetString(conf, "usbdevice", "keyboard") < 0)
                            return -1;

                        break;
                }
                break;
            }
        }
    }

    return 0;
}


2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
static int
xenFormatXMVif(virConfPtr conf,
               virConnectPtr conn,
               virDomainDefPtr def,
               int xendConfigVersion)
{
   virConfValuePtr netVal = NULL;
   size_t i;
   int hvm = STREQ(def->os.type, "hvm");

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

    for (i = 0; i < def->nnets; i++) {
        if (xenFormatXMNet(conn, netVal, def->nets[i],
                           hvm, xendConfigVersion) < 0)
           goto cleanup;
    }

    if (netVal->list != NULL) {
        int ret = virConfSetValue(conf, "vif", netVal);
        netVal = NULL;
        if (ret < 0)
            goto cleanup;
    }

    VIR_FREE(netVal);
    return 0;

 cleanup:
    virConfFreeValue(netVal);
    return -1;
}


2362 2363 2364 2365 2366
int
xenFormatConfigCommon(virConfPtr conf,
                      virDomainDefPtr def,
                      virConnectPtr conn,
                      int xendConfigVersion)
2367 2368
{
    if (xenFormatXMGeneralMeta(conf, def) < 0)
2369
        return -1;
2370 2371

    if (xenFormatXMMem(conf, def) < 0)
2372
        return -1;
2373 2374

    if (xenFormatXMCPUAllocation(conf, def) < 0)
2375
        return -1;
2376 2377

    if (xenFormatXMOS(conf, def) < 0)
2378
        return -1;
2379 2380

    if (xenFormatXMCPUFeatures(conf, def, xendConfigVersion) < 0)
2381
        return -1;;
2382 2383

    if (xenFormatXMCDROM(conf, def, xendConfigVersion) < 0)
2384
        return -1;
2385 2386

    if (xenFormatXMTimeOffset(conf, def, xendConfigVersion) < 0)
2387
        return -1;
2388 2389

    if (xenFormatXMEventActions(conf, def) < 0)
2390
        return -1;
2391 2392

    if (xenFormatXMEmulator(conf, def) < 0)
2393
        return -1;
2394

2395
    if (xenFormatXMInputDevs(conf, def) < 0)
2396
        return -1;
2397 2398

    if (xenFormatXMVfb(conf, def, xendConfigVersion) < 0)
2399
        return -1;
2400

2401
    if (xenFormatXMVif(conf, conn, def, xendConfigVersion) < 0)
2402
        return -1;
2403

M
Markus Groß 已提交
2404
    if (xenFormatXMPCI(conf, def) < 0)
2405
        return -1;
2406

2407
    if (xenFormatXMCharDev(conf, def) < 0)
2408
        return -1;
2409

2410
    if (xenFormatXMSound(conf, def) < 0)
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
        return -1;

    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);

virConfPtr
xenFormatXM(virConnectPtr conn,
            virDomainDefPtr def,
            int xendConfigVersion)
{
    virConfPtr conf = NULL;

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

    if (xenFormatConfigCommon(conf, def, conn, xendConfigVersion) < 0)
        goto cleanup;

    if (xenFormatXMDisks(conf, def, xendConfigVersion) < 0)
2435
        goto cleanup;
2436 2437 2438

    return conf;

2439
 cleanup:
2440 2441
    if (conf)
        virConfFree(conf);
2442
    return NULL;
2443
}