Saturday, 23 December 2017

Home Assistant + Node-RED with BTRFS on Arch

After using Home Assistant for a few months and having set it up to use external MySQL databases, etc. I have decided to re-install in a robust manner. I found that the ext4 that I was using kept going corrupt after power outages. I stupidly turned off the journal to "save" the sdcard but this was the last straw and many of my config files went missing... Time to start afresh!

This post pretty much contains a list of commands. I won't go into detail on each of them but they should be fairly self explanatory. I probably missed a bunch of stuff too but this is the gist of it!

First off, I am using Arch because I like the amount of control it gives me... I am also going to go with btrfs instead of ext4 this time around. My hardware is a Cubieboard2 with an external SATA 1TB drive which I use to store CCTV footage, the MySQL database, etc. I want to minimise writes to the sdcard and so that is the thing to keep in mind.

Following the Arch installation tutorial here: https://archlinuxarm.org/platforms/armv7/allwinner/cubieboard-2

Using an Ubuntu 16.04 VM to get the sdcard prepped, install btrfs-tools first.

Partition and create btrfs instead of ext4 and create a boot partition too. Make a 100MB boot partition and use the rest for root. Follow the guide for the start location.

sudo mkfs.ext2 /dev/sdc1
sudo mkfs.btrfs /dev/sdc2

I would like to use the btrfs compression for a performance boost (allegedly), space savings and perhaps a boost in reliability because of less space usage...

cd /tmp

mkdir boot
mkdir root

sudo mount -o compress=lzo /dev/sdc2 root
sudo mount /dev/sdc1 boot

Download the arch tar.gz file and decompress as per their guide. I got a few warnings about flags due to the btrfs so I used tar instead of bsdtar. I'll ignore them for now and hope for the best! It may take a while to do the extraction - depending on the speed of your SD Card.

sudo tar -xzvf ArchDownloadFile.tar.gz -C root

Create a file on the boot partition called boot.cmd and put this inside it:

if test -n ${distro_bootpart}; then setenv bootpart ${distro_bootpart}; else setenv bootpart 1; fi
part uuid ${devtype} ${devnum}:${bootpart} uuid

setenv bootargs console=${console} root=/dev/mmcblk0p2 rootfstype=btrfs rootflags=autodefrag,compress=lzo rw rootwait

if load ${devtype} ${devnum}:${bootpart} ${kernel_addr_r} /zImage; then
  if load ${devtype} ${devnum}:${bootpart} ${fdt_addr_r} /dtbs/${fdtfile}; then
    if load ${devtype} ${devnum}:${bootpart} ${ramdisk_addr_r} /initramfs-linux.img; then
      bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr_r};
    else
      bootz ${kernel_addr_r} - ${fdt_addr_r};
    fi;
  fi;
fi

This is essentially what is in the default boot.scr, with mods for btrfs and the boot partition. We need to compile this file. Get the u-boot-tools and the run with appropriate modifications to your paths if you aren't following mine:

mkimage -C none -A arm -T script -d boot/boot.cmd boot/boot.scr

Unmount everything and pop the sdcard into the Cubieboard. It should boot up to a login prompt. If not, start from scratch and try again :) the serial console helps a lot! It's probably worthwhile to edit the boot.scr to output to HDMI rather that serial.

When you are up and running, before installing uboot or other update stuff (as per the Arch tutorials), update your fstab to include a line for the boot partition:

/dev/mmcblk0p2 / btrfs defaults,relatime 0 0
/dev/mmcblk0p1 /boot ext2 defaults,noatime 0 0

tmpfs   /tmp tmpfs  rw,nodev,nosuid,size=512M  0  0

It's also important to install btrfs-tools and a couple of other things with pacman:

pacman -Syu btrfs-prog ntp wget nano

General Stuff

Do the usual things at: https://wiki.archlinux.org/index.php/installation_guide

Also, install yaourt for AUR stuff: https://www.ostechnix.com/install-yaourt-arch-linux/


su (the password is root by default)
useradd ...
pacman -S sudo
visudo

Follow the instructions and then check if your new user has sudo by typing su newuser and trying out a sudo command. If all goes well, ssh in via the new user and sudo userdel alarm.

Make sure your boot partition is mounted properly and then:

sudo pacman -Syu

Home Assistant and Node-Red Installation

yaourt -S home-assistant --noconfirm
yaourt -S nodejs-node-red --noconfirm

The default home assistant AUR install puts the config is /var/lib/hass and runs home assistant with user hass. Let's keep it this way but I want read write access to the config as my normal user.

sudo usermod -a -G hass username
sudo systemctl start home-assistant
sudo systemctl enable home-assistant

The first run of home-assistant (hass going forward) takes a while as it downloads and sets up all of the components.

Node-Red doesn't seem to come with a predefined service, user, etc. so let's create one...

sudo useradd -m -s /sbin/nologin nodered
sudo nano /lib/systemd/system/nodered.service

Paste this inside:

# systemd service file to start Node-RED
[Unit]
Description=Node-RED graphical event wiring tool.
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html
[Service]
Type=simple
# Run as root user in order to have access to gpio pins
User=nodered
Group=nodered
Nice=5
Environment="NODE_OPTIONS=--max-old-space-size=128"
#Environment="NODE_RED_OPTIONS=-v"
ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
KillSignal=SIGINT
Restart=on-failure
SyslogIdentifier=Node-RED
[Install]
WantedBy=multi-user.target

sudo systemctl start nodered
sudo systemctl enable nodered








The Node-Red config is in /home/nodered/.node-red

I then modified the permissions on the nodered home directory so that I could edit as my normal user:

sudo usermod -a -G nodered username
sudo chmod 770 /home/nodered

There's lots more to do such as installing pi-hole, samba, etc!


No comments:

Post a Comment

Home Assistant + Node-RED with BTRFS on Arch

After using Home Assistant for a few months and having set it up to use external MySQL databases, etc. I have decided to re-install in a rob...