bhyve_command.c 21.6 KB
Newer Older
R
Roman Bogorodskiy 已提交
1
/*
2
 * bhyve_command.c: bhyve command generation
R
Roman Bogorodskiy 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Copyright (C) 2014 Roman Bogorodskiy
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/types.h>
#include <net/if.h>
#include <net/if_tap.h>

28
#include "bhyve_capabilities.h"
R
Roman Bogorodskiy 已提交
29
#include "bhyve_command.h"
30
#include "bhyve_domain.h"
31
#include "bhyve_driver.h"
32
#include "datatypes.h"
R
Roman Bogorodskiy 已提交
33 34 35 36 37 38 39
#include "viralloc.h"
#include "virfile.h"
#include "virstring.h"
#include "virlog.h"
#include "virnetdev.h"
#include "virnetdevbridge.h"
#include "virnetdevtap.h"
R
Roman Bogorodskiy 已提交
40
#include "storage/storage_driver.h"
R
Roman Bogorodskiy 已提交
41 42 43

#define VIR_FROM_THIS VIR_FROM_BHYVE

44 45
VIR_LOG_INIT("bhyve.bhyve_command");

R
Roman Bogorodskiy 已提交
46
static int
47 48 49 50
bhyveBuildNetArgStr(const virDomainDef *def,
                    virDomainNetDefPtr net,
                    virCommandPtr cmd,
                    bool dryRun)
R
Roman Bogorodskiy 已提交
51
{
52
    char macaddr[VIR_MAC_STRING_BUFLEN];
R
Roman Bogorodskiy 已提交
53
    char *realifname = NULL;
54
    char *brname = NULL;
55
    int ret = -1;
56
    virDomainNetType actualType = virDomainNetGetActualType(net);
R
Roman Bogorodskiy 已提交
57

58 59
    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
        if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
60
            goto cleanup;
61 62 63 64
    } else {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Network type %d is not supported"),
                       virDomainNetGetActualType(net));
65
        goto cleanup;
R
Roman Bogorodskiy 已提交
66 67
    }

68 69 70 71
    if (!net->ifname ||
        STRPREFIX(net->ifname, VIR_NET_GENERATED_PREFIX) ||
        strchr(net->ifname, '%')) {
        VIR_FREE(net->ifname);
72 73
        if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_PREFIX "%d") < 0)
            goto cleanup;
74
    }
R
Roman Bogorodskiy 已提交
75

76 77
    if (!dryRun) {
        if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
78
                                           def->uuid, NULL, NULL, 0,
79 80 81
                                           virDomainNetGetActualVirtPortProfile(net),
                                           virDomainNetGetActualVlan(net),
                                           VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
82
            goto cleanup;
R
Roman Bogorodskiy 已提交
83 84
        }

85 86
        realifname = virNetDevTapGetRealDeviceName(net->ifname);

87 88
        if (realifname == NULL)
            goto cleanup;
R
Roman Bogorodskiy 已提交
89

90 91 92 93 94
        VIR_DEBUG("%s -> %s", net->ifname, realifname);
        /* hack on top of other hack: we need to set
         * interface to 'UP' again after re-opening to find its
         * name
         */
95 96
        if (virNetDevSetOnline(net->ifname, true) != 0)
            goto cleanup;
97 98
    } else {
        if (VIR_STRDUP(realifname, "tap0") < 0)
99
            goto cleanup;
R
Roman Bogorodskiy 已提交
100 101 102 103
    }


    virCommandAddArg(cmd, "-s");
104 105
    virCommandAddArgFormat(cmd, "%d:0,virtio-net,%s,mac=%s",
                           net->info.addr.pci.slot,
106
                           realifname, virMacAddrFormat(&net->mac, macaddr));
107 108 109 110 111 112

    ret = 0;
 cleanup:
    if (ret < 0)
        VIR_FREE(net->ifname);
    VIR_FREE(brname);
113
    VIR_FREE(realifname);
R
Roman Bogorodskiy 已提交
114

115
    return ret;
R
Roman Bogorodskiy 已提交
116 117
}

