diff --git a/create-vmachine.sh b/create-vmachine.sh index 0a86287..49c7e89 100755 --- a/create-vmachine.sh +++ b/create-vmachine.sh @@ -17,6 +17,27 @@ fi : ${MOUNTPOINT:=/mnt/new_vmachine} : ${PORTAGE_PROFILE:="default/linux/amd64/13.0"} + +quit() { + exit "$@" +} + +cleanup() { + local mnt + echo "Failed! Cleaning up..." + sync ; sync ; sync + if [ -n "${MOUNTPOINT}" ]; then + for d in home usr var run tmp ""; do + mnt="${MOUNTPOINT}"/$d + mountpoint -q "${mnt}" && umount -l "${mnt}" + done + fi + vgchange -an ${VMNAME} + lvchange -an ${VG}/${VMNAME} + virsh -c qemu:///system vol-delete ${VMNAME} ${POOL} +} + + while [[ -z ${VM_ROOTPW} ]]; do read -sp 'New VM root password: ' VM_ROOTPW echo @@ -50,6 +71,9 @@ lvcreate -nhome -L200M ${VMNAME} lvcreate -nvar -L400M ${VMNAME} lvcreate -nusr -l100%FREE ${VMNAME} +trap "quit" INT TERM +trap "cleanup" EXIT + echo "Creating XFS filesystems..." for fs in root home usr var; do mkfs.xfs -L$fs -q /dev/${VMNAME}/$fs @@ -104,6 +128,8 @@ echo "root:${VM_ROOTPW}" | chroot "${MOUNTPOINT}" chpasswd echo "Enabling serial console..." sed -i 's/#s0/s0/' "${MOUNTPOINT}"/etc/inittab +trap - INT TERM EXIT + echo "Cleaning up..." umount "${MOUNTPOINT}"/{run,usr,var,home,} vgchange -an ${VMNAME}