haos-vm.sh 13 KB

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