118 119 120 121 122 123 124 125 126 127 128
static int
bhyveBuildConsoleArgStr(const virDomainDef *def, virCommandPtr cmd)
{

    virDomainChrDefPtr chr = NULL;

    if (!def->nserials)
        return 0;

    chr = def->serials[0];

129
    if (chr->source->type != VIR_DOMAIN_CHR_TYPE_NMDM) {
130 131 132 133 134 135 136 137 138 139 140 141
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("only nmdm console types are supported"));
        return -1;
    }

    /* bhyve supports only two ports: com1 and com2 */
    if (chr->target.port > 2) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("only two serial ports are supported"));
        return -1;
    }

142
    virCommandAddArgList(cmd, "-s", "1,lpc", NULL);
143 144
    virCommandAddArg(cmd, "-l");
    virCommandAddArgFormat(cmd, "com%d,%s",
145
                           chr->target.port + 1, chr->source->data.file.path);
146 147 148 149

    return 0;
}

R
Roman Bogorodskiy 已提交
150
static int
151 152 153 154
bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
                               virDomainControllerDefPtr controller,
                               virConnectPtr conn,
                               virCommandPtr cmd)
R
Roman Bogorodskiy 已提交
155
{
156 157
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virBuffer device = VIR_BUFFER_INITIALIZER;
R
Roman Bogorodskiy 已提交
158
    const char *disk_source;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    size_t i;
    int ret = -1;

    for (i = 0; i < def->ndisks; i++) {
        virDomainDiskDefPtr disk = def->disks[i];
        if (disk->bus != VIR_DOMAIN_DISK_BUS_SATA)
            continue;

        if (disk->info.addr.drive.controller != controller->idx)
            continue;

        VIR_DEBUG("disk %zu controller %d", i, controller->idx);

        if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
            (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk type"));
            goto error;
        }

        if (virStorageTranslateDiskSourcePool(conn, disk) < 0)
            goto error;

        disk_source = virDomainDiskGetSource(disk);

        if ((disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM) &&
            (disk_source == NULL)) {
                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                               _("cdrom device without source path "
                                 "not supported"));
                goto error;
        }
R
Roman Bogorodskiy 已提交
191

R
Roman Bogorodskiy 已提交
192 193
        switch (disk->device) {
        case VIR_DOMAIN_DISK_DEVICE_DISK:
194 195 196 197
            if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_AHCI32SLOT))
                virBufferAsprintf(&device, ",hd:%s", disk_source);
            else
                virBufferAsprintf(&device, "-hd,%s", disk_source);
R
Roman Bogorodskiy 已提交
198 199
            break;
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
200 201 202 203
            if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_AHCI32SLOT))
                virBufferAsprintf(&device, ",cd:%s", disk_source);
            else
                virBufferAsprintf(&device, "-cd,%s", disk_source);
R
Roman Bogorodskiy 已提交
204 205 206 207
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
208
            goto error;
R
Roman Bogorodskiy 已提交
209
        }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        virBufferAddBuffer(&buf, &device);
        virBufferFreeAndReset(&device);
    }

    if (virBufferCheckError(&buf) < 0)
        goto error;

    virCommandAddArg(cmd, "-s");
    virCommandAddArgFormat(cmd, "%d:0,ahci%s",
                           controller->info.addr.pci.slot,
                           virBufferCurrentContent(&buf));

    ret = 0;
 error:
    virBufferFreeAndReset(&buf);
    return ret;
}

static int
bhyveBuildVirtIODiskArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
                     virDomainDiskDefPtr disk,
                     virConnectPtr conn,
                     virCommandPtr cmd)
{
    const char *disk_source;

    if (virStorageTranslateDiskSourcePool(conn, disk) < 0)
        return -1;

    if (disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
R
Roman Bogorodskiy 已提交
240
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
241
                       _("unsupported disk device"));
R
Roman Bogorodskiy 已提交
242 243 244
        return -1;
    }

R
Roman Bogorodskiy 已提交
245 246
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
247 248 249 250 251
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
252 253
    disk_source = virDomainDiskGetSource(disk);

