btrfs: Use word mathcing for _btrfs_get_subvolid()
authorQu Wenruo <wqu@suse.com>
Fri, 7 Feb 2020 01:59:40 +0000 (09:59 +0800)
committerEryu Guan <guaneryu@gmail.com>
Sun, 9 Feb 2020 15:56:09 +0000 (23:56 +0800)
Current _btrfs_get_subvolid() can't handle the following case at all:
  # btrfs subvol list $SCRATCH_MNT
  ID 256 gen 9 top level 5 path subv1
  ID 257 gen 7 top level 256 path subv1/subv2
  ID 258 gen 8 top level 256 path subv1/subv3
  ID 259 gen 9 top level 256 path subv1/subv4

If we call "_btrfs_get_subvolid $SCRATCH_MNT subv1" we will get a list
of all subvolumes, not the subvolid of subv1.

To address this problem, we go egrep to match $name which starts with a
space, and at the end of a line.
So that all other subvolumes won't hit.

Suggested-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
common/btrfs

index 19ac7cc4b18cd93000e3e2df92365f4e21a91638..85b33e4c067a020ea5f0d5043a9b25c02f1d40ad 100644 (file)
@@ -7,7 +7,7 @@ _btrfs_get_subvolid()
        mnt=$1
        name=$2
 
-       $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
+       $BTRFS_UTIL_PROG sub list $mnt | egrep "\s$name$" | awk '{ print $2 }'
 }
 
 # _require_btrfs_command <command> [<subcommand>|<option>]