zigbee2mqtt_container.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env bash
  2. while true; do
  3. read -p "This will create a New Zigbee2MQTT LXC Container. Proceed(y/n)?" yn
  4. case $yn in
  5. [Yy]* ) break;;
  6. [Nn]* ) exit;;
  7. * ) echo "Please answer yes or no.";;
  8. esac
  9. done
  10. # Setup script environment
  11. set -o errexit #Exit immediately if a pipeline returns a non-zero status
  12. set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
  13. set -o nounset #Treat unset variables as an error
  14. set -o pipefail #Pipe will exit with last non-zero status if applicable
  15. shopt -s expand_aliases
  16. alias die='EXIT=$? LINE=$LINENO error_exit'
  17. trap die ERR
  18. trap cleanup EXIT
  19. function error_exit() {
  20. trap - ERR
  21. local DEFAULT='Unknown failure occured.'
  22. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  23. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  24. msg "$FLAG $REASON"
  25. [ ! -z ${CTID-} ] && cleanup_ctid
  26. exit $EXIT
  27. }
  28. function warn() {
  29. local REASON="\e[97m$1\e[39m"
  30. local FLAG="\e[93m[WARNING]\e[39m"
  31. msg "$FLAG $REASON"
  32. }
  33. function info() {
  34. local REASON="$1"
  35. local FLAG="\e[36m[INFO]\e[39m"
  36. msg "$FLAG $REASON"
  37. }
  38. function msg() {
  39. local TEXT="$1"
  40. echo -e "$TEXT"
  41. }
  42. function cleanup_ctid() {
  43. if [ ! -z ${MOUNT+x} ]; then
  44. pct unmount $CTID
  45. fi
  46. if $(pct status $CTID &>/dev/null); then
  47. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  48. pct stop $CTID
  49. fi
  50. pct destroy $CTID
  51. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  52. pvesm free $ROOTFS
  53. fi
  54. }
  55. function cleanup() {
  56. popd >/dev/null
  57. rm -rf $TEMP_DIR
  58. }
  59. function load_module() {
  60. if ! $(lsmod | grep -Fq $1); then
  61. modprobe $1 &>/dev/null || \
  62. die "Failed to load '$1' module."
  63. fi
  64. MODULES_PATH=/etc/modules
  65. if ! $(grep -Fxq "$1" $MODULES_PATH); then
  66. echo "$1" >> $MODULES_PATH || \
  67. die "Failed to add '$1' module to load at boot."
  68. fi
  69. }
  70. TEMP_DIR=$(mktemp -d)
  71. pushd $TEMP_DIR >/dev/null
  72. # Download setup script
  73. wget -qL https://raw.githubusercontent.com/tteck/Proxmox/main/setup/zigbee2mqtt_setup.sh
  74. # Detect modules and automatically load at boot
  75. load_module overlay
  76. # Select storage location
  77. while read -r line; do
  78. TAG=$(echo $line | awk '{print $1}')
  79. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  80. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  81. ITEM=" Type: $TYPE Free: $FREE "
  82. OFFSET=2
  83. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  84. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  85. fi
  86. STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
  87. done < <(pvesm status -content rootdir | awk 'NR>1')
  88. if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
  89. warn "'Container' needs to be selected for at least one storage location."
  90. die "Unable to detect valid storage location."
  91. elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
  92. STORAGE=${STORAGE_MENU[0]}
  93. else
  94. while [ -z "${STORAGE:+x}" ]; do
  95. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  96. "Which storage pool you would like to use for the container?\n\n" \
  97. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  98. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  99. done
  100. fi
  101. info "Using '$STORAGE' for storage location."
  102. # Get the next guest VM/LXC ID
  103. CTID=$(pvesh get /cluster/nextid)
  104. info "Container ID is $CTID."
  105. # Download latest Debian 11 LXC template
  106. msg "Updating LXC template list..."
  107. pveam update >/dev/null
  108. msg "Downloading LXC template..."
  109. OSTYPE=debian
  110. OSVERSION=${OSTYPE}-11
  111. mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
  112. TEMPLATE="${TEMPLATES[-1]}"
  113. pveam download local $TEMPLATE >/dev/null ||
  114. die "A problem occured while downloading the LXC template."
  115. # Create variables for container disk
  116. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  117. case $STORAGE_TYPE in
  118. dir|nfs)
  119. DISK_EXT=".raw"
  120. DISK_REF="$CTID/"
  121. ;;
  122. zfspool)
  123. DISK_PREFIX="subvol"
  124. DISK_FORMAT="subvol"
  125. ;;
  126. esac
  127. DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
  128. ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
  129. # Create LXC
  130. msg "Creating LXC container..."
  131. DISK_SIZE=4G
  132. pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
  133. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  134. warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
  135. else
  136. mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
  137. fi
  138. ARCH=$(dpkg --print-architecture)
  139. HOSTNAME=zigbee2mqtt
  140. TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
  141. pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
  142. -hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 2 -memory 1024 \
  143. -ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
  144. # Modify LXC permissions to support Zigbee Sticks
  145. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  146. cat <<EOF >> $LXC_CONFIG
  147. lxc.cgroup2.devices.allow: a
  148. lxc.cap.drop:
  149. EOF
  150. # Set container timezone to match host
  151. MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
  152. ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
  153. pct unmount $CTID && unset MOUNT
  154. # Setup container
  155. msg "Starting LXC container..."
  156. pct start $CTID
  157. pct push $CTID zigbee2mqtt_setup.sh /zigbee2mqtt_setup.sh -perms 755
  158. pct exec $CTID /zigbee2mqtt_setup.sh
  159. # Get network details and show completion message
  160. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  161. info "Successfully created zigbee2mqtt LXC Container to $CTID at IP Address ${IP}"
  162. echo
  163. echo -e "\e[1;31m Update of configuration.yaml is required and found at /opt/zigbee2mqtt/data/ \e[0m"
  164. echo
  165. echo -e "Z2M can be started after completing the configuration buy running \e[1;33m sudo systemctl start zigbee2mqtt \e[0m"
  166. echo