bhyve_command.c 29.3 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 40 41 42
#include "viralloc.h"
#include "virfile.h"
#include "virstring.h"
#include "virlog.h"
#include "virnetdev.h"
#include "virnetdevbridge.h"
#include "virnetdevtap.h"

#define VIR_FROM_THIS VIR_FROM_BHYVE

43 44
VIR_LOG_INIT("bhyve.bhyve_command");

R
Roman Bogorodskiy 已提交
45
static int
R
Roman Bogorodskiy 已提交
46 47
bhyveBuildNetArgStr(virConnectPtr conn,
                    const virDomainDef *def,
48 49 50
                    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;
R
Roman Bogorodskiy 已提交
55
    char *nic_model = NULL;
56
    int ret = -1;
57
    virDomainNetType actualType = virDomainNetGetActualType(net);
R
Roman Bogorodskiy 已提交
58

59 60 61 62 63 64
    if (net->model == NULL) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("NIC model must be specified"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    if (STREQ(net->model, "virtio")) {
        if (VIR_STRDUP(nic_model, "virtio-net") < 0)
            return -1;
    } else if (STREQ(net->model, "e1000")) {
        if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_NET_E1000) != 0) {
            if (VIR_STRDUP(nic_model, "e1000") < 0)
                return -1;
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("NIC model 'e1000' is not supported "
                             "by given bhyve binary"));
            return -1;
        }
    } else {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("NIC model '%s' is not supported"),
                       net->model);
        return -1;
    }

85 86
    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
        if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
87
            goto cleanup;
88 89 90 91
    } else {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Network type %d is not supported"),
                       virDomainNetGetActualType(net));
92
        goto cleanup;
R
Roman Bogorodskiy 已提交
93 94
    }

95
    if (!net->ifname ||
96
        STRPREFIX(net->ifname, VIR_NET_GENERATED_TAP_PREFIX) ||
97 98
        strchr(net->ifname, '%')) {
        VIR_FREE(net->ifname);
99
        if (VIR_STRDUP(net->ifname, VIR_NET_GENERATED_TAP_PREFIX "%d") < 0)
100
            goto cleanup;
101
    }
R
Roman Bogorodskiy 已提交
102

103 104
    if (!dryRun) {
        if (virNetDevTapCreateInBridgePort(brname, &net->ifname, &net->mac,
105
                                           def->uuid, NULL, NULL, 0,
106 107
                                           virDomainNetGetActualVirtPortProfile(net),
                                           virDomainNetGetActualVlan(net),
108
                                           NULL, 0, NULL,
109
                                           VIR_NETDEV_TAP_CREATE_IFUP | VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
110
            goto cleanup;
R
Roman Bogorodskiy 已提交
111 112
        }

113 114
        realifname = virNetDevTapGetRealDeviceName(net->ifname);

115 116
        if (realifname == NULL)
            goto cleanup;
R
Roman Bogorodskiy 已提交
117

118 119 120 121 122
        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
         */
123 124
        if (virNetDevSetOnline(net->ifname, true) != 0)
            goto cleanup;
125 126
    } else {
        if (VIR_STRDUP(realifname, "tap0") < 0)
127
            goto cleanup;
R
Roman Bogorodskiy 已提交
128 129 130 131
    }


    virCommandAddArg(cmd, "-s");
R
Roman Bogorodskiy 已提交
132 133
    virCommandAddArgFormat(cmd, "%d:0,%s,%s,mac=%s",
                           net->info.addr.pci.slot, nic_model,
134
                           realifname, virMacAddrFormat(&net->mac, macaddr));
135 136 137 138 139 140

    ret = 0;
 cleanup:
    if (ret < 0)
        VIR_FREE(net->ifname);
    VIR_FREE(brname);
141
    VIR_FREE(realifname);
R
Roman Bogorodskiy 已提交
142
    VIR_FREE(nic_model);
R
Roman Bogorodskiy 已提交
143

144
    return ret;
R
Roman Bogorodskiy 已提交
145 146
}

147 148 149 150 151 152 153 154 155 156 157
static int
bhyveBuildConsoleArgStr(const virDomainDef *def, virCommandPtr cmd)
{

    virDomainChrDefPtr chr = NULL;

    if (!def->nserials)
        return 0;

    chr = def->serials[0];

158
    if (chr->source->type != VIR_DOMAIN_CHR_TYPE_NMDM) {
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        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;
    }

    virCommandAddArg(cmd, "-l");
    virCommandAddArgFormat(cmd, "com%d,%s",
173
                           chr->target.port + 1, chr->source->data.file.path);
174 175 176 177

    return 0;
}

