nextcloud-vm.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "\n Loading..."
  18. GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
  19. NEXTID=$(pvesh get /cluster/nextid)
  20. NAME="TurnKey Nexcloud VM"
  21. YW=$(echo "\033[33m")
  22. BL=$(echo "\033[36m")
  23. HA=$(echo "\033[1;34m")
  24. RD=$(echo "\033[01;31m")
  25. BGN=$(echo "\033[4;92m")
  26. GN=$(echo "\033[1;92m")
  27. DGN=$(echo "\033[32m")
  28. CL=$(echo "\033[m")
  29. BFR="\\r\\033[K"
  30. HOLD="-"
  31. CM="${GN}✓${CL}"
  32. CROSS="${RD}✗${CL}"
  33. THIN="discard=on,ssd=1,"
  34. set -e
  35. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  36. trap cleanup EXIT
  37. function error_handler() {
  38. local exit_code="$?"
  39. local line_number="$1"
  40. local command="$2"
  41. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  42. echo -e "\n$error_message\n"
  43. cleanup_vmid
  44. }
  45. function cleanup_vmid() {
  46. if qm status $VMID &>/dev/null; then
  47. qm stop $VMID &>/dev/null
  48. qm destroy $VMID &>/dev/null
  49. fi
  50. }
  51. function cleanup() {
  52. popd >/dev/null
  53. rm -rf $TEMP_DIR
  54. }
  55. TEMP_DIR=$(mktemp -d)
  56. pushd $TEMP_DIR >/dev/null
  57. if whiptail --backtitle "Proxmox VE Helper Scripts" --title "$NAME" --yesno "This will create a New $NAME. Proceed?" 10 58; then
  58. :
  59. else
  60. header_info && echo -e "⚠ User exited script \n" && exit
  61. fi
  62. function msg_info() {
  63. local msg="$1"
  64. echo -ne " ${HOLD} ${YW}${msg}..."
  65. }
  66. function msg_ok() {
  67. local msg="$1"
  68. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  69. }
  70. function msg_error() {
  71. local msg="$1"
  72. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  73. }
  74. function check_root() {
  75. if [[ "$(id -u)" -ne 0 || $(ps -o comm= -p $PPID) == "sudo" ]]; then
  76. clear
  77. msg_error "Please run this script as root."
  78. echo -e "\nExiting..."
  79. sleep 2
  80. exit
  81. fi
  82. }
  83. function pve_check() {
  84. if ! pveversion | grep -Eq "pve-manager/(7\.[2-9]|8\.[0-9])"; then
  85. msg_error "This version of Proxmox Virtual Environment is not supported"
  86. echo -e "Requires PVE Version 7.2 or higher"
  87. echo -e "Exiting..."
  88. sleep 2
  89. exit
  90. fi
  91. }
  92. function arch_check() {
  93. if [ "$(dpkg --print-architecture)" != "amd64" ]; then
  94. msg_error "This script will not work with PiMox! \n"
  95. echo -e "Exiting..."
  96. sleep 2
  97. exit
  98. fi
  99. }
  100. function ssh_check() {
  101. if command -v pveversion >/dev/null 2>&1; then
  102. if [ -n "${SSH_CLIENT:+x}" ]; then
  103. if whiptail --backtitle "Proxmox VE Helper Scripts" --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
  104. echo "you've been warned"
  105. else
  106. clear
  107. exit
  108. fi
  109. fi
  110. fi
  111. }
  112. function exit-script() {
  113. clear
  114. echo -e "⚠ User exited script \n"
  115. exit
  116. }
  117. function default_settings() {
  118. VMID="$NEXTID"
  119. FORMAT=",efitype=4m"
  120. MACHINE=""
  121. DISK_CACHE=""
  122. HN="turnkey-nextcloud-vm"
  123. CPU_TYPE=""
  124. CORE_COUNT="2"
  125. RAM_SIZE="2048"
  126. BRG="vmbr0"
  127. MAC="$GEN_MAC"
  128. VLAN=""
  129. MTU=""
  130. START_VM="no"
  131. echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
  132. echo -e "${DGN}Using Machine Type: ${BGN}i440fx${CL}"
  133. echo -e "${DGN}Using Disk Cache: ${BGN}None${CL}"
  134. echo -e "${DGN}Using Hostname: ${BGN}${HN}${CL}"
  135. echo -e "${DGN}Using CPU Model: ${BGN}KVM64${CL}"
  136. echo -e "${DGN}Allocated Cores: ${BGN}${CORE_COUNT}${CL}"
  137. echo -e "${DGN}Allocated RAM: ${BGN}${RAM_SIZE}${CL}"
  138. echo -e "${DGN}Using Bridge: ${BGN}${BRG}${CL}"
  139. echo -e "${DGN}Using MAC Address: ${BGN}${MAC}${CL}"
  140. echo -e "${DGN}Using VLAN: ${BGN}Default${CL}"
  141. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  142. echo -e "${DGN}Start VM when completed: ${BGN}no${CL}"
  143. echo -e "${BL}Creating a $NAME using the above default settings${CL}"
  144. }
  145. function advanced_settings() {
  146. while true; do
  147. if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  148. if [ -z "$VMID" ]; then
  149. VMID="$NEXTID"
  150. fi
  151. if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
  152. echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"
  153. sleep 2
  154. continue
  155. fi
  156. echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"
  157. break
  158. else
  159. exit-script
  160. fi
  161. done
  162. if MACH=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "MACHINE TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 10 58 2 \
  163. "i440fx" "Machine i440fx" ON \
  164. "q35" "Machine q35" OFF \
  165. 3>&1 1>&2 2>&3); then
  166. if [ $MACH = q35 ]; then
  167. echo -e "${DGN}Using Machine Type: ${BGN}$MACH${CL}"
  168. FORMAT=""
  169. MACHINE=" -machine q35"
  170. else
  171. echo -e "${DGN}Using Machine Type: ${BGN}$MACH${CL}"
  172. FORMAT=",efitype=4m"
  173. MACHINE=""
  174. fi
  175. else
  176. exit-script
  177. fi
  178. if DISK_CACHE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "DISK CACHE" --radiolist "Choose" --cancel-button Exit-Script 10 58 2 \
  179. "0" "None (Default)" ON \
  180. "1" "Write Through" OFF \
  181. 3>&1 1>&2 2>&3); then
  182. if [ $DISK_CACHE = "1" ]; then
  183. echo -e "${DGN}Using Disk Cache: ${BGN}Write Through${CL}"
  184. DISK_CACHE="cache=writethrough,"
  185. else
  186. echo -e "${DGN}Using Disk Cache: ${BGN}None${CL}"
  187. DISK_CACHE=""
  188. fi
  189. else
  190. exit-script
  191. fi
  192. if VM_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 $HN --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  193. if [ -z $VM_NAME ]; then
  194. HN="$HN"
  195. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  196. else
  197. HN=$(echo ${VM_NAME,,} | tr -d ' ')
  198. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  199. fi
  200. else
  201. exit-script
  202. fi
  203. if CPU_TYPE1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "CPU MODEL" --radiolist "Choose" --cancel-button Exit-Script 10 58 2 \
  204. "0" "KVM64 (Default)" ON \
  205. "1" "Host" OFF \
  206. 3>&1 1>&2 2>&3); then
  207. if [ $CPU_TYPE1 = "1" ]; then
  208. echo -e "${DGN}Using CPU Model: ${BGN}Host${CL}"
  209. CPU_TYPE=" -cpu host"
  210. else
  211. echo -e "${DGN}Using CPU Model: ${BGN}KVM64${CL}"
  212. CPU_TYPE=""
  213. fi
  214. else
  215. exit-script
  216. fi
  217. if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  218. if [ -z $CORE_COUNT ]; then
  219. CORE_COUNT="2"
  220. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  221. else
  222. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  223. fi
  224. else
  225. exit-script
  226. fi
  227. if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 2048 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  228. if [ -z $RAM_SIZE ]; then
  229. RAM_SIZE="2048"
  230. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  231. else
  232. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  233. fi
  234. else
  235. exit-script
  236. fi
  237. if BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  238. if [ -z $BRG ]; then
  239. BRG="vmbr0"
  240. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  241. else
  242. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  243. fi
  244. else
  245. exit-script
  246. fi
  247. if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 $GEN_MAC --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  248. if [ -z $MAC1 ]; then
  249. MAC="$GEN_MAC"
  250. echo -e "${DGN}Using MAC Address: ${BGN}$MAC${CL}"
  251. else
  252. MAC="$MAC1"
  253. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  254. fi
  255. else
  256. exit-script
  257. fi
  258. if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  259. if [ -z $VLAN1 ]; then
  260. VLAN1="Default"
  261. VLAN=""
  262. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  263. else
  264. VLAN=",tag=$VLAN1"
  265. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  266. fi
  267. else
  268. exit-script
  269. fi
  270. if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
  271. if [ -z $MTU1 ]; then
  272. MTU1="Default"
  273. MTU=""
  274. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  275. else
  276. MTU=",mtu=$MTU1"
  277. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  278. fi
  279. else
  280. exit-script
  281. fi
  282. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "START VIRTUAL MACHINE" --yesno "Start VM when completed?" 10 58); then
  283. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  284. START_VM="yes"
  285. else
  286. echo -e "${DGN}Start VM when completed: ${BGN}no${CL}"
  287. START_VM="no"
  288. fi
  289. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create a $NAME?" --no-button Do-Over 10 58); then
  290. echo -e "${RD}Creating a $NAME using the above advanced settings${CL}"
  291. else
  292. header_info
  293. echo -e "${RD}Using Advanced Settings${CL}"
  294. advanced_settings
  295. fi
  296. }
  297. function start_script() {
  298. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  299. header_info
  300. echo -e "${BL}Using Default Settings${CL}"
  301. default_settings
  302. else
  303. header_info
  304. echo -e "${RD}Using Advanced Settings${CL}"
  305. advanced_settings
  306. fi
  307. }
  308. check_root
  309. arch_check
  310. pve_check
  311. ssh_check
  312. start_script
  313. msg_info "Validating Storage"
  314. while read -r line; do
  315. TAG=$(echo $line | awk '{print $1}')
  316. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  317. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  318. ITEM=" Type: $TYPE Free: $FREE "
  319. OFFSET=2
  320. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  321. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  322. fi
  323. STORAGE_MENU+=("$TAG" "$ITEM" "OFF")
  324. done < <(pvesm status -content images | awk 'NR>1')
  325. VALID=$(pvesm status -content images | awk 'NR>1')
  326. if [ -z "$VALID" ]; then
  327. msg_error "Unable to detect a valid storage location."
  328. exit
  329. elif [ $((${#STORAGE_MENU[@]} / 3)) -eq 1 ]; then
  330. STORAGE=${STORAGE_MENU[0]}
  331. else
  332. while [ -z "${STORAGE:+x}" ]; do
  333. STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
  334. "Which storage pool you would like to use for ${HN}?\nTo make a selection, use the Spacebar.\n" \
  335. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  336. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  337. done
  338. fi
  339. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  340. msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
  341. msg_info "Retrieving the URL for the $NAME Disk Image"
  342. URL=http://mirror.turnkeylinux.org/turnkeylinux/images/iso/turnkey-nextcloud-18.0-bookworm-amd64.iso
  343. sleep 2
  344. msg_ok "${CL}${BL}${URL}${CL}"
  345. wget -q --show-progress $URL
  346. echo -en "\e[1A\e[0K"
  347. FILE=$(basename $URL)
  348. msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
  349. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  350. case $STORAGE_TYPE in
  351. nfs | dir)
  352. DISK_EXT=".raw"
  353. DISK_REF="$VMID/"
  354. DISK_IMPORT="-format raw"
  355. THIN=""
  356. ;;
  357. btrfs)
  358. DISK_EXT=".raw"
  359. DISK_REF="$VMID/"
  360. DISK_IMPORT="-format raw"
  361. FORMAT=",efitype=4m"
  362. THIN=""
  363. ;;
  364. esac
  365. for i in {0,1}; do
  366. disk="DISK$i"
  367. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  368. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  369. done
  370. msg_info "Creating a $NAME"
  371. qm create $VMID -agent 1${MACHINE} -tablet 0 -localtime 1 -bios seabios${CPU_TYPE} -cores $CORE_COUNT -memory $RAM_SIZE \
  372. -name $HN -tags proxmox-helper-scripts -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
  373. pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
  374. qm importdisk $VMID ${FILE} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
  375. qm set $VMID \
  376. -efidisk0 ${DISK0_REF}${FORMAT} \
  377. -scsi0 ${DISK1_REF},${DISK_CACHE}${THIN}size=12G \
  378. -boot order=scsi0 \
  379. -description "<div align='center'><a href='https://Helper-Scripts.com'><img src='https://raw.githubusercontent.com/tteck/Proxmox/main/misc/images/logo-81x112.png'/></a>
  380. # $NAME
  381. <a href='https://ko-fi.com/D1D7EP4GF'><img src='https://img.shields.io/badge/&#x2615;-Buy me a coffee-blue' /></a>
  382. </div>" >/dev/null
  383. msg_ok "Created a $NAME ${CL}${BL}(${HN})"
  384. if [ "$START_VM" == "yes" ]; then
  385. msg_info "Starting $NAME"
  386. qm start $VMID
  387. msg_ok "Started $NAME"
  388. fi
  389. msg_ok "Completed Successfully!\n"