zigbee2mqtt-v2.sh 6.7 KB

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