R
Roman Bogorodskiy 已提交
178
static int
179 180 181 182
bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
                               virDomainControllerDefPtr controller,
                               virConnectPtr conn,
                               virCommandPtr cmd)
R
Roman Bogorodskiy 已提交
183
{
184 185
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virBuffer device = VIR_BUFFER_INITIALIZER;
R
Roman Bogorodskiy 已提交
186
    const char *disk_source;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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;
        }

A
Andrea Bolognani 已提交
207
        if (virDomainDiskTranslateSourcePool(disk) < 0)
208 209 210 211 212 213 214 215 216 217 218
            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 已提交
219

R
Roman Bogorodskiy 已提交
220 221
        switch (disk->device) {
        case VIR_DOMAIN_DISK_DEVICE_DISK:
222 223 224 225
            if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_AHCI32SLOT))
                virBufferAsprintf(&device, ",hd:%s", disk_source);
            else
                virBufferAsprintf(&device, "-hd,%s", disk_source);
R
Roman Bogorodskiy 已提交
226 227
            break;
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
228 229 230 231
            if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_AHCI32SLOT))
                virBufferAsprintf(&device, ",cd:%s", disk_source);
            else
                virBufferAsprintf(&device, "-cd,%s", disk_source);
R
Roman Bogorodskiy 已提交
232 233 234 235
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
236
            goto error;
R
Roman Bogorodskiy 已提交
237
        }
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        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;
}

256 257 258 259 260 261 262 263 264 265 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
static int
bhyveBuildUSBControllerArgStr(const virDomainDef *def,
                              virDomainControllerDefPtr controller,
                              virCommandPtr cmd)
{
    size_t i;
    int ndevices = 0;

    for (i = 0; i < def->ninputs; i++) {
        virDomainInputDefPtr input = def->inputs[i];

        if (input->bus != VIR_DOMAIN_INPUT_BUS_USB) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("only USB input devices are supported"));
            return -1;
        }

        if (input->type != VIR_DOMAIN_INPUT_TYPE_TABLET) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("only tablet input devices are supported"));
            return -1;
        }
        ndevices++;
    }

    if (ndevices != 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("only single input device is supported"));
        return -1;
    }

    virCommandAddArg(cmd, "-s");
    virCommandAddArgFormat(cmd, "%d:%d,xhci,tablet",
                           controller->info.addr.pci.slot,
                           controller->info.addr.pci.function);

    return 0;
}

295 296 297 298 299 300 301
static int
bhyveBuildVirtIODiskArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
                     virDomainDiskDefPtr disk,
                     virCommandPtr cmd)
{
    const char *disk_source;

A
Andrea Bolognani 已提交
302
    if (virDomainDiskTranslateSourcePool(disk) < 0)
303 304 305
        return -1;

    if (disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) {
R
Roman Bogorodskiy 已提交
306
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
307
                       _("unsupported disk device"));
R
Roman Bogorodskiy 已提交
308 309 310
        return -1;
    }

R
Roman Bogorodskiy 已提交
311 312
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
313 314 315 316 317
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
318 319
    disk_source = virDomainDiskGetSource(disk);

R
Roman Bogorodskiy 已提交
320
    virCommandAddArg(cmd, "-s");
321 322
    virCommandAddArgFormat(cmd, "%d:0,virtio-blk,%s",
                           disk->info.addr.pci.slot,
R
Roman Bogorodskiy 已提交
323
                           disk_source);
R
Roman Bogorodskiy 已提交
324 325 326 327

    return 0;
}

328 329 330 331 332 333 334 335
static int
bhyveBuildLPCArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
                    virCommandPtr cmd)
{
    virCommandAddArgList(cmd, "-s", "1,lpc", NULL);
    return 0;
}

F
Fabian Freyer 已提交
336
static int
337
bhyveBuildGraphicsArgStr(const virDomainDef *def,
F
Fabian Freyer 已提交
338 339 340
                         virDomainGraphicsDefPtr graphics,
                         virDomainVideoDefPtr video,
                         virConnectPtr conn,
341 342
                         virCommandPtr cmd,
                         bool dryRun)
