Blog

How to run a VM on eisy (Home Assistant in this example)

bhyve

Would like to run a Guest OS on your eisy?

This tutorial describes a way to use your eisy as a host for a Guest operating system, we'll take a popular Home Automation platform Home Assistant as an example (however any other x64 OS image should work). Virtual Machine lifecycle is handled by vm-bhyve.

Warning

Following instructions assume that:
a. You already have an NVMe card installed in your eisy and
b. You are not on WiFi.

Helper script

Use the following script as an example, it works as is, but feel free to make any modifications. Execute script as root user, for example sudo ./create_ha_vm.sh .

#!/bin/sh

# Where do we want to storge VM resources (ZFS pool name and mount path)
VMFILESET="storage/vms"
VMDIR="/storage/vms"

# Home Assistant VM name and how much resources to allocate
HA_VM_NAME="homeassistant"
HA_VM_CPU="2"
HA_VM_MEM="1G"
HA_VM_DISC="16G"

# specify network interface - by default it's Ethernet re0
INTERFACE="re0"

# pick the latest x86-64 image from here https://github.com/home-assistant/operating-system/releases/
HA_IMAGE_URL="https://github.com/home-assistant/operating-system/releases/download/11.4/haos_generic-x86-64-11.4.img.xz"

# Internal variables
TMPDIR=`mktemp -d`
IMAGE_NAME="${TMPDIR}/haos_generic-x86-64.img"
VM_CONF=${VMDIR}/${HA_VM_NAME}/${HA_VM_NAME}.conf

# make sure ifconfig_DEFAULT is not set as it causes tap0 interface issues
# ensure re0 is set to DHCP
sysrc -x ifconfig_DEFAULT
sysrc ifconfig_re0="DHCP"

echo "Make sure necessary packages are installed"
pkg install -y vm-bhyve edk2-bhyve wget qemu-tools

echo "Prepare /etc/rc.conf"
sysrc vm_enable="YES"
sysrc vm_dir="zfs:${VMFILESET}"

# this makes Home Assistant VM start up automatically on boot, comment out if this is not desired
sysrc vm_list=${HA_VM_NAME}

echo "Create ZFS fileset for VMs and prepare templates"
zfs create ${VMFILESET}
vm init
cp /usr/local/share/examples/vm-bhyve/*.conf ${VMDIR}/.templates/

# create VM networking (common for all VMs on the system)
vm switch create public
vm switch add public ${INTERFACE}

echo "Downloading image"
wget -O ${IMAGE_NAME}.xz ${HA_IMAGE_URL}
echo "Extracting..."
unxz ${IMAGE_NAME}.xz

echo "Creating a VM"
vm create -t linux-zvol -s ${HA_VM_DISC} ${HA_VM_NAME}

echo "Copying image"
dd if=${IMAGE_NAME} of=/dev/zvol/${VMFILESET}/${HA_VM_NAME}/disk0 bs=1m
rm -rf ${TMPDIR}

sysrc -f ${VM_CONF} loader="uefi"
sysrc -f ${VM_CONF} cpu=${HA_VM_CPU}
sysrc -f ${VM_CONF} memory=${HA_VM_MEM}

vm start ${HA_VM_NAME}
vm info ${HA_VM_NAME}
vm list

echo "Please wait about 10 minutes and follow instructions at https://www.home-assistant.io/getting-started/onboarding/ to get your Home Assistant setup"
echo "ISY integration: https://www.home-assistant.io/integrations/isy994/"

Tip

Alternatively, and if you are OK with all the parameters, 
you can simply run the following command:
    curl -s https://pkg.isy.io/script/create_ha_vm.sh | sudo bash

VM basics

Home Assistant VM would be a separate host on your network with it's own IP address, you can use MAC address printed by the script to look up the IP and create a DHCP reservation on your router if desired. Although the script does this for you, if things do not work, please make sure you have:

    ifconfig_re0="DHCP" 

instead of

    ifconfig_DEFAULT="DHCP" 

in /etc/rc.conf.

If you don't, please do this:

    sudo sysrc -x ifconfig_DEFAULT
    sudo sysrc iconfig_re0="DHCP"

Home Assistant Port

Keep in mind that Home Assistant uses non-standard port 8123 for HTTP interface by default.

Home Assistant IoX integration

If you run into any issues getting the ISY/IoX integration to work, please do make sure you read the ISY Integration Instructions provided by the developer.

Shutting Down Home Assistant

To shut down Home Assistant - login to it's dashboard and navigate into Settings -> System, then use the power button in the top right corner, click advanced and select shutdown.

At any time you can shutdown the VM from eisy command line :

    sudo vm poweroff -f homeassistant

Please note that this is not a graceful shutdown, it's an equivalent to pulling the power.

Unfortunately, it does not look like Home Assistant is handling ACPI shutdown correctly (sudo vm stop homeassistant) at this moment.