#!/bin/sh # # Remove a VM for liw. set -eu # Command line parameters. name="$1" # Read config variables. . "$HOME/.config/ansibleness/vm.conf" # Shut down and remove the VM, if it exists. if virsh -c qemu:///system list --all | grep -F "$name" > /dev/null then virsh -c qemu:///system destroy "$name" || true virsh -c qemu:///system undefine "$name" fi # Remove the LV, if it exists. lvpath="/dev/$vg/$name" if [ -e "$lvpath" ] then sudo lvremove --force "$lvpath" fi # Remove the host from /etc/hosts, if there. awk -v "name=$name" '$2 != name' /etc/hosts | sudo sponge /etc/hosts # Done. echo "Virtual machine $name is gone."