F
Fabian Freyer 已提交
343 344 345 346
{
    virBuffer opt = VIR_BUFFER_INITIALIZER;
    virDomainGraphicsListenDefPtr glisten = NULL;
    bool escapeAddr;
347 348 349
    unsigned short port;

    bhyveConnPtr driver = conn->privateData;
F
Fabian Freyer 已提交
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

    if (!(bhyveDriverGetCaps(conn) & BHYVE_CAP_LPC_BOOTROM) ||
        def->os.bootloader ||
        !def->os.loader) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Graphics are only supported"
                         " when booting using UEFI"));
        return -1;
    }

    if (!(bhyveDriverGetCaps(conn) & BHYVE_CAP_FBUF)) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Bhyve version does not support framebuffer"));
        return -1;
    }

    if (graphics->type != VIR_DOMAIN_GRAPHICS_TYPE_VNC) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Only VNC supported"));
        return -1;
    }

    if (!(glisten = virDomainGraphicsGetListen(graphics, 0))) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Missing listen element"));
        goto error;
    }

    virBufferAsprintf(&opt, "%d:%d,fbuf", video->info.addr.pci.slot, video->info.addr.pci.function);

    switch (glisten->type) {
    case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_ADDRESS:
    case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NETWORK:
        virBufferAddLit(&opt, ",tcp=");

        if (!graphics->data.vnc.autoport &&
            (graphics->data.vnc.port < 5900 ||
             graphics->data.vnc.port > 65535)) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("vnc port must be in range [5900,65535]"));
            goto error;
        }

        if (graphics->data.vnc.auth.passwd) {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("vnc password auth not supported"));
            goto error;
        } else {
             /* Bhyve doesn't support VNC Auth yet, so print a warning about
              * unauthenticated VNC sessions */
             VIR_WARN("%s", _("Security warning: currently VNC auth is not"
                              " supported."));
        }

        if (glisten->address) {
            escapeAddr = strchr(glisten->address, ':') != NULL;
            if (escapeAddr)
                virBufferAsprintf(&opt, "[%s]", glisten->address);
            else
                virBufferAdd(&opt, glisten->address, -1);
        }

412 413 414 415 416 417
        if (!dryRun) {
            if (graphics->data.vnc.autoport) {
                if (virPortAllocatorAcquire(driver->remotePorts, &port) < 0)
                    return -1;
                graphics->data.vnc.port = port;
            } else {
418
                if (virPortAllocatorSetUsed(graphics->data.vnc.port) < 0)
419 420 421 422 423
                    VIR_WARN("Failed to mark VNC port '%d' as used by '%s'",
                             graphics->data.vnc.port, def->name);
            }
        }

F
Fabian Freyer 已提交
424 425
        virBufferAsprintf(&opt, ":%d", graphics->data.vnc.port);
        break;
426 427
    case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_SOCKET:
    case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_NONE:
F
Fabian Freyer 已提交
428 429
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("Unsupported listen type"));
430 431 432 433 434
        goto error;
    case VIR_DOMAIN_GRAPHICS_LISTEN_TYPE_LAST:
    default:
        virReportEnumRangeError(virDomainGraphicsListenType, glisten->type);
        goto error;
F
Fabian Freyer 已提交
435 436
    }

437 438 439 440
    if (video->driver)
        virBufferAsprintf(&opt, ",vga=%s",
                          virDomainVideoVGAConfTypeToString(video->driver->vgaconf));

F
Fabian Freyer 已提交
441 442 443 444 445 446 447 448 449
    virCommandAddArg(cmd, "-s");
    virCommandAddArgBuffer(cmd, &opt);
    return 0;

 error:
    virBufferFreeAndReset(&opt);
    return -1;
}

R
Roman Bogorodskiy 已提交
450
virCommandPtr
R
Roman Bogorodskiy 已提交
451
virBhyveProcessBuildBhyveCmd(virConnectPtr conn,
452
                             virDomainDefPtr def, bool dryRun)
R
Roman Bogorodskiy 已提交
453 454 455 456 457 458 459 460 461
{
    /*
     * /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
     */
462
    size_t i;
463
    bool add_lpc = false;
464
    int nusbcontrollers = 0;
465

R
Roman Bogorodskiy 已提交
466 467 468 469
    virCommandPtr cmd = virCommandNew(BHYVE);

    /* CPUs */
    virCommandAddArg(cmd, "-c");
470
    virCommandAddArgFormat(cmd, "%d", virDomainDefGetVcpus(def));
R
Roman Bogorodskiy 已提交
471 472 473 474

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

477 478 479
    if (def->mem.locked)
        virCommandAddArg(cmd, "-S"); /* Wire guest memory */

R
Roman Bogorodskiy 已提交
480
    /* Options */
J
Ján Tomko 已提交
481
    if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
482
        virCommandAddArg(cmd, "-A"); /* Create an ACPI table */
J
Ján Tomko 已提交
483
    if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
484 485
        virCommandAddArg(cmd, "-I"); /* Present ioapic to the guest */

R
Roman Bogorodskiy 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    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 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520
    /* 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 */

521
    virCommandAddArgList(cmd, "-s", "0:0,hostbridge", NULL);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

    if (def->os.bootloader == NULL &&
        def->os.loader) {
        if ((bhyveDriverGetCaps(conn) & BHYVE_CAP_LPC_BOOTROM)) {
            virCommandAddArg(cmd, "-l");
            virCommandAddArgFormat(cmd, "bootrom,%s", def->os.loader->path);
            add_lpc = true;
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Installed bhyve binary does not support "
                             "UEFI loader"));
            goto error;
        }
    }

