From 669eae0b415fe2284c998820b9380499e737ab3d Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Sun, 26 Jan 2020 20:45:51 +0100 Subject: [PATCH] btrfs-mksnapshot[-rootfs]: Clean up, use findmnt(8) --- btrfs/mksnapshot/btrfs-mksnapshot | 17 +++-------------- btrfs/mksnapshot/btrfs-mksnapshot-rootfs | 2 +- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/btrfs/mksnapshot/btrfs-mksnapshot b/btrfs/mksnapshot/btrfs-mksnapshot index 15edff9..3d2fde8 100755 --- a/btrfs/mksnapshot/btrfs-mksnapshot +++ b/btrfs/mksnapshot/btrfs-mksnapshot @@ -49,15 +49,7 @@ done [ -z "$DRY_RUN" ] || echo "VOLUME_PATH=$VOLUME_PATH" # Detect btrfs filesystem -FS_NAME=$(btrfs filesystem show -m / 2>/dev/null | head -1 | cut -d"'" -f2) -if [ -z "$FS_NAME" ]; then - FS_NAME=$(grep " btrfs " /etc/fstab \ - | grep "^LABEL=" \ - | grep " $VOLUME_PATH " \ - | cut -d'"' -f2 \ - | cut -d'=' -f2- \ - | cut -d' ' -f1) -fi +FS_NAME=$(btrfs filesystem show -m "$VOLUME_PATH" 2>/dev/null | head -1 | cut -d"'" -f2) if [ -z "$FS_NAME" ]; then echo "$NAME: Failed to detect btrfs filesystem label for \"$VOLUME_PATH\"!" exit 1 @@ -66,10 +58,7 @@ fi [ -z "$DRY_RUN" ] || echo "FS_NAME=$FS_NAME" # Detect btrfs subvolume name -VOLUME_NAME=$(grep " btrfs " /etc/fstab \ - | grep "subvol=" \ - | grep " $VOLUME_PATH " \ - | sed -r 's|.*subvol=([^, \t]*).*|\1|') +VOLUME_NAME=$(findmnt -nf -o source "$VOLUME_PATH" | grep -F '[' | sed -r 's|^.*\[/(.*)\]|\1|') if [ -z "$VOLUME_NAME" ] && [ "$VOLUME_PATH" = "/" ]; then VOLUME_NAME=$(btrfs subvolume get-default / | cut -d' ' -f9) fi @@ -90,7 +79,7 @@ if [ ! -d "$FS_MOUNT_PATH" ]; then echo "$NAME: Directory \"$FS_MOUNT_PATH\" does not exist!" exit 1 fi -if grep " btrfs " /proc/mounts | grep " $FS_MOUNT_PATH " >/dev/null 2>&1; then +if ! grep -q " $FS_MOUNT_PATH btrfs " /proc/mounts; then echo "$NAME: btrfs \"$FS_NAME\" seems not to be mounted on \"$FS_MOUNT_PATH\"!" exit 1 fi diff --git a/btrfs/mksnapshot/btrfs-mksnapshot-rootfs b/btrfs/mksnapshot/btrfs-mksnapshot-rootfs index 2744d03..dcb840d 100755 --- a/btrfs/mksnapshot/btrfs-mksnapshot-rootfs +++ b/btrfs/mksnapshot/btrfs-mksnapshot-rootfs @@ -19,7 +19,7 @@ Usage() [ $# -eq 0 ] || Usage -if grep -Fq ' / btrfs ' /etc/fstab; then +if [ "$(findmnt -fnr -o fstype /)" = "btrfs" ]; then if [ -x /usr/local/sbin/btrfs-mksnapshot ]; then /usr/local/sbin/btrfs-mksnapshot -1 / || exit 1 else -- 2.39.2