diff --git a/vm/pimox-haos-vm.sh b/vm/pimox-haos-vm.sh index aebc39f6c..8613ab982 100644 --- a/vm/pimox-haos-vm.sh +++ b/vm/pimox-haos-vm.sh @@ -23,65 +23,49 @@ EOF } clear header_info -echo -e "\n Loading..." -GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') +echo -e "Loading..." RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" -VERSIONS=(stable beta dev) METHOD="" NSAPP="pimox-haos-vm" var_os="pimox-haos" +var_version=" " DISK_SIZE="32G" - -for version in "${VERSIONS[@]}"; do - eval "$version=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep '"ova"' | cut -d '"' -f 4)" -done +GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]') +USEDID=$(pvesh get /cluster/resources --type vm --output-format yaml | egrep -i 'vmid' | awk '{print substr($2, 1, length($2)-0) }') +STABLE=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') +BETA=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/beta.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') +DEV=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/dev.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') YW=$(echo "\033[33m") BL=$(echo "\033[36m") +HA=$(echo "\033[1;34m") RD=$(echo "\033[01;31m") BGN=$(echo "\033[4;92m") GN=$(echo "\033[1;92m") DGN=$(echo "\033[32m") CL=$(echo "\033[m") - -CL=$(echo "\033[m") -BOLD=$(echo "\033[1m") BFR="\\r\\033[K" -HOLD=" " -TAB=" " - -CM="${TAB}✔️${TAB}${CL}" -CROSS="${TAB}✖️${TAB}${CL}" -INFO="${TAB}💡${TAB}${CL}" -OS="${TAB}🖥️${TAB}${CL}" -CONTAINERTYPE="${TAB}📦${TAB}${CL}" -DISKSIZE="${TAB}💾${TAB}${CL}" -CPUCORE="${TAB}🧠${TAB}${CL}" -RAMSIZE="${TAB}🛠️${TAB}${CL}" -CONTAINERID="${TAB}🆔${TAB}${CL}" -HOSTNAME="${TAB}🏠${TAB}${CL}" -BRIDGE="${TAB}🌉${TAB}${CL}" -GATEWAY="${TAB}🌐${TAB}${CL}" -DEFAULT="${TAB}⚙️${TAB}${CL}" -MACADDRESS="${TAB}🔗${TAB}${CL}" -VLANTAG="${TAB}🏷️${TAB}${CL}" -CREATING="${TAB}🚀${TAB}${CL}" -ADVANCED="${TAB}🧩${TAB}${CL}" -CLOUD="${TAB}☁️${TAB}${CL}" - -THIN="discard=on,ssd=1," -set -e -trap 'error_handler $LINENO "$BASH_COMMAND"' ERR +HOLD="-" +CM="${GN}✓${CL}" +CROSS="${RD}✗${CL}" +set -o errexit +set -o errtrace +set -o nounset +set -o pipefail +shopt -s expand_aliases +alias die='EXIT=$? LINE=$LINENO error_exit' +trap die ERR trap cleanup EXIT trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM -function error_handler() { - local exit_code="$?" - local line_number="$1" - local command="$2" - local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}" - post_update_to_api "failed" "${command}" - echo -e "\n$error_message\n" - cleanup_vmid +function error_exit() { + trap - ERR + local reason="Unknown failure occurred." + local msg="${1:-$reason}" + local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE" + post_update_to_api "failed" "unknown" + echo -e "$flag $msg" 1>&2 + [ ! -z ${VMID-} ] && cleanup_vmid + exit $EXIT } function get_valid_nextid() { @@ -102,111 +86,51 @@ function get_valid_nextid() { } function cleanup_vmid() { - if qm status $VMID &>/dev/null; then - qm stop $VMID &>/dev/null - qm destroy $VMID &>/dev/null + if $(qm status $VMID &>/dev/null); then + if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then + qm stop $VMID + fi + qm destroy $VMID fi } - function cleanup() { popd >/dev/null - post_update_to_api "done" "none" rm -rf $TEMP_DIR } - TEMP_DIR=$(mktemp -d) pushd $TEMP_DIR >/dev/null -if whiptail --backtitle "Proxmox VE Helper Scripts" --title "Pimox Homeassistant OS VM" --yesno "This will create a New Pimox Homeassistant OS VM. Proceed?" 10 58; then - : -else - header_info && echo -e "${CROSS}${RD}User exited script${CL}\n" && exit +if ! command -v whiptail &>/dev/null; then + echo "Installing whiptail..." + apt-get update &>/dev/null + apt-get install -y whiptail &>/dev/null fi - -function msg_info() { - local msg="$1" - echo -ne "${TAB}${YW}${HOLD}${msg}${HOLD}" -} - -function msg_ok() { - local msg="$1" - echo -e "${BFR}${CM}${GN}${msg}${CL}" -} - -function msg_error() { - local msg="$1" - echo -e "${BFR}${CROSS}${RD}${msg}${CL}" -} - -function check_root() { - if [[ "$(id -u)" -ne 0 || $(ps -o comm= -p $PPID) == "sudo" ]]; then - clear - msg_error "Please run this script as root." - echo -e "\nExiting..." - sleep 2 - exit - fi -} - -pve_check() { - local PVE_VER - PVE_VER="$(pveversion | awk -F'/' '{print $2}' | awk -F'-' '{print $1}')" - - # Check for Proxmox VE 8.x - if [[ "$PVE_VER" =~ ^8\.([0-9]+) ]]; then - local MINOR="${BASH_REMATCH[1]}" - if ((MINOR < 1 || MINOR > 4)); then - msg_error "This version of Proxmox VE is not supported." - echo -e "Required: Proxmox VE version 8.1 – 8.4" - exit 1 - fi - return 0 - fi - - # Check for Proxmox VE 9.x (Beta) — require confirmation - if [[ "$PVE_VER" =~ ^9\.([0-9]+) ]]; then - if whiptail --title "Proxmox 9.x Detected (Beta)" \ - --yesno "You are using Proxmox VE $PVE_VER, which is currently in Beta state.\n\nThis version is experimentally supported.\n\nDo you want to proceed anyway?" 12 70; then - msg_ok "Confirmed: Continuing with Proxmox VE $PVE_VER" - return 0 - else - msg_error "Aborted by user: Proxmox VE 9.x was not confirmed." - exit 1 - fi - fi - - # All other unsupported versions - msg_error "This version of Proxmox VE is not supported." - echo -e "Supported versions: Proxmox VE 8.1 – 8.4 or 9.x (Beta, with confirmation)" - exit 1 -} - -function arch_check() { - if [ "$(dpkg --print-architecture)" != "amd64" ]; then - echo -e "\n ${INFO}${YWB}This script will not work with PiMox! \n" - echo -e "\n ${YWB}Visit https://github.com/asylumexp/Proxmox for ARM64 support. \n" +if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "PiMox HAOS VM" --yesno "This will create a New PiMox HAOS VM. Proceed?" 10 58); then + echo "User selected Yes" +else + clear + echo -e "⚠ User exited script \n" + exit +fi +function ARCH_CHECK() { + ARCH=$(dpkg --print-architecture) + if [[ "$ARCH" == "amd64" ]]; then + echo -e "\n ❌ This script only works with PiMox! \n" echo -e "Exiting..." sleep 2 exit fi } - -function ssh_check() { - if command -v pveversion >/dev/null 2>&1; then - if [ -n "${SSH_CLIENT:+x}" ]; then - if whiptail --backtitle "Proxmox VE Helper Scripts" --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 - echo "you've been warned" - else - clear - exit - fi - fi - fi +function msg_info() { + local msg="$1" + echo -ne " ${HOLD} ${YW}${msg}..." } - -function exit-script() { - clear - echo -e "\n${CROSS}${RD}User exited script${CL}\n" - exit +function msg_ok() { + local msg="$1" + echo -e "${BFR} ${CM} ${GN}${msg}${CL}" +} +function msg_error() { + local msg="$1" + echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}" } function default_settings() { @@ -258,10 +182,10 @@ function advanced_settings() { if [ $exitstatus = 0 ]; then echo -e "${DGN}Virtual Machine ID: ${BGN}$VMID${CL}"; fi fi fi - VM_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 haosStable --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3) + 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) exitstatus=$? if [ -z $VM_NAME ]; then - HN="haosStable" + HN="haos${BRANCH}" echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}" else if [ $exitstatus = 0 ]; then @@ -333,7 +257,7 @@ function advanced_settings() { echo -e "${DGN}Start VM when completed: ${BGN}no${CL}" START_VM="no" fi - if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "ADVANCED SETTINGS COMPLETE" --yesno "Ready to create HAOS Stable VM?" --no-button Do-Over 10 58); then + 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 echo -e "${RD}Creating a HAOS VM using the above advanced settings${CL}" else clear @@ -355,7 +279,7 @@ function START_SCRIPT() { advanced_settings fi } - +ARCH_CHECK START_SCRIPT post_to_api_vm while read -r line; do @@ -384,14 +308,14 @@ else fi msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location." msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}." -msg_info "Getting URL for Home Assistant Stable Disk Image" -URL=https://github.com/home-assistant/operating-system/releases/download/16.0/haos_generic-aarch64-16.0.qcow2.xz +msg_info "Getting URL for Home Assistant ${BRANCH} Disk Image" +URL=https://github.com/home-assistant/operating-system/releases/download/${BRANCH}/haos_generic-aarch64-${BRANCH}.qcow2.xz sleep 2 msg_ok "${CL}${BL}${URL}${CL}" curl -f#SL -o "$(basename "$URL")" "$URL" echo -en "\e[1A\e[0K" FILE=$(basename $URL) -msg_ok "Downloaded ${CL}${BL}haos_generic-aarch64-16.0.qcow2.xz${CL}" +msg_ok "Downloaded ${CL}${BL}haos_generic-aarch64-${BRANCH}.qcow2.xz${CL}" msg_info "Extracting Disk Image" unxz $FILE STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}') @@ -417,47 +341,13 @@ qm set $VMID \ -efidisk0 ${DISK0_REF},efitype=4m,size=64M \ -scsi0 ${DISK1_REF},size=32G >/dev/null qm set $VMID \ - -boot order=scsi0 >/dev/null + -boot order=scsi0 \ + -description "
-DESCRIPTION=$(
- cat <
-
-
-
-
-
-
-