R
Roman Bogorodskiy 已提交
537
    /* Devices */
538 539 540 541 542 543 544 545 546 547 548 549 550 551
    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;
552 553 554 555 556 557 558 559 560 561
        case VIR_DOMAIN_CONTROLLER_TYPE_USB:
                if (++nusbcontrollers > 1) {
                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                       "%s", _("only single USB controller is supported"));
                        goto error;
                }

                if (bhyveBuildUSBControllerArgStr(def, controller, cmd) < 0)
                    goto error;
                break;
562 563
        }
    }
564 565
    for (i = 0; i < def->nnets; i++) {
        virDomainNetDefPtr net = def->nets[i];
R
Roman Bogorodskiy 已提交
566
        if (bhyveBuildNetArgStr(conn, def, net, cmd, dryRun) < 0)
567 568 569 570 571
            goto error;
    }
    for (i = 0; i < def->ndisks; i++) {
        virDomainDiskDefPtr disk = def->disks[i];

572 573 574 575 576
        switch (disk->bus) {
        case VIR_DOMAIN_DISK_BUS_SATA:
            /* Handled by bhyveBuildAHCIControllerArgStr() */
            break;
        case VIR_DOMAIN_DISK_BUS_VIRTIO:
A
Andrea Bolognani 已提交
577
            if (bhyveBuildVirtIODiskArgStr(def, disk, cmd) < 0)
578 579 580 581 582
                goto error;
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
583
            goto error;
584
        }
585
    }
586

F
Fabian Freyer 已提交
587 588
    if (def->ngraphics && def->nvideos) {
        if (def->ngraphics == 1 && def->nvideos == 1) {
589 590
            if (bhyveBuildGraphicsArgStr(def, def->graphics[0], def->videos[0],
                                         conn, cmd, dryRun) < 0)
F
Fabian Freyer 已提交
591 592 593 594 595 596 597 598 599
                goto error;
            add_lpc = true;
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("Multiple graphics devices are not supported"));
             goto error;
        }
    }

600 601 602
    if (add_lpc || def->nserials)
        bhyveBuildLPCArgStr(def, cmd);

603
    if (bhyveBuildConsoleArgStr(def, cmd) < 0)
604
        goto error;
605

606
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
607 608 609

    return cmd;

610
 error:
R
Roman Bogorodskiy 已提交
611 612 613 614 615 616
    virCommandFree(cmd);
    return NULL;
}

virCommandPtr
virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
617
                               virDomainDefPtr def)
R
Roman Bogorodskiy 已提交
618 619 620 621
{
    virCommandPtr cmd = virCommandNew(BHYVECTL);

    virCommandAddArg(cmd, "--destroy");
622
    virCommandAddArgPair(cmd, "--vm", def->name);
R
Roman Bogorodskiy 已提交
623 624 625 626

    return cmd;
}

627 628 629 630 631 632 633 634
static void
virAppendBootloaderArgs(virCommandPtr cmd, virDomainDefPtr def)
{
    char **blargs;

    /* XXX: Handle quoted? */
    blargs = virStringSplit(def->os.bootloaderArgs, " ", 0);
    virCommandAddArgSet(cmd, (const char * const *)blargs);
635
    virStringListFree(blargs);
636 637 638 639
}

