pimox-haos-vm.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. # for testing only
  3. set -o pipefail
  4. shopt -s expand_aliases
  5. alias die='EXIT=$? LINE=$LINENO error_exit'
  6. trap die ERR
  7. function error_exit() {
  8. REASON=$1
  9. MSG="\e[91mERROR: \e[93m$EXIT@"
  10. if [ -z "$REASON" ]; then
  11. MSG="$MSG$LINE:"
  12. REASON="Unknown failure occured."
  13. else
  14. MSG="$MSG`echo $(( $LINE - 1 ))`:"
  15. fi
  16. echo -e "$MSG \e[97m$REASON\e[39m\e[49m"
  17. exit $EXIT
  18. }
  19. function cleanup() {
  20. popd >/dev/null
  21. rm -rf $TMP
  22. }
  23. trap cleanup EXIT
  24. TMP=`mktemp -d`
  25. pushd $TMP >/dev/null
  26. VMID=$(cat<<EOF | python3
  27. import json
  28. with open('/etc/pve/.vmlist') as vmlist:
  29. vmids = json.load(vmlist)
  30. if 'ids' not in vmids:
  31. print(100)
  32. else:
  33. last_vm = sorted(vmids['ids'].keys())[-1:][0]
  34. print(int(last_vm)+1)
  35. EOF
  36. )
  37. echo -e "Getting latest HAOS Info \n"
  38. URL=$(cat<<EOF | python3
  39. import requests
  40. url = 'https://api.github.com/repos/home-assistant/hassos/releases/latest'
  41. r = requests.get(url).json()
  42. if 'message' in r:
  43. exit()
  44. for asset in r['assets']:
  45. if asset['name'].endswith('qcow2.xz'):
  46. print(asset['browser_download_url'])
  47. EOF
  48. )
  49. if [ -z "$URL" ]; then
  50. echo "Github has returned an error. A rate limit may have been applied to your connection. Please wait a while, then try again."
  51. exit 1
  52. fi
  53. echo -e "Downloading HAOS \n"
  54. wget -q --show-progress $URL
  55. FILE=$(basename $URL)
  56. echo -e "\n Extracting HAOS \n"
  57. xz -d $FILE
  58. echo -e "Creating new HAOS VM \n"
  59. qm create $VMID -agent 1 -bios ovmf -cores 2 -memory 4096 -bootdisk scsi0 \
  60. -efidisk0 local:vm-${VMID}-disk-0,size=128K \
  61. -name haos -net0 virtio,bridge=vmbr0 \
  62. -onboot 1 -ostype l26 -scsi0 local:vm-${VMID}-disk-1,size=32G \
  63. -scsihw virtio-scsi-pci && \
  64. pvesm alloc local $VMID vm-${VMID}-disk-0 128 1>&/dev/null && \
  65. qm importdisk $VMID ${FILE%".xz"} local 1>&/dev/null
  66. echo -e "Completed Successfully, New VM ID is $VMID \n"