Monday, 7 September 2020

Installing Home Assistant on bhyve

Introduction

This week I migrated my Home Assistant from a Windows 10 Hyper-V VM to FreeBSD bhyve VM. It was all pretty straightforward, but I thought it could be useful to document the procedure. While I did a migration, it's mostly the same procedure for a new installation in a bhyve VM.

I have been running Home Assistant for a while, on a number of different platforms:
  • On a Raspberry Pi Zero W which worked OK as proof of concept, but it has too little RAM so it's very slow.
  • On a Raspberry Pi 3 B which worked OK, though slow for some things, until the SD card died without warning.
  • In a Windows 10 Hyper-V VM which works well, but the particular PC isn't really intended to run VMs.
While Home Assistant was running under Hyper-V I moved data storage to a separate Postgresql database server which means another VM to manage, but also has the positive effect that the Home Assistant backups / snapshots are much smaller and migrations are simpler.

I tried to get the Home Assistant VM image to work with the bhyve grub boot loader to have a serial console, but I couldn't get that to work so I ended up going with UEFI boot instead. It works fine, but means you have to use VNC for console access. Since you rarely need console access for Home Assistant this is not a big issue.

Assumptions

  • You already have a working bhyve setup with vm-bhyve.
  • You have the bhyve-firmware and qemu-utils packages installed.
    • sudo pkg install bhyve-firmware qemu-utils
  • You have a running DHCP server on your network.
  • The root directory of your vm-bhyve is at ${VMDIR}.

Installation

Create a Home Assistant vm-bhyve template, by adding the following in ${VMDIR}/.templates/homeassistant.conf and adjust as needed:

loader="uefi"
cpu=1
memory=1G
graphics="yes"
# graphics_listen="127.0.0.1"
vnc_password="MY_VERY_SECRET_PASSWORD"

disk0_name="disk0"
# If you don't use ZFS, comment out the next line.
disk0_dev="sparse-zvol"
disk0_type="virtio-blk"

network0_type="virtio-net"
network0_switch="public"

Change the vnc_password to something random. Depending on your local network you may want to enable graphics_listen="127.0.0.1" if you don't want to have VNC on an IP directly.

Download the Home Assistant latest image. You may want to check the Home Assistant documentation if a newer image has come out since this blog post was written.

sudo vm img https://github.com/home-assistant/operating-system/releases/download/4.12/hassos_ova-4.12.vmdk.gz

Create the VM called hass0. By default you get a 20 GiB disk image. This is plenty if you have a small install or an external database server. If either of those are not true, consider a large disk image (By adding e.g. adding -s 40G to the command line).

sudo vm create -t homeassistant -i hassos_ova-4.12.vmdk hass0

Start the VM.

sudo vm start hass0

After Home Assistant has started (expect this to take at least some minutes), you may also be able to access it using mDNS at http://homeassistant.local:8123/. If not, find the IP address of the home assistant VM on your DHCP server. I haven't found an easy way to get the IP addresses from the VM itself. Then access the Home Assistant web interface on port 8123: https://192.168.0.42:8123/.

If you want to fix the IP address of home assistant just set IP on DHCP server and reboot the VM.

sudo vm restart hass0

 If you need to find the MAC address of the VM's network interface, you can run:

grep network0_mac ${VMDIR}/hass0/hass0.conf 

Configuration

Now you can follow the normal Home Assistant documentation at https://www.home-assistant.io/getting-started/onboarding/.

For my case of moving an existing installation, I simply did a basic Home Assistant setup, installed and configured the Samba addon, copied a recent backup / snapshot from my old installation, shut down the old VM, on the Home Assistant Supervisor Snapshot page (/hassio/snapshots), found the snapshot then selected "Wipe & Restore". Since I'm using an external database server I didn't have to worry about the snapshot being fully up to date. Do note for migrations, that if you are using the default built in SQLite there is a risk the database is not consistent when the snapshot was created.

You should now hopefully have a working Home Assistant installation.

Console

If you need to access the VM console first you can find the VNC port by running:

sudo vm list

Then connect with a VNC client.

Tip: For the Mac VNC Viewer I ran into that at times on connect it failed with a "RFB Protocol Error: Invalid message type 170". This can be worked around by changing Picture Quality from Automatic to High.