haos-vm.sh 14 KB

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