Install Home Assistant OS
Begin in the TrueNAS UI by adding a new ZVOL at least 32 GiB in size. Take note of the ZVOL path.
Then using the TrueNAS shell, run these commands to download, extract, and write the HAOS image for KVM to the ZVOL you just created. Replace tank/vm/haos
with your ZVOL path.
wget https://github.com/home-assistant/operating-system/releases/download/9.5/haos_ova-9.5.qcow2.xz
unxz haos_ova-9.5.qcow2.xz
qemu-img convert -O raw haos_ova-9.5.qcow2 /dev/zvol/tank/vm/haos
Create the Virtual Machine
In the TrueNAS UI, select Virtualization from the left-hand menu, then click the add button to begin the Virtual Machine Creation Wizard.
Step 1 - Set the guest operating system to Linux, give your VM a name, and ensure UEFI is the selected boot method. For now, uncheck the start on boot option and set the display type to SPICE.
Step 2 - Assign your vCPUs and a minimum of 2 GiB memory. Set the CPU mode to host passthrough.
Step 3 - Click use existing disk image and set the disk type to VirtIO. Select your existing HAOS ZVOL.
Step 4 - Choose a NIC for your VM. Set the adapter type to VirtIO. If you are not using a bridge, enable Trust Guest Filters to allow multicast.
Step 5 - Nothing is needed here.
Step 6 - Defaults should be fine.
Step 7 - Confirm options and save.
Add the QEMU guest agent
TrueNAS does not currently have any method to add a channel for the guest agent, but we can still do it by using a script to start the VM. For this to function correctly, the VM should have its autostart disabled; this is key. To start the VM when TrueNAS boots, we can configure the script to run as Post Init. While not the most convenient approach, it is likely safer than patching the middleware code.
With help from this post by @boti, a script on Reddit, and later this Red Hat documentation, here is a startup script I wrote. It works fine to start the VM from the TrueNAS shell, but it took a few tries to get working during Post Init. Increasing the script timeout (in the Post Init settings) was the solution - I’m using 60 seconds.
- Save the startup script to your NAS and make it executable
- Edit line 4 of the script, set the name of your HAOS VM
- Using the TrueNAS shell, run the script to start your VM
start-haos.sh
#!/bin/bash
## Set the name of your VM
vm='HomeAssistant'
if [ ${EUID} -ne 0 ]; then
echo "Please run this script as root or using sudo."
exit 1
fi
set -euo pipefail
# create a temporary XML file for the QEMU guest agent
guest_agent=$(mktemp -t agent.xml.XXXXX)
cat << END_XML > "${guest_agent}"
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
END_XML
# get id and use it to start the VM
id=$(/usr/bin/midclt call vm.query | /usr/bin/jq -r '.[] | ( .id|tostring ) + ":" + .name' | /usr/bin/grep "${vm}" | /usr/bin/cut -f1 -d':')
/usr/bin/midclt call vm.start "${id}" &> /dev/null
sleep 1
# get the VM name and use it to attach the QEMU guest agent
name=$(/usr/bin/virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" list --name | /usr/bin/grep "${vm}")
/usr/bin/virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" attach-device "${name}" "${guest_agent}"
# remove the temporary XML file
rm "${guest_agent}"
If you encounter a similar error, try using a SPICE display instead of VNC.
error: Failed to attach device from /some/path/to/file
error: internal error: no virtio-serial controllers are available
Show more about this error
Digging around in the middleware code, I came across this and this
if device.is_spice_type():
spice_server_available = True
if spice_server_available:
# We always add spicevmc channel device when a spice display device is available to allow users
# to install guest agents for improved vm experience
It looks like they are only creating the virtio-serial controller when the VM uses a SPICE display
For more information about creating a VM, see the TrueNAS Scale documentation found HERE
For more information about installing Home Assistant OS in a VM, see the Home Assistant documentation found HERE