vol=${d##*/}
# second field, non-comment line == $d
dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab /etc/mtab|head -n1)
vol=${d##*/}
# second field, non-comment line == $d
dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab /etc/mtab|head -n1)
d dev=$dev
subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab /etc/mtab|head -n1)
d subvol_dir=$subvol_dir
d dev=$dev
subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab /etc/mtab|head -n1)
d subvol_dir=$subvol_dir
# because the device in fstab for the rootfs can be different.
for devx in $(btrfs fi show $dev| sed -rn 's#.*path (/\S+)$#\1#p'); do
d devx=$devx
# because the device in fstab for the rootfs can be different.
for devx in $(btrfs fi show $dev| sed -rn 's#.*path (/\S+)$#\1#p'); do
d devx=$devx
- root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc/mtab|head -n1)
+ root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=[05]\b.*,\1,p" /etc/mtab /etc/fstab|head -n1)
last_received_cgen=0
for f in ${snaps[@]}; do
show="$(btrfs sub show $f)"
if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
last_received_cgen=0
for f in ${snaps[@]}; do
show="$(btrfs sub show $f)"
if echo "$show" | grep -E "Received UUID:\s+[[:alnum:]]" &>/dev/null; then
cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
if [[ $cgen -gt $last_received_cgen ]]; then
last_received_cgen=$cgen
cgen=$(echo "$show" | sed -rn 's,^\s*Gen at creation:\s+([0-9]+).*,\1,p')
if [[ $cgen -gt $last_received_cgen ]]; then
last_received_cgen=$cgen
stale=true
# fresh if $svp has $last_snap as a snapshot,
if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
stale=true
# fresh if $svp has $last_snap as a snapshot,
if btrfs sub show $svp 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
stale=false
else # or else $svp is a snapshot of $last_snap. we use a uuid
# comparison, which if I remember from the docs, is a bit more
stale=false
else # or else $svp is a snapshot of $last_snap. we use a uuid
# comparison, which if I remember from the docs, is a bit more