]> 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, 3 Jul 2025 14:52:24 +0000 (16:52 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Thu, 3 Jul 2025 14:52:24 +0000 (16:52 +0200)
This function calls the btrfsctrl binary and returns it's output as an
array.

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

index 65f3e7de79e01a3f1d55cb276ca9ffb80fd9efad..465a4beed459e3d537e8bcdec1556093a7b599d3 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 btrfsctrl binary lives.
+my $btrfsctrl = "/usr/local/bin/btrfsctrl";
+
 #
 ## 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("$btrfsctrl", "filesystem-usage", "$path");
+
+       # Return the grabbed output.
+       return @output;
+}
+
 1;