Proxmox/vm/haos-vm-v3.sh

360 lines
12 KiB
Bash
Raw Normal View History

2022-03-21 19:09:07 +01:00
#!/usr/bin/env bash
2022-07-21 14:47:16 +02:00
echo -e "Loading..."
2022-07-30 04:08:13 +02:00
PVE=$(pveversion | grep "pve-manager/7" | wc -l)
2022-04-27 14:59:10 +02:00
GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]')
2022-04-16 22:29:40 +02:00
NEXTID=$(pvesh get /cluster/nextid)
2022-04-26 14:25:37 +02:00
RELEASE=$(curl -sX GET "https://api.github.com/repos/home-assistant/operating-system/releases" | awk '/tag_name/{print $4;exit}' FS='[""]')
2022-07-21 13:50:42 +02:00
STABLE=$(curl -s https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }')
2022-04-16 18:29:29 +02:00
YW=`echo "\033[33m"`
BL=`echo "\033[36m"`
2022-04-16 22:36:17 +02:00
RD=`echo "\033[01;31m"`
BGN=`echo "\033[4;92m"`
2022-04-16 18:29:29 +02:00
GN=`echo "\033[1;92m"`
2022-04-16 22:36:17 +02:00
DGN=`echo "\033[32m"`
2022-04-16 18:29:29 +02:00
CL=`echo "\033[m"`
BFR="\\r\\033[K"
HOLD="-"
CM="${GN}${CL}"
2022-04-27 08:23:31 +02:00
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
function error_exit() {
trap - ERR
2022-07-20 01:18:18 +02:00
local reason="Unknown failure occurred."
2022-04-27 08:23:31 +02:00
local msg="${1:-$reason}"
local flag="${RD}‼ ERROR ${CL}$EXIT@$LINE"
echo -e "$flag $msg" 1>&2
[ ! -z ${VMID-} ] && cleanup_vmid
exit $EXIT
}
function cleanup_vmid() {
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
rm -rf $TEMP_DIR
}
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR >/dev/null
2022-07-30 04:08:13 +02:00
if [[ "$PVE" != "1" ]]; then
echo -e "${RD}This script requires Proxmox Virtual Environment 7.0 or greater${CL}"
echo -e "Exiting..."
sleep 2
exit
fi
2022-04-16 18:29:29 +02:00
2022-03-21 19:09:07 +01:00
while true; do
2022-07-21 14:47:16 +02:00
clear
2022-04-26 15:10:30 +02:00
read -p "This will create a New Home Assistant OS VM. Proceed(y/n)?" yn
2022-03-21 19:09:07 +01:00
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
clear
2022-04-16 18:29:29 +02:00
2022-03-21 19:09:07 +01:00
function header_info {
echo -e "${BL}
_ _ ____ _____
| | | | /\ / __ \ / ____|
| |__| | / \ | | | | (___
| __ | / /\ \| | | |\___ \
| | | |/ ____ \ |__| |____) |
2022-04-17 02:24:39 +02:00
|_| |_/_/ ${CL}${YW}v3${CL}${BL} \_\____/|_____/
2022-03-21 19:09:07 +01:00
${CL}"
}
header_info
2022-04-16 18:29:29 +02:00
function msg_info() {
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg}..."
}
function msg_ok() {
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
}
2022-04-16 22:29:40 +02:00
function default_settings() {
clear
header_info
echo -e "${BL}Using Default Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}${STABLE}${CL}"
BRANCH=${STABLE}
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$NEXTID${CL}"
VMID=$NEXTID
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using VM Name ${BGN}haos${STABLE}${CL}"
VM_NAME=haos${STABLE}
2022-04-27 14:35:14 +02:00
echo -e "${DGN}Using ${BGN}2${CL}${DGN}vCPU${CL}"
CORE_COUNT="2"
echo -e "${DGN}Using ${BGN}4096${CL}${DGN}MiB RAM${CL}"
RAM_SIZE="4096"
echo -e "${DGN}Using Bridge ${BGN}vmbr0${CL}"
BRG="vmbr0"
2022-04-27 14:59:10 +02:00
echo -e "${DGN}Using MAC Address ${BGN}$GEN_MAC${CL}"
MAC=$GEN_MAC
2022-04-27 14:35:14 +02:00
echo -e "${DGN}Using VLAN Tag ${BGN}NONE${CL}"
VLAN=""
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Start VM when completed ${BGN}yes${CL}"
START_VM="yes"
2022-04-16 22:29:40 +02:00
}
function advanced_settings() {
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${YW}Type Latest for Version ${RELEASE}, or Press [ENTER] for Stable Version ${STABLE} "
read BRANCH
if [ -z $BRANCH ]; then BRANCH=$STABLE;
else
BRANCH=$RELEASE; fi;
echo -en "${DGN}Set Version To ${BL}$BRANCH${CL}"
echo -e " ${CM}${CL} \r"
sleep 1
clear
header_info
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-16 22:36:17 +02:00
echo -e "${YW}Enter the VM ID, or Press [ENTER] to automatically generate (${NEXTID}) "
2022-04-17 02:24:39 +02:00
read VMID
if [ -z $VMID ]; then VMID=$NEXTID; fi;
2022-04-26 15:10:30 +02:00
echo -en "${DGN}Set VM ID To ${BL}$VMID${CL}"
2022-04-16 22:29:40 +02:00
echo -e " ${CM}${CL} \r"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${YW}Enter VM Name (no-spaces), or Press [ENTER] for Default: haos${BRANCH} "
2022-04-17 02:24:39 +02:00
read VMNAME
if [ -z $VMNAME ]; then
2022-04-26 15:10:30 +02:00
VM_NAME=haos${BRANCH}
2022-04-17 02:24:39 +02:00
else
VM_NAME=$(echo ${VMNAME,,} | tr -d ' ')
fi
echo -en "${DGN}Set CT Name To ${BL}$VM_NAME${CL}"
2022-04-16 22:29:40 +02:00
echo -e " ${CM}${CL} \r"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
2022-04-16 22:29:40 +02:00
echo -e "${YW}Allocate CPU cores, or Press [ENTER] for Default: 2 "
read CORE_COUNT
if [ -z $CORE_COUNT ]; then CORE_COUNT="2"; fi;
2022-04-16 23:02:32 +02:00
echo -en "${DGN}Set Cores To ${BL}${CORE_COUNT}${CL}"
2022-04-16 22:29:40 +02:00
echo -e " ${CM}${CL} \r"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
2022-04-27 14:35:14 +02:00
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
2022-04-16 22:29:40 +02:00
echo -e "${YW}Allocate RAM in MiB, or Press [ENTER] for Default: 4096 "
read RAM_SIZE
if [ -z $RAM_SIZE ]; then RAM_SIZE="4096"; fi;
echo -en "${DGN}Set RAM To ${BL}$RAM_SIZE${CL}"
echo -e " ${CM}${CL} \n"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
2022-04-27 14:35:14 +02:00
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
echo -e "${YW}Enter a Bridge, or Press [ENTER] for Default: vmbr0 "
read BRG
if [ -z $BRG ]; then BRG="vmbr0"; fi;
echo -en "${DGN}Set Bridge To ${BL}$BRG${CL}"
echo -e " ${CM}${CL} \n"
sleep 1
clear
2022-04-27 14:59:10 +02:00
header_info
echo -e "${RD}Using Advanced Settings${CL}"
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
echo -e "${YW}Enter a Valid MAC Address, or Press [ENTER] for Generated MAC: $GEN_MAC "
read MAC
if [ -z $MAC ]; then MAC=$GEN_MAC; fi;
echo -en "${DGN}Set MAC Address To ${BL}$MAC${CL}"
echo -e " ${CM}${CL} \n"
sleep 1
clear
2022-04-27 14:35:14 +02:00
header_info
echo -e "${RD}Using Advanced Settings${CL}"
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB RAM${CL}"
echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
echo -e "${YW}Enter a VLAN Tag, or Press [ENTER] for Default: NONE "
read VLAN1
if [ -z $VLAN1 ]; then VLAN1="NONE" VLAN="";
echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
else
VLAN=",tag=$VLAN1"
echo -en "${DGN}Set VLAN Tag To ${BL}$VLAN1${CL}"
fi;
echo -e " ${CM}${CL} \n"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB${CL}"
echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${YW}Start VM when completed, or Press [ENTER] for Default: yes "
read START_VM
if [ -z $START_VM ]; then START_VM="yes";
else
START_VM="no"; fi;
echo -en "${DGN}Starting VM when completed ${BL}$START_VM${CL}"
echo -e " ${CM}${CL} \n"
sleep 1
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-26 15:10:30 +02:00
echo -e "${DGN}Using Version ${BGN}$BRANCH${CL}"
2022-04-17 02:24:39 +02:00
echo -e "${DGN}Using VM ID ${BGN}$VMID${CL}"
echo -e "${DGN}Using VM Name ${BGN}$VM_NAME${CL}"
2022-04-27 14:35:14 +02:00
echo -e "${DGN}Using ${BGN}${CORE_COUNT}${CL}${DGN}vCPU${CL}"
echo -e "${DGN}Using ${BGN}${RAM_SIZE}${CL}${DGN}MiB${CL}"
echo -e "${DGN}Using Bridge ${BGN}${BRG}${CL}"
echo -e "${DGN}Using MAC Address ${BGN}$MAC${CL}"
echo -e "${DGN}Using VLAN Tag ${BGN}$VLAN1${CL}"
echo -e "${DGN}Start VM when completed ${BGN}$START_VM${CL}"
2022-04-16 22:29:40 +02:00
read -p "Are these settings correct(y/n)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
advanced_settings
fi
}
function start_script() {
2022-07-21 13:50:42 +02:00
echo -e "${YW}Type Advanced (Latest ${RELEASE}), or Press [ENTER] for Default Settings (Stable ${STABLE}) "
2022-04-16 22:29:40 +02:00
read SETTINGS
if [ -z $SETTINGS ]; then default_settings;
else
advanced_settings
fi;
}
start_script
2022-04-16 18:29:29 +02:00
2022-03-21 19:09:07 +01:00
while read -r line; do
TAG=$(echo $line | awk '{print $1}')
TYPE=$(echo $line | awk '{printf "%-10s", $2}')
FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
ITEM=" Type: $TYPE Free: $FREE "
OFFSET=2
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
fi
STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
done < <(pvesm status -content images | awk 'NR>1')
if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
2022-04-27 08:23:31 +02:00
echo -e "'Disk image' needs to be selected for at least one storage location."
2022-03-21 19:09:07 +01:00
die "Unable to detect valid storage location."
elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
STORAGE=${STORAGE_MENU[0]}
else
while [ -z "${STORAGE:+x}" ]; do
STORAGE=$(whiptail --title "Storage Pools" --radiolist \
2022-06-29 05:57:00 +02:00
"Which storage pool you would like to use for the HAOS VM?\n\n" \
2022-03-21 19:09:07 +01:00
16 $(($MSG_MAX_LENGTH + 23)) 6 \
"${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
done
fi
2022-04-16 20:15:42 +02:00
msg_ok "Using ${CL}${BL}$STORAGE${CL} ${GN}for Storage Location."
2022-07-01 16:48:52 +02:00
msg_ok "Virtual Machine ID is ${CL}${BL}$VMID${CL}."
2022-04-26 15:43:45 +02:00
msg_info "Getting URL for Home Assistant ${BRANCH} Disk Image"
2022-04-26 15:10:30 +02:00
URL=https://github.com/home-assistant/operating-system/releases/download/${BRANCH}/haos_ova-${BRANCH}.qcow2.xz
2022-07-02 18:59:51 +02:00
sleep 2
2022-04-16 20:15:42 +02:00
msg_ok "${CL}${BL}${URL}${CL}"
2022-03-21 19:09:07 +01:00
wget -q --show-progress $URL
echo -en "\e[1A\e[0K"
FILE=$(basename $URL)
2022-07-02 18:59:51 +02:00
msg_ok "Downloaded ${CL}${BL}haos_ova-${BRANCH}.qcow2.xz${CL}"
msg_info "Extracting KVM Disk Image"
2022-04-24 19:35:31 +02:00
unxz $FILE
2022-03-21 19:09:07 +01:00
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
case $STORAGE_TYPE in
2022-07-12 00:03:56 +02:00
nfs|dir)
DISK_EXT=".qcow2"
DISK_REF="$VMID/"
DISK_IMPORT="-format qcow2"
;;
btrfs)
DISK_EXT=".raw"
DISK_REF="$VMID/"
DISK_FORMAT="subvol"
DISK_IMPORT="-format raw"
;;
2022-03-21 19:09:07 +01:00
esac
for i in {0,1}; do
disk="DISK$i"
2022-04-17 02:24:39 +02:00
eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-}
2022-03-21 19:09:07 +01:00
eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
done
2022-07-02 18:59:51 +02:00
msg_ok "Extracted KVM Disk Image"
2022-04-16 18:29:29 +02:00
msg_info "Creating HAOS VM"
2022-07-26 23:08:44 +02:00
qm create $VMID -agent 1 -tablet 0 -localtime 1 -bios ovmf -cores $CORE_COUNT -memory $RAM_SIZE -name $VM_NAME -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN \
2022-03-21 19:09:07 +01:00
-onboot 1 -ostype l26 -scsihw virtio-scsi-pci
2022-07-01 13:56:06 +02:00
pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
2022-07-12 00:03:56 +02:00
qm importdisk $VMID ${FILE%.*} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
2022-04-17 02:24:39 +02:00
qm set $VMID \
2022-07-01 13:56:06 +02:00
-efidisk0 ${DISK0_REF},efitype=4m,size=4M \
2022-07-24 15:31:40 +02:00
-scsi0 ${DISK1_REF},discard=on,size=32G,ssd=1 >/dev/null
2022-04-17 02:24:39 +02:00
qm set $VMID \
2022-03-21 19:09:07 +01:00
-boot order=scsi0 >/dev/null
2022-07-15 18:55:17 +02:00
qm set $VMID -description "# Home Assistant OS
### https://github.com/tteck/Proxmox" >/dev/null
2022-04-16 20:15:42 +02:00
msg_ok "Created HAOS VM ${CL}${BL}${VM_NAME}"
2022-04-16 18:29:29 +02:00
2022-04-17 02:24:39 +02:00
if [ "$START_VM" == "yes" ]; then
2022-04-16 20:15:42 +02:00
msg_info "Starting Home Assistant OS VM"
2022-04-17 02:24:39 +02:00
qm start $VMID
2022-04-16 20:15:42 +02:00
msg_ok "Started Home Assistant OS VM"
2022-04-17 02:24:39 +02:00
fi
2022-04-16 18:43:35 +02:00
msg_ok "Completed Successfully!\n"