debian-v3.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. APP="Debian"
  15. NSAPP=$(echo ${APP,,} | tr -d ' ')
  16. while true; do
  17. read -p "This will create a New ${APP} LXC. Proceed(y/n)?" yn
  18. case $yn in
  19. [Yy]* ) break;;
  20. [Nn]* ) exit;;
  21. * ) echo "Please answer yes or no.";;
  22. esac
  23. done
  24. clear
  25. function header_info {
  26. echo -e "${RD}
  27. _____ _ _
  28. | __ \ | | (_)
  29. | | | | ___| |__ _ __ _ _ __
  30. | | | |/ _ \ _ \| |/ _ | _ \
  31. | |__| | __/ |_) | | (_| | | | |
  32. |_${YW}v3${RD}__/ \___|_.__/|_|\__,_|_| |_|
  33. ${CL}"
  34. }
  35. header_info
  36. function msg_info() {
  37. local msg="$1"
  38. echo -ne " ${HOLD} ${YW}${msg}..."
  39. }
  40. function msg_ok() {
  41. local msg="$1"
  42. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  43. }
  44. function PVE_CHECK() {
  45. PVE=$(pveversion | grep "pve-manager/7" | wc -l)
  46. if [[ $PVE != 1 ]]; then
  47. echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
  48. echo -e "Exiting..."
  49. sleep 2
  50. exit
  51. fi
  52. }
  53. function default_settings() {
  54. clear
  55. header_info
  56. echo -e "${BL}Using Default Settings${CL}"
  57. echo -e "${DGN}Using CT Type ${BGN}Unprivileged${CL} ${RD}NO DEVICE PASSTHROUGH${CL}"
  58. CT_TYPE="1"
  59. echo -e "${DGN}Using CT Password ${BGN}Automatic Login${CL}"
  60. PW=" "
  61. echo -e "${DGN}Using ID ${BGN}$NEXTID${CL}"
  62. CT_ID=$NEXTID
  63. echo -e "${DGN}Using CT Name ${BGN}$NSAPP${CL}"
  64. HN=$NSAPP
  65. echo -e "${DGN}Using Disk Size ${BGN}2GB${CL}"
  66. DISK_SIZE="2"
  67. echo -e "${DGN}Using ${BGN}1vCPU${CL}"
  68. CORE_COUNT="1"
  69. echo -e "${DGN}Using ${BGN}512MiB${CL}${GN} RAM${CL}"
  70. RAM_SIZE="512"
  71. echo -e "${DGN}Using IP Address ${BGN}DHCP${CL}"
  72. NET=dhcp
  73. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  74. VLAN=" "
  75. }
  76. function advanced_settings() {
  77. clear
  78. header_info
  79. echo -e "${RD}Using Advanced Settings${CL}"
  80. echo -e "${YW}Type Privileged, or Press [ENTER] for Default: Unprivileged (${RD}NO DEVICE PASSTHROUGH${CL}${YW})"
  81. read CT_TYPE1
  82. if [ -z $CT_TYPE1 ]; then CT_TYPE1="Unprivileged" CT_TYPE="1";
  83. echo -en "${DGN}Set CT Type ${BL}$CT_TYPE1${CL}"
  84. else
  85. CT_TYPE1="Privileged"
  86. CT_TYPE="0"
  87. echo -en "${DGN}Set CT Type ${BL}Privileged${CL}"
  88. fi;
  89. echo -e " ${CM}${CL} \r"
  90. sleep 1
  91. clear
  92. header_info
  93. echo -e "${RD}Using Advanced Settings${CL}"
  94. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  95. echo -e "${YW}Set Password, or Press [ENTER] for Default: Automatic Login "
  96. read PW1
  97. if [ -z $PW1 ]; then PW1="Automatic Login" PW=" ";
  98. echo -en "${DGN}Set CT ${BL}$PW1${CL}"
  99. else
  100. PW="-password $PW1"
  101. echo -en "${DGN}Set CT Password ${BL}$PW1${CL}"
  102. fi;
  103. echo -e " ${CM}${CL} \r"
  104. sleep 1
  105. clear
  106. header_info
  107. echo -e "${RD}Using Advanced Settings${CL}"
  108. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  109. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  110. echo -e "${YW}Enter the CT ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  111. read CT_ID
  112. if [ -z $CT_ID ]; then CT_ID=$NEXTID; fi;
  113. echo -en "${DGN}Set CT ID To ${BL}$CT_ID${CL}"
  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 "${DGN}Using CT Password ${BGN}$PW1${CL}"
  121. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  122. echo -e "${YW}Enter CT Name (no-spaces), or Press [ENTER] for Default: $NSAPP "
  123. read CT_NAME
  124. if [ -z $CT_NAME ]; then
  125. HN=$NSAPP
  126. else
  127. HN=$(echo ${CT_NAME,,} | tr -d ' ')
  128. fi
  129. echo -en "${DGN}Set CT Name To ${BL}$HN${CL}"
  130. echo -e " ${CM}${CL} \r"
  131. sleep 1
  132. clear
  133. header_info
  134. echo -e "${RD}Using Advanced Settings${CL}"
  135. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  136. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  137. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  138. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  139. echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 2Gb "
  140. read DISK_SIZE
  141. if [ -z $DISK_SIZE ]; then DISK_SIZE="2"; fi;
  142. if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
  143. echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${CL}"
  144. echo -e " ${CM}${CL} \r"
  145. sleep 1
  146. clear
  147. header_info
  148. echo -e "${RD}Using Advanced Settings${CL}"
  149. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  150. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  151. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  152. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  153. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  154. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 1 "
  155. read CORE_COUNT
  156. if [ -z $CORE_COUNT ]; then CORE_COUNT="1"; fi;
  157. echo -en "${DGN}Set Cores To ${BL}$CORE_COUNT${CL}"
  158. echo -e " ${CM}${CL} \r"
  159. sleep 1
  160. clear
  161. header_info
  162. echo -e "${RD}Using Advanced Settings${CL}"
  163. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  164. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  165. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  166. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  167. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  168. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  169. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 512 "
  170. read RAM_SIZE
  171. if [ -z $RAM_SIZE ]; then RAM_SIZE="512"; fi;
  172. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}"
  173. echo -e " ${CM}${CL} \n"
  174. sleep 1
  175. clear
  176. header_info
  177. echo -e "${RD}Using Advanced Settings${CL}"
  178. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  179. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  180. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  181. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  182. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  183. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  184. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  185. echo -e "${YW}Enter a IP Address, or Press [ENTER] for Default: DHCP "
  186. read NET
  187. if [ -z $NET ]; then NET="dhcp"; fi;
  188. echo -en "${DGN}Set IP Address To ${BL}$NET${CL}"
  189. echo -e " ${CM}${CL} \n"
  190. sleep 1
  191. clear
  192. header_info
  193. echo -e "${RD}Using Advanced Settings${CL}"
  194. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  195. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  196. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  197. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  198. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  199. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  200. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  201. echo -e "${DGN}Using IP Address ${BGN}$NET${CL}"
  202. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  203. read VLAN1
  204. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN=" ";
  205. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  206. else
  207. VLAN="-tag $VLAN1"
  208. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  209. fi;
  210. echo -e " ${CM}${CL} \n"
  211. sleep 1
  212. clear
  213. header_info
  214. echo -e "${RD}Using Advanced Settings${CL}"
  215. echo -e "${DGN}Using CT Type ${BGN}$CT_TYPE1${CL}"
  216. echo -e "${DGN}Using CT Password ${BGN}$PW1${CL}"
  217. echo -e "${DGN}Using ID ${BGN}$CT_ID${CL}"
  218. echo -e "${DGN}Using CT Name ${BGN}$HN${CL}"
  219. echo -e "${DGN}Using Disk Size ${BGN}$DISK_SIZE${CL}"
  220. echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
  221. echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${GN} RAM${CL}"
  222. echo -e "${DGN}Using IP Address ${BGN}$NET${CL}"
  223. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  224. read -p "Are these settings correct(y/n)? " -n 1 -r
  225. echo
  226. if [[ ! $REPLY =~ ^[Yy]$ ]]
  227. then
  228. advanced_settings
  229. fi
  230. }
  231. function start_script() {
  232. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  233. read SETTINGS
  234. if [ -z $SETTINGS ]; then default_settings;
  235. else
  236. advanced_settings
  237. fi;
  238. }
  239. start_script
  240. set -o errexit
  241. set -o errtrace
  242. set -o nounset
  243. set -o pipefail
  244. shopt -s expand_aliases
  245. alias die='EXIT=$? LINE=$LINENO error_exit'
  246. trap die ERR
  247. trap cleanup EXIT
  248. function error_exit() {
  249. trap - ERR
  250. local DEFAULT='Unknown failure occured.'
  251. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  252. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  253. msg "$FLAG $REASON"
  254. [ ! -z ${CTID-} ] && cleanup_ctid
  255. exit $EXIT
  256. }
  257. function warn() {
  258. local REASON="\e[97m$1\e[39m"
  259. local FLAG="\e[93m[WARNING]\e[39m"
  260. msg "$FLAG $REASON"
  261. }
  262. function info() {
  263. local REASON="$1"
  264. local FLAG="\e[36m[INFO]\e[39m"
  265. msg "$FLAG $REASON"
  266. }
  267. function msg() {
  268. local TEXT="$1"
  269. echo -e "$TEXT"
  270. }
  271. function cleanup_ctid() {
  272. if $(pct status $CTID &>/dev/null); then
  273. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  274. pct stop $CTID
  275. fi
  276. pct destroy $CTID
  277. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  278. pvesm free $ROOTFS
  279. fi
  280. }
  281. function cleanup() {
  282. popd >/dev/null
  283. rm -rf $TEMP_DIR
  284. }
  285. if [ "$CT_TYPE" == "1" ]; then
  286. FEATURES="nesting=1,keyctl=1"
  287. else
  288. FEATURES="nesting=1"
  289. fi
  290. TEMP_DIR=$(mktemp -d)
  291. pushd $TEMP_DIR >/dev/null
  292. export CTID=$CT_ID
  293. export PCT_OSTYPE=debian
  294. export PCT_OSVERSION=11
  295. export PCT_DISK_SIZE=$DISK_SIZE
  296. export PCT_OPTIONS="
  297. -features $FEATURES
  298. -hostname $HN
  299. -net0 name=eth0,bridge=vmbr0,ip=$NET
  300. $VLAN
  301. -onboot 1
  302. -cores $CORE_COUNT
  303. -memory $RAM_SIZE
  304. -unprivileged $CT_TYPE
  305. $PW
  306. "
  307. bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/ct/create_lxc.sh)" || exit
  308. STORAGE_TYPE=$(pvesm status -storage $(pct config $CTID | grep rootfs | awk -F ":" '{print $2}') | awk 'NR>1 {print $2}')
  309. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  310. warn "Some applications may not work properly due to ZFS not supporting 'fallocate'."
  311. fi
  312. msg_info "Starting LXC Container"
  313. pct start $CTID
  314. msg_ok "Started LXC Container"
  315. alias lxc-cmd="lxc-attach -n $CTID --"
  316. lxc-cmd bash -c "$(wget -qLO - https://raw.githubusercontent.com/tteck/Proxmox/main/setup/debian-install.sh)" || exit
  317. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  318. msg_ok "Completed Successfully!\n"