Last active
April 5, 2017 15:45
-
-
Save sferich888/4978868 to your computer and use it in GitHub Desktop.
While working with some of my KVM vm's I needed the ability to take snapshots quickly as well as revert back to a desired point. The following shell functions are used in my bashrc to make it simple for me to accomplish these task. NOTE: These snapshots are only for the file system and require qcow2 disk images for the backing storage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
wake(){ # Start your VM | |
sudo virsh start $1 | |
} | |
slay(){ # Stop your VM | |
sd_time=20 | |
sudo virsh shutdown $1 | |
if [[ $? ]]; then | |
echo "Shutting Down VM, hold $sd_time seconds." | |
sleep $sd_time | |
fi | |
echo "Checking to see if VM is down? (will kill VM violently if not!)" | |
if [[ running == `sudo virsh list | grep $1 | awk '{print $3}'` ]]; then | |
echo "Killing VM violently" | |
sudo virsh destroy $1 | |
else | |
echo "VM was shutdown successfuly!" | |
fi | |
} | |
breed(){ # Create a qcow2 storage device | |
if [[ -n "$1" ]] && [[ -n "$2" ]]; then | |
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/${1}.img ${2}G | |
else | |
echo "Disk Name or size (in GB) not supplied." | |
echo " breed NAME SIZE" | |
fi | |
} | |
hatch(){ # Create a VM | |
if [[ -n $1 ]]; then | |
NAME=$1 | |
else | |
echo "Must supply a VM name." | |
echo " hatch VM_NAME DISK_NAME RAM_SIZE NETWORK INSTALL_MEDIA" | |
return 1 | |
fi | |
if [[ -n $2 ]]; then | |
DISK=/var/lib/libvirt/images/$2 | |
else | |
echo "Must supply an image name. Try one of the following:" | |
ls /var/lib/libvirt/images/ | |
echo " hatch VM_NAME DISK_NAME RAM_SIZE NETWORK INSTALL_MEDIA" | |
return 1 | |
fi | |
if [[ -n $3 ]]; then | |
RAM=$3 | |
else | |
echo "Must supply your RAM size in MB (1024 = 1GB)" | |
echo " hatch VM_NAME DISK_NAME RAM_SIZE NETWORK INSTALL_MEDIA" | |
return 1 | |
fi | |
if [[ -n $4 ]]; then | |
NETWORK=$4 | |
else | |
echo "Must supply a VM Network. Try:" | |
sudo virsh net-list | tail -n+3 | awk '{print $1}' | |
echo " hatch VM_NAME DISK_NAME RAM_SIZE NETWORK INSTALL_MEDIA" | |
return 1 | |
fi | |
if [[ -n $5 ]]; then | |
INSTALL_IMAGE=$5 | |
else | |
echo "Must supply the location of install media." | |
echo " hatch VM_NAME DISK_NAME RAM_SIZE NETWORK INSTALL_MEDIA" | |
return 1 | |
fi | |
sudo virt-install --cdrom $INSTALL_IMAGE --name $NAME --ram $RAM --disk $DISK,bus="virtio" --network network=$NETWORK | |
} | |
hunt(){ # Get Snapshot informaion from the VM | |
if [[ -n "$1" ]]; then | |
base_image=`sudo virsh domblklist $1 | grep vda | awk '{print $2}'` | |
else | |
echo "Must supply Domain" | |
return 1 | |
fi | |
IP=$(ip neighbour | grep $(sudo virsh dumpxml $1 | grep "mac address" | awk -F\' '{ print $2 }') | awk '{ print $1 }') | |
if [[ -z $IP ]]; then | |
IP="Can Not Determin!" | |
fi | |
echo "Base Image: $base_image" | |
echo IP: $IP | |
sudo qemu-img snapshot -l $base_image | |
} | |
tame(){ # Create a Snapshot of a VM # qemu-img snapshot -c SNAPSHOT BASE_IMAGE | |
# This assumes that you are using a VIRTIO device (need to find a better way of doing this. | |
if [[ -n "$1" ]]; then | |
base_image=`sudo virsh domblklist $1 | grep vda | awk '{print $2}'` | |
else | |
echo "Must supply Domain" | |
return 1 | |
fi | |
if [[ -z $2 ]]; then | |
echo "Must supply Snapshot Name" | |
else | |
# VM has to be down to save it's state.. | |
echo "Shutting down $1 to save it current disk state." | |
sudo virsh shutdown $1 | |
if [[ running == `sudo virsh list | grep $1 | awk '{print $3}'` ]]; then | |
echo "Killing VM violently" | |
sudo virsh destroy $1 | |
fi | |
echo "Base Image: $base_image" | |
for snapshot in `sudo qemu-img snapshot -l $base_image | tail -n+3 | awk '{print $2}'`; do | |
if [[ $snapshot == $2 ]]; then | |
echo "Snapshot Already exsists! You can chose to overwrite or create a new snapshot with the same name." | |
read -p "Would you like to overwrite[Y|y]? " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo " deleting snapshot ..." | |
sudo qemu-img snapshot -d $2 $base_image | |
echo "Deleted Snapshot ..." | |
else | |
echo | |
fi | |
fi | |
done | |
sudo qemu-img snapshot -c $2 $base_image | |
echo "Snapshot Created!" | |
fi | |
} | |
revive(){ # Apply a VM Snapshot | |
if [[ -n "$1" ]]; then | |
base_image=`sudo virsh domblklist $1 | grep vda | awk '{print $2}'` | |
else | |
echo "Must supply Domain" | |
return 1 | |
fi | |
if [[ -n "$2" ]]; then | |
echo "reverting $base_image to $2 snapshot." | |
slay $1 | |
sudo qemu-img snapshot -a $2 $base_image | |
echo " Revert Complete!" | |
sudo virsh start $1 | |
return 0 | |
else | |
echo "Must Supply Snapshot!" | |
echo " Choose from the following:" | |
sudo qemu-img snapshot -l $base_image | |
return 1 | |
fi | |
return 1 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment