2017-03-28 21:41:32 +02:00
|
|
|
#!/hint/bash
|
2017-03-28 22:13:18 +02:00
|
|
|
# License: Unspecified
|
2017-05-06 00:41:10 +02:00
|
|
|
:
|
2017-03-28 21:41:32 +02:00
|
|
|
|
2017-05-06 00:41:10 +02:00
|
|
|
# shellcheck disable=2034
|
2017-03-07 19:19:29 +01:00
|
|
|
CHROOT_VERSION='v4'
|
2017-03-07 19:27:36 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
# usage : check_root
|
|
|
|
##
|
|
|
|
orig_argv=("$0" "$@")
|
|
|
|
check_root() {
|
|
|
|
(( EUID == 0 )) && return
|
|
|
|
if type -P sudo >/dev/null; then
|
|
|
|
exec sudo -- "${orig_argv[@]}"
|
|
|
|
else
|
|
|
|
exec su root -c "$(printf ' %q' "${orig_argv[@]}")"
|
|
|
|
fi
|
|
|
|
}
|
2017-03-07 19:55:05 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
# usage : is_btrfs( $path )
|
|
|
|
# return : whether $path is on a btrfs
|
|
|
|
##
|
|
|
|
is_btrfs() {
|
|
|
|
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
|
|
|
|
}
|
2017-03-07 20:14:50 +01:00
|
|
|
|
2017-05-06 00:40:58 +02:00
|
|
|
##
|
|
|
|
# usage : is_subvolume( $path )
|
|
|
|
# return : whether $path is a the root of a btrfs subvolume (including
|
|
|
|
# the top-level subvolume).
|
|
|
|
##
|
|
|
|
is_subvolume() {
|
|
|
|
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs && "$(stat -c %i "$1")" == 256 ]]
|
|
|
|
}
|
|
|
|
|
2017-05-06 00:41:00 +02:00
|
|
|
##
|
|
|
|
# usage : is_same_fs( $path_a, $path_b )
|
|
|
|
# return : whether $path_a and $path_b are on the same filesystem
|
|
|
|
##
|
|
|
|
is_same_fs() {
|
|
|
|
[[ "$(stat -c %d "$1")" == "$(stat -c %d "$1")" ]]
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:14:50 +01:00
|
|
|
##
|
|
|
|
# usage : subvolume_delete_recursive( $path )
|
|
|
|
#
|
|
|
|
# Find all btrfs subvolumes under and including $path and delete them.
|
|
|
|
##
|
|
|
|
subvolume_delete_recursive() {
|
|
|
|
local subvol
|
|
|
|
|
2017-05-06 00:40:58 +02:00
|
|
|
is_subvolume "$1" || return 0
|
2017-03-07 20:14:50 +01:00
|
|
|
|
|
|
|
while IFS= read -d $'\0' -r subvol; do
|
2017-05-06 00:41:01 +02:00
|
|
|
if ! subvolume_delete_recursive "$subvol"; then
|
2017-03-07 20:14:50 +01:00
|
|
|
return 1
|
|
|
|
fi
|
2017-05-06 00:41:01 +02:00
|
|
|
done < <(find "$1" -mindepth 1 -xdev -depth -inum 256 -print0)
|
|
|
|
if ! btrfs subvolume delete "$1" &>/dev/null; then
|
|
|
|
error "Unable to delete subvolume %s" "$subvol"
|
|
|
|
return 1
|
|
|
|
fi
|
2017-03-07 20:14:50 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|