Skip to content

Instantly share code, notes, and snippets.

@jasontbradshaw
Last active March 24, 2016 02:18
Show Gist options
  • Save jasontbradshaw/7a08f03f0999f91208be to your computer and use it in GitHub Desktop.
Save jasontbradshaw/7a08f03f0999f91208be to your computer and use it in GitHub Desktop.
This script starts FreeNAS 9.3 jails that are configured to auto-start, but due to a bug (https://bugs.pcbsd.org/issues/7155) don't do so after pool unlock. This should be run as a CRON job as root every minute, and will only attempt to start the jails a single time.
#!/usr/bin/env bash
# Strict mode (see: http://redsymbol.net/articles/unofficial-bash-strict-mode/).
set -euo pipefail
IFS=$'\n\t'
# Exit if not running as root.
if [ "$(id -u)" != "0" ]; then
>&2 echo "This script must be run as root."
exit 1
fi
# Get a list of all jails that have auto-start enabled and aren't currently
# running.
jails="$(warden list | tail -n +3 | grep -E ' Enabled +Stopped ' | cut -d ' ' -f 1)"
# If we have no jails to start, exit cleanly _before_ creating the lock file,
# otherwise we might not ever do any work!
if [ -z "${jails}" ]; then
echo "No jails need starting."
exit 0
fi
# Grab a "permanent" lock to ensure this script may only run once per reset of
# the `tmp` directory. The `tmp` directory gets cleared on reboot, so we'll only
# auto-start the jails a single time after the pool is unlocked.
lockfile='/tmp/jails-autostart.lock'
if ! mkdir "${lockfile}" &> '/dev/null'; then
>&2 echo "This script may only be run once. To re-run it, remove '${lockfile}' and try again."
exit 1
fi
# Start all the jails.
for jail in "${jails[@]}"; do
warden start "${jail}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment