127 lines
3.1 KiB
Bash
127 lines
3.1 KiB
Bash
#!/bin/sh
|
|
# Build an iocage jail under FreeNAS 11.3-12.0 using the current release of Caddy
|
|
# git clone https://github.com/basilhendroff/freenas-iocage-caddyv2
|
|
|
|
# Check for root privileges
|
|
if ! [ $(id -u) = 0 ]; then
|
|
echo "This script must be run with root privileges"
|
|
exit 1
|
|
fi
|
|
|
|
#####
|
|
#
|
|
# General configuration
|
|
#
|
|
#####
|
|
|
|
# Initialize defaults
|
|
JAIL_IP=""
|
|
JAIL_INTERFACES=""
|
|
DEFAULT_GW_IP=""
|
|
INTERFACE="vnet0"
|
|
VNET="on"
|
|
POOL_PATH=""
|
|
JAIL_NAME="caddy"
|
|
CONFIG_NAME="caddy-config"
|
|
|
|
# Check for caddy-config and set configuration
|
|
SCRIPT=$(readlink -f "$0")
|
|
SCRIPTPATH=$(dirname "${SCRIPT}")
|
|
if ! [ -e "${SCRIPTPATH}/${CONFIG_NAME}" ]; then
|
|
echo "${SCRIPTPATH}/${CONFIG_NAME} must exist."
|
|
exit 1
|
|
fi
|
|
. "${SCRIPTPATH}/${CONFIG_NAME}"
|
|
INCLUDES_PATH="${SCRIPTPATH}"/includes
|
|
|
|
JAILS_MOUNT=$(zfs get -H -o value mountpoint $(iocage get -p)/iocage)
|
|
RELEASE=$(freebsd-version | sed "s/STABLE/RELEASE/g" | sed "s/-p[0-9]*//")
|
|
|
|
# Check that necessary variables were set by nextcloud-config
|
|
if [ -z "${JAIL_IP}" ]; then
|
|
echo 'Configuration error: JAIL_IP must be set'
|
|
exit 1
|
|
fi
|
|
if [ -z "${JAIL_INTERFACES}" ]; then
|
|
echo 'JAIL_INTERFACES not set, defaulting to: vnet0:bridge0'
|
|
JAIL_INTERFACES="vnet0:bridge0"
|
|
fi
|
|
if [ -z "${DEFAULT_GW_IP}" ]; then
|
|
echo 'Configuration error: DEFAULT_GW_IP must be set'
|
|
exit 1
|
|
fi
|
|
if [ -z "${POOL_PATH}" ]; then
|
|
echo 'Configuration error: POOL_PATH must be set'
|
|
exit 1
|
|
fi
|
|
if [ -z "${TIME_ZONE}" ]; then
|
|
echo 'Configuration error: TIME_ZONE must be set'
|
|
exit 1
|
|
fi
|
|
if [ -z "${HOST_NAME}" ]; then
|
|
echo 'Configuration error: HOST_NAME must be set'
|
|
exit 1
|
|
fi
|
|
|
|
if [ $DNS_CERT -eq 1 ] && [ -z "${DNS_PLUGIN}" ] ; then
|
|
echo "DNS_PLUGIN must be set to a supported DNS provider."
|
|
echo "See https://caddyserver.com/docs under the heading of \"DNS Providers\" for list."
|
|
echo "Be sure to omit the prefix of \"tls.dns.\"."
|
|
exit 1
|
|
fi
|
|
|
|
# If CONFIG_PATH wasnn't set in nextcloud-config, set it
|
|
if [ -z "${CONFIG_PATH}" ]; then
|
|
CONFIG_PATH="${POOL_PATH}"/caddy/config
|
|
fi
|
|
|
|
# Extract IP and netmask, sanity check netmask
|
|
IP=$(echo ${JAIL_IP} | cut -f1 -d/)
|
|
NETMASK=$(echo ${JAIL_IP} | cut -f2 -d/)
|
|
if [ "${NETMASK}" = "${IP}" ]
|
|
then
|
|
NETMASK="24"
|
|
fi
|
|
if [ "${NETMASK}" -lt 8 ] || [ "${NETMASK}" -gt 30 ]
|
|
then
|
|
NETMASK="24"
|
|
fi
|
|
|
|
#####
|
|
#
|
|
# Jail Creation
|
|
#
|
|
#####
|
|
|
|
# List packages to be auto-installed after jail creation
|
|
cat <<__EOF__ >/tmp/pkg.json
|
|
{
|
|
"pkgs":[
|
|
"nano"
|
|
]
|
|
}
|
|
__EOF__
|
|
|
|
# Create the jail and install previously listed packages
|
|
if ! iocage create --name "${JAIL_NAME}" -p /tmp/pkg.json -r "${RELEASE}" interfaces="${JAIL_INTERFACES}" ip4_addr="${INTERFACE}|${IP}/${NETMASK}" defaultrouter="${DEFAULT_GW_IP}" boot="on" host_hostname="${JAIL_NAME}" vnet="${VNET}"
|
|
then
|
|
echo "Failed to create jail"
|
|
exit 1
|
|
fi
|
|
rm /tmp/pkg.json
|
|
|
|
#####
|
|
#
|
|
# Directory Creation and Mounting
|
|
#
|
|
#####
|
|
|
|
mkdir -p "${CONFIG_PATH}"
|
|
|
|
iocage exec "${JAIL_NAME}" mkdir -p /mnt/includes
|
|
iocage exec "${JAIL_NAME}" mkdir -p /usr/local/www
|
|
#mkdir -p "${JAILS_MOUNT}"/jails/${JAIL_NAME}/root/mnt/includes
|
|
|
|
iocage fstab -a "${JAIL_NAME}" "${CONFIG_PATH}" /usr/local/www nullfs rw 0 0
|
|
iocage fstab -a "${JAIL_NAME}" "${INCLUDES_PATH}" /mnt/includes nullfs rw 0 0
|