In bash scripting, how could I check elegantly if a specific location is a btrfs subvolume?
I do NOT want to know if the given location is in a btrfs file system (or subvolume). I want to know if the given location is the head of a subvolume.
Ideally, the solution could be written in a bash function so I could write:
if is_btrfs_subvolume $LOCATION; then
# ... stuff ...
fi
An 'elegant' solution would be readable, small in code, small in resource consumption.
Solution1: Using @kdave suggestions:
is_btrfs_subvolume() {
local dir=$1
[ "$(stat -f --format="%T" "$dir")" == "btrfs" ] || return 1
inode="$(stat --format="%i" "$dir")"
case "$inode" in
2|256)
return 0;;
*)
return 1;;
esac
}
Solution2: What I used before (only one call, but probably brittle):
is_btrfs_subvolume() {
btrfs subvolume show "$1" >/dev/null 2>&1
}
EDIT: Corrected and replaced list
by show
as the behavior of list
would not answer correctly on any normal btrfs
directory.
EDIT2: as @kdave didn't post a full version of his superior answer, I added it to my answer.