zigbee2mqtt-v3.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. CM='\xE2\x9C\x94\033'
  8. BGN=`echo "\033[4;92m"`
  9. GN=`echo "\033[32m"`
  10. CL=`echo "\033[m"`
  11. APP="Zigbee2MQTT"
  12. NSAPP=$(echo ${APP,,} | tr -d ' ')
  13. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  14. while true; do
  15. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  16. case $yn in
  17. [Yy]* ) break;;
  18. [Nn]* ) exit;;
  19. * ) echo "Please answer yes or no.";;
  20. esac
  21. done
  22. clear
  23. function header_info {
  24. echo -e "${YW}
  25. _______ _ ___ __ __ ____ _______ _______
  26. |___ (_) | | |__ \| \/ |/ __ \__ __|__ __|
  27. / / _ __ _| |__ ___ ___ ) | \ / | | | | | | | |
  28. / / | |/ _ | _ \ / _ \/ _ \ / /| |\/| | | | | | | | |
  29. / /__| | (_| | |_) | __/ __// /_| | | | |__| | | | | |
  30. /_____|_|\__, |____/ \___|\___|____|_| |_|\___\_\ |_| |_|
  31. v3 __/ |
  32. |___/
  33. ${CL}"
  34. }
  35. header_info
  36. function pve_check() {
  37. if [[ $PVE != 1 ]]; then
  38. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater"
  39. echo -e "Exiting..."
  40. sleep 2
  41. exit
  42. fi
  43. }
  44. function default_settings() {
  45. clear
  46. header_info
  47. echo -e "${BL}Using Default Settings${CL}"
  48. echo -e "${GN}Using CT Type ${BGN}Privileged${CL}"
  49. CT_TYPE="0"
  50. echo -e "${GN}Using CT Password ${BGN}Automatic Login${CL}"
  51. PW=" "
  52. echo -e "${GN}Using ID ${BGN}$NEXTID${CL}"
  53. CT_ID=$NEXTID
  54. echo -e "${GN}Using CT Name ${BGN}$NSAPP${CL}"
  55. HN=$NSAPP
  56. echo -e "${GN}Using Disk Size ${BGN}4GB${CL}"
  57. SIZEDISK="4"
  58. echo -e "${GN}Using Storage ${BGN}local-lvm${CL}"
  59. STORAGETYPE="local-lvm"
  60. echo -e "${GN}Using ${BGN}2vCPU${CL}"
  61. CORE_COUNT="2"
  62. echo -e "${GN}Using ${BGN}1024MiB${CL}${GN} RAM${CL}"
  63. RAM_SIZE="1024"
  64. echo -e "${GN}Using IP Address ${BGN}DHCP${CL}"
  65. NET=dhcp
  66. echo -e "${GN}Using VLAN Tag ${BGN}NONE${CL}"
  67. VLAN=" "
  68. }
  69. function advanced_settings() {
  70. clear
  71. header_info
  72. echo -e "${RD}Using Advanced Settings${CL}"
  73. echo -e "${YW}Type Unprivileged, or Press [ENTER] for Default: Privileged "
  74. read CT_TYPE1
  75. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Privileged" CT_TYPE="0";
  76. echo -en "${GN}Set CT Type ${BL}$CT_TYPE1${CL}"
  77. else
  78. CT_TYPE1="Unprivileged"
  79. CT_TYPE="1"
  80. echo -en "${GN}Set CT Type ${BL}Unprivileged${CL}"
  81. fi;
  82. echo -e " ${CM}${CL} \r"
  83. sleep 1
  84. clear
  85. header_info
  86. echo -e "${RD}Using Advanced Settings${CL}"
  87. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  88. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  89. read PW1
  90. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  91. echo -en "${GN}Set CT ${BL}$PW1${CL}"
  92. else
  93. PW="-password $PW1"
  94. echo -en "${GN}Set CT Password ${BL}$PW1${CL}"
  95. fi;
  96. echo -e " ${CM}${CL} \r"
  97. sleep 1
  98. clear
  99. header_info
  100. echo -e "${RD}Using Advanced Settings${CL}"
  101. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  102. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  103. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  104. read CT_ID
  105. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  106. echo -en "${GN}Set CT ID To ${BL}$CT_ID${CL}"
  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 "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  113. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  114. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  115. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  116. read CT_NAME
  117. if [ -z $CT_NAME ]; then
  118. HN=$NSAPP
  119. else
  120. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  121. fi
  122. echo -en "${GN}Set CT Name To ${BL}$HN${CL}"
  123. echo -e " ${CM}${CL} \r"
  124. sleep 1
  125. clear
  126. header_info
  127. echo -e "${RD}Using Advanced Settings${CL}"
  128. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  129. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  130. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  131. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  132. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 4Gb "
  133. read SIZEDISK
  134. if [ -z $SIZEDISK ]; then SIZEDISK="4"; fi;
  135. if ! [[ $SIZEDISK =~ $INTEGER ]] ; then echo "ERROR! SIZEDISK MUST HAVE INTEGER NUMBER!"; exit; fi;
  136. echo -en "${GN}Set Disk Size To ${BL}$SIZEDISK${CL}"
  137. echo -e " ${CM}${CL} \r"
  138. sleep 1
  139. clear
  140. header_info
  141. echo -e "${RD}Using Advanced Settings${CL}"
  142. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  143. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  144. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  145. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  146. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  147. echo -e "${YW}Storages Available:${CL}"
  148. echo " "
  149. for stg in `pvesh get storage --noborder --noheader`
  150. do
  151. echo -e "${BL} - ${stg}${CL}"
  152. done
  153. echo " "
  154. echo -e "${YW}Enter which storage to create the CT, or Press [ENTER] for Default: local-lvm "
  155. read STORAGETYPE
  156. if [ -z $STORAGETYPE ]; then STORAGETYPE="local-lvm"; fi;
  157. echo -en "${GN}Set Storage To ${BL}$STORAGETYPE${CL}"
  158. echo -e " ${CM}${CL} \r"
  159. sleep 1
  160. clear
  161. header_info
  162. echo -e "${RD}Using Advanced Settings${CL}"
  163. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  164. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  165. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  166. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  167. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  168. echo -e "${GN}Using Storage ${BGN}$STORAGETYPE${CL}"
  169. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 2 "
  170. read CORE_COUNT
  171. if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; fi;
  172. echo -en "${GN}Set Cores To ${BL}$CORE_COUNT${CL}"
  173. echo -e " ${CM}${CL} \r"
  174. sleep 1
  175. clear
  176. header_info
  177. echo -e "${RD}Using Advanced Settings${CL}"
  178. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  179. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  180. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  181. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  182. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  183. echo -e "${GN}Using Storage ${BGN}$STORAGETYPE${CL}"
  184. echo -e "${GN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  185. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 1024 "
  186. read RAM_SIZE
  187. if [ -z $RAM_SIZE ]; then RAM_SIZE="1024"; fi;
  188. echo -en "${GN}Set RAM To ${BL}$RAM_SIZE${CL}"
  189. echo -e " ${CM}${CL} \n"
  190. sleep 1
  191. clear
  192. header_info
  193. echo -e "${RD}Using Advanced Settings${CL}"
  194. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  195. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  196. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  197. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  198. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  199. echo -e "${GN}Using Storage ${BGN}$STORAGETYPE${CL}"
  200. echo -e "${GN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  201. echo -e "${GN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  202. echo -e "${YW}Enter a IP Address, or Press [ENTER] for Default: DHCP "
  203. read NET
  204. if [ -z $NET ]; then NET="dhcp"; fi;
  205. echo -en "${GN}Set IP Address To ${BL}$NET${CL}"
  206. echo -e " ${CM}${CL} \n"
  207. sleep 1
  208. clear
  209. header_info
  210. echo -e "${RD}Using Advanced Settings${CL}"
  211. echo -e "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  212. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  213. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  214. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  215. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  216. echo -e "${GN}Using Storage ${BGN}$STORAGETYPE${CL}"
  217. echo -e "${GN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  218. echo -e "${GN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  219. echo -e "${GN}Using IP Address ${BGN}$NET${CL}"
  220. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  221. read VLAN1
  222. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN=" ";
  223. echo -en "${GN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  224. else
  225. VLAN="-tag $VLAN1"
  226. echo -en "${GN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  227. fi;
  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 "${GN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  234. echo -e "${GN}Using CT Password ${BGN}$PW1${CL}"
  235. echo -e "${GN}Using ID ${BGN}$CT_ID${CL}"
  236. echo -e "${GN}Using CT Name ${BGN}$HN${CL}"
  237. echo -e "${GN}Using Disk Size ${BGN}$SIZEDISK${CL}"
  238. echo -e "${GN}Using Storage ${BGN}$STORAGETYPE${CL}"
  239. echo -e "${GN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  240. echo -e "${GN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  241. echo -e "${GN}Using IP Address ${BGN}$NET${CL}"
  242. echo -e "${GN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  243. read -p "Are these settings correct(y/n)? " -n 1 -r
  244. echo
  245. if [[ ! $REPLY =~ ^[Yy]$ ]]
  246. then
  247. advanced_settings
  248. fi
  249. }
  250. function start_script() {
  251. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  252. read SETTINGS
  253. if [ -z $SETTINGS ]; then default_settings;
  254. else
  255. advanced_settings
  256. fi;
  257. }
  258. pve_check
  259. start_script
  260. set -o errexit
  261. set -o errtrace
  262. set -o nounset
  263. set -o pipefail
  264. shopt -s expand_aliases
  265. alias die='EXIT=$? LINE=$LINENO error_exit'
  266. trap die ERR
  267. trap cleanup EXIT
  268. function error_exit() {
  269. trap - ERR
  270. local DEFAULT='Unknown failure occured.'
  271. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  272. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  273. msg "$FLAG $REASON"
  274. [ ! -z ${CTID-} ] && cleanup_ctid
  275. exit $EXIT
  276. }
  277. function warn() {
  278. local REASON="\e[97m$1\e[39m"
  279. local FLAG="\e[93m[WARNING]\e[39m"
  280. msg "$FLAG $REASON"
  281. }
  282. function info() {
  283. local REASON="$1"
  284. local FLAG="\e[36m[INFO]\e[39m"
  285. msg "$FLAG $REASON"
  286. }
  287. function msg() {
  288. local TEXT="$1"
  289. echo -e "$TEXT"
  290. }
  291. function cleanup_ctid() {
  292. if $(pct status $CTID &>/dev/null); then
  293. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  294. pct stop $CTID
  295. fi
  296. pct destroy $CTID
  297. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  298. pvesm free $ROOTFS
  299. fi
  300. }
  301. function cleanup() {
  302. popd >/dev/null
  303. rm -rf $TEMP_DIR
  304. }
  305. if [ "$CT_TYPE" == "1" ]; then
  306. FEATURES="nesting=1,keyctl=1"
  307. else
  308. FEATURES="nesting=1"
  309. fi
  310. TEMP_DIR=$(mktemp -d)
  311. pushd $TEMP_DIR >/dev/null
  312. export CTID=$CT_ID
  313. export PCT_OSTYPE=debian
  314. export PCT_OSVERSION=11
  315. export PCT_DISK_SIZE=$SIZEDISK
  316. export PCT_OPTIONS="
  317. -features $FEATURES
  318. -hostname $HN
  319. -net0 name=eth0,bridge=vmbr0,ip=$NET
  320. $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. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  329. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  330. warn "Some addons may not work due to ZFS not supporting 'fallocate'."
  331. fi
  332. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  333. cat <<EOF >> $LXC_CONFIG
  334. lxc.cgroup2.devices.allow: a
  335. lxc.cap.drop:
  336. lxc.cgroup2.devices.allow: c 188:* rwm
  337. lxc.cgroup2.devices.allow: c 189:* rwm
  338. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  339. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  340. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  341. EOF
  342. if [ "$DHCP" == "1" ]; then
  343. MAC=$(pct config $CTID \
  344. | grep -i hwaddr \
  345. | awk '{print substr($2, 31, length($3) 17 ) }') \
  346. echo -e "MAC Address ${BL}$MAC${CL}"
  347. dhcp_reservation(){
  348. printf "Please set DHCP reservation and press Enter."
  349. read
  350. }
  351. dhcp_reservation
  352. fi
  353. echo -en "${GN} Starting LXC Container... "
  354. pct start $CTID
  355. echo -e "${CM}${CL} \r"
  356. alias lxc-cmd="lxc-attach -n $CTID --"
  357. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/zigbee2mqtt-install.sh)" || exit
  358. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  359. echo -e "${GN}Successfully created ${APP} LXC to${CL} ${BL}$CTID${CL}. \n"