github.com/olljanat/moby@v1.13.1/contrib/nuke-graph-directory.sh (about)

     1  #!/bin/sh
     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  ( set -x; sleep 10 )
    35  echo
    36  
    37  dir_in_dir() {
    38  	inner="$1"
    39  	outer="$2"
    40  	[ "${inner#$outer}" != "$inner" ]
    41  }
    42  
    43  # let's start by unmounting any submounts in $dir
    44  #   (like -v /home:... for example - DON'T DELETE MY HOME DIRECTORY BRU!)
    45  for mount in $(awk '{ print $5 }' /proc/self/mountinfo); do
    46  	mount="$(readlink -f "$mount" || true)"
    47  	if [ "$dir" != "$mount" ] && dir_in_dir "$mount" "$dir"; then
    48  		( set -x; umount -f "$mount" )
    49  	fi
    50  done
    51  
    52  # now, let's go destroy individual btrfs subvolumes, if any exist
    53  if command -v btrfs > /dev/null 2>&1; then
    54  	# Find btrfs subvolumes under $dir checking for inode 256
    55  	# Source: http://stackoverflow.com/a/32865333
    56  	for subvol in $(find "$dir" -type d -inum 256 | sort -r); do
    57  		if [ "$dir" != "$subvol" ]; then
    58  			( set -x; btrfs subvolume delete "$subvol" )
    59  		fi
    60  	done
    61  fi
    62  
    63  # finally, DESTROY ALL THINGS
    64  ( shopt -s dotglob; set -x; rm -rf "$dir"/* )