R
Roman Bogorodskiy 已提交
254
    virCommandAddArg(cmd, "-s");
255 256
    virCommandAddArgFormat(cmd, "%d:0,virtio-blk,%s",
                           disk->info.addr.pci.slot,
R
Roman Bogorodskiy 已提交
257
                           disk_source);
R
Roman Bogorodskiy 已提交
258 259 260 261 262

    return 0;
}

virCommandPtr
R
Roman Bogorodskiy 已提交
263
virBhyveProcessBuildBhyveCmd(virConnectPtr conn,
264
                             virDomainDefPtr def, bool dryRun)
R
Roman Bogorodskiy 已提交
265 266 267 268 269 270 271 272 273
{
    /*
     * /usr/sbin/bhyve -c 2 -m 256 -AI -H -P \
     *            -s 0:0,hostbridge \
     *            -s 1:0,virtio-net,tap0 \
     *            -s 2:0,ahci-hd,${IMG} \
     *            -S 31,uart,stdio \
     *            vm0
     */
274 275
    size_t i;

R
Roman Bogorodskiy 已提交
276 277 278 279
    virCommandPtr cmd = virCommandNew(BHYVE);

    /* CPUs */
    virCommandAddArg(cmd, "-c");
280
    virCommandAddArgFormat(cmd, "%d", virDomainDefGetVcpus(def));
R
Roman Bogorodskiy 已提交
281 282 283 284

    /* Memory */
    virCommandAddArg(cmd, "-m");
    virCommandAddArgFormat(cmd, "%llu",
285
                           VIR_DIV_UP(virDomainDefGetMemoryInitial(def), 1024));
R
Roman Bogorodskiy 已提交
286 287

    /* Options */
J
Ján Tomko 已提交
288
    if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
289
        virCommandAddArg(cmd, "-A"); /* Create an ACPI table */
J
Ján Tomko 已提交
290
    if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
291 292
        virCommandAddArg(cmd, "-I"); /* Present ioapic to the guest */

R
Roman Bogorodskiy 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    switch (def->clock.offset) {
    case VIR_DOMAIN_CLOCK_OFFSET_LOCALTIME:
        /* used by default in bhyve */
        break;
    case VIR_DOMAIN_CLOCK_OFFSET_UTC:
        if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_RTC_UTC) != 0) {
            virCommandAddArg(cmd, "-u");
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Installed bhyve binary does not support "
                          "UTC clock"));
            goto error;
        }
        break;
    default:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("unsupported clock offset '%s'"),
                        virDomainClockOffsetTypeToString(def->clock.offset));
         goto error;
    }

R
Roman Bogorodskiy 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327
    /* Clarification about -H and -P flags from Peter Grehan:
     * -H and -P flags force the guest to exit when it executes IA32 HLT and PAUSE
     * instructions respectively.
     *
     * For the HLT exit, bhyve uses that to infer that the guest is idling and can
     * be put to sleep until an external event arrives. If this option is not used,
     * the guest will always use 100% of CPU on the host.
     *
     * The PAUSE exit is most useful when there are large numbers of guest VMs running,
     * since it forces the guest to exit when it spins on a lock acquisition.
     */
    virCommandAddArg(cmd, "-H"); /* vmexit from guest on hlt */
    virCommandAddArg(cmd, "-P"); /* vmexit from guest on pause */

328
    virCommandAddArgList(cmd, "-s", "0:0,hostbridge", NULL);
R
Roman Bogorodskiy 已提交
329
    /* Devices */
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    for (i = 0; i < def->ncontrollers; i++) {
        virDomainControllerDefPtr controller = def->controllers[i];
        switch (controller->type) {
        case VIR_DOMAIN_CONTROLLER_TYPE_PCI:
                if (controller->model != VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT) {
                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                       "%s", _("unsupported PCI controller model: only PCI root supported"));
                        goto error;
                }
                break;
        case VIR_DOMAIN_CONTROLLER_TYPE_SATA:
                if (bhyveBuildAHCIControllerArgStr(def, controller, conn, cmd) < 0)
                    goto error;
                break;
        }
    }
346 347 348 349 350 351 352 353
    for (i = 0; i < def->nnets; i++) {
        virDomainNetDefPtr net = def->nets[i];
        if (bhyveBuildNetArgStr(def, net, cmd, dryRun) < 0)
            goto error;
    }
    for (i = 0; i < def->ndisks; i++) {
        virDomainDiskDefPtr disk = def->disks[i];

354 355 356 357 358 359 360 361 362 363 364
        switch (disk->bus) {
        case VIR_DOMAIN_DISK_BUS_SATA:
            /* Handled by bhyveBuildAHCIControllerArgStr() */
            break;
        case VIR_DOMAIN_DISK_BUS_VIRTIO:
            if (bhyveBuildVirtIODiskArgStr(def, disk, conn, cmd) < 0)
                goto error;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
365
            goto error;
366
        }
367
    }
368
    if (bhyveBuildConsoleArgStr(def, cmd) < 0)
369
        goto error;
370
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
371 372 373

    return cmd;

374
 error:
R
Roman Bogorodskiy 已提交
375 376 377 378 379 380
    virCommandFree(cmd);
    return NULL;
}

virCommandPtr
virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
381
                               virDomainDefPtr def)
R
Roman Bogorodskiy 已提交
382 383 384 385
{
    virCommandPtr cmd = virCommandNew(BHYVECTL);

    virCommandAddArg(cmd, "--destroy");
386
    virCommandAddArgPair(cmd, "--vm", def->name);
R
Roman Bogorodskiy 已提交
387 388 389 390

    return cmd;
}

391 392 393 394 395 396 397 398
static void
virAppendBootloaderArgs(virCommandPtr cmd, virDomainDefPtr def)
{
    char **blargs;

    /* XXX: Handle quoted? */
    blargs = virStringSplit(def->os.bootloaderArgs, " ", 0);
    virCommandAddArgSet(cmd, (const char * const *)blargs);
399
    virStringListFree(blargs);
400 401 402 403
}

static virCommandPtr
virBhyveProcessBuildBhyveloadCmd(virDomainDefPtr def, virDomainDiskDefPtr disk)
R
Roman Bogorodskiy 已提交
404 405 406
{
    virCommandPtr cmd;

407 408 409 410 411 412 413 414
    cmd = virCommandNew(BHYVELOAD);

    if (def->os.bootloaderArgs == NULL) {
        VIR_DEBUG("bhyveload with default arguments");

        /* Memory (MB) */
        virCommandAddArg(cmd, "-m");
        virCommandAddArgFormat(cmd, "%llu",
415
                               VIR_DIV_UP(virDomainDefGetMemoryInitial(def), 1024));
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

        /* Image path */
        virCommandAddArg(cmd, "-d");
        virCommandAddArg(cmd, virDomainDiskGetSource(disk));

        /* VM name */
        virCommandAddArg(cmd, def->name);
    } else {
        VIR_DEBUG("bhyveload with arguments");
        virAppendBootloaderArgs(cmd, def);
    }

    return cmd;
}

static virCommandPtr
virBhyveProcessBuildCustomLoaderCmd(virDomainDefPtr def)
{
    virCommandPtr cmd;

    if (def->os.bootloaderArgs == NULL) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Custom loader requires explicit %s configuration"),
                       "bootloader_args");
R
Roman Bogorodskiy 已提交
440 441 442
        return NULL;
    }

443 444 445 446 447 448 449 450 451 452
    VIR_DEBUG("custom loader '%s' with arguments", def->os.bootloader);

    cmd = virCommandNew(def->os.bootloader);
    virAppendBootloaderArgs(cmd, def);
    return cmd;
}

static bool
virBhyveUsableDisk(virConnectPtr conn, virDomainDiskDefPtr disk)
{
R
Roman Bogorodskiy 已提交
453

R
Roman Bogorodskiy 已提交
454
    if (virStorageTranslateDiskSourcePool(conn, disk) < 0)
455
        return false;
R
Roman Bogorodskiy 已提交
456

R
Roman Bogorodskiy 已提交
457 458
    if ((disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) &&
        (disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM)) {
R
Roman Bogorodskiy 已提交
459 460
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk device"));
461
        return false;
R
Roman Bogorodskiy 已提交
462 463
    }

R
Roman Bogorodskiy 已提交
464 465
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
466 467
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
468
        return false;
R
Roman Bogorodskiy 已提交
469 470
    }

