Guest User

Untitled

a guest
May 27th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Script to handle automatic snapshotting and restoring of test machines.
  4. # This script should be run early in the startup process to see if the
  5. # user wants to continue/create a session or to delete an existing
  6. # session and start over fresh.
  7.  
  8. set -e
  9. [ $EUID == 0 ] || exec sudo "$0" "$@"
  10.  
  11. # If a snapshot is being merged, wait until the process completes
  12. # since we're not allowed to make some kinds of changes until then.
  13. ROOT_DEV=$(mount | awk '/ on \/ /{print $1}')
  14. while true; do
  15. ROOT_DATA=$(lvs --noheadings --aligned --separator "$(echo -e '\t')" $ROOT_DEV | tr -d ' ')
  16. if [[ -n "$(echo "$ROOT_DATA" | cut -f7)" ]]; then
  17. echo "Restoring snapshot, please wait..."
  18. sleep 5
  19. else
  20. break
  21. fi
  22. done
  23. ROOT_VG=$(echo "$ROOT_DATA" | cut -f2)
  24.  
  25. # If a snapshot is present, ask if they want to continue their session.
  26. # If they don't, see if they want to delete the snapshot to start
  27. # fresh instead.
  28. SNAP_PREFIX="wacsnap"
  29. SNAP_LIST=$(lvs --noheadings --select "lv_name=~$SNAP_PREFIX" --aligned --separator "$(echo -e '\t')" | tr -d ' ')
  30. while [[ -n "$SNAP_LIST" ]]; do
  31. echo "Previous snapshots(s) detected:"
  32. echo $(echo "$SNAP_LIST" | cut -f1)
  33.  
  34. LV=$(echo "$SNAP_LIST" | cut -f1 | head -n1)
  35. VG=$(echo "$SNAP_LIST" | cut -f2 | head -n1)
  36.  
  37. while true; do
  38. read -p "Do you want to continue your last session (Y/N)? " CONTINUE
  39. case $CONTINUE in
  40. [Yy]*)
  41. echo "Continuing last session."
  42. exit;;
  43. [Nn]*)
  44. break;;
  45. esac
  46. done
  47.  
  48. while true; do
  49. read -p "Delete current session (Y/N)? " DELETE
  50. case $DELETE in
  51. [Yy]*)
  52. lvconvert --merge ${VG}/${LV}
  53. read -p "Press enter to reboot." FOO
  54. reboot;;
  55. [Nn]*)
  56. break;;
  57. esac
  58. done
  59. echo
  60. echo
  61. done
  62.  
  63. # Create a temporary snapshot just in case the user takes a long
  64. # time to decide if they want to start a new session or to enter
  65. # maintainence mode. We don't want changes made while this prompt
  66. # is waiting to be committed to the base system...
  67. SIZE=$(echo "$ROOT_DATA" | cut -f4)
  68. lvcreate --snapshot --name $SNAP_PREFIX --size $SIZE $ROOT_DEV
  69.  
  70. # Ask if the user wants to start a new session or if they
  71. # are performing system maintainence (e.g. installing updates).
  72. # Delete temp snapshot and exit if performing system maintainence.
  73. while true; do
  74. read -p "Start new session? (Y/N) " STARTNEW
  75. case $STARTNEW in
  76. [Yy]*)
  77. break;;
  78. [Nn]*)
  79. echo "Entering maintainence mode. Enter 'Y' if asked to remove the '${SNAP_PREFIX}' volume."
  80. lvremove ${ROOT_VG}/${SNAP_PREFIX}
  81. exit;;
  82. esac
  83. done
  84.  
  85. # Ask user for their name and a one-word note. Rename the snapshot
  86. # with this information so that in future reboots we know what is
  87. # going on with this machine.
  88. read -p "What is your name (e.g ping)? " USER
  89. read -p "One-word session note (e.g. test-the-thing)? " NOTE
  90. lvrename ${ROOT_VG}/${SNAP_PREFIX} ${SNAP_PREFIX}_${USER}_${NOTE}
Add Comment
Please, Sign In to add comment