bhyve_command.c 14.2 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 28
 *
 * 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>

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

#define VIR_FROM_THIS VIR_FROM_BHYVE

42 43
VIR_LOG_INIT("bhyve.bhyve_command");

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

55 56 57 58 59 60 61
    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE) {
        if (VIR_STRDUP(brname, virDomainNetGetActualBridgeName(net)) < 0)
            return -1;
    } else {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                       _("Network type %d is not supported"),
                       virDomainNetGetActualType(net));
R
Roman Bogorodskiy 已提交
62 63 64
        return -1;
    }

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

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

86 87 88
        realifname = virNetDevTapGetRealDeviceName(net->ifname);

        if (realifname == NULL) {
R
Roman Bogorodskiy 已提交
89 90 91 92 93
            VIR_FREE(net->ifname);
            VIR_FREE(brname);
            return -1;
        }

94 95 96 97 98 99
        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
         */
        if (virNetDevSetOnline(net->ifname, true) != 0) {
100
            VIR_FREE(realifname);
101 102 103 104 105 106 107
            VIR_FREE(net->ifname);
            VIR_FREE(brname);
            return -1;
        }
    } else {
        if (VIR_STRDUP(realifname, "tap0") < 0)
            return -1;
R
Roman Bogorodskiy 已提交
108 109 110 111
    }


    virCommandAddArg(cmd, "-s");
112 113
    virCommandAddArgFormat(cmd, "%d:0,virtio-net,%s,mac=%s",
                           net->info.addr.pci.slot,
114
                           realifname, virMacAddrFormat(&net->mac, macaddr));
115
    VIR_FREE(realifname);
R
Roman Bogorodskiy 已提交
116 117 118 119

    return 0;
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static int
bhyveBuildConsoleArgStr(const virDomainDef *def, virCommandPtr cmd)
{

    virDomainChrDefPtr chr = NULL;

    if (!def->nserials)
        return 0;

    chr = def->serials[0];

    if (chr->source.type != VIR_DOMAIN_CHR_TYPE_NMDM) {
        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;
    }

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

    return 0;
}

R
Roman Bogorodskiy 已提交
152
static int
153 154 155
bhyveBuildDiskArgStr(const virDomainDef *def ATTRIBUTE_UNUSED,
                     virDomainDiskDefPtr disk,
                     virCommandPtr cmd)
R
Roman Bogorodskiy 已提交
156
{
157
    const char *bus_type;
R
Roman Bogorodskiy 已提交
158
    const char *disk_source;
R
Roman Bogorodskiy 已提交
159

160 161
    switch (disk->bus) {
    case VIR_DOMAIN_DISK_BUS_SATA:
R
Roman Bogorodskiy 已提交
162 163 164 165 166 167 168 169 170 171 172 173
        switch (disk->device) {
        case VIR_DOMAIN_DISK_DEVICE_DISK:
            bus_type = "ahci-hd";
            break;
        case VIR_DOMAIN_DISK_DEVICE_CDROM:
            bus_type = "ahci-cd";
            break;
        default:
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
            return -1;
        }
174 175
        break;
    case VIR_DOMAIN_DISK_BUS_VIRTIO:
R
Roman Bogorodskiy 已提交
176 177 178 179 180 181 182
        if (disk->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
            bus_type = "virtio-blk";
        } else {
            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                           _("unsupported disk device"));
            return -1;
        }
183 184
        break;
    default:
R
Roman Bogorodskiy 已提交
185 186 187 188 189
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk bus type"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
190 191
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
192 193 194 195 196
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
        return -1;
    }

R
Roman Bogorodskiy 已提交
197 198 199 200 201 202 203 204 205 206
    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"));
            return -1;
    }

R
Roman Bogorodskiy 已提交
207
    virCommandAddArg(cmd, "-s");
208 209
    virCommandAddArgFormat(cmd, "%d:0,%s,%s",
                           disk->info.addr.pci.slot, bus_type,
R
Roman Bogorodskiy 已提交
210
                           disk_source);
R
Roman Bogorodskiy 已提交
211 212 213 214 215

    return 0;
}

virCommandPtr
R
Roman Bogorodskiy 已提交
216
virBhyveProcessBuildBhyveCmd(virConnectPtr conn,
217
                             virDomainDefPtr def, bool dryRun)
R
Roman Bogorodskiy 已提交
218 219 220 221 222 223 224 225 226
{
    /*
     * /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
     */
227 228
    size_t i;

R
Roman Bogorodskiy 已提交
229 230 231 232
    virCommandPtr cmd = virCommandNew(BHYVE);

    /* CPUs */
    virCommandAddArg(cmd, "-c");
233
    virCommandAddArgFormat(cmd, "%d", def->vcpus);
R
Roman Bogorodskiy 已提交
234 235 236 237

    /* Memory */
    virCommandAddArg(cmd, "-m");
    virCommandAddArgFormat(cmd, "%llu",
238
                           VIR_DIV_UP(def->mem.max_balloon, 1024));
R
Roman Bogorodskiy 已提交
239 240

    /* Options */
J
Ján Tomko 已提交
241
    if (def->features[VIR_DOMAIN_FEATURE_ACPI] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
242
        virCommandAddArg(cmd, "-A"); /* Create an ACPI table */
J
Ján Tomko 已提交
243
    if (def->features[VIR_DOMAIN_FEATURE_APIC] == VIR_TRISTATE_SWITCH_ON)
R
Roman Bogorodskiy 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        virCommandAddArg(cmd, "-I"); /* Present ioapic to the guest */

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

260
    virCommandAddArgList(cmd, "-s", "0:0,hostbridge", NULL);
R
Roman Bogorodskiy 已提交
261
    /* Devices */
262 263 264 265 266 267 268 269
    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];

R
Roman Bogorodskiy 已提交
270 271 272
        if (virStorageTranslateDiskSourcePool(conn, disk) < 0)
            goto error;

273 274 275
        if (bhyveBuildDiskArgStr(def, disk, cmd) < 0)
            goto error;
    }
