File: //scripts.20110531.215904.25158/clear_orphaned_virtfs_mounts
#!/usr/bin/perl
# cpanel - clear_orphaned_virtfs_mounts Copyright(c) 2010 cPanel, Inc.
# All Rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use warnings;
use Cpanel::Config::LoadUserDomains ();
use Cpanel::CPAN::Getopt::Param ();
use Cpanel::Filesys::Virtfs ();
use Cpanel::AcctUtils ();
my $prm = Cpanel::CPAN::Getopt::Param->new(
{
'help_coderef' => sub {
print <<"END_USAGE";
Unmount any virtfs mounts whose users no longer exist or whose shell is not currently jailshell
$0 --help - this screen
$0 --errorsonly - Do not have any output unless there are errors
$0 --clearall - Unmount all virtfs mounts regardless of user's jailshell status
END_USAGE
exit;
},
# 'actions' => [
# [ 'usage', 1 ], # help alias
# [ 'version', sub { print "$0 v0.1\n"; exit; } ], # $0 --version - print version informtaion and exit
# ],
}
);
my %user_map = %{ Cpanel::Config::LoadUserDomains::loaduserdomains( undef, 0, 1 ) };
my $errorsonly = $prm->get_param('errorsonly') ? 1 : 0;
my $clear_all = $prm->get_param('clearall') ? 1 : 0;
my %processed_users;
for my $mount ( Cpanel::Filesys::Virtfs::get_virtfs_mounts() ) {
my $username = Cpanel::Filesys::Virtfs::get_username_from_virtfs_mount_string($mount);
next if exists $processed_users{$username};
print "-- Begin user '$username' --\n" unless $errorsonly;
my $umount = 0;
if ( !exists $user_map{$username} ) {
print "User no longer exists, cleaning orphan...\n" unless $errorsonly;
$umount++;
}
elsif ( $clear_all || ( Cpanel::AcctUtils::getshell($username) ne Cpanel::Filesys::Virtfs::get_jailshell_path() ) ) {
print "User no longer has jailshell, cleaning orphan...\n" unless ( $errorsonly || $clear_all );
$umount++;
}
else {
print "No action needed\n" unless $errorsonly;
}
if ($umount) {
print "Cleaning virtfs mounts (if any)\n" unless $errorsonly;
my ( $rc, @errors ) = Cpanel::Filesys::Virtfs::clean_user_virtfs($username);
if ($rc) {
print "Done\n" unless $errorsonly;
$processed_users{$username}++;
}
else {
print "Failed\n" . join( "\t", @errors ) . "\n";
}
}
else {
$processed_users{$username}++;
}
print "-- End user '$username' -- \n\n" unless $errorsonly;
}