mariadb-v4.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/env bash
  2. echo -e "Loading..."
  3. APP="MariaDB"
  4. var_disk="4"
  5. var_cpu="1"
  6. var_ram="1024"
  7. var_os="debian"
  8. var_version="11"
  9. NSAPP=$(echo ${APP,,} | tr -d ' ')
  10. var_install="${NSAPP}-install"
  11. NEXTID=$(pvesh get /cluster/nextid)
  12. INTEGER='^[0-9]+$'
  13. YW=$(echo "\033[33m")
  14. BL=$(echo "\033[36m")
  15. RD=$(echo "\033[01;31m")
  16. BGN=$(echo "\033[4;92m")
  17. GN=$(echo "\033[1;92m")
  18. DGN=$(echo "\033[32m")
  19. CL=$(echo "\033[m")
  20. BFR="\\r\\033[K"
  21. HOLD="-"
  22. CM="${GN}✓${CL}"
  23. set -o errexit
  24. set -o errtrace
  25. set -o nounset
  26. set -o pipefail
  27. shopt -s expand_aliases
  28. alias die='EXIT=$? LINE=$LINENO error_exit'
  29. trap die ERR
  30. function error_exit() {
  31. trap - ERR
  32. local reason="Unknown failure occurred."
  33. local msg="${1:-$reason}"
  34. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  35. echo -e "$flag $msg" 1>&2
  36. exit $EXIT
  37. }
  38. if (whiptail --title "${APP} LXC" --yesno "This will create a New ${APP} LXC. Proceed?" 10 58); then
  39. echo "User selected Yes"
  40. else
  41. clear
  42. echo -e "⚠ User exited script \n"
  43. exit
  44. fi
  45. function header_info {
  46. echo -e "${BL}
  47. __ ___ _ ____ ____
  48. / |/ /___ ______(_)___ _/ __ \/ __ )
  49. / /|_/ / __ / ___/ / __ / / / / __ |
  50. / / / / /_/ / / / / /_/ / /_/ / /_/ /
  51. /_/ /_/\__,_/_/v4/_/\__,_/_____/_____/
  52. ${CL}"
  53. }
  54. function msg_info() {
  55. local msg="$1"
  56. echo -ne " ${HOLD} ${YW}${msg}..."
  57. }
  58. function msg_ok() {
  59. local msg="$1"
  60. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  61. }
  62. function PVE_CHECK() {
  63. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  64. if [[ $PVE != 1 ]]; then
  65. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  66. echo -e "Exiting..."
  67. sleep 2
  68. exit
  69. fi
  70. }
  71. function default_settings() {
  72. echo -e "${DGN}Using Container Type: ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  73. CT_TYPE="1"
  74. echo -e "${DGN}Using Root Password: ${BGN}Automatic Login${CL}"
  75. PW=""
  76. echo -e "${DGN}Using Container ID: ${BGN}$NEXTID${CL}"
  77. CT_ID=$NEXTID
  78. echo -e "${DGN}Using Hostname: ${BGN}$NSAPP${CL}"
  79. HN=$NSAPP
  80. echo -e "${DGN}Using Disk Size: ${BGN}$var_disk${CL}${DGN}GB${CL}"
  81. DISK_SIZE="$var_disk"
  82. echo -e "${DGN}Allocated Cores ${BGN}$var_cpu${CL}"
  83. CORE_COUNT="$var_cpu"
  84. echo -e "${DGN}Allocated Ram ${BGN}$var_ram${CL}"
  85. RAM_SIZE="$var_ram"
  86. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  87. BRG="vmbr0"
  88. echo -e "${DGN}Using Static IP Address: ${BGN}dhcp${CL}"
  89. NET=dhcp
  90. echo -e "${DGN}Using Gateway Address: ${BGN}Default${CL}"
  91. GATE=""
  92. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  93. SD=""
  94. echo -e "${DGN}Using DNS Server Address: ${BGN}Host${CL}"
  95. NS=""
  96. echo -e "${DGN}Using MAC Address: ${BGN}Default${CL}"
  97. MAC=""
  98. echo -e "${DGN}Using VLAN Tag: ${BGN}Default${CL}"
  99. VLAN=""
  100. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  101. SSH="no"
  102. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  103. VERB="no"
  104. echo -e "${BL}Creating a ${APP} LXC using the above default settings${CL}"
  105. }
  106. function advanced_settings() {
  107. CT_TYPE=$(whiptail --title "CONTAINER TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 10 58 2 \
  108. "1" "Unprivileged" ON \
  109. "0" "Privileged" OFF \
  110. 3>&1 1>&2 2>&3)
  111. exitstatus=$?
  112. if [ $exitstatus = 0 ]; then
  113. echo -e "${DGN}Using Container Type: ${BGN}$CT_TYPE${CL}"
  114. fi
  115. PW1=$(whiptail --inputbox "Set Root Password (needed for root ssh access)" 8 58 --title "PASSWORD(leave blank for automatic login)" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  116. exitstatus=$?
  117. if [ $exitstatus = 0 ]; then
  118. if [ -z $PW1 ]; then
  119. PW1="Automatic Login" PW=" "
  120. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  121. else
  122. PW="-password $PW1"
  123. echo -e "${DGN}Using Root Password: ${BGN}$PW1${CL}"
  124. fi
  125. fi
  126. CT_ID=$(whiptail --inputbox "Set Container ID" 8 58 $NEXTID --title "CONTAINER ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  127. exitstatus=$?
  128. if [ -z $CT_ID ]; then
  129. CT_ID="$NEXTID"
  130. echo -e "${DGN}Container ID: ${BGN}$CT_ID${CL}"
  131. else
  132. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Container ID: ${BGN}$CT_ID${CL}"; fi
  133. fi
  134. CT_NAME=$(whiptail --inputbox "Set Hostname" 8 58 $NSAPP --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  135. exitstatus=$?
  136. if [ -z $CT_NAME ]; then
  137. HN="$NSAPP"
  138. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  139. else
  140. if [ $exitstatus = 0 ]; then
  141. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  142. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  143. fi
  144. fi
  145. DISK_SIZE=$(whiptail --inputbox "Set Disk Size in GB" 8 58 $var_disk --title "DISK SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  146. exitstatus=$?
  147. if [ -z $DISK_SIZE ]; then
  148. DISK_SIZE="$var_disk"
  149. echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"
  150. else
  151. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Disk Size: ${BGN}$DISK_SIZE${CL}"; fi
  152. if ! [[ $DISK_SIZE =~ $INTEGER ]]; then
  153. echo -e "${RD}⚠ DISK SIZE MUST BE A INTEGER NUMBER!${CL}"
  154. advanced_settings
  155. fi
  156. fi
  157. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 $var_cpu --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  158. exitstatus=$?
  159. if [ -z $CORE_COUNT ]; then
  160. CORE_COUNT="$var_cpu"
  161. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  162. else
  163. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi
  164. fi
  165. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 $var_ram --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  166. exitstatus=$?
  167. if [ -z $RAM_SIZE ]; then
  168. RAM_SIZE="$var_ram"
  169. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  170. else
  171. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi
  172. fi
  173. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  174. exitstatus=$?
  175. if [ -z $BRG ]; then
  176. BRG="vmbr0"
  177. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  178. else
  179. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"; fi
  180. fi
  181. NET=$(whiptail --inputbox "Set a Static IPv4 CIDR Address(/24)" 8 58 dhcp --title "IP ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  182. exitstatus=$?
  183. if [ -z $NET ]; then
  184. NET="dhcp"
  185. echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"
  186. else
  187. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using IP Address: ${BGN}$NET${CL}"; fi
  188. fi
  189. GATE1=$(whiptail --inputbox "Set a Gateway IP (mandatory if Static IP was used)" 8 58 --title "GATEWAY IP" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  190. exitstatus=$?
  191. if [ $exitstatus = 0 ]; then
  192. if [ -z $GATE1 ]; then
  193. GATE1="Default" GATE=""
  194. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  195. else
  196. GATE=",gw=$GATE1"
  197. echo -e "${DGN}Using Gateway IP Address: ${BGN}$GATE1${CL}"
  198. fi
  199. fi
  200. SD=$(whiptail --inputbox "Set a DNS Search Domain (leave blank for HOST)" 8 58 --title "DNS Search Domain" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  201. exitstatus=$?
  202. if [ $exitstatus = 0 ]; then
  203. if [ -z $SD ]; then
  204. SD=""
  205. echo -e "${DGN}Using DNS Search Domain: ${BGN}Host${CL}"
  206. else
  207. SX=$SD
  208. SD="-searchdomain=$SD"
  209. echo -e "${DGN}Using DNS Search Domain: ${BGN}$SX${CL}"
  210. fi
  211. fi
  212. NS=$(whiptail --inputbox "Set a DNS Server IP (leave blank for HOST)" 8 58 --title "DNS SERVER IP" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  213. exitstatus=$?
  214. if [ $exitstatus = 0 ]; then
  215. if [ -z $NS ]; then
  216. NS=""
  217. echo -e "${DGN}Using DNS Server IP Address: ${BGN}Host${CL}"
  218. else
  219. NX=$NS
  220. NS="-nameserver=$NS"
  221. echo -e "${DGN}Using DNS Server IP Address: ${BGN}$NX${CL}"
  222. fi
  223. fi
  224. MAC1=$(whiptail --inputbox "Set a MAC Address(leave blank for default)" 8 58 --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  225. exitstatus=$?
  226. if [ $exitstatus = 0 ]; then
  227. if [ -z $MAC1 ]; then
  228. MAC1="Default" MAC=""
  229. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  230. else
  231. MAC=",hwaddr=$MAC1"
  232. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  233. fi
  234. fi
  235. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  236. exitstatus=$?
  237. if [ $exitstatus = 0 ]; then
  238. if [ -z $VLAN1 ]; then
  239. VLAN1="Default" VLAN=""
  240. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  241. else
  242. VLAN=",tag=$VLAN1"
  243. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  244. fi
  245. fi
  246. if (whiptail --defaultno --title "SSH ACCESS" --yesno "Enable Root SSH Access?" 10 58); then
  247. echo -e "${DGN}Enable Root SSH Access: ${BGN}Yes${CL}"
  248. SSH="yes"
  249. else
  250. echo -e "${DGN}Enable Root SSH Access: ${BGN}No${CL}"
  251. SSH="no"
  252. fi
  253. if (whiptail --defaultno --title "VERBOSE MODE" --yesno "Enable Verbose Mode?" 10 58); then
  254. echo -e "${DGN}Enable Verbose Mode: ${BGN}Yes${CL}"
  255. VERB="yes"
  256. else
  257. echo -e "${DGN}Enable Verbose Mode: ${BGN}No${CL}"
  258. VERB="no"
  259. fi
  260. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create ${APP} LXC?" --no-button Do-Over 10 58); then
  261. echo -e "${RD}Creating a ${APP} LXC using the above advanced settings${CL}"
  262. else
  263. clear
  264. header_info
  265. echo -e "${RD}Using Advanced Settings${CL}"
  266. advanced_settings
  267. fi
  268. }
  269. function start_script() {
  270. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  271. header_info
  272. echo -e "${BL}Using Default Settings${CL}"
  273. default_settings
  274. else
  275. header_info
  276. echo -e "${RD}Using Advanced Settings${CL}"
  277. advanced_settings
  278. fi
  279. }
  280. clear
  281. start_script
  282. if [ "$VERB" == "yes" ]; then set -x; fi
  283. if [ "$CT_TYPE" == "1" ]; then
  284. FEATURES="nesting=1,keyctl=1"
  285. else
  286. FEATURES="nesting=1"
  287. fi
  288. TEMP_DIR=$(mktemp -d)
  289. pushd $TEMP_DIR >/dev/null
  290. export VERBOSE=$VERB
  291. export SSH_ROOT=${SSH}
  292. export CTID=$CT_ID
  293. export PCT_OSTYPE=$var_os
  294. export PCT_OSVERSION=$var_version
  295. export PCT_DISK_SIZE=$DISK_SIZE
  296. export PCT_OPTIONS="
  297. -features $FEATURES
  298. -hostname $HN
  299. $SD
  300. $NS
  301. -net0 name=eth0,bridge=$BRG$MAC,ip=$NET$GATE$VLAN
  302. -onboot 1
  303. -cores $CORE_COUNT
  304. -memory $RAM_SIZE
  305. -unprivileged $CT_TYPE
  306. $PW
  307. "
  308. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  309. msg_info "Starting LXC Container"
  310. pct start $CTID
  311. msg_ok "Started LXC Container"
  312. lxc-attach -n $CTID -- bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/install/$var_install.sh)" || exit
  313. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  314. pct set $CTID -description "# ${APP} LXC
  315. ### https://tteck.github.io/Proxmox/
  316. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/☕-Buy me a coffee-red' /></a>"
  317. msg_ok "Completed Successfully!\n"