nuke-graph-directory.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. set -e
  3. dir="$1"
  4. if [ -z "$dir" ]; then
  5. {
  6. echo 'This script is for destroying old /var/lib/docker directories more safely than'
  7. echo ' "rm -rf", which can cause data loss or other serious issues.'
  8. echo
  9. echo "usage: $0 directory"
  10. echo " ie: $0 /var/lib/docker"
  11. } >&2
  12. exit 1
  13. fi
  14. if [ "$(id -u)" != 0 ]; then
  15. echo >&2 "error: $0 must be run as root"
  16. exit 1
  17. fi
  18. if [ ! -d "$dir" ]; then
  19. echo >&2 "error: $dir is not a directory"
  20. exit 1
  21. fi
  22. dir="$(readlink -f "$dir")"
  23. echo
  24. echo "Nuking $dir ..."
  25. echo ' (if this is wrong, press Ctrl+C NOW!)'
  26. echo
  27. ( set -x; sleep 10 )
  28. echo
  29. dir_in_dir() {
  30. inner="$1"
  31. outer="$2"
  32. [ "${inner#$outer}" != "$inner" ]
  33. }
  34. # let's start by unmounting any submounts in $dir
  35. # (like -v /home:... for example - DON'T DELETE MY HOME DIRECTORY BRU!)
  36. for mount in $(awk '{ print $5 }' /proc/self/mountinfo); do
  37. mount="$(readlink -f "$mount" || true)"
  38. if [ "$dir" != "$mount" ] && dir_in_dir "$mount" "$dir"; then
  39. ( set -x; umount -f "$mount" )
  40. fi
  41. done
  42. # now, let's go destroy individual btrfs subvolumes, if any exist
  43. if command -v btrfs > /dev/null 2>&1; then
  44. # Find btrfs subvolumes under $dir checking for inode 256
  45. # Source: http://stackoverflow.com/a/32865333
  46. for subvol in $(find "$dir" -type d -inum 256 | sort -r); do
  47. if [ "$dir" != "$subvol" ]; then
  48. ( set -x; btrfs subvolume delete "$subvol" )
  49. fi
  50. done
  51. fi
  52. # finally, DESTROY ALL THINGS
  53. ( shopt -s dotglob; set -x; rm -rf "$dir"/* )