podman-homeassistant-v3.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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="P-Home Assistant"
  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 "${BL}
  43. _____ _
  44. | __ \ | |
  45. | |__) |__ __| |_ __ ___ __ _ _ __
  46. | ___/ _ \ / _ | _ _ \ / _ | _ \
  47. | | | (_) | (_| | | | | | | (_| | | | |
  48. |_| \___/ \__,_|_| |_| |_|\__,_|_| |_| _ _ _
  49. | | v3 (_) | | | |
  50. | |__ ___ _ __ ___ ___ __ _ ___ ___ _ ___| |_ __ _ _ __ | |_
  51. | _ \ / _ \| _ _ \ / _ \/ _ / __/ __| / __| __/ _ | _ \| __|
  52. | | | | (_) | | | | | | __/ (_| \__ \__ \ \__ \ || (_| | | | | |_
  53. |_| |_|\___/|_| |_| |_|\___|\__,_|___/___/_|___/\__\__,_|_| |_|\__|
  54. ${CL}"
  55. }
  56. header_info
  57. function msg_info() {
  58. local msg="$1"
  59. echo -ne " ${HOLD} ${YW}${msg}..."
  60. }
  61. function msg_ok() {
  62. local msg="$1"
  63. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  64. }
  65. function PVE_CHECK() {
  66. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  67. if [[ $PVE != 1 ]]; then
  68. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  69. echo -e "Exiting..."
  70. sleep 2
  71. exit
  72. fi
  73. }
  74. function default_settings() {
  75. clear
  76. header_info
  77. echo -e "${BL}Using Default Settings${CL}"
  78. echo -e "${DGN}Using CT Type ${BGN}Privileged${CL}"
  79. CT_TYPE="0"
  80. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  81. PW=" "
  82. echo -e "${DGN}Using CT ID ${BGN}$NEXTID${CL}"
  83. CT_ID=$NEXTID
  84. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  85. HN=$NSAPP
  86. echo -e "${DGN}Using Disk Size ${BGN}16${CL}${DGN}GB${CL}"
  87. DISK_SIZE="16"
  88. echo -e "${DGN}Using ${BGN}2${CL}${DGN}vCPU${CL}"
  89. CORE_COUNT="2"
  90. echo -e "${DGN}Using ${BGN}2048${CL}${DGN}MiB RAM${CL}"
  91. RAM_SIZE="2048"
  92. echo -e "${DGN}Using Bridge ${BGN}vmbr0${CL}"
  93. BRG="vmbr0"
  94. echo -e "${DGN}Using Static IP Address ${BGN}DHCP${CL}"
  95. NET=dhcp
  96. echo -e "${DGN}Using Gateway Address ${BGN}NONE${CL}"
  97. GATE=""
  98. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  99. VLAN=""
  100. }
  101. function advanced_settings() {
  102. clear
  103. header_info
  104. echo -e "${RD}Using Advanced Settings${CL}"
  105. echo -e "${YW}Type ${CROSS}${YW}Unprivileged, or Press [ENTER] for Default: Privileged"
  106. read CT_TYPE1
  107. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Privileged" CT_TYPE="0";
  108. echo -en "${DGN}Set CT Type ${BL}$CT_TYPE1${CL}"
  109. else
  110. CT_TYPE1="Unprivileged"
  111. CT_TYPE="1"
  112. echo -en "${DGN}Set CT Type ${BL}UnPrivileged${CL}"
  113. fi;
  114. echo -e " ${CM}${CL} \r"
  115. sleep 1
  116. clear
  117. header_info
  118. echo -e "${RD}Using Advanced Settings${CL}"
  119. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  120. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  121. read PW1
  122. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  123. echo -en "${DGN}Set CT ${BL}$PW1${CL}"
  124. else
  125. PW="-password $PW1"
  126. echo -en "${DGN}Set CT Password ${BL}$PW1${CL}"
  127. fi;
  128. echo -e " ${CM}${CL} \r"
  129. sleep 1
  130. clear
  131. header_info
  132. echo -e "${RD}Using Advanced Settings${CL}"
  133. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  134. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  135. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  136. read CT_ID
  137. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  138. echo -en "${DGN}Set CT ID To ${BL}$CT_ID${CL}"
  139. echo -e " ${CM}${CL} \r"
  140. sleep 1
  141. clear
  142. header_info
  143. echo -e "${RD}Using Advanced Settings${CL}"
  144. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  145. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  146. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  147. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  148. read CT_NAME
  149. if [ -z $CT_NAME ]; then
  150. HN=$NSAPP
  151. else
  152. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  153. fi
  154. echo -en "${DGN}Set CT Name To ${BL}$HN${CL}"
  155. echo -e " ${CM}${CL} \r"
  156. sleep 1
  157. clear
  158. header_info
  159. echo -e "${RD}Using Advanced Settings${CL}"
  160. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  161. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  162. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  163. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  164. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 16 "
  165. read DISK_SIZE
  166. if [ -z $DISK_SIZE ]; then DISK_SIZE="16"; fi;
  167. if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
  168. echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${CL}${DGN}GB${CL}"
  169. echo -e " ${CM}${CL} \r"
  170. sleep 1
  171. clear
  172. header_info
  173. echo -e "${RD}Using Advanced Settings${CL}"
  174. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  175. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  176. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  177. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  178. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  179. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 2 "
  180. read CORE_COUNT
  181. if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; fi;
  182. echo -en "${DGN}Set Cores To ${BL}$CORE_COUNT${CL}${DGN}vCPU${CL}"
  183. echo -e " ${CM}${CL} \r"
  184. sleep 1
  185. clear
  186. header_info
  187. echo -e "${RD}Using Advanced Settings${CL}"
  188. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  189. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  190. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  191. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  192. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  193. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  194. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 2048 "
  195. read RAM_SIZE
  196. if [ -z $RAM_SIZE ]; then RAM_SIZE="2048"; fi;
  197. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}${DGN}MiB RAM${CL}"
  198. echo -e " ${CM}${CL} \n"
  199. sleep 1
  200. clear
  201. header_info
  202. echo -e "${RD}Using Advanced Settings${CL}"
  203. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  204. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  205. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  206. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  207. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  208. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  209. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  210. echo -e "${YW}Enter a Bridge, or Press [ENTER] for Default: vmbr0 "
  211. read BRG
  212. if [ -z $BRG ]; then BRG="vmbr0"; fi;
  213. echo -en "${DGN}Set Bridge To ${BL}$BRG${CL}"
  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 CT 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}${DGN}GB${CL}"
  224. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  225. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  226. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  227. echo -e "${YW}Enter a Static IP Address, or Press [ENTER] for Default: DHCP "
  228. read NET
  229. if [ -z $NET ]; then NET="dhcp"; fi;
  230. echo -en "${DGN}Set Static IP Address To ${BL}$NET${CL}"
  231. echo -e " ${CM}${CL} \n"
  232. sleep 1
  233. clear
  234. header_info
  235. echo -e "${RD}Using Advanced Settings${CL}"
  236. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  237. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  238. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  239. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  240. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  241. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  242. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  243. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  244. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  245. echo -e "${YW}Enter a Gateway IP, or Press [ENTER] for Default: NONE "
  246. read GATE1
  247. if [ -z $GATE1 ]; then GATE1="NONE" GATE="";
  248. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  249. else
  250. GATE=",gw=$GATE1"
  251. echo -en "${DGN}Set Gateway IP To ${BL}$GATE1${CL}"
  252. fi;
  253. echo -e " ${CM}${CL} \n"
  254. sleep 1
  255. clear
  256. header_info
  257. echo -e "${RD}Using Advanced Settings${CL}"
  258. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  259. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  260. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  261. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  262. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  263. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  264. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  265. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  266. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  267. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  268. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  269. read VLAN1
  270. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN="";
  271. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  272. else
  273. VLAN=",tag=$VLAN1"
  274. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  275. fi;
  276. echo -e " ${CM}${CL} \n"
  277. sleep 1
  278. clear
  279. header_info
  280. echo -e "${RD}Using Advanced Settings${CL}"
  281. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  282. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  283. echo -e "${DGN}Using CT ID ${BGN}$CT_ID${CL}"
  284. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  285. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}${DGN}GB${CL}"
  286. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  287. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  288. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  289. echo -e "${DGN}Using Static IP Address ${BGN}$NET${CL}"
  290. echo -e "${DGN}Using Gateway IP Address ${BGN}$GATE1${CL}"
  291. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  292. read -p "Are these settings correct(y/n)? " -n 1 -r
  293. echo
  294. if [[ ! $REPLY =~ ^[Yy]$ ]]
  295. then
  296. advanced_settings
  297. fi
  298. }
  299. function start_script() {
  300. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  301. read SETTINGS
  302. if [ -z $SETTINGS ]; then default_settings;
  303. else
  304. advanced_settings
  305. fi;
  306. }
  307. PVE_CHECK
  308. start_script
  309. if [ "$CT_TYPE" == "1" ]; then
  310. FEATURES="nesting=1,keyctl=1"
  311. else
  312. FEATURES="nesting=1"
  313. fi
  314. TEMP_DIR=$(mktemp -d)
  315. pushd $TEMP_DIR >/dev/null
  316. export CTID=$CT_ID
  317. export PCT_OSTYPE=debian
  318. export PCT_OSVERSION=11
  319. export PCT_DISK_SIZE=$DISK_SIZE
  320. export PCT_OPTIONS="
  321. -features $FEATURES
  322. -hostname $HN
  323. -net0 name=eth0,bridge=$BRG,ip=$NET$GATE$VLAN
  324. -onboot 1
  325. -cores $CORE_COUNT
  326. -memory $RAM_SIZE
  327. -unprivileged $CT_TYPE
  328. $PW
  329. "
  330. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  331. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  332. cat <<EOF >> $LXC_CONFIG
  333. lxc.cgroup2.devices.allow: a
  334. lxc.cap.drop:
  335. EOF
  336. msg_info "Starting LXC Container"
  337. pct start $CTID
  338. msg_ok "Started LXC Container"
  339. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/podman-homeassistant-install.sh)" || exit
  340. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  341. msg_ok "Completed Successfully!\n"
  342. echo -e "${APP} should be reachable by going to the following URL.
  343. ${BL}http://${IP}:8123${CL}
  344. Yacht should be reachable by going to the following URL.
  345. ${BL}http://${IP}:8000${CL}\n"