zpool: activate: move mount check out and make program flow easier

Early return when mounted heuristics returns true, that allows to get
rid of an indentation level.

Moving the heuristic out makes the activate method smaller and easier
to grasp

Best viewed with ignoring whitespace changes (`git show -w`).

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2021-02-19 15:21:16 +01:00
parent 9440330aba
commit 26de022b56

View File

@ -522,6 +522,21 @@ sub volume_snapshot_list {
return $snaps; return $snaps;
} }
my sub dataset_mounted_heuristic {
my ($dataset) = @_;
my $mounts = PVE::ProcFSTools::parse_proc_mounts();
for my $mp (@$mounts) {
my ($what, $dir, $fs) = $mp->@*;
next if $fs ne 'zfs';
# check for root-dataset or any child-dataset (root-dataset could have 'canmount=off')
# If any child is mounted heuristically assume that `zfs mount -a` was successful
next if $what !~ m!^$dataset(?:/|$)!;
return 1;
}
return 0;
}
sub activate_storage { sub activate_storage {
my ($class, $storeid, $scfg, $cache) = @_; my ($class, $storeid, $scfg, $cache) = @_;
@ -529,19 +544,7 @@ sub activate_storage {
my $dataset = $scfg->{pool}; my $dataset = $scfg->{pool};
my $pool = ($dataset =~ s!/.*$!!r); my $pool = ($dataset =~ s!/.*$!!r);
my $dataset_mounted = sub { return 1 if dataset_mounted_heuristic($dataset); # early return
my $mounts = PVE::ProcFSTools::parse_proc_mounts();
foreach my $mp (@$mounts) {
my ($what, $dir, $fs) = @$mp;
next if $fs ne 'zfs';
# check for root-dataset of storage or any child-dataset.
# root-dataset could have 'canmount=off'. If any child is mounted
# heuristically assume that `zfs mount -a` was successful
next if $what !~ m!^$dataset(?:/|$)!;
return 1;
}
return 0;
};
my $pool_imported = sub { my $pool_imported = sub {
my @param = ('-o', 'name', '-H', $pool); my @param = ('-o', 'name', '-H', $pool);
@ -551,7 +554,6 @@ sub activate_storage {
return defined($res) && $res =~ m/$pool/; return defined($res) && $res =~ m/$pool/;
}; };
if (!$dataset_mounted->()) {
if (!$pool_imported->()) { if (!$pool_imported->()) {
# import can only be done if not yet imported! # import can only be done if not yet imported!
my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool); my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool);
@ -563,7 +565,6 @@ sub activate_storage {
} }
eval { $class->zfs_request($scfg, undef, 'mount', '-a') }; eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
die "could not activate storage '$storeid', $@\n" if $@; die "could not activate storage '$storeid', $@\n" if $@;
}
return 1; return 1;
} }