]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
filesystem-functions.pl: Proper grab usage details on BTRFS volumes
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 2 May 2024 12:08:16 +0000 (14:08 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 2 May 2024 12:08:16 +0000 (14:08 +0200)
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 <stefan.schantl@ipfire.org>
config/cfgroot/filesystem-functions.pl

index f822657f2b796e12dd2cbd04792e651e09d6468c..28c84fade954267b22ac0655ce03677171d77e19 100644 (file)
@@ -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;
+                               }
                        }
                 }
         }