From 04f2ce1765121db8e3f649d834d87ef0d2d56805 Mon Sep 17 00:00:00 2001 From: whiskerz007 <2713522+whiskerz007@users.noreply.github.com> Date: Fri, 15 Mar 2019 01:18:01 +0100 Subject: [PATCH] Fixed #15, added error handling, refactored (#17) * Update install.sh * Update README.md --- README.md | 2 +- install.sh | 117 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 12c9f9e..a6c7d77 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This script will create a new Proxmox VM with the latest version of HassOS. To create a new VM in the `local-lvm` storage, run the following in a SSH session or the console from Proxmox interface ``` -TMP=`mktemp -d`;pushd $TMP > /dev/null;wget -qO - https://raw.githubusercontent.com/whiskerz007/proxmox_hassos_install/master/install.sh | bash -s local-lvm;popd > /dev/null;rm -rf $TMP;unset TMP +wget -qO - https://raw.githubusercontent.com/whiskerz007/proxmox_hassos_install/master/install.sh | bash -s local-lvm ``` After script completes, click on the new VM (_the script will tell you the ID_), click on the `Hardware` tab for the VM and change the `Memory` and `Processors` settings to what you desire. The `Hard Disk` can be expanded by clicking on it, then click on the `Resize disk` button above (_Note: additional steps must be taken for storage to take effect in the VM after the first boot_). The network MAC address can be changed by selecting `Network Device` and clicking `Edit` above. Once all changes have been made, click `Start` above. diff --git a/install.sh b/install.sh index 948130e..e9394d4 100644 --- a/install.sh +++ b/install.sh @@ -1,63 +1,96 @@ #!/bin/bash +set -o pipefail +shopt -s expand_aliases +alias die='EXIT=$? LINE=$LINENO error_exit' +trap die ERR +function error_exit() { + REASON=$1 + MSG="\e[91mERROR: \e[93m$EXIT@" + if [ -z "$REASON" ]; then + MSG="$MSG$LINE:" + REASON="Unknown failure occured." + else + MSG="$MSG`echo $(( $LINE - 1 ))`:" + fi + echo -e "$MSG \e[97m$REASON\e[39m\e[49m" + exit $EXIT +} +function cleanup() { + popd >/dev/null + rm -rf $TMP +} +trap cleanup EXIT +TMP=`mktemp -d` +pushd $TMP >/dev/null -VM_STORAGE=${1:-local-lvm} && \ -pvesm list $VM_STORAGE >& /dev/null || { \ -echo -e "\n\n\nERROR: '$VM_STORAGE' is not a valid storage ID.\n\n\n" && \ -exit 1 -} && \ +STORAGE=${1:-local-lvm} +pvesm list $STORAGE >& /dev/null || + die "'$STORAGE' is not a valid storage ID." +pvesm status -content images -storage $STORAGE >&/dev/null || + die "'$STORAGE' does not allow 'Disk image' to be stored." +STORAGE_TYPE=`pvesm status -storage $STORAGE | awk 'NR>1 {print $2}'` VMID=$(cat<&/dev/null && \ -qm importdisk $VMID ${FILE%".gz"} $VM_STORAGE 1>&/dev/null && \ + "********************************\n" \ + "* Creating new VM *\n" \ + "********************************\n" +if [ "$STORAGE_TYPE" = "dir" ]; then + DISK_EXT=".qcow2" + DISK_REF="$VMID/" + IMPORT_OPT="-format qcow2" +fi +for i in {0,1}; do + disk="DISK$i" + eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT} + eval DISK${i}_REF=${STORAGE}:$DISK_REF${!disk} +done +qm create $VMID -bios ovmf -name $(sed -e "s/\_//g" -e "s/.vdi.gz//" <<< $FILE) \ + -net0 virtio,bridge=vmbr0 -onboot 1 -ostype l26 -scsihw virtio-scsi-pci +pvesm alloc $STORAGE $VMID $DISK0 128 1>&/dev/null +qm importdisk $VMID ${FILE%".gz"} $STORAGE $IMPORT_OPT 1>&/dev/null +qm set $VMID -bootdisk sata0 -efidisk0 ${DISK0_REF},size=128K \ + -sata0 ${DISK1_REF},size=6G > /dev/null echo -e "\n\n\n" \ - "********************************\n" \ - "* Completed Successfully *\n" \ - "* New VM ID is $VMID *\n" \ - "********************************\n" + "********************************\n" \ + "* Completed Successfully *\n" \ + "* New VM ID is \e[1m$VMID\e[0m *\n" \ + "********************************\n"