276
    if (bhyveBuildConsoleArgStr(def, cmd) < 0)
277
        goto error;
278
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
279 280 281

    return cmd;

282
 error:
R
Roman Bogorodskiy 已提交
283 284 285 286 287 288
    virCommandFree(cmd);
    return NULL;
}

virCommandPtr
virBhyveProcessBuildDestroyCmd(bhyveConnPtr driver ATTRIBUTE_UNUSED,
289
                               virDomainDefPtr def)
R
Roman Bogorodskiy 已提交
290 291 292 293
{
    virCommandPtr cmd = virCommandNew(BHYVECTL);

    virCommandAddArg(cmd, "--destroy");
294
    virCommandAddArgPair(cmd, "--vm", def->name);
R
Roman Bogorodskiy 已提交
295 296 297 298

    return cmd;
}

299 300 301 302 303 304 305 306 307 308 309 310 311
static void
virAppendBootloaderArgs(virCommandPtr cmd, virDomainDefPtr def)
{
    char **blargs;

    /* XXX: Handle quoted? */
    blargs = virStringSplit(def->os.bootloaderArgs, " ", 0);
    virCommandAddArgSet(cmd, (const char * const *)blargs);
    virStringFreeList(blargs);
}

static virCommandPtr
virBhyveProcessBuildBhyveloadCmd(virDomainDefPtr def, virDomainDiskDefPtr disk)
R
Roman Bogorodskiy 已提交
312 313 314
{
    virCommandPtr cmd;

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 342 343 344 345 346 347
    cmd = virCommandNew(BHYVELOAD);

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

        /* Memory (MB) */
        virCommandAddArg(cmd, "-m");
        virCommandAddArgFormat(cmd, "%llu",
                               VIR_DIV_UP(def->mem.max_balloon, 1024));

        /* 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 已提交
348 349 350
        return NULL;
    }

351 352 353 354 355 356 357 358 359 360
    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 已提交
361

R
Roman Bogorodskiy 已提交
362
    if (virStorageTranslateDiskSourcePool(conn, disk) < 0)
363
        return false;
R
Roman Bogorodskiy 已提交
364

R
Roman Bogorodskiy 已提交
365 366
    if ((disk->device != VIR_DOMAIN_DISK_DEVICE_DISK) &&
        (disk->device != VIR_DOMAIN_DISK_DEVICE_CDROM)) {
R
Roman Bogorodskiy 已提交
367 368
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk device"));
369
        return false;
R
Roman Bogorodskiy 已提交
370 371
    }

R
Roman Bogorodskiy 已提交
372 373
    if ((virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_FILE) &&
        (virDomainDiskGetType(disk) != VIR_STORAGE_TYPE_VOLUME)) {
R
Roman Bogorodskiy 已提交
374 375
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("unsupported disk type"));
376
        return false;
R
Roman Bogorodskiy 已提交
377 378
    }

379 380
    return true;
}
R
Roman Bogorodskiy 已提交
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 435 436 437 438 439 440 441 442 443 444 445 446
static virCommandPtr
virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
                                 virConnectPtr conn,
                                 const char *devmap_file,
                                 char **devicesmap_out)
{
    virDomainDiskDefPtr disk, cd;
    virBuffer devicemap;
    virCommandPtr cmd;
    size_t i;

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

    devicemap = (virBuffer)VIR_BUFFER_INITIALIZER;

    /* Search disk list for CD or HDD device. */
    cd = disk = NULL;
    for (i = 0; i < def->ndisks; i++) {
        if (!virBhyveUsableDisk(conn, def->disks[i]))
            continue;

        if (cd == NULL &&
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_CDROM) {
            cd = def->disks[i];
            VIR_INFO("Picking %s as boot CD", virDomainDiskGetSource(cd));
        }

        if (disk == NULL &&
            def->disks[i]->device == VIR_DOMAIN_DISK_DEVICE_DISK) {
            disk = def->disks[i];
            VIR_INFO("Picking %s as HDD", virDomainDiskGetSource(disk));
        }
    }

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

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

    if (devicesmap_out != NULL) {
        /* Grub device.map (just for boot) */
        if (disk != NULL)
            virBufferAsprintf(&devicemap, "(hd0) %s\n",
                              virDomainDiskGetSource(disk));

        if (cd != NULL)
            virBufferAsprintf(&devicemap, "(cd) %s\n",
                              virDomainDiskGetSource(cd));

        *devicesmap_out = virBufferContentAndReset(&devicemap);
    }

    if (cd != NULL) {
        virCommandAddArg(cmd, "--root");
        virCommandAddArg(cmd, "cd");
    } else {
        virCommandAddArg(cmd, "--root");
        virCommandAddArg(cmd, "hd0,msdos1");
    }

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

    /* Memory in MB */
    virCommandAddArg(cmd, "--memory");
R
Roman Bogorodskiy 已提交
447
    virCommandAddArgFormat(cmd, "%llu",
448
                           VIR_DIV_UP(def->mem.max_balloon, 1024));
R
Roman Bogorodskiy 已提交
449 450

    /* VM name */
451
    virCommandAddArg(cmd, def->name);
R
Roman Bogorodskiy 已提交
452 453 454

    return cmd;
}
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

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

    if (def->ndisks < 1) {
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                       _("domain should have at least one disk defined"));
        return NULL;
    }

    if (def->os.bootloader == NULL) {
        disk = def->disks[0];

        if (!virBhyveUsableDisk(conn, disk))
            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);
    }
}