static virCommandPtr
virBhyveProcessBuildBhyveloadCmd(virDomainDefPtr def, virDomainDiskDefPtr disk)
R
Roman Bogorodskiy 已提交
640 641 642
{
    virCommandPtr cmd;

643 644 645 646 647 648 649 650
    cmd = virCommandNew(BHYVELOAD);

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

        /* Memory (MB) */
        virCommandAddArg(cmd, "-m");
        virCommandAddArgFormat(cmd, "%llu",
651
                               VIR_DIV_UP(virDomainDefGetMemoryInitial(def), 1024));
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

        /* 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 已提交
676 677 678
        return NULL;
    }

679 680 681 682 683 684 685 686
    VIR_DEBUG("custom loader '%s' with arguments", def->os.bootloader);

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

static bool
A
Andrea Bolognani 已提交
687
virBhyveUsableDisk(virDomainDiskDefPtr disk)
688
{
R
Roman Bogorodskiy 已提交
689

A
Andrea Bolognani 已提交
690
    if (virDomainDiskTranslateSourcePool(disk) < 0)
691
        return false;
R
Roman Bogorodskiy 已提交
692

R
Roman Bogorodskiy 已提交
693 694
    if ((disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) &&
        (disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM)) {
R
Roman Bogorodskiy 已提交
695 696
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk device"));
697
        return false;
R
Roman Bogorodskiy 已提交
698 699
    }

R
Roman Bogorodskiy 已提交
700 701
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
702 703
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
704
        return false;
R
Roman Bogorodskiy 已提交
705 706
    }

707 708
    return true;
}
R
Roman Bogorodskiy 已提交
709

710 711 712 713 714 715 716 717 718 719 720 721
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));
}

722 723 724 725 726 727
static virCommandPtr
virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
                                 virConnectPtr conn,
                                 const char *devmap_file,
                                 char **devicesmap_out)
{
728
    virDomainDiskDefPtr hdd, cd, userdef, diskdef;
729 730
    virBuffer devicemap;
    virCommandPtr cmd;
731
    unsigned int best_idx = UINT_MAX;
732 733 734 735 736 737 738
    size_t i;

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

    devicemap = (virBuffer)VIR_BUFFER_INITIALIZER;

739 740 741 742
    /* 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;
743
    for (i = 0; i < def->ndisks; i++) {
A
Andrea Bolognani 已提交
744
        if (!virBhyveUsableDisk(def->disks[i]))
745 746
            continue;

747 748 749 750 751 752 753 754
        diskdef = def->disks[i];

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

755 756
        if (cd == NULL &&
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
757 758
            cd = diskdef;
            VIR_INFO("Picking %s as CD", virDomainDiskGetSource(cd));
759 760
        }

761
        if (hdd == NULL &&
762
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
763 764
            hdd = diskdef;
            VIR_INFO("Picking %s as HDD", virDomainDiskGetSource(hdd));
765 766 767 768 769 770 771 772 773
        }
    }

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

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

    if (devicesmap_out != NULL) {
        /* Grub device.map (just for boot) */
774 775 776 777 778
        if (userdef != NULL) {
            virBhyveFormatGrubDevice(&devicemap, userdef);
        } else {
            if (hdd != NULL)
                virBhyveFormatGrubDevice(&devicemap, hdd);
779

780 781 782
            if (cd != NULL)
                virBhyveFormatGrubDevice(&devicemap, cd);
        }
783 784 785 786

        *devicesmap_out = virBufferContentAndReset(&devicemap);
    }

787 788 789 790 791 792 793
    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) {
794 795 796 797 798 799 800 801 802 803
        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 已提交
804
    virCommandAddArgFormat(cmd, "%llu",
805
                           VIR_DIV_UP(virDomainDefGetMemoryInitial(def), 1024));
R
Roman Bogorodskiy 已提交
806

807 808 809 810 811 812
    if ((bhyveDriverGetGrubCaps(conn) & BHYVE_GRUB_CAP_CONSDEV) != 0 &&
        def->nserials > 0) {
        virDomainChrDefPtr chr;

        chr = def->serials[0];

813
        if (chr->source->type != VIR_DOMAIN_CHR_TYPE_NMDM) {
814 815 816 817 818 819
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("only nmdm console types are supported"));
            return NULL;
        }

        virCommandAddArg(cmd, "--cons-dev");
820
        virCommandAddArg(cmd, chr->source->data.file.path);
821 822
    }

R
Roman Bogorodskiy 已提交
823
    /* VM name */
824
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
825 826 827

    return cmd;
}
828

829
static virDomainDiskDefPtr
A
Andrea Bolognani 已提交
830
virBhyveGetBootDisk(virDomainDefPtr def)
831
{
832 833 834
    size_t i;
    virDomainDiskDefPtr match = NULL;
    int boot_dev = -1;
835 836 837

    if (def->ndisks < 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
838 839 840 841 842 843 844
                       _("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"));
845
        return NULL;
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    } 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++) {
A
Andrea Bolognani 已提交
866
            if (!virBhyveUsableDisk(def->disks[i]))
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
                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++) {
A
Andrea Bolognani 已提交
890
            if (!virBhyveUsableDisk(def->disks[i]))
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
                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];
910 911
    }

912 913 914 915 916 917 918 919 920
    return match;
}

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

921
    if (def->os.bootloader == NULL) {
A
Andrea Bolognani 已提交
922
        disk = virBhyveGetBootDisk(def);
923

924
        if (disk == NULL)
925 926 927 928 929 930 931 932 933 934
            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);
    }
}