From 32bc55e068a0ab7f50b65c397650a386fcf6cbb9 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Thu, 2 May 2024 14:08:16 +0200 Subject: [PATCH] filesystem-functions.pl: Proper grab usage details on BTRFS volumes Use the newly intoduced btrfs_filesystem_usage() function to proper request and calculate the free and used space on a BTRFS volume. Signed-off-by: Stefan Schantl --- config/cfgroot/filesystem-functions.pl | 77 +++++++++++++++++++++----- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/config/cfgroot/filesystem-functions.pl b/config/cfgroot/filesystem-functions.pl index f822657f2..28c84fade 100644 --- a/config/cfgroot/filesystem-functions.pl +++ b/config/cfgroot/filesystem-functions.pl @@ -178,21 +178,68 @@ sub volumes_status (%) { $volumes{$dev}{'mpoint'} = $mpoint unless($volumes{$dev}{'mpoint'}); if ($options{'df'} eq "True") { - # Call df module to get details about space usage - # and request the output in bytes (1). - my $df = Filesys::Df::df($mpoint, 1); - - # Assign grabbed storrage details to the hash. - if (defined($df)) { - $volumes{$dev}{"size"} = $df->{blocks}; - $volumes{$dev}{"free"} = $df->{bfree}; - $volumes{$dev}{"avail"} = $df->{bavail}; - $volumes{$dev}{"used"} = $df->{used}; - $volumes{$dev}{"percent_used"} = $df->{per}; - $volumes{$dev}{"percent_free"} = int(100) - $df->{per}; - - # Undefine created df object for the processed mountpoint. - undef $df; + # Check if the device contains a BTRFS. + if ($fs eq "btrfs") { + # Call the corresponding BTRFS function to proper get + # the filesystem usage details. + my @usage = &btrfs_filesystem_usage($mpoint, "nocheck"); + + my $size; + my $allocated; + + # Loop through the output of the usage function. + foreach my $line (@usage) { + # Remove any newlines. + chomp($line); + + # Grab the total size of the filesystem. + if ($line =~/.*Device size:\s+(.*)/) { + $size = $1; + + # Grab the allocated space. + } elsif ($line =~/.*Device allocated:\s+(.*)/) { + $allocated = $1; + } + + # Break the loop in case the size and free space have been grabbed. + last if(($size) && ($allocated)); + } + + # Compute the unallocated space. + my $unallocated = $size - $allocated; + + # Compute the allocated space in percent. + my $allocated_percent = $allocated * 100 / $size; + + # Format the allocated percent with only 2 digits after comma. + $allocated_percent = sprintf("%.2f", $allocated_percent); + + # Assign the values to the hash. + $volumes{$dev}{"size"} = $size; + $volumes{$dev}{"free"} = $unallocated; + $volumes{$dev}{"avail"} = $unallocated; + $volumes{$dev}{"used"} = $allocated; + $volumes{$dev}{"percent_used"} = $allocated_percent; + $volumes{$dev}{"percent_free"} = int(100) - $allocated_percent; + + # Perform any other used filesystem. + } else { + # Call df module to get details about space usage + # and request the output in bytes (1). + my $df = Filesys::Df::df($mpoint, 1); + + # Assign grabbed storrage details to the hash. + if (defined($df)) { + $volumes{$dev}{"size"} = $df->{blocks}; + $volumes{$dev}{"free"} = $df->{bfree}; + $volumes{$dev}{"avail"} = $df->{bavail}; + $volumes{$dev}{"used"} = $df->{used}; + $volumes{$dev}{"percent_used"} = $df->{per}; + $volumes{$dev}{"percent_free"} = int(100) - $df->{per}; + + # Undefine created df object for the processed mountpoint. + undef $df; + } } } } -- 2.39.5