Browse code

Nuke when graphdir is a mount

Previously, when the graphdir was a root of a mountpoint, we would not
be able to cleanup the graphdir, as the script would umount in case
target dir is a mount itself

```/etc/mtab
/dev/xvdc1 /var/lib/docker btrfs
```

When running the script, the graphdir would be unmounted and it would
remove a (possibly) empty folder.

```
Nuking /var/lib/docker ...
(if this is wrong, press Ctrl+C NOW!)

+ sleep 10

+ umount -f /var/lib/docker
+ rm -rf /var/lib/docker
```

This PR includes the necessary changes to nuke the folder on this
scenario, including when the graphdir is a btrfs mount iself.

Signed-off-by: Bruno Tavares <btavare@thoughtworks.com>

Bruno Tavares authored on 2016/10/29 04:41:40
Showing 1 changed files
... ...
@@ -44,7 +44,7 @@ dir_in_dir() {
44 44
 #   (like -v /home:... for example - DON'T DELETE MY HOME DIRECTORY BRU!)
45 45
 for mount in $(awk '{ print $5 }' /proc/self/mountinfo); do
46 46
 	mount="$(readlink -f "$mount" || true)"
47
-	if dir_in_dir "$mount" "$dir"; then
47
+	if [ "$dir" != "$mount" ] && dir_in_dir "$mount" "$dir"; then
48 48
 		( set -x; umount -f "$mount" )
49 49
 	fi
50 50
 done
... ...
@@ -54,9 +54,11 @@ if command -v btrfs > /dev/null 2>&1; then
54 54
 	# Find btrfs subvolumes under $dir checking for inode 256
55 55
 	# Source: http://stackoverflow.com/a/32865333
56 56
 	for subvol in $(find "$dir" -type d -inum 256 | sort -r); do
57
-		( set -x; btrfs subvolume delete "$subvol" )
57
+		if [ "$dir" != "$subvol" ]; then
58
+			( set -x; btrfs subvolume delete "$subvol" )
59
+		fi
58 60
 	done
59 61
 fi
60 62
 
61 63
 # finally, DESTROY ALL THINGS
62
-( set -x; rm -rf "$dir" )
64
+( shopt -s dotglob; set -x; rm -rf "$dir"/* )