- vol=${d##*/}
- dev=$(sed -rn "s,^\s*([^#]\S*)\s+$d\s.*,\1,p" /etc/fstab|head -n1)
- subvol_dir=$(sed -rn "s,^\s*[^#]\S*\s+$d\s.*\bsubvol=([a-zA-A/]+).*,\1,p" /etc/fstab|head -n1)
- # note, we need $dev because $d might not be mounted, and we do this loop
- # 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
- root_dir=$(sed -rn "s,^\s*$devx\s+(\S+).*\bsubvolid=0\b.*,\1,p" /etc/fstab|head -n1)
- if [[ $root_dir ]]; then break; fi
- done
- svp=$root_dir/$subvol_dir # subvolume path
-
- snaps=($root_dir/btrbk/$subvol_dir.20*) # Assumes we are in the 21st century.
- if [[ ! $snaps ]]; then
- # no snapshots yet
- echo "$0: warning. no snapshots found. this is expected for a brand new volume"
- continue
- fi
-
- # get info on last received sub
- last_received_gen=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_gen ]]; then
- last_received_cgen=$cgen
- last_received=$f
- fi
- fi
- done
-
- # Get last_snap by date.
- # when a btrbk bugfix makes it into the distro,
- # we might replace this with btrbk list latest /mnt/root/$vol | ...
- last_snap=$(
- for s in ${snaps[@]}; do
- f=${s##*/}
- unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
- printf "%s %s\n" $unix_time $s
- done | sort -r | head -n 1 | awk '{print $2}'
- )
- if [[ ! $last_snap ]]; then
- # should not happen.
- echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}"
- ret=1
- continue
+ 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
+ # note, we need $dev because $d might not be mounted, and we do this loop
+ # 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 /etc/fstab|head -n1)
+ if [[ $root_dir ]]; then
+ d root_dir=$root_dir
+ break