471 472
    return true;
}
R
Roman Bogorodskiy 已提交
473

474 475 476 477 478 479 480 481 482 483 484 485
static void
virBhyveFormatGrubDevice(virBufferPtr devicemap, virDomainDiskDefPtr def)
{

    if (def->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
        virBufferAsprintf(devicemap, "(cd) %s\n",
                          virDomainDiskGetSource(def));
    else
        virBufferAsprintf(devicemap, "(hd0) %s\n",
                          virDomainDiskGetSource(def));
}

486 487 488 489 490 491
static virCommandPtr
virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
                                 virConnectPtr conn,
                                 const char *devmap_file,
                                 char **devicesmap_out)
{
492
    virDomainDiskDefPtr hdd, cd, userdef, diskdef;
493 494
    virBuffer devicemap;
    virCommandPtr cmd;
495
    unsigned int best_idx = UINT_MAX;
496 497 498 499 500 501 502
    size_t i;

    if (def->os.bootloaderArgs != NULL)
        return virBhyveProcessBuildCustomLoaderCmd(def);

    devicemap = (virBuffer)VIR_BUFFER_INITIALIZER;

503 504 505 506
    /* Search disk list for CD or HDD device. We'll respect <boot order=''> if
     * present and otherwise pick the first CD or failing that HDD we come
     * across. */
    cd = hdd = userdef = NULL;
507 508 509 510
    for (i = 0; i < def->ndisks; i++) {
        if (!virBhyveUsableDisk(conn, def->disks[i]))
            continue;

511 512 513 514 515 516 517 518
        diskdef = def->disks[i];

        if (diskdef->info.bootIndex && diskdef->info.bootIndex < best_idx) {
            userdef = diskdef;
            best_idx = userdef->info.bootIndex;
            continue;
        }

519 520
        if (cd == NULL &&
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
521 522
            cd = diskdef;
            VIR_INFO("Picking %s as CD", virDomainDiskGetSource(cd));
523 524
        }

525
        if (hdd == NULL &&
526
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
527 528
            hdd = diskdef;
            VIR_INFO("Picking %s as HDD", virDomainDiskGetSource(hdd));
529 530 531 532 533 534 535 536 537
        }
    }

    cmd = virCommandNew(def->os.bootloader);

    VIR_DEBUG("grub-bhyve with default arguments");

    if (devicesmap_out != NULL) {
        /* Grub device.map (just for boot) */
538 539 540 541 542
        if (userdef != NULL) {
            virBhyveFormatGrubDevice(&devicemap, userdef);
        } else {
            if (hdd != NULL)
                virBhyveFormatGrubDevice(&devicemap, hdd);
543

544 545 546
            if (cd != NULL)
                virBhyveFormatGrubDevice(&devicemap, cd);
        }
547 548 549 550

        *devicesmap_out = virBufferContentAndReset(&devicemap);
    }

551 552 553 554 555 556 557
    virCommandAddArg(cmd, "--root");
    if (userdef != NULL) {
        if (userdef->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
            virCommandAddArg(cmd, "cd");
        else
            virCommandAddArg(cmd, "hd0,msdos1");
    } else if (cd != NULL) {
558 559 560 561 562 563 564 565 566 567
        virCommandAddArg(cmd, "cd");
    } else {
        virCommandAddArg(cmd, "hd0,msdos1");
    }

    virCommandAddArg(cmd, "--device-map");
    virCommandAddArg(cmd, devmap_file);

    /* Memory in MB */
    virCommandAddArg(cmd, "--memory");
R
Roman Bogorodskiy 已提交
568
    virCommandAddArgFormat(cmd, "%llu",
569
                           VIR_DIV_UP(virDomainDefGetMemoryInitial(def), 1024));
R
Roman Bogorodskiy 已提交
570

571 572 573 574 575 576
    if ((bhyveDriverGetGrubCaps(conn) & BHYVE_GRUB_CAP_CONSDEV) != 0 &&
        def->nserials > 0) {
        virDomainChrDefPtr chr;

        chr = def->serials[0];

577
        if (chr->source->type != VIR_DOMAIN_CHR_TYPE_NMDM) {
578 579 580 581 582 583
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("only nmdm console types are supported"));
            return NULL;
        }

        virCommandAddArg(cmd, "--cons-dev");
584
        virCommandAddArg(cmd, chr->source->data.file.path);
585 586
    }

R
Roman Bogorodskiy 已提交
587
    /* VM name */
588
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
589 590 591

    return cmd;
}
592

