vm-bhyve

repository·master·Indexed 21 days ago

https://github.com/churchers/vm-bhyve

A management system for FreeBSD bhyve virtual machines that provides a simplified interface for creating, configuring, and managing guests. It includes support for ZFS, virtual switches, cloud-init, and cloud images (RAW/QCOW2), as well as the ability to use templates for hardware specifications.

Tokens
2.3K
Snippets
10
Records
10
Agent score
25%

What's inside vm-bhyve

  1. Use Virtual Machine Templates

    master

    Templates define guest hardware specifications like CPU, memory, and networking. They are stored in $vm_dir/.templates/.

    Installing Sample Templates: Copy the provided examples to your template directory:

    cp /usr/local/share/examples/vm-bhyve/* /your/vm/path/.templates/

    Template Format Example: A template file (e.g., web-server.conf) uses simple key-value pairs:

    guest="freebsd"
    loader="bhyveload"
    cpu=1
    memory=256M
    disk0_type="virtio-blk"
    disk0_name="disk0.img"
    network0_type="virtio-net"
    network0_switch="public"

    You can add multiple network interfaces by incrementing the index (e.g., network1_type, network1_switch).

    # Copy sample templates to your VM directory
    cp /usr/local/share/examples/vm-bhyve/* /my/vm/path/.templates/
  2. Create and Install a Virtual Machine

    master

    To deploy a new guest, follow these steps:

    1. Download an ISO: vm iso <url>

    2. Create the VM:

      • Using default template: vm create <name>
      • Using specific template and disk size: vm create -t <template> -s <size> <name> (e.g., vm create -t mytemplate -s 50G myguest)
    3. Install the OS:

      • Background mode (recommended): Runs the installer in the background. You must then use vm console <name> to interact with it. vm install <name> <iso_name>
      • Foreground mode: Runs the installer directly in your current terminal. vm install -f <name> <iso_name>

    Note: When using vm console, use ~+Ctrl-D to exit back to the host.

    # Quick workflow for a FreeBSD guest
    vm iso https://download.freebsd.org/ftp/releases/ISO-IMAGES/14.2/FreeBSD-14.2-RELEASE-amd64-bootonly.iso
    vm create myguest
    vm install myguest FreeBSD-14.2-RELEASE-amd64-bootonly.iso
    vm console myguest
  3. Use cloud-init to configure guests

    master

    You can provide cloud-init configuration to a guest during creation. Use the -C flag with the vm create command to enable cloud-init support. Additionally, you can inject public SSH keys into the guest using the -k <file> option. The specified file should contain one public SSH key per line in the standard authorized_keys format.

    Example

    # vm create -t linux -i xenial-server-cloudimg-amd64-uefi1.img -C -k ~/.ssh/id_rsa.pub cloud-init-ubuntu
    # vm start cloud-init-ubuntu
    # vm create -t linux -i xenial-server-cloudimg-amd64-uefi1.img -C -k ~/.ssh/id_rsa.pub cloud-init-ubuntu
  4. Configure vm-bhyve initial setup

    master

    After installation, you must define a directory (or ZFS dataset) to store virtual machines and configuration. This directory is referred to as $vm_dir.

    1. Create the storage location:

      • For standard directories: mkdir /somefolder/vm
      • For ZFS: zfs create pool/vm
    2. Enable in /etc/rc.conf:

      • For standard directories:
        vm_enable="YES"
        vm_dir="/somefolder/vm"
      • For ZFS:
        vm_enable="YES"
        vm_dir="zfs:pool/vm"
    3. Initialize: Run vm init to create required directories and load kernel modules. This should be run once after each host reboot (though rc.d usually handles this).

    # Example for ZFS
    zfs create pool/vm
    sysrc vm_enable="YES"
    sysrc vm_dir="zfs:pool/vm"
    vm init
  5. Install vm-bhyve

    master

    You can install vm-bhyve via the FreeBSD package manager or by building from source.

    Dependencies:

    • For Linux or guests requiring a Grub bootloader: pkg install grub2-bhyve
    • For UEFI guests: pkg install bhyve-firmware
    • For tmux console access: pkg install tmux
    • For cloud image support (RAW/QCOW2): pkg install qemu-tools
    # Install via pkg
    # pkg install vm-bhyve
    
    # Or build from source
    # make install
  6. Add custom disks to a virtual machine

    master

    To add a new virtual disk to an existing VM, especially if the disk needs to reside on a different ZFS pool, follow these steps:

    1. Create a sparse-zvol on the target pool: zfs create -sV <size> -o volmode=dev "<zpool>/<path>/<disk_name>"

    2. Update the VM configuration using vm configure <vm_name>. You must define the device name, type, and set the device to custom.

    Important for Windows guests: Use diskX_type="ahci-hd" instead of virtio-blk, as Windows guests may lack virtio-blk drivers.

    1. Restart the VM to apply changes.

    Example (Linux/Virtio)

    # zfs create -sV 50G -o volmode=dev "zpool2/vm/yourvm/disk1"
    # vm configure yourvm
    # disk1_name="/dev/zvol/zpool2/vm/yourvm/disk1"
    # disk1_type="virtio-blk"
    # disk1_dev="custom"
    # zfs create -sV 50G -o volmode=dev "zpool2/vm/yourvm/disk1"
    
    # vm configure yourvm
    
    disk1_name="/dev/zvol/zpool2/vm/yourvm/disk1"
    disk1_type="virtio-blk"
    disk1_dev="custom"
  7. Enable autocomplete for the vm CLI in csh/tcsh

    master

    If you use the default csh or tcsh shell in FreeBSD, you can enable autocomplete for vm commands, switches, ISO files, and guest names.

    To make this permanent, add the following block to your $HOME/.cshrc file. Note: You must replace all three occurrences of /path/to/vm with the actual directory where your virtual machines are stored.

    complete vm \
     'p@1@(list create install start stop console configure reset poweroff destroy clone snapshot rollback add switch iso)@' \
     'n@create@n@' \
     'n@list@n@' \
     'n@iso@n@' \
     'n@switch@(list create add remove destroy vlan nat)@' \
     'N@switch@`sysrc -inqf /path/to/vm/.config/switch switch_list`@' \
     'N@install@`ls -1 /path/to/vm/.iso`@' \
     'N@nat@(off on)@' \
     'p@2@`ls -1 /path/to/vm | grep -v "^\." | grep -v "^images"`@'
    complete vm \
     'p@1@(list create install start stop console configure reset poweroff destroy clone snapshot rollback add switch iso)@' \
     'n@create@n@' \
     'n@list@n@' \
     'n@iso@n@' \
     'n@switch@(list create add remove destroy vlan nat)@' \
     'N@switch@`sysrc -inqf /path/to/vm/.config/switch switch_list`@' \
     'N@install@`ls -1 /path/to/vm/.iso`@' \
     'N@nat@(off on)@' \
     'p@2@`ls -1 /path/to/vm | grep -v "^\." | grep -v "^images"`@'
  8. Use Cloud Images

    master

    You can download and deploy cloud images (RAW or QCOW2) using the vm img command. This requires the qemu-tools package.

    Workflow:

    1. Download/Uncompress image: vm img <url>
    2. Create VM from image: vm create -t <template> -i <image_name> <name>
    3. Start VM: vm start <name>

    Example (FreeBSD Cloud Image):

    pkg install qemu-tools
    vm img https://download.freebsd.org/ftp/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64.raw.xz
    vm create -t freebsd-zvol -i FreeBSD-14.2-RELEASE-amd64.raw freebsd-cloud
    vm start freebsd-cloud
    # Download and create from cloud image
    vm img https://download.freebsd.org/ftp/releases/VM-IMAGES/14.2-RELEASE/amd64/Latest/FreeBSD-14.2-RELEASE-amd64.raw.xz
    vm create -t freebsd-zvol -i FreeBSD-14.2-RELEASE-amd64.raw freebsd-cloud
    vm start freebsd-cloud
  9. Manage Virtual Machine Lifecycle

    master

    Use the following commands to control running guests:

    • List all VMs: vm list (shows name, guest type, CPU, memory, and state)
    • Start a VM: vm start <name>
    • Stop a VM: vm stop <name>
    • Stop all running VMs: vm stopall
    • Configure a VM: vm configure <name> (opens the guest's configuration file in your default editor. Changes require a full shutdown/restart to take effect).

    Autostart Configuration: To start specific machines on host boot, add these to /etc/rc.conf:

    • vm_list="vm1 vm2": List of machines and their start order.
    • vm_delay="5": Seconds to wait between starting each machine (5s recommended).
    # Start and stop
    vm start testvm
    vm stop testvm
    
    # Bulk operations
    vm stopall
    
    # Edit configuration
    vm configure testvm
  10. Manage Virtual Switches

    master

    Virtual switches connect guest network interfaces to each other or to physical network interfaces.

    Common Tasks:

    • Create a switch: vm switch create <name>
    • Attach a physical interface: vm switch add <switch_name> <interface> (e.g., em0)
    • Configure VLANs:
      • Set a VLAN: vm switch vlan <switch_name> <vlan_id>
      • Disable VLANs: vm switch vlan <switch_name> 0
    • List switches: vm switch list
    # Create and attach a switch to em0
    vm switch create public
    vm switch add public em0
    
    # Set VLAN 10 on the switch
    vm switch vlan public 10