Skip to content

Instantly share code, notes, and snippets.

@seb26
Last active August 8, 2024 08:20
Show Gist options
  • Save seb26/962315ef04ef842b0217f1318174139d to your computer and use it in GitHub Desktop.
Save seb26/962315ef04ef842b0217f1318174139d to your computer and use it in GitHub Desktop.
Please scroll down to read the README.md.
#!/usr/bin/env bash
# (c) Sebastian Reategui 2024 <http://github.com/seb26>
# MIT License
# USER OPTIONS
SANOID_HOME_CONFIG_PATH= # ~/.config/sanoid/sanoid.conf
SANOID_REPO_URL=https://github.com/jimsalterjrs/sanoid.git
# QUIT ON ERROR
set -e
set -o pipefail
set -u
# PATHS
TRUENAS_VERSION=$(cat /etc/version | cut -d' ' -f1)
TRUENAS_USR_DATASET=boot-pool/ROOT/$TRUENAS_VERSION/usr
# INSTALL
# Mount OS filesystem /usr as readwrite
mount -o remount,rw $TRUENAS_USR_DATASET
# Modify path for this period of time using apt
export PATH=/usr/bin:/usr/sbin
# Make apt executable
chmod +x /bin/apt*
chmod +x /usr/bin/dpkg
# Install dependencies for Sanoid
# Note: Exclude pv and mbuffer - these are already available, reduce any hypothetical interference with trueos
apt update
apt install libcapture-tiny-perl libconfig-inifiles-perl lzop
# Get Sanoid - Clone, switch to latest stable, copy executables, default.conf and a sample conf
TMP_SANOID_INSTALL_PATH=/tmp/install-sanoid-$(date +%s)
mkdir -p $TMP_SANOID_INSTALL_PATH && cd $TMP_SANOID_INSTALL_PATH
git clone $SANOID_REPO_URL
cd sanoid
git checkout $(git tag | grep "^v" | tail -n 1)
cp sanoid syncoid findoid sleepymutex /usr/local/sbin
mkdir -p /etc/sanoid
cp sanoid.defaults.conf /etc/sanoid/sanoid.defaults.conf
cp sanoid.conf /etc/sanoid/sanoid.example.conf
# CLEANUP
# Remove executable flag from apt and dpkg
chmod -x /bin/apt*
chmod -x /usr/bin/dpkg
# Read-only the /usr filesystem again
mount -o remount,ro $TRUENAS_USR_DATASET
# Remove temp download of sanoid git
rm -r $TMP_SANOID_INSTALL_PATH
# CONFIGURE
# Symlink home config to /etc/sanoid/sanoid.conf
# - Allows config to be kept within user data storage and not be lost
# - However only the conf is linked, anything else is kept at /etc for real
if [ -z "$SANOID_HOME_CONFIG_PATH" ]; then
ln -s $SANOID_HOME_CONFIG_PATH /etc/sanoid/sanoid.conf
fi
# Create service for Sanoid Take Snapshots
cat << "EOF" | sudo tee /etc/systemd/system/sanoid.service
[Unit]
Description=Snapshot ZFS Pool
Requires=zfs.target
After=zfs.target
Wants=sanoid-prune.service
Before=sanoid-prune.service
ConditionFileNotEmpty=/etc/sanoid/sanoid.conf
[Service]
Environment=TZ=UTC
Type=oneshot
ExecStart=/usr/local/sbin/sanoid --take-snapshots --verbose
EOF
# Create service for Sanoid Prune Snapshots
cat << "EOF" | sudo tee /etc/systemd/system/sanoid-prune.service
[Unit]
Description=Cleanup ZFS Pool
Requires=zfs.target
After=zfs.target sanoid.service
ConditionFileNotEmpty=/etc/sanoid/sanoid.conf
[Service]
Environment=TZ=UTC
Type=oneshot
ExecStart=/usr/local/sbin/sanoid --prune-snapshots --verbose
[Install]
WantedBy=sanoid.service
EOF
# Create service for Sanoid Timer
cat << "EOF" | sudo tee /etc/systemd/system/sanoid.timer
[Unit]
Description=Run Sanoid Every 15 Minutes
Requires=sanoid.service
[Timer]
OnCalendar=*:0/15
Persistent=true
[Install]
WantedBy=timers.target
EOF
# Inform the daemon
systemctl daemon-reload
# Enable Sanoid services
systemctl enable sanoid-prune.service
systemctl enable --now sanoid.timer

truenas-install-sanoid.sh

To install the ZFS utility Sanoid on TrueNAS systems, which do not permit user-installed packages.

Tested on:

  • TrueNAS Scale 24.04.2

Operations

This script:

  • Mounts /usr dataset, makes apt executable
  • apt update for new repo lists, but no upgrade
  • Install Sanoid dependencies (only those not found already in the TrueNAS distro)
  • Normal Sanoid installation steps recommended for Linux distros, including Sanoid, Syncoid and Findoid
    • Downloads Sanoid from Github
    • Copies the Sanoid executables to /usr/local/sbin
    • Creates a systemd service for sanoid, sanoid pruning and a timer for it to run every 15 minutes
  • (Optional) Symlinks sanoid.conf to a location in a user's home directory, to preserve it from being lost
  • Cleans up after itself: returns apt to unexecutable, /usr to read-only and deletes downloaded repo

Please read the source to confirm its operation before using.

Usage

  1. Take a configuration backup of your TrueNAS system first.
  2. Download the script (in this gist):
wget https://gist.github.com/seb26/962315ef04ef842b0217f1318174139d/raw/0a3cc86572aa8e3e089e549d4693c2737821b6cd/truenas-install-sanoid.sh
nano truenas-install-sanoid.sh
  1. Optionally (but recommended) edit the script and fill in the option SANOID_HOME_CONFIG_PATH.
# Insert an absolute path (no ~ tilde)
SANOID_HOME_CONFIG_PATH=/home/username/.config/sanoid/sanoid.conf

When specifying this, /etc/sanoid/sanoid.conf will be symlinked to the home path you specify and Sanoid will read it from there. Therefore, you can maintain it/back it up in your home directory, in the event of a system upgrade.

By default, SANOID_HOME_CONFIG_PATH is empty and no symlink will be created. A Sanoid config will be expected at /etc/sanoid/sanoid.conf.

  1. Make the script executable, then execute it as sudo:
chmod +x truenas-install-sanoid.sh
sudo ./truenas-install-sanoid.sh
  1. Watch for any error output.
  2. Confirm sanoid services are operating fine:
systemctl status sanoid.service
systemctl status sanoid-prune.service
systemctl status sanoid.timer

System upgrades

When you update TrueNAS, it is most likely (but I'm not sure) that the Sanoid binaries and configuration will be lost.

You would need to run the script again to reinstall Sanoid.

Ideally, you have created a backup of your /etc/sanoid/sanoid.conf. Or you have specified a path at SANOID_HOME_CONFIG_PATH and maintain this config file in your home directory.

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment