zigbee2mqtt.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/usr/bin/env bash
  2. YW=`echo "\033[33m"`
  3. BL=`echo "\033[36m"`
  4. RD=`echo "\033[01;31m"`
  5. CM='\xE2\x9C\x94\033'
  6. GN=`echo "\033[1;92m"`
  7. CL=`echo "\033[m"`
  8. while true; do
  9. read -p "This will create a New Zigbee2MQTT LXC. Proceed(y/n)?" yn
  10. case $yn in
  11. [Yy]* ) break;;
  12. [Nn]* ) exit;;
  13. * ) echo "Please answer yes or no.";;
  14. esac
  15. done
  16. clear
  17. function header_info {
  18. echo -e "${YW}
  19. _______ _ ___ __ __ ____ _______ _______
  20. |___ (_) | | |__ \| \/ |/ __ \__ __|__ __|
  21. / / _ __ _| |__ ___ ___ ) | \ / | | | | | | | |
  22. / / | |/ _ | _ \ / _ \/ _ \ / /| |\/| | | | | | | | |
  23. / /__| | (_| | |_) | __/ __// /_| | | | |__| | | | | |
  24. /_____|_|\__, |____/ \___|\___|____|_| |_|\___\_\ |_| |_|
  25. __/ |
  26. |___/
  27. ${CL}"
  28. }
  29. header_info
  30. show_menu(){
  31. printf " ${YW} 1)${GN} Privileged ${CL}\n"
  32. printf " ${YW} 2)${RD} Unprivileged (no device passthrough) ${CL}\n"
  33. printf "Please choose a Install Method and hit enter or ${RD}x${CL} to exit."
  34. read opt
  35. }
  36. option_picked(){
  37. message1=${@:-"${CL}Error: No message passed"}
  38. printf " ${YW}${message1}${CL}\n"
  39. }
  40. show_menu
  41. while [ $opt != '' ]
  42. do
  43. if [ $opt = '' ]; then
  44. exit;
  45. else
  46. case $opt in
  47. 1) clear;
  48. header_info;
  49. option_picked "Using Privileged Install";
  50. IM=0
  51. break;
  52. ;;
  53. 2) clear;
  54. header_info;
  55. option_picked "Using Unprivileged Install";
  56. IM=1
  57. break;
  58. ;;
  59. x)exit;
  60. ;;
  61. \n)exit;
  62. ;;
  63. *)clear;
  64. option_picked "Please choose a Install Method from the menu";
  65. show_menu;
  66. ;;
  67. esac
  68. fi
  69. done
  70. show_menu2(){
  71. printf " ${YW} 1)${GN} Use Automatic Login ${CL}\n"
  72. printf " ${YW} 2)${GN} Use Password (changeme) ${CL}\n"
  73. printf "Please choose a Password Type and hit enter or ${RD}x${CL} to exit."
  74. read opt
  75. }
  76. option_picked(){
  77. message2=${@:-"${CL}Error: No message passed"}
  78. printf " ${YW}${message1}${CL}\n"
  79. printf " ${YW}${message2}${CL}\n"
  80. }
  81. show_menu2
  82. while [ $opt != '' ]
  83. do
  84. if [ $opt = '' ]; then
  85. exit;
  86. else
  87. case $opt in
  88. 1) clear;
  89. header_info;
  90. option_picked "Using Automatic Login";
  91. PW=" "
  92. break;
  93. ;;
  94. 2) clear;
  95. header_info;
  96. option_picked "Using Password (changeme)";
  97. PW="-password changeme"
  98. break;
  99. ;;
  100. x)exit;
  101. ;;
  102. \n)exit;
  103. ;;
  104. *)clear;
  105. option_picked "Please choose a Password Type from the menu";
  106. show_menu2;
  107. ;;
  108. esac
  109. fi
  110. done
  111. show_menu3(){
  112. printf " ${YW} 1)${GN} Automatic DHCP ${CL}\n"
  113. printf " ${YW} 2)${GN} Manual DHCP ${CL}\n"
  114. printf "Please choose a DHCP Type and hit enter or ${RD}x${CL} to exit."
  115. read opt
  116. }
  117. option_picked(){
  118. message3=${@:-"${CL}Error: No message passed"}
  119. printf " ${YW}${message1}${CL}\n"
  120. printf " ${YW}${message2}${CL}\n"
  121. printf " ${YW}${message3}${CL}\n"
  122. }
  123. show_menu3
  124. while [ $opt != '' ]
  125. do
  126. if [ $opt = '' ]; then
  127. exit;
  128. else
  129. case $opt in
  130. 1) clear;
  131. header_info;
  132. option_picked "Using Automatic DHCP";
  133. DHCP=" "
  134. break;
  135. ;;
  136. 2) clear;
  137. header_info;
  138. option_picked "Using Manual DHCP";
  139. DHCP="1"
  140. break;
  141. ;;
  142. x)exit;
  143. ;;
  144. \n)exit;
  145. ;;
  146. *)clear;
  147. option_picked "Please choose a DHCP Type from the menu";
  148. show_menu3;
  149. ;;
  150. esac
  151. fi
  152. done
  153. set -o errexit
  154. set -o errtrace
  155. set -o nounset
  156. set -o pipefail
  157. shopt -s expand_aliases
  158. alias die='EXIT=$? LINE=$LINENO error_exit'
  159. trap die ERR
  160. trap cleanup EXIT
  161. function error_exit() {
  162. trap - ERR
  163. local DEFAULT='Unknown failure occured.'
  164. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  165. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  166. msg "$FLAG $REASON"
  167. [ ! -z ${CTID-} ] && cleanup_ctid
  168. exit $EXIT
  169. }
  170. function warn() {
  171. local REASON="\e[97m$1\e[39m"
  172. local FLAG="\e[93m[WARNING]\e[39m"
  173. msg "$FLAG $REASON"
  174. }
  175. function info() {
  176. local REASON="$1"
  177. local FLAG="\e[36m[INFO]\e[39m"
  178. msg "$FLAG $REASON"
  179. }
  180. function msg() {
  181. local TEXT="$1"
  182. echo -e "$TEXT"
  183. }
  184. function cleanup_ctid() {
  185. if $(pct status $CTID &>/dev/null); then
  186. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  187. pct stop $CTID
  188. fi
  189. pct destroy $CTID
  190. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  191. pvesm free $ROOTFS
  192. fi
  193. }
  194. function cleanup() {
  195. popd >/dev/null
  196. rm -rf $TEMP_DIR
  197. }
  198. if [ "$IM" == "1" ]; then
  199. FEATURES="nesting=1,keyctl=1"
  200. else
  201. FEATURES="nesting=1"
  202. fi
  203. TEMP_DIR=$(mktemp -d)
  204. pushd $TEMP_DIR >/dev/null
  205. export CTID=$(pvesh get /cluster/nextid)
  206. export PCT_OSTYPE=debian
  207. export PCT_OSVERSION=11
  208. export PCT_DISK_SIZE=4
  209. export PCT_OPTIONS="
  210. -features $FEATURES
  211. -hostname zigbee2mqtt
  212. -net0 name=eth0,bridge=vmbr0,ip=dhcp
  213. -onboot 1
  214. -cores 2
  215. -memory 1024
  216. -unprivileged ${IM}
  217. ${PW}
  218. "
  219. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  220. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  221. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  222. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  223. fi
  224. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  225. cat <<EOF >> $LXC_CONFIG
  226. lxc.cgroup2.devices.allow: a
  227. lxc.cap.drop:
  228. lxc.cgroup2.devices.allow: c 188:* rwm
  229. lxc.cgroup2.devices.allow: c 189:* rwm
  230. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  231. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  232. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  233. EOF
  234. if [ "$DHCP" == "1" ]; then
  235. MAC=$(pct config $CTID \
  236. | grep -i hwaddr \
  237. | awk '{print substr($2, 31, length($3) 17 ) }') \
  238. echo -e "MAC Address ${BL}$MAC${CL}"
  239. dhcp_reservation(){
  240. printf "Please set DHCP reservation and press Enter."
  241. read
  242. }
  243. dhcp_reservation
  244. fi
  245. echo -en "${GN} Starting LXC Container... "
  246. pct start $CTID
  247. echo -e "${CM}${CL} \r"
  248. alias lxc-cmd="lxc-attach -n $CTID --"
  249. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/zigbee2mqtt-install.sh)" || exit
  250. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  251. echo -e "${GN}Successfully created Zigbee2MQTT LXC to${CL} ${BL}$CTID${CL}. \n"