Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Script to handle automatic snapshotting and restoring of test machines.
- # This script should be run early in the startup process to see if the
- # user wants to continue/create a session or to delete an existing
- # session and start over fresh.
- set -e
- [ $EUID == 0 ] || exec sudo "$0" "$@"
- # If a snapshot is being merged, wait until the process completes
- # since we're not allowed to make some kinds of changes until then.
- ROOT_DEV=$(mount | awk '/ on \/ /{print $1}')
- while true; do
- ROOT_DATA=$(lvs --noheadings --aligned --separator "$(echo -e '\t')" $ROOT_DEV | tr -d ' ')
- if [[ -n "$(echo "$ROOT_DATA" | cut -f7)" ]]; then
- echo "Restoring snapshot, please wait..."
- sleep 5
- else
- break
- fi
- done
- ROOT_VG=$(echo "$ROOT_DATA" | cut -f2)
- # If a snapshot is present, ask if they want to continue their session.
- # If they don't, see if they want to delete the snapshot to start
- # fresh instead.
- SNAP_PREFIX="wacsnap"
- SNAP_LIST=$(lvs --noheadings --select "lv_name=~$SNAP_PREFIX" --aligned --separator "$(echo -e '\t')" | tr -d ' ')
- while [[ -n "$SNAP_LIST" ]]; do
- echo "Previous snapshots(s) detected:"
- echo $(echo "$SNAP_LIST" | cut -f1)
- LV=$(echo "$SNAP_LIST" | cut -f1 | head -n1)
- VG=$(echo "$SNAP_LIST" | cut -f2 | head -n1)
- while true; do
- read -p "Do you want to continue your last session (Y/N)? " CONTINUE
- case $CONTINUE in
- [Yy]*)
- echo "Continuing last session."
- exit;;
- [Nn]*)
- break;;
- esac
- done
- while true; do
- read -p "Delete current session (Y/N)? " DELETE
- case $DELETE in
- [Yy]*)
- lvconvert --merge ${VG}/${LV}
- read -p "Press enter to reboot." FOO
- reboot;;
- [Nn]*)
- break;;
- esac
- done
- echo
- echo
- done
- # Create a temporary snapshot just in case the user takes a long
- # time to decide if they want to start a new session or to enter
- # maintainence mode. We don't want changes made while this prompt
- # is waiting to be committed to the base system...
- SIZE=$(echo "$ROOT_DATA" | cut -f4)
- lvcreate --snapshot --name $SNAP_PREFIX --size $SIZE $ROOT_DEV
- # Ask if the user wants to start a new session or if they
- # are performing system maintainence (e.g. installing updates).
- # Delete temp snapshot and exit if performing system maintainence.
- while true; do
- read -p "Start new session? (Y/N) " STARTNEW
- case $STARTNEW in
- [Yy]*)
- break;;
- [Nn]*)
- echo "Entering maintainence mode. Enter 'Y' if asked to remove the '${SNAP_PREFIX}' volume."
- lvremove ${ROOT_VG}/${SNAP_PREFIX}
- exit;;
- esac
- done
- # Ask user for their name and a one-word note. Rename the snapshot
- # with this information so that in future reboots we know what is
- # going on with this machine.
- read -p "What is your name (e.g ping)? " USER
- read -p "One-word session note (e.g. test-the-thing)? " NOTE
- lvrename ${ROOT_VG}/${SNAP_PREFIX} ${SNAP_PREFIX}_${USER}_${NOTE}
Add Comment
Please, Sign In to add comment