zigbee2mqtt-v3.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/bin/env bash -ex
  2. set -euo pipefail
  3. shopt -s inherit_errexit nullglob
  4. NEXTID=$(pvesh get /cluster/nextid)
  5. INTEGER='^[0-9]+$'
  6. YW=`echo "\033[33m"`
  7. BL=`echo "\033[36m"`
  8. RD=`echo "\033[01;31m"`
  9. BGN=`echo "\033[4;92m"`
  10. GN=`echo "\033[1;92m"`
  11. DGN=`echo "\033[32m"`
  12. CL=`echo "\033[m"`
  13. BFR="\\r\\033[K"
  14. HOLD="-"
  15. CM="${GN}✓${CL}"
  16. APP="Zigbee2MQTT"
  17. NSAPP=$(echo ${APP,,} | tr -d ' ')
  18. while true; do
  19. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  20. case $yn in
  21. [Yy]* ) break;;
  22. [Nn]* ) exit;;
  23. * ) echo "Please answer yes or no.";;
  24. esac
  25. done
  26. clear
  27. function header_info {
  28. echo -e "${YW}
  29. _______ _ ___ __ __ ____ _______ _______
  30. |___ (_) | | |__ \| \/ |/ __ \__ __|__ __|
  31. / / _ __ _| |__ ___ ___ ) | \ / | | | | | | | |
  32. / / | |/ _ | _ \ / _ \/ _ \ / /| |\/| | | | | | | | |
  33. / /__| | (_| | |_) | __/ __// /_| | | | |__| | | | | |
  34. /_____|_|\__, |____/ \___|\___|____|_| |_|\___\_\ |_| |_|
  35. v3 __/ |
  36. |___/
  37. ${CL}"
  38. }
  39. header_info
  40. function msg_info() {
  41. local msg="$1"
  42. echo -ne " ${HOLD} ${YW}${msg}..."
  43. }
  44. function msg_ok() {
  45. local msg="$1"
  46. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  47. }
  48. function PVE_CHECK() {
  49. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  50. if [[ $PVE != 1 ]]; then
  51. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  52. echo -e "Exiting..."
  53. sleep 2
  54. exit
  55. fi
  56. }
  57. function default_settings() {
  58. clear
  59. header_info
  60. echo -e "${BL}Using Default Settings${CL}"
  61. echo -e "${DGN}Using CT Type ${BGN}Privileged${CL}"
  62. CT_TYPE="0"
  63. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  64. PW=" "
  65. echo -e "${DGN}Using ID ${BGN}$NEXTID${CL}"
  66. CT_ID=$NEXTID
  67. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  68. HN=$NSAPP
  69. echo -e "${DGN}Using Disk Size ${BGN}4GB${CL}"
  70. DISK_SIZE="4"
  71. echo -e "${DGN}Using ${BGN}2vCPU${CL}"
  72. CORE_COUNT="2"
  73. echo -e "${DGN}Using ${BGN}1024MiB${CL}${DGN} RAM${CL}"
  74. RAM_SIZE="1024"
  75. echo -e "${DGN}Using IP Address ${BGN}DHCP${CL}"
  76. NET=dhcp
  77. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  78. VLAN=" "
  79. }
  80. function advanced_settings() {
  81. clear
  82. header_info
  83. echo -e "${RD}Using Advanced Settings${CL}"
  84. echo -e "${YW}Type Unprivileged, or Press [ENTER] for Default: Privileged"
  85. read CT_TYPE1
  86. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Privileged" CT_TYPE="0";
  87. echo -en "${DGN}Set CT Type ${BL}$CT_TYPE1${CL}"
  88. else
  89. CT_TYPE1="Unprivileged"
  90. CT_TYPE="1"
  91. echo -en "${DGN}Set CT Type ${BL}Unprivileged${CL}"
  92. fi;
  93. echo -e " ${CM}${CL} \r"
  94. sleep 1
  95. clear
  96. header_info
  97. echo -e "${RD}Using Advanced Settings${CL}"
  98. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  99. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  100. read PW1
  101. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  102. echo -en "${DGN}Set CT ${BL}$PW1${CL}"
  103. else
  104. PW="-password $PW1"
  105. echo -en "${DGN}Set CT Password ${BL}$PW1${CL}"
  106. fi;
  107. echo -e " ${CM}${CL} \r"
  108. sleep 1
  109. clear
  110. header_info
  111. echo -e "${RD}Using Advanced Settings${CL}"
  112. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  113. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  114. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  115. read CT_ID
  116. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  117. echo -en "${DGN}Set CT ID To ${BL}$CT_ID${CL}"
  118. echo -e " ${CM}${CL} \r"
  119. sleep 1
  120. clear
  121. header_info
  122. echo -e "${RD}Using Advanced Settings${CL}"
  123. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  124. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  125. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  126. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  127. read CT_NAME
  128. if [ -z $CT_NAME ]; then
  129. HN=$NSAPP
  130. else
  131. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  132. fi
  133. echo -en "${DGN}Set CT Name To ${BL}$HN${CL}"
  134. echo -e " ${CM}${CL} \r"
  135. sleep 1
  136. clear
  137. header_info
  138. echo -e "${RD}Using Advanced Settings${CL}"
  139. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  140. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  141. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  142. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  143. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 4Gb "
  144. read DISK_SIZE
  145. if [ -z $DISK_SIZE ]; then DISK_SIZE="4"; fi;
  146. if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
  147. echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${CL}"
  148. echo -e " ${CM}${CL} \r"
  149. sleep 1
  150. clear
  151. header_info
  152. echo -e "${RD}Using Advanced Settings${CL}"
  153. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  154. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  155. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  156. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  157. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  158. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 2 "
  159. read CORE_COUNT
  160. if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; fi;
  161. echo -en "${DGN}Set Cores To ${BL}$CORE_COUNT${CL}"
  162. echo -e " ${CM}${CL} \r"
  163. sleep 1
  164. clear
  165. header_info
  166. echo -e "${RD}Using Advanced Settings${CL}"
  167. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  168. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  169. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  170. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  171. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  172. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  173. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 1024 "
  174. read RAM_SIZE
  175. if [ -z $RAM_SIZE ]; then RAM_SIZE="1024"; fi;
  176. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}"
  177. echo -e " ${CM}${CL} \n"
  178. sleep 1
  179. clear
  180. header_info
  181. echo -e "${RD}Using Advanced Settings${CL}"
  182. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  183. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  184. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  185. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  186. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  187. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  188. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  189. echo -e "${YW}Enter a IP Address, or Press [ENTER] for Default: DHCP "
  190. read NET
  191. if [ -z $NET ]; then NET="dhcp"; fi;
  192. echo -en "${DGN}Set IP Address To ${BL}$NET${CL}"
  193. echo -e " ${CM}${CL} \n"
  194. sleep 1
  195. clear
  196. header_info
  197. echo -e "${RD}Using Advanced Settings${CL}"
  198. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  199. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  200. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  201. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  202. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  203. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  204. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  205. echo -e "${DGN}Using IP Address ${BGN}$NET${CL}"
  206. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  207. read VLAN1
  208. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN=" ";
  209. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  210. else
  211. VLAN="-tag $VLAN1"
  212. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  213. fi;
  214. echo -e " ${CM}${CL} \n"
  215. sleep 1
  216. clear
  217. header_info
  218. echo -e "${RD}Using Advanced Settings${CL}"
  219. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  220. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  221. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  222. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  223. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  224. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  225. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${CL}"
  226. echo -e "${DGN}Using IP Address ${BGN}$NET${CL}"
  227. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  228. read -p "Are these settings correct(y/n)? " -n 1 -r
  229. echo
  230. if [[ ! $REPLY =~ ^[Yy]$ ]]
  231. then
  232. advanced_settings
  233. fi
  234. }
  235. function start_script() {
  236. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  237. read SETTINGS
  238. if [ -z $SETTINGS ]; then default_settings;
  239. else
  240. advanced_settings
  241. fi;
  242. }
  243. start_script
  244. if [ "$CT_TYPE" == "1" ]; then
  245. FEATURES="nesting=1,keyctl=1"
  246. else
  247. FEATURES="nesting=1"
  248. fi
  249. TEMP_DIR=$(mktemp -d)
  250. pushd $TEMP_DIR >/dev/null
  251. export CTID=$CT_ID
  252. export PCT_OSTYPE=debian
  253. export PCT_OSVERSION=11
  254. export PCT_DISK_SIZE=$DISK_SIZE
  255. export PCT_OPTIONS="
  256. -features $FEATURES
  257. -hostname $HN
  258. -net0 name=eth0,bridge=vmbr0,ip=$NET
  259. $VLAN
  260. -onboot 1
  261. -cores $CORE_COUNT
  262. -memory $RAM_SIZE
  263. -unprivileged $CT_TYPE
  264. $PW
  265. "
  266. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  267. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  268. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  269. echo -e "${RD}Some applications may not work properly due to ZFS not supporting 'fallocate'.${CL}"
  270. fi
  271. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  272. cat <<EOF >> $LXC_CONFIG
  273. lxc.cgroup2.devices.allow: a
  274. lxc.cap.drop:
  275. lxc.cgroup2.devices.allow: c 188:* rwm
  276. lxc.cgroup2.devices.allow: c 189:* rwm
  277. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  278. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  279. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  280. EOF
  281. msg_info "Starting LXC Container"
  282. pct start $CTID
  283. msg_ok "Started LXC Container"
  284. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/zigbee2mqtt-install.sh)" || exit
  285. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  286. msg_ok "Completed Successfully!\n"