Proxmox/ha_container.sh

177 lines
5.3 KiB
Bash
Raw Normal View History

2021-08-12 18:24:08 +02:00
#!/usr/bin/env bash
2021-09-29 00:09:11 +02:00
while true; do
2021-11-28 07:32:12 +01:00
read -p "This will create a New Home Assistant Container LXC. Proceed(y/n)?" yn
2021-09-29 00:09:11 +02:00
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
2022-01-16 08:17:21 +01:00
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
2021-08-12 18:24:08 +02:00
shopt -s expand_aliases
alias die='EXIT=$? LINE=$LINENO error_exit'
2022-01-16 08:17:21 +01:00
CHECKMARK='\033[0;32m\xE2\x9C\x94\033[0m'
2021-08-12 18:24:08 +02:00
trap die ERR
trap cleanup EXIT
function error_exit() {
trap - ERR
local DEFAULT='Unknown failure occured.'
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
msg "$FLAG $REASON"
[ ! -z ${CTID-} ] && cleanup_ctid
exit $EXIT
}
function warn() {
local REASON="\e[97m$1\e[39m"
local FLAG="\e[93m[WARNING]\e[39m"
msg "$FLAG $REASON"
}
function info() {
local REASON="$1"
local FLAG="\e[36m[INFO]\e[39m"
msg "$FLAG $REASON"
}
function msg() {
local TEXT="$1"
echo -e "$TEXT"
}
function cleanup_ctid() {
if [ ! -z ${MOUNT+x} ]; then
pct unmount $CTID
fi
if $(pct status $CTID &>/dev/null); then
if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
pct stop $CTID
fi
pct destroy $CTID
elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
pvesm free $ROOTFS
fi
}
function cleanup() {
popd >/dev/null
rm -rf $TEMP_DIR
}
function load_module() {
if ! $(lsmod | grep -Fq $1); then
modprobe $1 &>/dev/null || \
die "Failed to load '$1' module."
fi
MODULES_PATH=/etc/modules
if ! $(grep -Fxq "$1" $MODULES_PATH); then
echo "$1" >> $MODULES_PATH || \
die "Failed to add '$1' module to load at boot."
fi
}
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR >/dev/null
2022-01-17 20:03:18 +01:00
wget -qL https://raw.githubusercontent.com/tteck/Proxmox/main/ha_setup.sh
2021-08-12 18:24:08 +02:00
load_module overlay
while read -r line; do
TAG=$(echo $line | awk '{print $1}')
TYPE=$(echo $line | awk '{printf "%-10s", $2}')
FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
ITEM=" Type: $TYPE Free: $FREE "
OFFSET=2
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
fi
STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
done < <(pvesm status -content rootdir | awk 'NR>1')
if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
warn "'Container' needs to be selected for at least one storage location."
die "Unable to detect valid storage location."
elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
STORAGE=${STORAGE_MENU[0]}
else
while [ -z "${STORAGE:+x}" ]; do
STORAGE=$(whiptail --title "Storage Pools" --radiolist \
"Which storage pool you would like to use for the container?\n\n" \
16 $(($MSG_MAX_LENGTH + 23)) 6 \
"${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
done
fi
info "Using '$STORAGE' for storage location."
CTID=$(pvesh get /cluster/nextid)
info "Container ID is $CTID."
2022-01-16 08:17:21 +01:00
echo -e "${CHECKMARK} \e[1;92m Updating LXC Template List... \e[0m"
2021-08-12 18:24:08 +02:00
pveam update >/dev/null
2022-01-16 08:17:21 +01:00
echo -e "${CHECKMARK} \e[1;92m Downloading LXC Template... \e[0m"
2021-08-12 18:24:08 +02:00
OSTYPE=debian
2021-08-19 17:24:19 +02:00
OSVERSION=${OSTYPE}-11
2021-08-12 18:24:08 +02:00
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
TEMPLATE="${TEMPLATES[-1]}"
pveam download local $TEMPLATE >/dev/null ||
die "A problem occured while downloading the LXC template."
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
case $STORAGE_TYPE in
dir|nfs)
DISK_EXT=".raw"
DISK_REF="$CTID/"
;;
zfspool)
DISK_PREFIX="subvol"
DISK_FORMAT="subvol"
;;
esac
DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
2022-01-16 08:17:21 +01:00
echo -e "${CHECKMARK} \e[1;92m Creating LXC Container... \e[0m"
2021-12-02 12:19:20 +01:00
DISK_SIZE=8G
2021-08-12 18:24:08 +02:00
pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
if [ "$STORAGE_TYPE" == "zfspool" ]; then
2022-01-17 19:15:36 +01:00
wget -qL -O fuse-overlayfs https://github.com/containers/fuse-overlayfs/releases/download/v1.8/fuse-overlayfs-x86_64
2021-08-12 18:24:08 +02:00
warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
else
mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
fi
2022-01-17 19:15:36 +01:00
ARCH=$(dpkg --print-architecture)
2021-08-12 18:24:08 +02:00
HOSTNAME=homeassistant
TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
2022-01-17 19:15:36 +01:00
if [ "$STORAGE_TYPE" == "zfspool" ]; then
2022-01-18 04:24:53 +01:00
CT_FEATURES="fuse=1,keyctl=1,mknod=1,nesting=1"
2022-01-17 19:15:36 +01:00
else
2022-01-18 04:24:53 +01:00
CT_FEATURES="nesting=1"
2022-01-17 19:15:36 +01:00
fi
2022-01-18 04:24:53 +01:00
pct create $CTID $TEMPLATE_STRING -arch $ARCH -features $CT_FEATURES \
-hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 2 -memory 2048 \
-ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
2021-08-12 18:24:08 +02:00
LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
cat <<EOF >> $LXC_CONFIG
lxc.cgroup2.devices.allow: a
lxc.cap.drop:
EOF
MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
pct unmount $CTID && unset MOUNT
2022-01-16 08:17:21 +01:00
echo -e "${CHECKMARK} \e[1;92m Starting LXC Container... \e[0m"
2021-08-12 18:24:08 +02:00
pct start $CTID
2022-01-17 19:15:36 +01:00
if [ "$STORAGE_TYPE" == "zfspool" ]; then
pct push $CTID fuse-overlayfs /usr/local/bin/fuse-overlayfs -perms 755
info "Using fuse-overlayfs."
fi
pct push $CTID ha_setup.sh /ha_setup.sh -perms 755
pct exec $CTID /ha_setup.sh
2021-08-12 18:24:08 +02:00
IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
2022-01-16 08:17:21 +01:00
info "Successfully Created Home Assistant Container LXC to $CTID."
2021-08-12 18:24:08 +02:00
msg "
2022-01-16 08:17:21 +01:00
Home Assistant should be reachable by going to the following URL.
2021-08-12 18:24:08 +02:00
http://${IP}:8123
2021-08-12 18:36:26 +02:00
"