[pve-devel] [PATCH pve-common] new helper PVE::Tools::du() - get disk usage

Dietmar Maurer dietmar at proxmox.com
Wed Apr 25 10:37:36 CEST 2018


We simply call the external binary 'du', so that we can abort the command
when we run into a timeout.

Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 src/PVE/Tools.pm | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 6a2dae4..1c5b502 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -997,6 +997,26 @@ sub df {
     };
 }
 
+sub du {
+    my ($path, $timeout) = @_;
+
+    my $size;
+
+    $timeout //= 10;
+
+    my $parser = sub {
+	my $line = shift;
+
+	if ($line =~ m/^(\d+)\s+total$/) {
+	    $size = $1;
+	}
+    };
+
+    run_command(['du', '-scb', $path], outfunc => $parser, timeout => $timeout);
+
+    return $size;
+}
+
 # UPID helper
 # We use this to uniquely identify a process.
 # An 'Unique Process ID' has the following format:
-- 
2.11.0



More information about the pve-devel mailing list