zigbee2mqtt-v3.sh 13 KB

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