FreeNAS Jail Creation Script

Lately I've been in a mindset of "automate everything" and I am taking that to heart. There are many things that I don't enjoy having to do, like clicking through FreeNAS menus to create jails and installing plugins. Not to mention that even after I create a jail, I also install a certain set of packages that I'm bound to use. During the past weekend, I decided to just go ahead and write a create that does all of that for me!

I present to you: Alex's Easy Jail Creation script

#!/bin/bash

# Take in and validate jail name from user
JAIL_NAME=$1
SLEEP_TIME=3
if [[ -z ${JAIL_NAME} ]]; then
  >&2 echo "Error: jail name is unset"
  exit 1
else
  echo "jail name will be '$JAIL_NAME'"
fi

# Packages to install by default on the new jail
# We're installing vim and neovim (for options), as well as java and mono just in case we want to do any C# stuff
echo '{"pkgs":["mediainfo","sqlite3","ca_root_nss","curl","wget","git","vim","zsh","neovim","nano","java/openjdk8","mono"]}' >/tmp/pkg.json

# Create new jail with default params
iocage create --name=$JAIL_NAME --release=12.2-RELEASE --pkglist=/tmp/pkg.json defaultrouter="192.168.1.1" dhcp="on" vnet="on" bpf="on" allow_raw_sockets="1" allow_tun="on" boot="on"

# Commands to run on newly created jail
# Install Oh My Zsh and set it as the default shell experience
echo "Installing Oh My ZSH"
iocage exec $JAIL_NAME -- "pkg update --force --quiet"
iocage exec $JAIL_NAME -- "wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh; sh install.sh"
iocage exec $JAIL_NAME -- "chsh -s $(which zsh) $(whoami)"

# Exit successfully
echo "Exiting"
iocage exec $JAIL_NAME -- "exit 0"
exit 0

Github gist: https://gist.github.com/dragid10/814421b95e2d86a1bd5b68a322b09cdf