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