docker-v5.sh 13 KB

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