From b658fba08c44bd3352768e85d9c8596501857366 Mon Sep 17 00:00:00 2001 From: Axel Burri Date: Sun, 25 Jul 2021 16:36:18 +0200 Subject: [PATCH] btrbk: print snapdir and targets for action "usage" --- btrbk | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/btrbk b/btrbk index 413e412..9edd872 100755 --- a/btrbk +++ b/btrbk @@ -262,9 +262,9 @@ my %table_formats = ( }, usage => { - table => [ qw( -host -port path size used free ) ], - long => [ qw( type -host -port path size used device_size device_allocated device_unallocated device_missing device_used free free_min data_ratio metadata_ratio global_reserve global_reserve_used ) ], - raw => [ qw( type host port path size used device_size device_allocated device_unallocated device_missing device_used free free_min data_ratio metadata_ratio global_reserve global_reserve_used ) ], + table => [ qw( -host -port mount_source path size used free ) ], + long => [ qw( type -host -port mount_source path size used device_size device_allocated device_unallocated device_missing device_used free free_min data_ratio metadata_ratio global_reserve global_reserve_used ) ], + raw => [ qw( type host port mount_source path size used device_size device_allocated device_unallocated device_missing device_used free free_min data_ratio metadata_ratio global_reserve global_reserve_used ) ], RALIGN => { size=>1, used=>1, device_size=>1, device_allocated=>1, device_unallocated=>1, device_missing=>1, device_used=>1, free=>1, free_min=>1, data_ratio=>1, metadata_ratio=>1, global_reserve=>1, global_reserve_used=>1 }, }, @@ -6503,32 +6503,30 @@ MAIN: # print filesystem information # my @data; + my %usage_cache; my %processed; - foreach my $sroot (vinfo_subsection($config, 'volume')) { - unless($processed{$sroot->{URL}}) { - my $usage = btrfs_filesystem_usage($sroot) // {}; - push @data, { %$usage, - type => "source", - vinfo_prefixed_keys("", $sroot), - }; - $processed{$sroot->{URL}} = 1; - } - } - + my $push_data = sub { + my ($vol, $type) = @_; + return if $processed{$vol->{URL}}; + my (undef, undef, undef, $mount_source, undef) = btrfs_mountpoint($vol); + my $mid = $vol->{MACHINE_ID} . $mount_source; + $usage_cache{$mid} //= btrfs_filesystem_usage($vol); + push @data, { %{$usage_cache{$mid}}, + type => $type, + mount_source => $mount_source, + vinfo_prefixed_keys("", $vol), + }; + $processed{$vol->{URL}} = 1; + }; foreach my $sroot (vinfo_subsection($config, 'volume')) { foreach my $svol (vinfo_subsection($sroot, 'subvolume')) { + $push_data->(vinfo_snapshot_root($svol), "source"); foreach my $droot (vinfo_subsection($svol, 'target')) { - unless($processed{$droot->{URL}}) { - my $usage = btrfs_filesystem_usage($droot) // {}; - push @data, { %$usage, - type => "target", - vinfo_prefixed_keys("", $droot), - }; - $processed{$droot->{URL}} = 1; - } + $push_data->(vinfo_snapshot_root($droot), "target"); } } } + @data = sort { $a->{type} cmp $b->{type} || $a->{url} cmp $b->{url} } @data; print_formatted("usage", \@data); exit exit_status($config); }