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


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

# 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="" 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; sh"
iocage exec $JAIL_NAME -- "chsh -s $(which zsh) $(whoami)"

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

Github gist: