Please refer to this article for an updated version.
Finally! The updated guide to installing Home Assistant with iocage. Really this has been a bunch up build up to just a few simple commands
I recently performed some upgrades to my FreeNAS. Among other things, a new pair of SSDs for my jails. My system has been running for a few years now so I wanted to reinstall everything from scratch, ensuring a clean start with my server refresh. Due to the increasing difficulty required get homeassistant running in the now outdated “warden” jail system on FreeNAS, I wanted to switch over to the new and up coming “iocage” jail system. Read here.
The iocage system is in place now but until FreeNAS 11.2 releases - I think iocage is best used from the cli. Also some package requirements for homeassistant have changed. I wanted to take this time and update the now outdated guide I wrote before, as well as include the few extra steps needed to create a jail with iocage for homeassistant. I’m trying to keep this short. I won’t skip any needed steps but instead spend less time explaining. After creating the new jail most of the old commands remain the same. Just some package names will have changed.
FIRST TIME ONLY:
Activate iocage and downloading a release. These first steps are only required for the initial setup of iocage. Activating iocage is basically just telling it which zpool to use when installing jails. I used the same zpool I use for plugins. You can list your available zpools with zpool list
In my case it’s called jail
iocage activate jail
Finally, get a list of freebsd releases and select one to download. Running the following command for the first time will create additional datasets, setup the folder structure inside the “iocage” dataset and present a menu available freebsd releases. I select the currant version [ 6 ] 11.1-RELEASE
iocage fetch
That’s it for first time setup.
More details on iocage can be found here
New jails can be created with one command. I named this one “homeassistant” and since I assign the IP address from my router I set the jail to use dhcp
iocage create -r 11.1-RELEASE -n homeassistant dhcp=on bpf=yes vnet=on boot=on
The next step is optional and requires creating the dataset ahead of time. I’m only providing this step as an example: How to mount a dataset inside you’re jail. I’m going to mount a dataset named hass (matching the hass user for which it’s intended). It will be mounted inside the jail as hass user’s home directory.
iocage fstab -a homeassistant "/mnt/jail/ioyard/hass/ /home/hass/ nullfs rw 0 0"
That’s it for creating the jail. Time to get inside to install homeassistant.
iocage console homeassistant
While I personally prefer to have the latest packages install, I believe this optional.
mkdir -p /usr/local/etc/pkg/repos
ee /usr/local/etc/pkg/repos/FreeBSD.conf
copy and paste. Press escape then enter twice to save and exit
FreeBSD: {
url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest"
}
Install requirements for homeassistant and z-wave
pkg update
pkg upgrade
pkg install python36 py36-sqlite3 bash gmake ca_root_nss
From this point on I believe you can finish installing homeassistant following any desired pip3 install method.
My preference is to create a user/group hass
then install homeassistant inside a python virtualenv. hass
will be the user used to run homeassistant. My virtualenv lives in a directory I will create inside my jail: /srv/homeassistant
NOTE: It has been pointed out that a virtualenv inside a jail is not needed since the jail itself is already well… a jail! I still prefer this method.
The next commands create a home directory if it doesn’t already exist, as well as group/user hass
I will add hass
to the dialer
group as required for using the Aeotec Z-Wave stick. I’m setting the UID/GUID of hass
to 990. The is just a personal preference and not required.
install -d -g hass -o hass -m 775 -- /home/hass
pw addgroup -g 990 -n hass
pw adduser -u 990 -n hass -d /home/hass -s /usr/local/bin/bash -G dialer -c "Daemon user for Homeassistant"
Install pip and virtualenv then create directory to install virtualenv.
python3.6 -m ensurepip
pip3 install --upgrade pip
pip3 install --upgrade virtualenv
install -d -g hass -o hass -m 775 -- /srv/homeassistant
Switch to user hass
, activate the virtualenv and finally install homeassistant!
su hass
virtualenv -p /usr/local/bin/python3.6 /srv/homeassistant
source /srv/homeassistant/bin/activate
pip3 install --upgrade homeassistant
deactivate
You should now be able to test homeassistant! Be sure your still user hass
. If you typed exit or used ‘Ctrl+D’ to exit the virtualenv you may have been switched back to user root
su hass
/srv/homeassistant/bin/hass
While it is very simple to just add one line to rc.local and get homeassistant to start on boot up, I wanted to try starting homeassistant with a slightly more advanced startup script. Since I started using Emby as my first plugin, that’s one of the first places I looked for an example to get started. As such this script started by copying and editing the Emby script to reflect homeassistant. This is my latest version which I am now using. It differs from the example I posted in the previous guide.
If not already, open cli inside the jail
iocage console homeassistant
Create directory if it doesn’t exist and add the startup script
mkdir -p /usr/local/etc/rc.d
ee /usr/local/etc/rc.d/homeassistant
copy and paste. Press escape then enter twice to save and exit
#!/bin/sh
#
# PROVIDE: hass-server
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# homeassistant_enable: Set to YES to enable the homeassistant service.
# Default: NO
# homeassistant_user: The user account used to run the homeassistant daemon.
# This is optional, however do not specifically set this to an
# empty string as this will cause the daemon to run as root.
# Default: hass
# homeassistant_group: The group account used to run the homeassistant daemon.
# This is optional, however do not specifically set this to an
# empty string as this will cause the daemon to run with group wheel.
# Default: hass
# homeassistant_dir: Directory where config files are located.
# Default: /home/hass/.homeassistant
# sysrc homeassistant_enable=yes
# service homeassistant start
. /etc/rc.subr
name=homeassistant
rcvar=${name}_enable
pidfile_child="/var/run/${name}.pid"
pidfile="/var/run/${name}_daemon.pid"
load_rc_config ${name}
: ${homeassistant_enable:="NO"}
: ${homeassistant_user:="hass"}
: ${homeassistant_group:="hass"}
: ${homeassistant_config_dir:="/home/hass/homeassistant"}
command="/usr/sbin/daemon"
start_precmd=${name}_precmd
homeassistant_precmd()
{
rc_flags="-f -P ${pidfile} -p ${pidfile_child} /srv/homeassistant/bin/hass --config ${homeassistant_config_dir} ${rc_flags}"
if [ ! -e "${pidfile_child}" ]; then
install -g ${homeassistant_group} -o ${homeassistant_user} -- /dev/null "${pidfile_child}";
fi
if [ ! -e "${pidfile}" ]; then
install -g ${homeassistant_group} -o ${homeassistant_user} -- /dev/null "${pidfile}";
fi
if [ ! -d "${homeassistant_config_dir}" ]; then
install -d -g ${homeassistant_group} -o ${homeassistant_user} -- "${homeassistant_config_dir}";
fi
}
stop_postcmd=${name}_postcmd
homeassistant_postcmd()
{
rm -f -- "${pidfile}"
rm -f -- "${pidfile_child}"
}
run_rc_command "$1"
Make this file executable and start homeassistant
chmod +x /usr/local/etc/rc.d/homeassistant
service homeassistant start
Once satisfied everything is working you can enable homeassistant to start automatically
sysrc homeassistant_enable=yes
A few ways to control homeassistant from the cli INSIDE your jail
service homeassistant [ start | stop | status | restart ]
A few ways to control your homeassistant JAIL from the FreeNAS cli (NOT inside your jail)
iocage [ start | stop ] homeassistant
If you need to restart the jail iocage docs recommend using the -s switch. This “soft-restart” will help prevent issues that could arise from a “hard-restart” if vnet=on
iocage restart -s homeassistant
You can list jails you have installed and see if they are running. To see the ip address requires root
iocage list -l
+-----+---------------+------+-------+------+-----------------+---------------------+-----+----------+
| JID | NAME | BOOT | STATE | TYPE | RELEASE | IP4 | IP6 | TEMPLATE |
+=====+===============+======+=======+======+=================+=====================+=====+==========+
| 1 | homeassistant | on | up | jail | 11.1-RELEASE-p8 | epair0b|192.0.1.145 | - | - |
+-----+---------------+------+-------+------+-----------------+---------------------+-----+----------+
Upgrading:
First get in the jail, stop homeassistant, then become user hass
iocage console homeassistant
service homeassistant stop
su hass
Next activate the VirtualEnv
source /srv/homeassistant/bin/activate
Finally we can update
pip3 install --upgrade homeassistant
And exit the VirtualEnv and restart Homeassistant
exit
service homeassistant start