kdave / btrfsmaintenance

Scripts for btrfs maintenance tasks like periodic scrub, balance, trim or defrag on selected mountpoints or directories.
GNU General Public License v2.0
897 stars 79 forks source link

Fix function is_btrfs to cover <path|device> correctly. #112

Open develroo opened 1 year ago

develroo commented 1 year ago

Currently, in the functions is_btrfs, stat -f will incorrectly report device paths because /dev/ is a virtual filesystem and classified as tmpfs.

Eg.

stat -f /dev/sdb 
  File: "/dev/sdb"
    ID: eb91af7d7bda02dd Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2012292    Free: 2012292    Available: 2012292
Inodes: Total: 2012292    Free: 2011839

This is correct behaviour for stat as according to the coreutils info docs:

"stat does not search for specified device nodes in the file system list, instead operating on them directly"

As the BTRFS documentation specifies <path|device> in all the commands, this is obviously undesirable behaviour for the is_btrfs function.

This patch uses df to work around the virtual nature of devices in Linux, while still retaining compatibility with paths.

Hope this helps.

TheGreatMcPain commented 1 year ago

I tried this patch on my system which uses RAID5.

Unfortunately this only works for the first device in the array, since df only sees the first disk.

TheGreatMcPain commented 1 year ago

I think we can change is_btrfs() to this.

is_btrfs() {
    btrfs device stats $1 &>/dev/null
}