Proxmox/vm/haos-vm.sh

299 lines
8.8 KiB
Bash
Raw Normal View History

2022-03-21 19:09:07 +01:00
#!/usr/bin/env bash
2022-04-16 22:29:40 +02:00
NEXTID=$(pvesh get /cluster/nextid)
2022-04-16 22:36:17 +02:00
INTEGER='^[0-9]+$'
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-03-21 19:09:07 +01:00
while true; do
read -p "This will create a New Home Assistant OS VM. Proceed(y/n)?" yn
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}
_ _ ____ _____
| | | | /\ / __ \ / ____|
| |__| | / \ | | | | (___
| __ | / /\ \| | | |\___ \
| | | |/ ____ \ |__| |____) |
|_| |_/_/ \_\____/|_____/
${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}"
echo -e "${DGN}Using ID ${BGN}$NEXTID${CL}"
2022-04-16 22:36:17 +02:00
VM_ID=$NEXTID
2022-04-16 22:29:40 +02:00
echo -e "${DGN}Using Disk Size ${BGN}32GB${CL}"
2022-04-16 23:21:33 +02:00
DISK_SIZE=16
2022-04-16 22:29:40 +02:00
echo -e "${DGN}Using ${BGN}2vCPU${CL}"
CORE_COUNT="2"
echo -e "${DGN}Using ${BGN}4096MiB${CL}${GN} RAM${CL}"
RAM_SIZE="4096"
}
function advanced_settings() {
clear
header_info
echo -e "${RD}Using Advanced Settings${CL}"
2022-04-16 22:36:17 +02:00
echo -e "${YW}Enter the VM ID, or Press [ENTER] to automatically generate (${NEXTID}) "
read VM_ID
2022-04-16 22:43:57 +02:00
if [ -z $VM_ID ]; then VM_ID=$NEXTID; fi;
echo -en "${DGN}Set VM ID To ${BL}$VM_ID${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-16 22:36:17 +02:00
echo -e "${DGN}Using ID ${BGN}$VM_ID${CL}"
2022-04-16 22:29:40 +02:00
echo -e "${YW}Enter a Disk Size, or Press [ENTER] for Default: 32Gb "
read DISK_SIZE
2022-04-16 23:21:33 +02:00
if [ -z $DISK_SIZE ]; then DISK_SIZE=32; fi;
2022-04-16 22:29:40 +02:00
if ! [[ $DISK_SIZE =~ $INTEGER ]] ; then echo "ERROR! DISK SIZE MUST HAVE INTEGER NUMBER!"; exit; fi;
2022-04-16 23:02:32 +02:00
echo -en "${DGN}Set Disk Size To ${BL}$DISK_SIZE${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-16 22:36:17 +02:00
echo -e "${DGN}Using ID ${BGN}$VM_ID${CL}"
2022-04-16 23:02:32 +02:00
echo -e "${DGN}Using Disk Size ${BGN}${DISK_SIZE}GB${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-16 22:36:17 +02:00
echo -e "${DGN}Using ID ${BGN}$VM_ID${CL}"
2022-04-16 23:02:32 +02:00
echo -e "${DGN}Using Disk Size ${BGN}${DISK_SIZE}GB${CL}"
2022-04-16 22:29:40 +02:00
echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
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-16 22:36:17 +02:00
echo -e "${DGN}Using ID ${BGN}$VM_ID${CL}"
2022-04-16 23:02:32 +02:00
echo -e "${DGN}Using Disk Size ${BGN}${DISK_SIZE}GB${CL}"
2022-04-16 22:29:40 +02:00
echo -e "${DGN}Using ${BGN}${CORE_COUNT}vCPU${CL}"
2022-04-16 23:21:33 +02:00
echo -e "${DGN}Using ${BGN}${RAM_SIZE}MiB${CL}${DGN} RAM${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() {
echo -e "${YW}Type Advanced, or Press [ENTER] for Default Settings "
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
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
local DEFAULT='Unknown failure occured.'
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
msg "$FLAG $REASON"
2022-04-16 22:55:14 +02:00
[ ! -z ${VM_ID-} ] && cleanup_vm_id
2022-03-21 19:09:07 +01:00
exit $EXIT
}
function warn() {
local REASON="\e[97m$1\e[39m"
local FLAG="\e[93m[WARNING]\e[39m"
msg "$FLAG $REASON"
}
function info() {
local REASON="$1"
local FLAG="\e[36m[INFO]\e[39m"
msg "$FLAG $REASON"
}
function msg() {
local TEXT="$1"
echo -e "$TEXT"
}
2022-04-16 22:55:14 +02:00
function cleanup_vm_id() {
if $(qm status $VM_ID &>/dev/null); then
if [ "$(qm status $VM_ID | awk '{print $2}')" == "running" ]; then
qm stop $VM_ID
2022-03-21 19:09:07 +01:00
fi
2022-04-16 22:55:14 +02:00
qm destroy $VM_ID
2022-03-21 19:09:07 +01:00
fi
}
function cleanup() {
popd >/dev/null
rm -rf $TEMP_DIR
}
TEMP_DIR=$(mktemp -d)
pushd $TEMP_DIR >/dev/null
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
warn "'Disk image' needs to be selected for at least one storage location."
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 \
"Which storage pool you would like to use for the container?\n\n" \
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-04-16 22:43:57 +02:00
msg_ok "Virtual Machine ID is ${CL}${BL}$VM_ID${CL}."
2022-04-16 18:29:29 +02:00
msg_info "Getting URL for Latest Home Assistant Disk Image"
2022-03-21 19:09:07 +01:00
RELEASE_TYPE=qcow2
URL=$(cat<<EOF | python3
import requests
2022-04-16 19:37:57 +02:00
url = "https://api.github.com/repos/home-assistant/operating-system/releases"
2022-03-21 19:09:07 +01:00
r = requests.get(url).json()
if "message" in r:
exit()
for release in r:
2022-04-16 20:06:49 +02:00
if release["prerelease"]:
continue
for asset in release["assets"]:
if asset["name"].find("$RELEASE_TYPE") != -1:
image_url = asset["browser_download_url"]
print(image_url)
exit()
2022-03-21 19:09:07 +01:00
EOF
)
if [ -z "$URL" ]; then
die "Github has returned an error. A rate limit may have been applied to your connection."
fi
2022-04-16 18:29:29 +02:00
msg_ok "Found URL for Latest Home Assistant Disk Image"
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-04-16 18:29:29 +02:00
msg_ok "Downloaded ${RELEASE_TYPE} Disk Image"
msg_info "Extracting Disk Image"
2022-03-21 19:09:07 +01:00
case $FILE in
*"gz") gunzip -f $FILE ;;
*"zip") gunzip -f -S .zip $FILE ;;
*"xz") xz -d $FILE ;;
*) die "Unable to handle file extension '${FILE##*.}'.";;
esac
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
case $STORAGE_TYPE in
btrfs|nfs|dir)
DISK_EXT=".qcow2"
2022-04-16 22:43:57 +02:00
DISK_REF="$VM_ID/"
2022-03-21 19:09:07 +01:00
IMPORT_OPT="-format qcow2"
esac
for i in {0,1}; do
disk="DISK$i"
2022-04-16 22:43:57 +02:00
eval DISK${i}=vm-${VM_ID}-disk-${i}${DISK_EXT:-}
2022-03-21 19:09:07 +01:00
eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk}
done
2022-04-16 18:29:29 +02:00
msg_ok "Extracted Disk Image"
msg_info "Creating HAOS VM"
2022-03-21 19:09:07 +01:00
VM_NAME=$(sed -e "s/\_//g" -e "s/.${RELEASE_TYPE}.*$//" <<< $FILE)
2022-04-16 22:36:17 +02:00
qm create $VM_ID -agent 1 -bios ovmf -cores ${CORE_COUNT} -memory ${RAM_SIZE} -name $VM_NAME -net0 virtio,bridge=vmbr0 \
2022-03-21 19:09:07 +01:00
-onboot 1 -ostype l26 -scsihw virtio-scsi-pci
2022-04-16 22:48:35 +02:00
pvesm alloc $STORAGE $VM_ID $DISK0 128 1>&/dev/null
qm importdisk $VM_ID ${FILE%.*} $STORAGE ${IMPORT_OPT:-} 1>&/dev/null
qm set $VM_ID \
2022-03-21 19:09:07 +01:00
-efidisk0 ${DISK0_REF},size=128K \
2022-04-16 22:43:57 +02:00
-scsi0 ${DISK1_REF},size=${DISK_SIZE}G >/dev/null
2022-04-16 22:48:35 +02:00
qm set $VM_ID \
2022-03-21 19:09:07 +01:00
-boot order=scsi0 >/dev/null
set +o errtrace
(
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-16 18:43:35 +02:00
msg_info "Adding Serial Port and Configuring Console"
trap '
warn "Unable to configure serial port. VM is still functional."
2022-04-16 22:48:35 +02:00
if [ "$(qm config $VM_ID | sed -n ''/serial0/p'')" != "" ]; then
qm set $VM_ID --delete serial0 >/dev/null
2022-04-16 18:43:35 +02:00
fi
exit
2022-03-21 19:09:07 +01:00
' ERR
2022-04-16 18:43:35 +02:00
msg_ok "Added Serial Port and Configured Console"
2022-03-21 19:09:07 +01:00
if [ "$(command -v kpartx)" = "" ]; then
2022-04-16 18:29:29 +02:00
msg_info "Installing kpartx"
2022-03-21 19:09:07 +01:00
apt-get update >/dev/null
apt-get -qqy install kpartx &>/dev/null
2022-04-16 18:29:29 +02:00
msg_ok "Installed kpartx"
2022-03-21 19:09:07 +01:00
fi
DISK1_PATH="$(pvesm path $DISK1_REF)"
DISK1_PART1="$(kpartx -al $DISK1_PATH | awk 'NR==1 {print $1}')"
DISK1_PART1_PATH="/dev/mapper/$DISK1_PART1"
TEMP_MOUNT="${TEMP_DIR}/mnt"
trap '
findmnt $TEMP_MOUNT >/dev/null && umount $TEMP_MOUNT
command -v kpartx >/dev/null && kpartx -d $DISK1_PATH
' EXIT
kpartx -a $DISK1_PATH
mkdir $TEMP_MOUNT
mount $DISK1_PART1_PATH $TEMP_MOUNT
sed -i 's/$/ console=ttyS0/' ${TEMP_MOUNT}/cmdline.txt
2022-04-16 22:55:14 +02:00
qm set $VM_ID -serial0 socket >/dev/null
2022-03-21 19:09:07 +01:00
)
2022-04-16 20:15:42 +02:00
msg_info "Starting Home Assistant OS VM"
2022-04-16 22:48:35 +02:00
qm start $VM_ID
2022-04-16 20:15:42 +02:00
msg_ok "Started Home Assistant OS VM"
2022-03-21 19:09:07 +01:00
2022-04-16 18:43:35 +02:00
msg_ok "Completed Successfully!\n"