593 594
static virDomainDiskDefPtr
virBhyveGetBootDisk(virConnectPtr conn, virDomainDefPtr def)
595
{
596 597 598
    size_t i;
    virDomainDiskDefPtr match = NULL;
    int boot_dev = -1;
599 600 601

    if (def->ndisks < 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
602 603 604 605 606 607 608
                       _("Domain should have at least one disk defined"));
        return NULL;
    }

    if (def->os.nBootDevs > 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only one boot device is supported"));
609
        return NULL;
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 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
    } else if (def->os.nBootDevs == 1) {
        switch (def->os.bootDevs[0]) {
        case VIR_DOMAIN_BOOT_CDROM:
            boot_dev = VIR_DOMAIN_DISK_DEVICE_CDROM;
            break;
        case VIR_DOMAIN_BOOT_DISK:
            boot_dev = VIR_DOMAIN_DISK_DEVICE_DISK;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Cannot boot from device %s"),
                           virDomainBootTypeToString(def->os.bootDevs[0]));
            return NULL;
        }
    }

    if (boot_dev != -1) {
        /* If boot_dev is set, we return the first device of
         * the request type */
        for (i = 0; i < def->ndisks; i++) {
            if (!virBhyveUsableDisk(conn, def->disks[i]))
                continue;

            if (def->disks[i]->device == boot_dev) {
                match = def->disks[i];
                break;
            }
        }

        if (match == NULL) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                           _("Cannot find boot device of requested type %s"),
                           virDomainBootTypeToString(def->os.bootDevs[0]));
            return NULL;
        }
    } else {
        /* Otherwise, if boot_dev is not set, we try to find if bootIndex
         * is set for individual device. However, as bhyve does not support
         * specifying real boot priority for devices, we allow only single
         * device with boot priority set.
         */
        int first_usable_disk_index = -1;

        for (i = 0; i < def->ndisks; i++) {
            if (!virBhyveUsableDisk(conn, def->disks[i]))
                continue;
            else
                first_usable_disk_index = i;

            if (def->disks[i]->info.bootIndex > 0) {
                if (match == NULL) {
                    match = def->disks[i];
                } else {
                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                   _("Only one boot device is supported"));
                    return NULL;
                }
            }
        }

        /* If user didn't explicily specify boot priority,
         * just return the first usable disk */
        if ((match == NULL) && (first_usable_disk_index >= 0))
            return def->disks[first_usable_disk_index];
674 675
    }

676 677 678 679 680 681 682 683 684
    return match;
}

virCommandPtr
virBhyveProcessBuildLoadCmd(virConnectPtr conn, virDomainDefPtr def,
                            const char *devmap_file, char **devicesmap_out)
{
    virDomainDiskDefPtr disk = NULL;

685
    if (def->os.bootloader == NULL) {
686
        disk = virBhyveGetBootDisk(conn, def);
687

688
        if (disk == NULL)
689 690 691 692 693 694 695 696 697 698
            return NULL;

        return virBhyveProcessBuildBhyveloadCmd(def, disk);
    } else if (strstr(def->os.bootloader, "grub-bhyve") != NULL) {
        return virBhyveProcessBuildGrubbhyveCmd(def, conn, devmap_file,
                                                devicesmap_out);
    } else {
        return virBhyveProcessBuildCustomLoaderCmd(def);
    }
}