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