haos-vm-v5.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. / / / /___ ____ ___ ___ / | __________(_)____/ /_____ _____v5/ /_ / __ \/ ___/
  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 -o errexit
  38. set -o errtrace
  39. set -o nounset
  40. set -o pipefail
  41. shopt -s expand_aliases
  42. alias die='EXIT=$? LINE=$LINENO error_exit'
  43. trap die ERR
  44. trap cleanup EXIT
  45. function error_exit() {
  46. trap - ERR
  47. local reason="Unknown failure occurred."
  48. local msg="${1:-$reason}"
  49. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  50. echo -e "$flag $msg" 1>&2
  51. [ ! -z ${VMID-} ] && cleanup_vmid
  52. exit $EXIT
  53. }
  54. function cleanup_vmid() {
  55. if qm status $VMID &>/dev/null; then
  56. qm stop $VMID &>/dev/null
  57. qm destroy $VMID &>/dev/null
  58. fi
  59. }
  60. function cleanup() {
  61. popd >/dev/null
  62. rm -rf $TEMP_DIR
  63. }
  64. TEMP_DIR=$(mktemp -d)
  65. pushd $TEMP_DIR >/dev/null
  66. if whiptail --title "HOME ASSISTANT OS VM" --yesno "This will create a New Home Assistant OS VM. Proceed?" 10 58; then
  67. :
  68. else
  69. header_info && echo -e "⚠ User exited script \n" && exit
  70. fi
  71. function msg_info() {
  72. local msg="$1"
  73. echo -ne " ${HOLD} ${YW}${msg}..."
  74. }
  75. function msg_ok() {
  76. local msg="$1"
  77. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  78. }
  79. function msg_error() {
  80. local msg="$1"
  81. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  82. }
  83. function PVE_CHECK() {
  84. if [ $(pveversion | grep -c "pve-manager/7\.[2-9]") -eq 0 ]; then
  85. echo -e "${CROSS} 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. echo -e "\n ${CROSS} This script will not work with PiMox! \n"
  95. echo -e "Exiting..."
  96. sleep 2
  97. exit
  98. fi
  99. }
  100. function default_settings() {
  101. BRANCH="$stable"
  102. VMID="$NEXTID"
  103. FORMAT=",efitype=4m"
  104. MACHINE=""
  105. HN="haos$stable"
  106. CORE_COUNT="2"
  107. RAM_SIZE="4096"
  108. BRG="vmbr0"
  109. MAC="$GEN_MAC"
  110. VLAN=""
  111. MTU=""
  112. START_VM="yes"
  113. echo -e "${DGN}Using HAOS Version: ${BGN}${BRANCH}${CL}"
  114. echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
  115. echo -e "${DGN}Using Machine Type: ${BGN}i440fx${CL}"
  116. echo -e "${DGN}Using Hostname: ${BGN}${HN}${CL}"
  117. echo -e "${DGN}Allocated Cores: ${BGN}${CORE_COUNT}${CL}"
  118. echo -e "${DGN}Allocated RAM: ${BGN}${RAM_SIZE}${CL}"
  119. echo -e "${DGN}Using Bridge: ${BGN}${BRG}${CL}"
  120. echo -e "${DGN}Using MAC Address: ${BGN}${MAC}${CL}"
  121. echo -e "${DGN}Using VLAN: ${BGN}Default${CL}"
  122. echo -e "${DGN}Using Interface MTU Size: ${BGN}Default${CL}"
  123. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  124. echo -e "${BL}Creating a HAOS VM using the above default settings${CL}"
  125. }
  126. function advanced_settings() {
  127. BRANCH=$(whiptail --title "HAOS VERSION" --radiolist "Choose Version" --cancel-button Exit-Script 10 58 3 \
  128. "$stable" "Stable " ON \
  129. "$beta" "Beta " OFF \
  130. "$dev" "Dev " OFF \
  131. 3>&1 1>&2 2>&3)
  132. exitstatus=$?
  133. if [ $exitstatus = 0 ]; then echo -e "${DGN}Using HAOS Version: ${BGN}$BRANCH${CL}"; fi
  134. while true; do
  135. VMID=$(whiptail --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  136. exitstatus=$?
  137. if [ -z "$VMID" ]; then
  138. VMID="$NEXTID"
  139. echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"
  140. break
  141. else
  142. if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
  143. echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"
  144. sleep 2
  145. continue
  146. fi
  147. echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"
  148. break
  149. fi
  150. done
  151. MACH=$(whiptail --title "MACHINE TYPE" --radiolist --cancel-button Exit-Script "Choose Type" 10 58 2 \
  152. "i440fx" "Machine i440fx" ON \
  153. "q35" "Machine q35" OFF \
  154. 3>&1 1>&2 2>&3)
  155. exitstatus=$?
  156. if [ $MACH = q35 ]; then
  157. echo -e "${DGN}Using Machine Type: ${BGN}$MACH${CL}"
  158. FORMAT=""
  159. MACHINE=" -machine q35"
  160. else
  161. echo -e "${DGN}Using Machine Type: ${BGN}$MACH${CL}"
  162. FORMAT=",efitype=4m"
  163. MACHINE=""
  164. fi
  165. VM_NAME=$(whiptail --inputbox "Set Hostname" 8 58 haos${BRANCH} --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  166. exitstatus=$?
  167. if [ -z $VM_NAME ]; then HN="haos${BRANCH}"; echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}";
  168. else
  169. if [ $exitstatus = 0 ]; then HN=$(echo ${VM_NAME,,} | tr -d ' '); echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}"; fi;
  170. fi
  171. CORE_COUNT=$(whiptail --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  172. exitstatus=$?
  173. if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}";
  174. else
  175. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}"; fi;
  176. fi
  177. RAM_SIZE=$(whiptail --inputbox "Allocate RAM in MiB" 8 58 4096 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  178. exitstatus=$?
  179. if [ -z $RAM_SIZE ]; then RAM_SIZE="4096"; echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}";
  180. else
  181. if [ $exitstatus = 0 ]; then echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}"; fi;
  182. fi
  183. BRG=$(whiptail --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  184. exitstatus=$?
  185. if [ -z $BRG ]; then BRG="vmbr0"; 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 --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 MAC="$GEN_MAC"; echo -e "${DGN}Using MAC Address: ${BGN}$MAC${CL}";
  192. else
  193. if [ $exitstatus = 0 ]; then MAC="$MAC1"; echo -e "${DGN}Using MAC Address: ${BGN}$MAC1${CL}"; fi
  194. fi
  195. VLAN1=$(whiptail --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3)
  196. exitstatus=$?
  197. if [ $exitstatus = 0 ]; then
  198. if [ -z $VLAN1 ]; then VLAN1="Default" VLAN="";
  199. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  200. else
  201. VLAN=",tag=$VLAN1"
  202. echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}"
  203. fi
  204. fi
  205. 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)
  206. exitstatus=$?
  207. if [ $exitstatus = 0 ]; then
  208. if [ -z $MTU1 ]; then
  209. MTU1="Default" MTU=""
  210. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  211. else
  212. MTU=",mtu=$MTU1"
  213. echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}"
  214. fi
  215. fi
  216. if (whiptail --title "START VIRTUAL MACHINE" --yesno "Start VM when completed?" 10 58); then
  217. echo -e "${DGN}Start VM when completed: ${BGN}yes${CL}"
  218. START_VM="yes"
  219. else
  220. echo -e "${DGN}Start VM when completed: ${BGN}no${CL}"
  221. START_VM="no"
  222. fi
  223. if (whiptail --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create HAOS ${BRANCH} VM?" --no-button Do-Over 10 58); then
  224. echo -e "${RD}Creating a HAOS VM using the above advanced settings${CL}"
  225. else
  226. header_info
  227. echo -e "${RD}Using Advanced Settings${CL}"
  228. advanced_settings
  229. fi
  230. }
  231. function START_SCRIPT() {
  232. if (whiptail --title "SETTINGS" --yesno "Use Default Settings?" --no-button Advanced 10 58); then
  233. header_info
  234. echo -e "${BL}Using Default Settings${CL}"
  235. default_settings
  236. else
  237. header_info
  238. echo -e "${RD}Using Advanced Settings${CL}"
  239. advanced_settings
  240. fi
  241. }
  242. ARCH_CHECK
  243. PVE_CHECK
  244. START_SCRIPT
  245. msg_info "Validating Storage"
  246. while read -r line; do
  247. TAG=$(echo $line | awk '{print $1}')
  248. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  249. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  250. ITEM=" Type: $TYPE Free: $FREE "
  251. OFFSET=2
  252. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  253. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  254. fi
  255. STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
  256. done < <(pvesm status -content images | awk 'NR>1')
  257. VALID=$(pvesm status -content images | awk 'NR>1')
  258. if [ -z "$VALID" ]; then
  259. msg_error "Unable to detect a valid storage location."
  260. exit
  261. elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
  262. STORAGE=${STORAGE_MENU[0]}
  263. else
  264. while [ -z "${STORAGE:+x}" ]; do
  265. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  266. "Which storage pool you would like to use for the HAOS VM?\n\n" \
  267. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  268. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  269. done
  270. fi
  271. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  272. msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
  273. msg_info "Retrieving the URL for Home Assistant ${BRANCH} Disk Image"
  274. if [ "$BRANCH" == "$dev" ]; then
  275. URL=https://os-builds.home-assistant.io/${BRANCH}/haos_ova-${BRANCH}.qcow2.xz
  276. else
  277. URL=https://github.com/home-assistant/operating-system/releases/download/${BRANCH}/haos_ova-${BRANCH}.qcow2.xz
  278. fi
  279. sleep 2
  280. msg_ok "${CL}${BL}${URL}${CL}"
  281. wget -q --show-progress $URL
  282. echo -en "\e[1A\e[0K"
  283. FILE=$(basename $URL)
  284. msg_ok "Downloaded ${CL}${BL}haos_ova-${BRANCH}.qcow2.xz${CL}"
  285. msg_info "Extracting KVM Disk Image"
  286. unxz $FILE
  287. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  288. case $STORAGE_TYPE in
  289. nfs|dir)
  290. DISK_EXT=".raw"
  291. DISK_REF="$VMID/"
  292. DISK_IMPORT="-format raw"
  293. THIN=""
  294. ;;
  295. btrfs)
  296. DISK_EXT=".raw"
  297. DISK_REF="$VMID/"
  298. DISK_IMPORT="-format raw"
  299. FORMAT=",efitype=4m"
  300. THIN=""
  301. ;;
  302. esac
  303. for i in {0,1}; do
  304. disk="DISK$i"
  305. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  306. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  307. done
  308. msg_ok "Extracted KVM Disk Image"
  309. msg_info "Creating HAOS VM"
  310. qm create $VMID -agent 1${MACHINE} -tablet 0 -localtime 1 -bios ovmf -cores $CORE_COUNT -memory $RAM_SIZE \
  311. -name $HN -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
  312. pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
  313. qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
  314. qm set $VMID \
  315. -efidisk0 ${DISK0_REF}${FORMAT} \
  316. -scsi0 ${DISK1_REF},${THIN}size=32G \
  317. -boot order=scsi0 \
  318. -description "# Home Assistant OS
  319. ### https://github.com/tteck/Proxmox
  320. [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/D1D7EP4GF)" >/dev/null
  321. msg_ok "Created HAOS VM ${CL}${BL}(${HN})"
  322. if [ "$START_VM" == "yes" ]; then
  323. msg_info "Starting Home Assistant OS VM"
  324. qm start $VMID
  325. msg_ok "Started Home Assistant OS VM"
  326. fi
  327. msg_ok "Completed Successfully!\n"