Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Check if home dir is mounted?

by Anonymous Monk
on Apr 10, 2007 at 23:22 UTC ( #609267=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, All,

I have written a perl script that reads in a list of workstation names and checks the system load of each. After probing each system (Linux workstations), it auto-derives the one with the lightest load and completes by automatically logging the user into that system.

It works great 90% of the time, but sometimes a user gets automatically logged into a system that doesn't have his or her home directory mounted. How can I perform a simple check to make sure a user's home directory is mounted to a specific system?

Assume the worst: I only know the user's login name and the workstation to probe (i.e. I don't even know which NFS volume possesses the user's home...)

Thanks!

Replies are listed 'Best First'.
Re: Check if home dir is mounted?
by f00li5h (Chaplain) on Apr 11, 2007 at 03:46 UTC

    First off, you need to know where the user lives, ask the system with getpwnam and friends. Then try to mount the home dir. It's up to your system's /etc/fstab (and by fstab, nsswitch as you may use NIS+ or something)

    use User::pwent; my $username = 'f00li5h'; my $user = getpwnam($username); printf "%s aka (%s) lives in %s $/", $user->name, $user->gecos, $user->dir; #f00li5h aka (f00li5h and5illy) lives in /home/f00li5h use File::Spec; my @paths = File::Spec->splitdir( $user->dir ); # start at the most specific directory (/home/f00li5h) head # to the lest specific (/) while( @paths ){ # get the system to mount (rather than just print) printf "mount %s $/", File::Spec->catdir( @paths ); last if -d $user->dir; # stop looking when the dir turns up pop @paths ; }

    This has massive massive YMMV attached to it and I'm sure there's a better way than blindly mounting things until the homedir appears, particularly if the user's homedir is with a symlink of some sort.

    /home/f00li5h -> /media/homes/big/f00li5h
    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: Check if home dir is mounted?
by thezip (Vicar) on Apr 11, 2007 at 00:18 UTC

    Hello,

    Could you please share the pertinent portion(s) of your script with us so that we see where to integrate the requested check?


    Where do you want *them* to go today?
Re: Check if home dir is mounted?
by dana (Monk) on Apr 11, 2007 at 02:12 UTC

    Although users don't necessarily have home directories mounted on each system, do their home directories definitely match their usernames? (for instance username = dana and home directory = /home/dana).

Re: Check if home dir is mounted?
by shmem (Chancellor) on Apr 11, 2007 at 10:11 UTC
    In order to see what volumes are mounted on a particular machine, you have to login into it. I'd just log in and chdir to $HOME (or ~$user, if not logging in as that particular user), failing that, log out, mark that machine as bad for this particular user and grab the next one in the list.

    Easiest would be a statement in the system's SHELL rc (.profile, .bashrc, whatever) like

    cd $HOME || exit
    so users who don't get their home on that machine get logged out immediately.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Hi,

      Thanks for the replies. Previously, the last line of my code was this:-

      $Launch = system ("rsh $BestHost");

      ...where $BestHost was derived earlier. Now, however, I am just going to do this:-

      $Launch = system ("rsh $BestHost \"cd ~$user\"");

      ...and if I detect a "not found", I will move to the next best machine name.

      Thanks for all the replies!

      Kind Regards,

      Kehliah

Re: Check if home dir is mounted?
by klekker (Pilgrim) on Apr 11, 2007 at 13:31 UTC

    > How can I perform a simple check to make sure a user's home directory is mounted to a specific system?

    sub has_mounted_home { my $uid = shift || die 'Wrong Parameters!\n'; my @userinfo = getpwnam("$uid"); # $uid not found: return if @userinfo == 0; my $local_homedir = $userinfo[7]; # perhaps use Linux::Mounts instead of: my $mount_result = `/bin/mount | /bin/grep $local_homedir`; # needs improvement because /homes/user1 will match e.g. /homes/us +er12 ... return $mount_result ne ""; } foreach my $uid ('uid1', 'uid2', 'uid3') { print "$uid has a mounted home\n" if has_mounted_home($uid); }

    That should do if you don't have an automounter but static mounted homes. If you have an automounter, try f00li5h or shmems solutions since you'll have to access the directories before they are mounted automatically what you'll have to check then.

    I prefere looking into the 'mounts' instead of just checking if e.g. '/homes/user1' exists. It may exist but without a mounted home directory to it!

    k

      /bin/mount will print out that /home is mounted but trying to match "/home/f00li5h" against "/dev/wd0k on /home type ext2fs (local)" will not match, unless every single user's homedir is a it's own mount point... has_mounted_home() won't work for user's who's homedirs are below mount points...

      it all depends on which solution is more sensible for your unique set of mountpoints

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://609267]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2023-09-28 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?