Ignore the "nfs unmount" part, it's just a quick and dirty way to get the thing done. The neat trick here was the use of single quotes around 'EOF'. This way, I was able to embed perl code without having to worry about interpolation. No ugly escaping required ;)
#!/bin/bash
# quick and dirty
# assumes that anything that's not solaris is linux.
HOST=$1
cat <<'EOF' | ssh -q -oStrictHostKeyChecking=no -oConnectTimeOut=10 $HOST 'cat > /tmp/nfsUnmount.pl;
chmod +x /tmp/nfsUnmount.pl; /tmp/nfsUnmount.pl'
#!/usr/bin/perl
use strict;
die "execute me as root!\n" if $> ne 0;
# main
my @mounts = @{grabNsMounts()};
if (@mounts) {
foreach (@mounts) {
unmount($_);
}
} else {
print "no nfs mounts found!\n";
}
handleNscd();
exit 0;
sub handleNscd {
qx[pkill -9 nscd];
if ($^O eq "solaris") {
print qx[svcadm enable nscd];
} else {
print qx[/etc/init.d/nscd restart];
}
return 0;
}
sub unmount {
my $share = shift;
die "empty share detected" if ! $share;
chomp(my $out = qx[umount $share 2>&1]);
if ($?) {
print "WARNING: $share encountered some errors while unmounting: $out. forcing unmount...\n";
fUnmount($share);
} else {
if (qx[mount | grep $share | grep -v grep]) {
print "WARNING: $share is still mounted, force unmounting...\n";
fUnmount($share);
}
}
}
sub fUnmount {
my $share = shift;
die "empty share detected" if ! $share;
my $out;
if ($^O eq "solaris") {
chomp($out = qx[umount -f $share 2>&1]);
} else {
chomp($out = qx[umount -l $share 2>&1]);
}
if (qx[mount | grep $share | grep -v grep]) {
print "ERROR: $share is still mounted after force/lazy unmount. Skipping...\n";
}
return 0;
}
sub grabNfsMounts {
my ($out, @shares);
if ($^O eq "solaris") {
$out = qx[mount -p | grep ' nfs '];
} else {
$out = qx[mount -t nfs];
}
my @out = split(/\n/, $out);
foreach (@out) {
push(@shares, (split(/\s+/, $_))[2]);
}
return \@shares;
}
EOF
No comments:
Post a Comment