]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
filesystem-functions.pl: Add btrfs_filesystem_usage() function
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 2 May 2024 12:05:16 +0000 (14:05 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 2 May 2024 12:05:16 +0000 (14:05 +0200)
This function calls the btrfs binary and returns it's output as an
array.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/filesystem-functions.pl

index cc355efa31bc52effb5cde919c218cc8cb057897..f822657f2b796e12dd2cbd04792e651e09d6468c 100644 (file)
@@ -21,6 +21,8 @@
 
 package Filesystem;
 
+require '/var/ipfire/general-functions.pl';
+
 use strict;
 
 # Import perl module to get details about the space usage.
@@ -32,6 +34,9 @@ my $sysfs_block_dir = "/sys/class/block";
 # Directory where the uuid mappings can be found.
 my $uuid_dir = "/dev/disk/by-uuid";
 
+# Location where the btrfs binary lives.
+my $btrfs_bin = "/usr/bin/btrfs";
+
 #
 ## Function which returns a lot of details about the known devices
 ## as a hash of hashes.
@@ -475,4 +480,28 @@ sub is_btrfs($) {
        # Return nothing if the mointpoint is not mounted as BTRFS.
        return;
 }
+
+#
+## Function which simply calls the filesystem usage of the btrfs tool and
+## returns it's output as array.
+#
+sub btrfs_filesystem_usage($$) {
+       my ($path, $check) = @_;
+
+       # Default to check to determine if the given path contains a BTRFS
+       $check //= "check";
+
+       # Only run the is_btrfs check if requested or not set.
+       if ($check eq "check") {
+               # Abort if the given path does not exist or does not contain a BTRFS.
+               return unless(&is_btrfs($path));
+       }
+
+       # Call btrfs binary to get details about the current filesystem usage.
+       my @output = &General::system_output("$btrfs_bin", "filesystem", "usage", "-b", "$path");
+
+       # Return the grabbed output.
+       return @output;
+}
+
 1;