pimox-haos-vm.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. cat <<"EOF"
  8. ____ _ __ ___
  9. / __ \(_) |/ /___ _ __
  10. / /_/ / / /|_/ / __ \| |/_/
  11. / ____/ / / / / /_/ /> <
  12. __ __ /_/_ /_/_/ /_/\____/_/|_| __ ____ _____
  13. / / / /___ ____ ___ ___ / | __________(_)____/ /_____ _____ / /_ / __ \/ ___/
  14. / /_/ / __ \/ __ `__ \/ _ \ / /| | / ___/ ___/ / ___/ __/ __ `/ __ \/ __/ / / / /\__ \
  15. / __ / /_/ / / / / / / __/ / ___ |(__ |__ ) (__ ) /_/ /_/ / / / / /_ / /_/ /___/ /
  16. /_/ /_/\____/_/ /_/ /_/\___/ /_/ |_/____/____/_/____/\__/\__,_/_/ /_/\__/ \____//____/
  17. EOF
  18. }
  19. clear
  20. header_info
  21. echo -e "Loading..."
  22. GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]')
  23. USEDID=$(pvesh get /cluster/resources --type vm --output-format yaml | egrep -i 'vmid' | awk '{print substr($2, 1, length($2)-0) }')
  24. NEXTID=$(pvesh get /cluster/nextid)
  25. STABLE=$(curl -s https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }')
  26. BETA=$(curl -s https://raw.githubusercontent.com/home-assistant/version/master/beta.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }')
  27. DEV=$(curl -s https://raw.githubusercontent.com/home-assistant/version/master/dev.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }')
  28. YW=$(echo "\033[33m")
  29. BL=$(echo "\033[36m")
  30. HA=$(echo "\033[1;34m")
  31. RD=$(echo "\033[01;31m")
  32. BGN=$(echo "\033[4;92m")
  33. GN=$(echo "\033[1;92m")
  34. DGN=$(echo "\033[32m")
  35. CL=$(echo "\033[m")
  36. BFR="\\r\\033[K"
  37. HOLD="-"
  38. CM="${GN}✓${CL}"
  39. CROSS="${RD}✗${CL}"
  40. set -o errexit
  41. set -o errtrace
  42. set -o nounset
  43. set -o pipefail
  44. shopt -s expand_aliases
  45. alias die='EXIT=$? LINE=$LINENO error_exit'
  46. trap die ERR
  47. trap cleanup EXIT
  48. function error_exit() {
  49. trap - ERR
  50. local reason="Unknown failure occurred."
  51. local msg="${1:-$reason}"
  52. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  53. echo -e "$flag $msg" 1>&2
  54. [ ! -z ${VMID-} ] && cleanup_vmid
  55. exit $EXIT
  56. }
  57. function cleanup_vmid() {
  58. if $(qm status $VMID &>/dev/null); then
  59. if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then
  60. qm stop $VMID
  61. fi
  62. qm destroy $VMID
  63. fi
  64. }
  65. function cleanup() {
  66. popd >/dev/null
  67. rm -rf $TEMP_DIR
  68. }
  69. TEMP_DIR=$(mktemp -d)
  70. pushd $TEMP_DIR >/dev/null
  71. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "PiMox HAOS VM" --yesno "This will create a New PiMox HAOS VM. Proceed?" 10 58); then
  72. echo "User selected Yes"
  73. else
  74. clear
  75. echo -e "⚠ User exited script \n"
  76. exit
  77. fi
  78. function PVE_CHECK() {
  79. if ! pveversion | grep -Eq "pve-manager/(7\.[2-9]|8\.[0-9])"; then
  80. echo -e "${CROSS} This version of Proxmox Virtual Environment is not supported"
  81. echo -e "Requires PVE Version 7.2 or higher"
  82. echo -e "Exiting..."
  83. sleep 2
  84. exit
  85. fi
  86. }
  87. function ARCH_CHECK() {
  88. ARCH=$(dpkg --print-architecture)
  89. if [[ "$ARCH" == "amd64" ]]; then
  90. echo -e "\n ❌ This script only works with PiMox! \n"
  91. echo -e "Exiting..."
  92. sleep 2
  93. exit
  94. fi
  95. }
  96. function msg_info() {
  97. local msg="$1"
  98. echo -ne " ${HOLD} ${YW}${msg}..."
  99. }
  100. function msg_ok() {
  101. local msg="$1"
  102. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  103. }
  104. function msg_error() {
  105. local msg="$1"
  106. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  107. }
  108. function default_settings() {
  109. echo -e "${DGN}Using HAOS Version: ${BGN}${STABLE}${CL}"
  110. BRANCH=${STABLE}
  111. echo -e "${DGN}Using Virtual Machine ID: ${BGN}$NEXTID${CL}"
  112. VMID=$NEXTID
  113. echo -e "${DGN}Using Hostname: ${BGN}haos${STABLE}${CL}"
  114. HN=haos${STABLE}
  115. echo -e "${DGN}Allocated Cores: ${BGN}2${CL}"
  116. CORE_COUNT="2"
  117. echo -e "${DGN}Allocated RAM: ${BGN}4096${CL}"
  118. RAM_SIZE="4096"
  119. echo -e "${DGN}Using Bridge: ${BGN}vmbr0${CL}"
  120. BRG="vmbr0"
  121. echo -e "${DGN}Using MAC Address: ${BGN}$GEN_MAC${CL}"
  122. MAC=$GEN_MAC
  123. echo -e "${DGN}Using VLAN: ${BGN}Default${CL}"
  124. VLAN=""
  125. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  126. MTU=""
  127. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  128. START_VM="yes"
  129. echo -e "${BL}Creating a HAOS VM using the above default settings${CL}"
  130. }
  131. function advanced_settings() {
  132. BRANCH=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "HAOS VERSION" --radiolist "Choose Version" --cancel-button Exit-Script 10 58 3 \
  133. "$STABLE" "Stable" ON \
  134. "$BETA" "Beta" OFF \
  135. "$DEV" "Dev" OFF \
  136. 3>&1 1>&2 2>&3)
  137. exitstatus=$?
  138. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using HAOS Version: ${BGN}$BRANCH${CL}"; fi
  139. 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)
  140. exitstatus=$?
  141. if [ -z $VMID ]; then
  142. VMID="$NEXTID"
  143. echo -e "${DGN}Virtual Machine: ${BGN}$VMID${CL}"
  144. else
  145. if echo "$USEDID" | egrep -q "$VMID"; then
  146. echo -e "\n🚨 ${RD}ID $VMID is already in use${CL} \n"
  147. echo -e "Exiting Script \n"
  148. sleep 2
  149. exit
  150. else
  151. if [ $exitstatus = 0 ]; then echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"; fi
  152. fi
  153. fi
  154. 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)
  155. exitstatus=$?
  156. if [ -z $VM_NAME ]; then
  157. HN="haos${BRANCH}"
  158. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  159. else
  160. if [ $exitstatus = 0 ]; then
  161. HN=$(echo ${VM_NAME,,} | tr -d ' ')
  162. echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"
  163. fi
  164. fi
  165. 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)
  166. exitstatus=$?
  167. if [ -z $CORE_COUNT ]; then
  168. CORE_COUNT="2"
  169. echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"
  170. else
  171. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi
  172. fi
  173. 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)
  174. exitstatus=$?
  175. if [ -z $RAM_SIZE ]; then
  176. RAM_SIZE="4096"
  177. echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"
  178. else
  179. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi
  180. fi
  181. 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)
  182. exitstatus=$?
  183. if [ -z $BRG ]; then
  184. BRG="vmbr0"
  185. echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"
  186. else
  187. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}"; fi
  188. fi
  189. 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)
  190. exitstatus=$?
  191. if [ -z $MAC1 ]; then
  192. MAC="$GEN_MAC"
  193. echo -e "${DGN}Using MAC Address: ${BGN}$MAC${CL}"
  194. else
  195. if [ $exitstatus = 0 ]; then
  196. MAC="$MAC1"
  197. echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"
  198. fi
  199. fi
  200. 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)
  201. exitstatus=$?
  202. if [ $exitstatus = 0 ]; then
  203. if [ -z $VLAN1 ]; then
  204. VLAN1="Default" VLAN=""
  205. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  206. else
  207. VLAN=",tag=$VLAN1"
  208. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  209. fi
  210. fi
  211. 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)
  212. exitstatus=$?
  213. if [ $exitstatus = 0 ]; then
  214. if [ -z $MTU1 ]; then
  215. MTU1="Default" MTU=""
  216. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  217. else
  218. MTU=",mtu=$MTU1"
  219. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  220. fi
  221. fi
  222. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "START VIRTUAL MACHINE" --yesno "Start VM when completed?" 10 58); then
  223. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  224. START_VM="yes"
  225. else
  226. echo -e "${DGN}Start VM when completed: ${BGN}no${CL}"
  227. START_VM="no"
  228. fi
  229. 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
  230. echo -e "${RD}Creating a HAOS VM using the above advanced settings${CL}"
  231. else
  232. clear
  233. header_info
  234. echo -e "${RD}Using Advanced Settings${CL}"
  235. advanced_settings
  236. fi
  237. }
  238. function START_SCRIPT() {
  239. if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  240. clear
  241. header_info
  242. echo -e "${BL}Using Default Settings${CL}"
  243. default_settings
  244. else
  245. clear
  246. header_info
  247. echo -e "${RD}Using Advanced Settings${CL}"
  248. advanced_settings
  249. fi
  250. }
  251. ARCH_CHECK
  252. PVE_CHECK
  253. START_SCRIPT
  254. while read -r line; do
  255. TAG=$(echo $line | awk '{print $1}')
  256. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  257. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  258. ITEM=" Type: $TYPE Free: $FREE "
  259. OFFSET=2
  260. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  261. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  262. fi
  263. STORAGE_MENU+=("$TAG" "$ITEM" "OFF")
  264. done < <(pvesm status -content images | awk 'NR>1')
  265. if [ $((${#STORAGE_MENU[@]} / 3)) -eq 0 ]; then
  266. echo -e "'Disk image' needs to be selected for at least one storage location."
  267. die "Unable to detect valid storage location."
  268. elif [ $((${#STORAGE_MENU[@]} / 3)) -eq 1 ]; then
  269. STORAGE=${STORAGE_MENU[0]}
  270. else
  271. while [ -z "${STORAGE:+x}" ]; do
  272. STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
  273. "Which storage pool you would like to use for the HAOS VM?\n\n" \
  274. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  275. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  276. done
  277. fi
  278. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  279. msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
  280. msg_info "Getting URL for Home Assistant ${BRANCH} Disk Image"
  281. URL=https://github.com/home-assistant/operating-system/releases/download/${BRANCH}/haos_generic-aarch64-${BRANCH}.qcow2.xz
  282. sleep 2
  283. msg_ok "${CL}${BL}${URL}${CL}"
  284. wget -q --show-progress $URL
  285. echo -en "\e[1A\e[0K"
  286. FILE=$(basename $URL)
  287. msg_ok "Downloaded ${CL}${BL}haos_generic-aarch64-${BRANCH}.qcow2.xz${CL}"
  288. msg_info "Extracting Disk Image"
  289. unxz $FILE
  290. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  291. case $STORAGE_TYPE in
  292. nfs | dir)
  293. DISK_EXT=".qcow2"
  294. DISK_REF="$VMID/"
  295. DISK_IMPORT="-format qcow2"
  296. ;;
  297. esac
  298. for i in {0,1}; do
  299. disk="DISK$i"
  300. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  301. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  302. done
  303. msg_ok "Extracted Disk Image"
  304. msg_info "Creating HAOS VM"
  305. qm create $VMID -bios ovmf -cores $CORE_COUNT -memory $RAM_SIZE -name $HN \
  306. -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
  307. pvesm alloc $STORAGE $VMID $DISK0 64M 1>&/dev/null
  308. qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
  309. qm set $VMID \
  310. -efidisk0 ${DISK0_REF},efitype=4m,size=64M \
  311. -scsi0 ${DISK1_REF},size=32G >/dev/null
  312. qm set $VMID \
  313. -boot order=scsi0 >/dev/null
  314. qm set $VMID -description "# PiMox HAOS
  315. ### https://github.com/tteck/Proxmox
  316. [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/D1D7EP4GF)" >/dev/null
  317. msg_ok "Created HAOS VM ${CL}${BL}(${HN})"
  318. if [ "$START_VM" == "yes" ]; then
  319. msg_info "Starting Home Assistant OS VM"
  320. qm start $VMID
  321. msg_ok "Started Home Assistant OS VM"
  322. fi
  323. msg_ok "Completed Successfully!\n"