github.com/nguyentm83/docker@v1.5.0/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_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  	root="$(df "$dir" | awk 'NR>1 { print $NF }')"
    55  	root="${root#/}" # if root is "/", we want it to become ""
    56  	for subvol in $(btrfs subvolume list -o "$root/" 2>/dev/null | awk -F' path ' '{ print $2 }' | sort -r); do
    57  		subvolDir="$root/$subvol"
    58  		if dir_in_dir "$subvolDir" "$dir"; then
    59  			( set -x; btrfs subvolume delete "$subvolDir" )
    60  		fi
    61  	done
    62  fi
    63  
    64  # finally, DESTROY ALL THINGS
    65  ( set -x; rm -rf "$dir" )