build.func 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. variables() {
  2. NSAPP=$(echo ${APP,,} | tr -d ' ')
  3. var_install="${NSAPP}-install"
  4. INTEGER='^[0-9]+([.][0-9]+)?$'
  5. }
  6. color() {
  7. YW=$(echo "\033[33m")
  8. BL=$(echo "\033[36m")
  9. RD=$(echo "\033[01;31m")
  10. BGN=$(echo "\033[4;92m")
  11. GN=$(echo "\033[1;92m")
  12. DGN=$(echo "\033[32m")
  13. CL=$(echo "\033[m")
  14. CM="${GN}✓${CL}"
  15. CROSS="${RD}✗${CL}"
  16. BFR="\\r\\033[K"
  17. HOLD="-"
  18. }
  19. catch_errors() {
  20. set -Eeuo pipefail
  21. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  22. }
  23. error_handler() {
  24. local exit_code="$?"
  25. local line_number="$1"
  26. local command="$2"
  27. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  28. echo -e "\n$error_message\n"
  29. }
  30. msg_info() {
  31. local msg="$1"
  32. echo -ne " ${HOLD} ${YW}${msg}..."
  33. }
  34. msg_ok() {
  35. local msg="$1"
  36. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  37. }
  38. msg_error() {
  39. local msg="$1"
  40. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  41. }
  42. pve_check() {
  43. if [ $(pveversion | grep -c "pve-manager/7\.[0-9]") -eq 0 ]; then
  44. echo -e "${CROSS} This version of Proxmox Virtual Environment is not supported"
  45. echo -e "Requires PVE Version 7.0 or higher"
  46. echo -e "Exiting..."
  47. sleep 2
  48. exit
  49. fi
  50. }
  51. arch_check() {
  52. if [ "$(dpkg --print-architecture)" != "amd64" ]; then
  53. echo -e "\n ${CROSS} This script will not work with PiMox! \n"
  54. echo -e "Exiting..."
  55. sleep 2
  56. exit
  57. fi
  58. }
  59. ssh_check() {
  60. if command -v pveversion >/dev/null 2>&1; then
  61. if [ -n "${SSH_CLIENT:+x}" ]; then
  62. if whiptail --defaultno --title "SSH DETECTED" --yesno "It's suggested to use the Proxmox shell instead of SSH, since SSH can create issues while gathering variables. Would you like to proceed with using SSH?" 10 62; then
  63. echo "you've been warned"
  64. else
  65. clear
  66. exit
  67. fi
  68. fi
  69. fi
  70. }
  71. echo_default() {
  72. echo -e "${DGN}Using Distribution: ${BGN}$var_os${CL}"
  73. echo -e "${DGN}Using $var_os Version: ${BGN}$var_version${CL}"
  74. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  75. echo -e "${DGN}Using Root Password: ${BGN}Automatic Login${CL}"
  76. echo -e "${DGN}Using Container ID: ${BGN}$NEXTID${CL}"
  77. echo -e "${DGN}Using Hostname: ${BGN}$NSAPP${CL}"
  78. echo -e "${DGN}Using Disk Size: ${BGN}$var_disk${CL}${DGN}GB${CL}"
  79. echo -e "${DGN}Allocated Cores ${BGN}$var_cpu${CL}"
  80. echo -e "${DGN}Allocated Ram ${BGN}$var_ram${CL}"
  81. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  82. echo -e "${DGN}Using Static IP Address: ${BGN}dhcp${CL}"
  83. echo -e "${DGN}Using Gateway Address: ${BGN}Default${CL}"
  84. echo -e "${DGN}Disable IPv6: ${BGN}No${CL}"
  85. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  86. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  87. echo -e "${DGN}Using DNS Server Address: ${BGN}Host${CL}"
  88. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  89. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  90. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  91. if [[ "$APP" == "Docker" || "$APP" == "Umbrel" || "$APP" == "CasaOS" || "$APP" == "Home Assistant" ]]; then
  92. echo -e "${DGN}Enable Fuse Overlayfs (ZFS): ${BGN}No${CL}"
  93. fi
  94. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  95. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  96. }
  97. exit-script() {
  98. clear
  99. echo -e "⚠ User exited script \n"
  100. exit
  101. }
  102. advanced_settings() {
  103. if [ "$var_os" != "alpine" ]; then
  104. var_os=""
  105. while [ -z "$var_os" ]; do
  106. if var_os=$(whiptail --title "DISTRIBUTION" --radiolist "Choose" 10 58 2 \
  107. "debian" "" OFF \
  108. "ubuntu" "" OFF \
  109. 3>&1 1>&2 2>&3); then
  110. if [ -n "$var_os" ]; then
  111. echo -e "${DGN}Using Distribution: ${BGN}$var_os${CL}"
  112. fi
  113. else
  114. exit-script
  115. fi
  116. done
  117. fi
  118. if [ "$var_os" == "debian" ]; then
  119. var_version="11"
  120. fi
  121. if [ "$var_os" == "ubuntu" ]; then
  122. var_version=""
  123. while [ -z "$var_version" ]; do
  124. if var_version=$(whiptail --title "UBUNTU VERSION" --radiolist "Choose Version" 10 58 3 \
  125. "20.04" "Focal" OFF \
  126. "22.04" "Jammy" OFF \
  127. "22.10" "Kinetic" OFF \
  128. 3>&1 1>&2 2>&3); then
  129. if [ -n "$var_version" ]; then
  130. echo -e "${DGN}Using Ubuntu Version: ${BGN}$var_version${CL}"
  131. fi
  132. else
  133. exit-script
  134. fi
  135. done
  136. fi
  137. CT_TYPE=""
  138. while [ -z "$CT_TYPE" ]; do
  139. if CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist "Choose Type" 10 58 2 \
  140. "1" "Unprivileged" OFF \
  141. "0" "Privileged" OFF \
  142. 3>&1 1>&2 2>&3); then
  143. if [ -n "$CT_TYPE" ]; then
  144. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  145. fi
  146. else
  147. exit-script
  148. fi
  149. done
  150. if PW1=$(whiptail --inputbox "\nSet Root Password (needed for root ssh access)" 9 58 --title "PASSWORD(leave blank for automatic login)" 3>&1 1>&2 2>&3); then
  151. if [ -z $PW1 ]; then
  152. PW1="Automatic Login"
  153. PW=" "
  154. else
  155. PW="-password $PW1"
  156. fi
  157. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  158. else
  159. exit-script
  160. fi
  161. if CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" 3>&1 1>&2 2>&3); then
  162. if [ -z "$CT_ID" ]; then
  163. CT_ID="$NEXTID"
  164. echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"
  165. else
  166. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  167. fi
  168. else
  169. exit
  170. fi
  171. if CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" 3>&1 1>&2 2>&3); then
  172. if [ -z "$CT_NAME" ]; then
  173. HN="$NSAPP"
  174. else
  175. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  176. fi
  177. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  178. else
  179. exit-script
  180. fi
  181. if DISK_SIZE=$(whiptail --inputbox "Set Disk Size in GB" 8 58 $var_disk --title "DISK SIZE" 3>&1 1>&2 2>&3); then
  182. if [ -z "$DISK_SIZE" ]; then
  183. DISK_SIZE="$var_disk"
  184. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  185. else
  186. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  187. echo -e "${RD}⚠ DISK SIZE MUST BE AN INTEGER NUMBER!${CL}"
  188. advanced_settings
  189. fi
  190. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  191. fi
  192. else
  193. exit-script
  194. fi
  195. if CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" 3>&1 1>&2 2>&3); then
  196. if [ -z "$CORE_COUNT" ]; then
  197. CORE_COUNT="$var_cpu"
  198. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  199. else
  200. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  201. fi
  202. else
  203. exit-script
  204. fi
  205. if RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" 3>&1 1>&2 2>&3); then
  206. if [ -z "$RAM_SIZE" ]; then
  207. RAM_SIZE="$var_ram"
  208. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  209. else
  210. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  211. fi
  212. else
  213. exit-script
  214. fi
  215. if BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" 3>&1 1>&2 2>&3); then
  216. if [ -z "$BRG" ]; then
  217. BRG="vmbr0"
  218. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  219. else
  220. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  221. fi
  222. else
  223. exit-script
  224. fi
  225. if NET=$(whiptail --inputbox "Set a Static IPv4 CIDR Address(/24)" 8 58 dhcp --title "IP ADDRESS" 3>&1 1>&2 2>&3); then
  226. if [ -z $NET ]; then
  227. NET="dhcp"
  228. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  229. else
  230. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  231. fi
  232. else
  233. exit-script
  234. fi
  235. if GATE1=$(whiptail --inputbox "Set a Gateway IP (mandatory if Static IP was used)" 8 58 --title "GATEWAY IP" 3>&1 1>&2 2>&3); then
  236. if [ -z $GATE1 ]; then
  237. GATE1="Default"
  238. GATE=""
  239. else
  240. GATE=",gw=$GATE1"
  241. fi
  242. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  243. else
  244. exit-script
  245. fi
  246. if (whiptail --defaultno --title "IPv6" --yesno "Disable IPv6?" 10 58); then
  247. DISABLEIP6="yes"
  248. else
  249. DISABLEIP6="no"
  250. fi
  251. echo -e "${DGN}Disable IPv6: ${BGN}$DISABLEIP6${CL}"
  252. if MTU1=$(whiptail --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" 3>&1 1>&2 2>&3); then
  253. if [ -z $MTU1 ]; then
  254. MTU1="Default"
  255. MTU=""
  256. else
  257. MTU=",mtu=$MTU1"
  258. fi
  259. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  260. else
  261. exit-script
  262. fi
  263. if SD=$(whiptail --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 --title "DNS Search Domain" 3>&1 1>&2 2>&3); then
  264. if [ -z $SD ]; then
  265. SX=Host
  266. SD=""
  267. else
  268. SX=$SD
  269. SD="-searchdomain=$SD"
  270. fi
  271. echo -e "${DGN}Using DNS Search Domain: ${BGN}$SX${CL}"
  272. else
  273. exit-script
  274. fi
  275. if NX=$(whiptail --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 --title "DNS SERVER IP" 3>&1 1>&2 2>&3); then
  276. if [ -z $NX ]; then
  277. NX=Host
  278. NS=""
  279. else
  280. NS="-nameserver=$NX"
  281. fi
  282. echo -e "${DGN}Using DNS Server IP Address: ${BGN}$NX${CL}"
  283. else
  284. exit-script
  285. fi
  286. if MAC1=$(whiptail --inputbox "Set a MAC Address(leave blank for default)" 8 58 --title "MAC ADDRESS" 3>&1 1>&2 2>&3); then
  287. if [ -z $MAC1 ]; then
  288. MAC1="Default"
  289. MAC=""
  290. else
  291. MAC=",hwaddr=$MAC1"
  292. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  293. fi
  294. else
  295. exit-script
  296. fi
  297. if VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" 3>&1 1>&2 2>&3); then
  298. if [ -z $VLAN1 ]; then
  299. VLAN1="Default"
  300. VLAN=""
  301. else
  302. VLAN=",tag=$VLAN1"
  303. fi
  304. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  305. else
  306. exit-script
  307. fi
  308. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  309. SSH="yes"
  310. else
  311. SSH="no"
  312. fi
  313. echo -e "${DGN}Enable Root SSH Access: ${BGN}$SSH${CL}"
  314. if [[ "$APP" == "Docker" || "$APP" == "Umbrel" || "$APP" == "CasaOS" || "$APP" == "Home Assistant" ]]; then
  315. if (whiptail --defaultno --title "FUSE OVERLAYFS" --yesno "(ZFS) Enable Fuse Overlayfs?" 10 58); then
  316. FUSE="yes"
  317. else
  318. FUSE="no"
  319. fi
  320. echo -e "${DGN}Enable Fuse Overlayfs (ZFS): ${BGN}$FUSE${CL}"
  321. fi
  322. if (whiptail --defaultno --title "VERBOSE MODE" --yesno "Enable Verbose Mode?" 10 58); then
  323. VERB="yes"
  324. else
  325. VERB="no"
  326. fi
  327. echo -e "${DGN}Enable Verbose Mode: ${BGN}$VERB${CL}"
  328. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" 10 58); then
  329. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  330. else
  331. clear
  332. header_info
  333. echo -e "${RD}Using Advanced Settings${CL}"
  334. advanced_settings
  335. fi
  336. }
  337. install_script() {
  338. ssh_check
  339. arch_check
  340. pve_check
  341. NEXTID=$(pvesh get /cluster/nextid)
  342. timezone=$(cat /etc/timezone)
  343. header_info
  344. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  345. header_info
  346. echo -e "${BL}Using Default Settings${CL}"
  347. default_settings
  348. else
  349. header_info
  350. echo -e "${RD}Using Advanced Settings${CL}"
  351. advanced_settings
  352. fi
  353. }
  354. start() {
  355. if command -v pveversion >/dev/null 2>&1; then
  356. if ! (whiptail --title "${APP} LXC" --yesno "This will create a New ${APP} LXC. Proceed?" 10 58); then
  357. clear
  358. echo -e "⚠ User exited script \n"
  359. exit
  360. fi
  361. install_script
  362. fi
  363. if ! command -v pveversion >/dev/null 2>&1; then
  364. if ! (whiptail --title "${APP} LXC UPDATE" --yesno "This will update ${APP} LXC. Proceed?" 10 58); then
  365. clear
  366. echo -e "⚠ User exited script \n"
  367. exit
  368. fi
  369. update_script
  370. fi
  371. }
  372. build_container() {
  373. if [ "$VERB" == "yes" ]; then set -x; fi
  374. if [[ "$APP" == "Docker" || "$APP" == "Umbrel" || "$APP" == "CasaOS" || "$APP" == "Home Assistant" ]]; then
  375. if [ "$FUSE" == "yes" ]; then
  376. FEATURES="fuse=1,keyctl=1,nesting=1"
  377. else
  378. FEATURES="keyctl=1,nesting=1"
  379. fi
  380. fi
  381. if [[ "$APP" != "Docker" && "$APP" != "Umbrel" && "$APP" != "CasaOS" && "$APP" != "Home Assistant" ]]; then
  382. if [ "$CT_TYPE" == "1" ]; then
  383. FEATURES="keyctl=1,nesting=1"
  384. else
  385. FEATURES="nesting=1"
  386. fi
  387. fi
  388. TEMP_DIR=$(mktemp -d)
  389. pushd $TEMP_DIR >/dev/null
  390. if [ "$var_os" == "alpine" ]; then
  391. export FUNCTIONS_FILE_PATH="$(curl -s https://raw.githubusercontent.com/tteck/Proxmox/main/misc/alpine-install.func)"
  392. else
  393. export FUNCTIONS_FILE_PATH="$(curl -s https://raw.githubusercontent.com/tteck/Proxmox/main/misc/install.func)"
  394. fi
  395. export tz="$timezone"
  396. if [[ "$APP" == "Docker" || "$APP" == "Umbrel" || "$APP" == "CasaOS" || "$APP" == "Home Assistant" ]]; then
  397. export ST="$FUSE"
  398. fi
  399. export DISABLEIPV6="$DISABLEIP6"
  400. export APPLICATION="$APP"
  401. export VERBOSE="$VERB"
  402. export SSH_ROOT="${SSH}"
  403. export CTID="$CT_ID"
  404. export PCT_OSTYPE="$var_os"
  405. export PCT_OSVERSION="$var_version"
  406. export PCT_DISK_SIZE="$DISK_SIZE"
  407. export PCT_OPTIONS="
  408. -features $FEATURES
  409. -hostname $HN
  410. $SD
  411. $NS
  412. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN$MTU
  413. -onboot 1
  414. -cores $CORE_COUNT
  415. -memory $RAM_SIZE
  416. -unprivileged $CT_TYPE
  417. $PW
  418. "
  419. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  420. LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
  421. if [ "$CT_TYPE" == "0" ]; then
  422. if [[ "$APP" != "Emby" && "$APP" != "Jellyfin" && "$APP" != "Plex" ]]; then
  423. cat <<EOF >>$LXC_CONFIG
  424. lxc.cgroup2.devices.allow: a
  425. lxc.cap.drop:
  426. lxc.cgroup2.devices.allow: c 188:* rwm
  427. lxc.cgroup2.devices.allow: c 189:* rwm
  428. lxc.mount.entry: /dev/serial/by-id dev/serial/by-id none bind,optional,create=dir
  429. lxc.mount.entry: /dev/ttyUSB0 dev/ttyUSB0 none bind,optional,create=file
  430. lxc.mount.entry: /dev/ttyUSB1 dev/ttyUSB1 none bind,optional,create=file
  431. lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
  432. lxc.mount.entry: /dev/ttyACM1 dev/ttyACM1 none bind,optional,create=file
  433. EOF
  434. fi
  435. fi
  436. if [ "$CT_TYPE" == "0" ]; then
  437. if [[ "$APP" == "Emby" || "$APP" == "Jellyfin" || "$APP" == "Plex" ]]; then
  438. cat <<EOF >>$LXC_CONFIG
  439. lxc.cgroup2.devices.allow: a
  440. lxc.cap.drop:
  441. lxc.cgroup2.devices.allow: c 226:0 rwm
  442. lxc.cgroup2.devices.allow: c 226:128 rwm
  443. lxc.cgroup2.devices.allow: c 29:0 rwm
  444. lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file
  445. lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
  446. lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file
  447. EOF
  448. fi
  449. fi
  450. msg_info "Starting LXC Container"
  451. pct start "$CTID"
  452. msg_ok "Started LXC Container"
  453. if [ "$var_os" == "alpine" ]; then
  454. sleep 2
  455. pct exec "$CTID" -- ash -c "apk add bash >/dev/null"
  456. fi
  457. lxc-attach -n "$CTID" -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/dev/install/$var_install.sh)" || exit
  458. }
  459. description() {
  460. IP=$(pct exec "$CTID" ip a s dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1)
  461. pct set "$CTID" -description "# ${APP} LXC
  462. ### https://tteck.github.io/Proxmox/
  463. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  464. }