summaryrefslogtreecommitdiff
path: root/remove-vm
blob: e60bc93099e9e75a2bd86e2d120604b2b75c01c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh
#
# Remove a VM for liw.

set -eu

run_virsh()
{
    virsh -c qemu:///system "$@"
}

vm_exists()
{
    run_virsh list --all | grep -F "$name" > /dev/null
}

remove_vm()
{
    local name="$1"

    # Shut down and remove the VM, if it exists.
    if vm_exists "$name"
    then
        run_virsh destroy "$name" || true
        run_virsh undefine --nvram "$name"
    fi


    # Remove the LV, if it exists.
    lvpath="/dev/$vg/$name"
    if [ -e "$lvpath" ]
    then
        if ! sudo lvremove --force "$lvpath"
        then
            # In case the LV was kpartx'd, undo that
            sudo kpartx -dsv "$lvpath"
            sudo lvremove --force "$lvpath"
        fi
    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."
}


# Read config variables.
. "$HOME/.config/ansibleness/vm.conf"

for name in "$@"
do
    remove_vm "$name"
done