~larry-e-works/+junk/ubuntu-server-admin

« back to all changes in this revision

Viewing changes to skel/scripts/destroy-server-vm.sh

  • Committer: Larry Works
  • Date: 2016-09-13 00:08:42 UTC
  • Revision ID: larry.works@canonical.com-20160913000842-q6qymq7ogatg6usm
Initial addition of the Ubuntu Server Administration support files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
# The VM name will need to be passed as an argument to this script. Check to make sure we received a command line parameter
 
4
if [ $# -ne 1 ]; then
 
5
    echo "  Error: This script expects one and only one parameter. That parameter should be the name of the VM to create. Example:"
 
6
    echo ""
 
7
    echo "        $0 student01"
 
8
    echo ""
 
9
    echo "  Exiting ..."
 
10
    exit 1
 
11
else
 
12
    VMNAME=$1
 
13
fi
 
14
 
 
15
# Stop the VM
 
16
echo " Shutting down the VM $VMNAME ..."
 
17
virsh -q destroy $VMNAME > /dev/null 2>&1
 
18
 
 
19
# Make sure there aren't any snapshots that would prevent us from undefining
 
20
# the VM
 
21
SNAPSHOTS=`virsh -q snapshot-list $VMNAME | awk '{print $1}'`
 
22
if [ -n $SNAPSHOTS ]; then
 
23
    echo "   Removing any existing snapshots for VM $VNMAME ..."
 
24
    for SS in $SNAPSHOTS; do
 
25
        virsh snapshot-delete $VMNAME $SS > /dev/null 2>&1
 
26
    done
 
27
else
 
28
    echo "   No snapshots to remove. Proceeding ..."
 
29
fi
 
30
 
 
31
# Undefine the VM
 
32
echo " Undefining the VM $VMNAME ..."
 
33
OP=`virsh undefine $VMNAME`
 
34
if [ $? -ne 0 ]; then
 
35
    echo "   Hmmmm, something appears to have gone wrong when attempting to undefine the VM. The output of the command is:
 
36
"
 
37
    echo "$OP"
 
38
    exit 1
 
39
else
 
40
    if [ -d /home/VMs/$VMNAME ]; then
 
41
        echo " Removing the disk image files for VM $VMNAME. This will require sudo privileges."
 
42
        sudo /bin/rm -rf /home/VMs/$VMNAME > /dev/null
 
43
    fi
 
44
fi
 
45
    
 
46
 
 
47
echo ""
 
48
echo "  The removal proccess for the VM $VMNAME is complete."
 
49
echo ""
 
50
echo ""