create-vmachine: Clean up VMs that fail to deploy completely

master
Dustin C. Hatch 2014-01-11 17:23:31 -06:00
parent 242211a599
commit 55664477e3
1 changed files with 26 additions and 0 deletions

View File

@ -17,6 +17,27 @@ fi
: ${MOUNTPOINT:=/mnt/new_vmachine} : ${MOUNTPOINT:=/mnt/new_vmachine}
: ${PORTAGE_PROFILE:="default/linux/amd64/13.0"} : ${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 while [[ -z ${VM_ROOTPW} ]]; do
read -sp 'New VM root password: ' VM_ROOTPW read -sp 'New VM root password: ' VM_ROOTPW
echo echo
@ -50,6 +71,9 @@ lvcreate -nhome -L200M ${VMNAME}
lvcreate -nvar -L400M ${VMNAME} lvcreate -nvar -L400M ${VMNAME}
lvcreate -nusr -l100%FREE ${VMNAME} lvcreate -nusr -l100%FREE ${VMNAME}
trap "quit" INT TERM
trap "cleanup" EXIT
echo "Creating XFS filesystems..." echo "Creating XFS filesystems..."
for fs in root home usr var; do for fs in root home usr var; do
mkfs.xfs -L$fs -q /dev/${VMNAME}/$fs mkfs.xfs -L$fs -q /dev/${VMNAME}/$fs
@ -104,6 +128,8 @@ echo "root:${VM_ROOTPW}" | chroot "${MOUNTPOINT}" chpasswd
echo "Enabling serial console..." echo "Enabling serial console..."
sed -i 's/#s0/s0/' "${MOUNTPOINT}"/etc/inittab sed -i 's/#s0/s0/' "${MOUNTPOINT}"/etc/inittab
trap - INT TERM EXIT
echo "Cleaning up..." echo "Cleaning up..."
umount "${MOUNTPOINT}"/{run,usr,var,home,} umount "${MOUNTPOINT}"/{run,usr,var,home,}
vgchange -an ${VMNAME} vgchange -an ${VMNAME}