archlinux.sh 11 KB

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