From b5e08b243df8501fe9114ef060c3871acce61150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sun, 30 Aug 2026 19:51:31 -0300 Subject: [PATCH] refactor(svrutils): centralize NFS export setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Vinícius Ferrão <2031761+viniciusferrao@users.noreply.github.com> --- xCAT-server/lib/perl/xCAT/SvrUtils.pm | 96 ++++++++++++--------------- 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/xCAT-server/lib/perl/xCAT/SvrUtils.pm b/xCAT-server/lib/perl/xCAT/SvrUtils.pm index d28a456d4..801039123 100644 --- a/xCAT-server/lib/perl/xCAT/SvrUtils.pm +++ b/xCAT-server/lib/perl/xCAT/SvrUtils.pm @@ -1667,33 +1667,7 @@ sub setupNFSTree { my $nfsdirectory = $2; if ($nfsserver eq $sip) { # on the service node - - unless (-d $nfsdirectory) { - if (-e $nfsdirectory) { - unlink $nfsdirectory; - } - mkpath $nfsdirectory; - } - - $cmd = "showmount -e $nfsserver"; - my @entries = xCAT::Utils->runcmd($cmd, 0); - shift @entries; - if (grep /\Q$nfsdirectory\E/, @entries) { - $callback->({ data => ["$nfsdirectory has been exported already!"] }); - } else { - $cmd = "/usr/sbin/exportfs :$nfsdirectory -o rw,no_root_squash,sync,no_subtree_check,insecure"; - xCAT::Utils->runcmd($cmd, 0); - $callback->({ data => ["now $nfsdirectory is exported!"] }); - } - - if (ensure_nfs_export_option($nfsdirectory, 'insecure')) { - xCAT::Utils->runcmd("/usr/sbin/exportfs -r", 0); - $callback->({ data => ["added insecure to existing $nfsdirectory export"] }); - } elsif (!nfs_export_exists($nfsdirectory)) { - $cmd = qq{echo "$nfsdirectory *(rw,no_root_squash,sync,no_subtree_check,insecure)" >> /etc/exports}; - xCAT::Utils->runcmd($cmd, 0); - $callback->({ data => ["$nfsdirectory is added to /etc/exports with default option"] }); - } + _ensure_nfs_exported($nfsserver, $nfsdirectory, $callback); } } } @@ -1712,6 +1686,44 @@ sub nfs_export_exists { return 0; } +sub _ensure_nfs_exported { + my ($nfsserver, $nfsdirectory, $callback, %opts) = @_; + my $export_options = 'rw,no_root_squash,sync,no_subtree_check,insecure'; + + unless (-d $nfsdirectory) { + if (-e $nfsdirectory) { + unlink $nfsdirectory; + } + mkpath $nfsdirectory; + } + + my $cmd = "showmount -e $nfsserver"; + my @entries = xCAT::Utils->runcmd($cmd, 0); + shift @entries; + if (grep /\Q$nfsdirectory\E/, @entries) { + $callback->({ data => ["$nfsdirectory has been exported already!"] }); + } else { + $cmd = "/usr/sbin/exportfs :$nfsdirectory -o $export_options"; + xCAT::Utils->runcmd($cmd, 0); + $callback->({ data => ["now $nfsdirectory is exported!"] }); + } + + if (ensure_nfs_export_option($nfsdirectory, 'insecure')) { + xCAT::Utils->runcmd("/usr/sbin/exportfs -r", 0); + $callback->({ data => ["added insecure to existing $nfsdirectory export"] }); + } elsif (!nfs_export_exists($nfsdirectory)) { + my $append_separator = exists($opts{append_separator}) + ? $opts{append_separator} + : ' '; + $cmd = qq{echo "$nfsdirectory *($export_options)" >>$append_separator/etc/exports}; + xCAT::Utils->runcmd($cmd, 0); + my $message = exists($opts{added_message}) + ? $opts{added_message} + : "$nfsdirectory is added to /etc/exports with default option"; + $callback->({ data => [$message] }); + } +} + sub setupStatemnt { my $sip = shift; if (($sip) && ($sip =~ /xCAT::SvrUtils/)) @@ -1726,31 +1738,11 @@ sub setupStatemnt { my $nfsserver = $1; my $nfsdirectory = $2; if ($sip eq xCAT::NetworkUtils->getipaddr($nfsserver)) { - unless (-d $nfsdirectory) { - if (-e $nfsdirectory) { - unlink $nfsdirectory; - } - mkpath $nfsdirectory; - } - - my $cmd = "showmount -e $nfsserver"; - my @entries = xCAT::Utils->runcmd($cmd, 0); - shift @entries; - if (grep /\Q$nfsdirectory\E/, @entries) { - $callback->({ data => ["$nfsdirectory has been exported already!"] }); - } else { - $cmd = "/usr/sbin/exportfs :$nfsdirectory -o rw,no_root_squash,sync,no_subtree_check,insecure"; - xCAT::Utils->runcmd($cmd, 0); - $callback->({ data => ["now $nfsdirectory is exported!"] }); - } - - if (ensure_nfs_export_option($nfsdirectory, 'insecure')) { - xCAT::Utils->runcmd("/usr/sbin/exportfs -r", 0); - $callback->({ data => ["added insecure to existing $nfsdirectory export"] }); - } elsif (!nfs_export_exists($nfsdirectory)) { - xCAT::Utils->runcmd(qq{echo "$nfsdirectory *(rw,no_root_squash,sync,no_subtree_check,insecure)" >>/etc/exports}, 0); - $callback->({ data => ["$nfsdirectory is added into /etc/exports with default options"] }); - } + _ensure_nfs_exported( + $nfsserver, $nfsdirectory, $callback, + append_separator => '', + added_message => "$nfsdirectory is added into /etc/exports with default options", + ); } }