haos-vm-v3.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #!/usr/bin/env bash
  2. GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]')
  3. NEXTID=$(pvesh get /cluster/nextid)
  4. RELEASE=$(curl -sX GET "https://api.github.com/repos/home-assistant/operating-system/releases" | awk '/tag_name/{print $4;exit}' FS='[""]')
  5. STABLE="7.6"
  6. YW=`echo "\033[33m"`
  7. BL=`echo "\033[36m"`
  8. RD=`echo "\033[01;31m"`
  9. BGN=`echo "\033[4;92m"`
  10. GN=`echo "\033[1;92m"`
  11. DGN=`echo "\033[32m"`
  12. CL=`echo "\033[m"`
  13. BFR="\\r\\033[K"
  14. HOLD="-"
  15. CM="${GN}✓${CL}"
  16. set -o errexit
  17. set -o errtrace
  18. set -o nounset
  19. set -o pipefail
  20. shopt -s expand_aliases
  21. alias die='EXIT=$? LINE=$LINENO error_exit'
  22. trap die ERR
  23. trap cleanup EXIT
  24. function error_exit() {
  25. trap - ERR
  26. local reason="Unknown failure occured."
  27. local msg="${1:-$reason}"
  28. local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
  29. echo -e "$flag $msg" 1>&2
  30. [ ! -z ${VMID-} ] && cleanup_vmid
  31. exit $EXIT
  32. }
  33. function cleanup_vmid() {
  34. if $(qm status $VMID &>/dev/null); then
  35. if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then
  36. qm stop $VMID
  37. fi
  38. qm destroy $VMID
  39. fi
  40. }
  41. function cleanup() {
  42. popd >/dev/null
  43. rm -rf $TEMP_DIR
  44. }
  45. TEMP_DIR=$(mktemp -d)
  46. pushd $TEMP_DIR >/dev/null
  47. while true; do
  48. read -p "This will create a New Home Assistant OS VM. Proceed(y/n)?" yn
  49. case $yn in
  50. [Yy]* ) break;;
  51. [Nn]* ) exit;;
  52. * ) echo "Please answer yes or no.";;
  53. esac
  54. done
  55. clear
  56. function header_info {
  57. echo -e "${BL}
  58. _ _ ____ _____
  59. | | | | /\ / __ \ / ____|
  60. | |__| | / \ | | | | (___
  61. | __ | / /\ \| | | |\___ \
  62. | | | |/ ____ \ |__| |____) |
  63. |_| |_/_/ ${CL}${YW}v3${CL}${BL} \_\____/|_____/
  64. ${CL}"
  65. }
  66. header_info
  67. function msg_info() {
  68. local msg="$1"
  69. echo -ne " ${HOLD} ${YW}${msg}..."
  70. }
  71. function msg_ok() {
  72. local msg="$1"
  73. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  74. }
  75. function default_settings() {
  76. clear
  77. header_info
  78. echo -e "${BL}Using Default Settings${CL}"
  79. echo -e "${DGN}Using Version ${BGN}${STABLE}${CL}"
  80. BRANCH=${STABLE}
  81. echo -e "${DGN}Using VM ID ${BGN}$NEXTID${CL}"
  82. VMID=$NEXTID
  83. echo -e "${DGN}Using VM Name ${BGN}haos${STABLE}${CL}"
  84. VM_NAME=haos${STABLE}
  85. echo -e "${DGN}Using ${BGN}2${CL}${DGN}vCPU${CL}"
  86. CORE_COUNT="2"
  87. echo -e "${DGN}Using ${BGN}4096${CL}${DGN}MiB RAM${CL}"
  88. RAM_SIZE="4096"
  89. echo -e "${DGN}Using Bridge ${BGN}vmbr0${CL}"
  90. BRG="vmbr0"
  91. echo -e "${DGN}Using MAC Address ${BGN}$GEN_MAC${CL}"
  92. MAC=$GEN_MAC
  93. echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
  94. VLAN=""
  95. echo -e "${DGN}Start VM when completed ${BGN}yes${CL}"
  96. START_VM="yes"
  97. }
  98. function advanced_settings() {
  99. clear
  100. header_info
  101. echo -e "${RD}Using Advanced Settings${CL}"
  102. echo -e "${YW}Type Latest for Version ${RELEASE}, or Press [ENTER] for Stable Version ${STABLE} "
  103. read BRANCH
  104. if [ -z $BRANCH ]; then BRANCH=$STABLE;
  105. else
  106. BRANCH=$RELEASE; fi;
  107. echo -en "${DGN}Set Version To ${BL}$BRANCH${CL}"
  108. echo -e " ${CM}${CL} \r"
  109. sleep 1
  110. clear
  111. header_info
  112. clear
  113. header_info
  114. echo -e "${RD}Using Advanced Settings${CL}"
  115. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  116. echo -e "${YW}Enter the VM ID, or Press [ENTER] to automatically generate (${NEXTID}) "
  117. read VMID
  118. if [ -z $VMID ]; then VMID=$NEXTID; fi;
  119. echo -en "${DGN}Set VM ID To ${BL}$VMID${CL}"
  120. echo -e " ${CM}${CL} \r"
  121. sleep 1
  122. clear
  123. header_info
  124. echo -e "${RD}Using Advanced Settings${CL}"
  125. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  126. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  127. echo -e "${YW}Enter VM Name (no-spaces), or Press [ENTER] for Default: haos${BRANCH} "
  128. read VMNAME
  129. if [ -z $VMNAME ]; then
  130. VM_NAME=haos${BRANCH}
  131. else
  132. VM_NAME=$(echo ${VMNAME,,} | tr -d ' ')
  133. fi
  134. echo -en "${DGN}Set CT Name To ${BL}$VM_NAME${CL}"
  135. echo -e " ${CM}${CL} \r"
  136. sleep 1
  137. clear
  138. header_info
  139. echo -e "${RD}Using Advanced Settings${CL}"
  140. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  141. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  142. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  143. echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 2 "
  144. read CORE_COUNT
  145. if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; fi;
  146. echo -en "${DGN}Set Cores To ${BL}${CORE_COUNT}${CL}"
  147. echo -e " ${CM}${CL} \r"
  148. sleep 1
  149. clear
  150. header_info
  151. echo -e "${RD}Using Advanced Settings${CL}"
  152. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  153. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  154. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  155. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  156. echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 4096 "
  157. read RAM_SIZE
  158. if [ -z $RAM_SIZE ]; then RAM_SIZE="4096"; fi;
  159. echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}"
  160. echo -e " ${CM}${CL} \n"
  161. sleep 1
  162. clear
  163. header_info
  164. echo -e "${RD}Using Advanced Settings${CL}"
  165. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  166. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  167. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  168. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  169. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  170. echo -e "${YW}Enter a Bridge, or Press [ENTER] for Default: vmbr0 "
  171. read BRG
  172. if [ -z $BRG ]; then BRG="vmbr0"; fi;
  173. echo -en "${DGN}Set Bridge To ${BL}$BRG${CL}"
  174. echo -e " ${CM}${CL} \n"
  175. sleep 1
  176. clear
  177. header_info
  178. echo -e "${RD}Using Advanced Settings${CL}"
  179. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  180. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  181. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  182. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  183. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  184. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  185. echo -e "${YW}Enter a Valid MAC Address, or Press [ENTER] for Generated MAC: $GEN_MAC "
  186. read MAC
  187. if [ -z $MAC ]; then MAC=$GEN_MAC; fi;
  188. echo -en "${DGN}Set MAC Address To ${BL}$MAC${CL}"
  189. echo -e " ${CM}${CL} \n"
  190. sleep 1
  191. clear
  192. header_info
  193. echo -e "${RD}Using Advanced Settings${CL}"
  194. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  195. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  196. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  197. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  198. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
  199. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  200. echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
  201. echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
  202. read VLAN1
  203. if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN="";
  204. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  205. else
  206. VLAN=",tag=$VLAN1"
  207. echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
  208. fi;
  209. echo -e " ${CM}${CL} \n"
  210. sleep 1
  211. clear
  212. header_info
  213. echo -e "${RD}Using Advanced Settings${CL}"
  214. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  215. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  216. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  217. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  218. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB${CL}"
  219. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  220. echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
  221. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  222. echo -e "${YW}Start VM when completed, or Press [ENTER] for Default: yes "
  223. read START_VM
  224. if [ -z $START_VM ]; then START_VM="yes";
  225. else
  226. START_VM="no"; fi;
  227. echo -en "${DGN}Starting VM when completed ${BL}$START_VM${CL}"
  228. echo -e " ${CM}${CL} \n"
  229. sleep 1
  230. clear
  231. header_info
  232. echo -e "${RD}Using Advanced Settings${CL}"
  233. echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
  234. echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
  235. echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
  236. echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
  237. echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB${CL}"
  238. echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
  239. echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
  240. echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
  241. echo -e "${DGN}Start VM when completed ${BGN}$START_VM${CL}"
  242. read -p "Are these settings correct(y/n)? " -n 1 -r
  243. echo
  244. if [[ ! $REPLY =~ ^[Yy]$ ]]
  245. then
  246. advanced_settings
  247. fi
  248. }
  249. function start_script() {
  250. echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
  251. read SETTINGS
  252. if [ -z $SETTINGS ]; then default_settings;
  253. else
  254. advanced_settings
  255. fi;
  256. }
  257. start_script
  258. while read -r line; do
  259. TAG=$(echo $line | awk '{print $1}')
  260. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  261. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  262. ITEM=" Type: $TYPE Free: $FREE "
  263. OFFSET=2
  264. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  265. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  266. fi
  267. STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
  268. done < <(pvesm status -content images | awk 'NR>1')
  269. if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
  270. echo -e "'Disk image' needs to be selected for at least one storage location."
  271. die "Unable to detect valid storage location."
  272. elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
  273. STORAGE=${STORAGE_MENU[0]}
  274. else
  275. while [ -z "${STORAGE:+x}" ]; do
  276. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  277. "Which storage pool you would like to use for the container?\n\n" \
  278. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  279. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  280. done
  281. fi
  282. msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
  283. msg_ok "Container ID is ${CL}${BL}$VMID${CL}."
  284. msg_info "Getting URL for Home Assistant ${BRANCH} Disk Image"
  285. URL=https://github.com/home-assistant/operating-system/releases/download/${BRANCH}/haos_ova-${BRANCH}.qcow2.xz
  286. msg_ok "Found URL for Home Assistant ${BRANCH} Disk Image"
  287. msg_ok "${CL}${BL}${URL}${CL}"
  288. wget -q --show-progress $URL
  289. echo -en "\e[1A\e[0K"
  290. FILE=$(basename $URL)
  291. msg_ok "Downloaded ${CL}${BL}${BRANCH}.qcow2${CL}${GN} Disk Image"
  292. msg_info "Extracting Disk Image"
  293. unxz $FILE
  294. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  295. case $STORAGE_TYPE in
  296. btrfs|nfs|dir)
  297. DISK_EXT=".qcow2"
  298. DISK_REF="$VMID/"
  299. IMPORT_OPT="-format qcow2"
  300. esac
  301. for i in {0,1}; do
  302. disk="DISK$i"
  303. eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
  304. eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
  305. done
  306. msg_ok "Extracted Disk Image"
  307. msg_info "Creating HAOS VM"
  308. qm create $VMID -agent 1 -bios ovmf -cores $CORE_COUNT -memory $RAM_SIZE -name $VM_NAME -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN \
  309. -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
  310. pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null
  311. qm importdisk $VMID ${FILE%.*} $STORAGE ${IMPORT_OPT:-} 1>&/dev/null
  312. qm set $VMID \
  313. -efidisk0 ${DISK0_REF},size=128K \
  314. -scsi0 ${DISK1_REF},size=32G >/dev/null
  315. qm set $VMID \
  316. -boot order=scsi0 >/dev/null
  317. set +o errtrace
  318. (
  319. msg_ok "Created HAOS VM ${CL}${BL}${VM_NAME}"
  320. msg_info "Adding Serial Port and Configuring Console"
  321. trap '
  322. echo -e "Unable to configure serial port. VM is still functional."
  323. if [ "$(qm config $VMID | sed -n ''/serial0/p'')" != "" ]; then
  324. qm set $VMID --delete serial0 >/dev/null
  325. fi
  326. exit
  327. ' ERR
  328. msg_ok "Added Serial Port and Configured Console"
  329. if [ "$(command -v kpartx)" = "" ]; then
  330. msg_info "Installing kpartx"
  331. apt-get update >/dev/null
  332. apt-get -qqy install kpartx &>/dev/null
  333. msg_ok "Installed kpartx"
  334. fi
  335. DISK1_PATH="$(pvesm path $DISK1_REF)"
  336. DISK1_PART1="$(kpartx -al $DISK1_PATH | awk 'NR==1 {print $1}')"
  337. DISK1_PART1_PATH="/dev/mapper/$DISK1_PART1"
  338. TEMP_MOUNT="${TEMP_DIR}/mnt"
  339. trap '
  340. findmnt $TEMP_MOUNT >/dev/null && umount $TEMP_MOUNT
  341. command -v kpartx >/dev/null && kpartx -d $DISK1_PATH
  342. ' EXIT
  343. kpartx -a $DISK1_PATH
  344. mkdir $TEMP_MOUNT
  345. mount $DISK1_PART1_PATH $TEMP_MOUNT
  346. sed -i 's/$/ console=ttyS0/' ${TEMP_MOUNT}/cmdline.txt
  347. qm set $VMID -serial0 socket >/dev/null
  348. )
  349. if [ "$START_VM" == "yes" ]; then
  350. msg_info "Starting Home Assistant OS VM"
  351. qm start $VMID
  352. msg_ok "Started Home Assistant OS VM"
  353. fi
  354. msg_ok "Completed Successfully!\n"