mqtt_container.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env bash
  2. while true; do
  3. read -p "This will create a New MQTT 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/mqtt_setup.sh
  74. # Detect modules and automatically load at boot
  75. #load_module aufs
  76. load_module overlay
  77. # Select storage location
  78. while read -r line; do
  79. TAG=$(echo $line | awk '{print $1}')
  80. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  81. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  82. ITEM=" Type: $TYPE Free: $FREE "
  83. OFFSET=2
  84. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  85. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  86. fi
  87. STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
  88. done < <(pvesm status -content rootdir | awk 'NR>1')
  89. if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
  90. warn "'Container' needs to be selected for at least one storage location."
  91. die "Unable to detect valid storage location."
  92. elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
  93. STORAGE=${STORAGE_MENU[0]}
  94. else
  95. while [ -z "${STORAGE:+x}" ]; do
  96. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  97. "Which storage pool you would like to use for the container?\n\n" \
  98. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  99. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  100. done
  101. fi
  102. info "Using '$STORAGE' for storage location."
  103. # Get the next guest VM/LXC ID
  104. CTID=$(pvesh get /cluster/nextid)
  105. info "Container ID is $CTID."
  106. # Download latest Debian 11 LXC template
  107. msg "Updating LXC template list..."
  108. pveam update >/dev/null
  109. msg "Downloading LXC template..."
  110. OSTYPE=debian
  111. OSVERSION=${OSTYPE}-11
  112. mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
  113. TEMPLATE="${TEMPLATES[-1]}"
  114. pveam download local $TEMPLATE >/dev/null ||
  115. die "A problem occured while downloading the LXC template."
  116. # Create variables for container disk
  117. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  118. case $STORAGE_TYPE in
  119. dir|nfs)
  120. DISK_EXT=".raw"
  121. DISK_REF="$CTID/"
  122. ;;
  123. zfspool)
  124. DISK_PREFIX="subvol"
  125. DISK_FORMAT="subvol"
  126. ;;
  127. esac
  128. DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
  129. ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
  130. # Create LXC
  131. msg "Creating LXC container..."
  132. DISK_SIZE=2G
  133. pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
  134. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  135. warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
  136. else
  137. mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
  138. fi
  139. ARCH=$(dpkg --print-architecture)
  140. HOSTNAME=mqtt
  141. TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
  142. pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
  143. -hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 1 -memory 512 \
  144. -ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
  145. # Set container timezone to match host
  146. MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
  147. ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
  148. pct unmount $CTID && unset MOUNT
  149. # Setup container
  150. msg "Starting LXC container..."
  151. pct start $CTID
  152. pct push $CTID mqtt_setup.sh /mqtt_setup.sh -perms 755
  153. pct exec $CTID /mqtt_setup.sh
  154. # Get network details and show completion message
  155. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  156. info "Successfully created a MQTT LXC Container to $CTID at IP Address ${IP}"