[olug] Auto remount a windows share when connection is broken

Jay Hannah jay at jays.net
Wed Apr 2 00:07:38 UTC 2008


On Tue, 1 Apr 2008, Ryan Stille wrote:
> I am using smbmount to mount a windows share on my linux server.
> Sometimes the connection gets severed somehow, and running umount and
> smbmount again fixes it.
>
> Is there a way to have this happen automatically?  Put it in fstab and
> tell it to always keep the connection up or something?
>
> My plan B is to have a perl script run every minute, check the mount and
> if its down unmount it and remount it.  This won't be simple though
> because I think the check may hang depending on how 'down' (severity)
> the mount is...

Here's some Perl I wrote in 2001 to do that. YMMV.

j
Omaha Perl Mongers: http://omaha.pm.org




my $mount_point = "/mntsmb/datamoverx/c";
my $remount_cmd = 'smbmount //datamoverx/c$ /mntsmb/datamoverx/c -o "username=davet,password=davet"';

sub readstatuses {
    unless (opendir(DIR, "$mount_point/$ds_dir")) {
       # Oops... Let's remount and try again...
       remount_filesystem();
       opendir(DIR, "$mount_point/$ds_dir");
    }
    ...

sub remount_filesystem {
    # Built this because an NT reboot freaked out the Samba mountpoint...
    # This sub remounts the mountpoint if it is unuseable.
    # -jhannah 10/10/01
    $< = $>;   # Set the real UID to the fake UID (root)...
    print "<pre>Unable to read the ds* files on datamoverx!!\n   Maybe the NT server was rebooted and Samba didn't survive?\n   Perhaps remounting the filesystem will fix the problem... let's try.\n";
    print "RUNNING THIS: /bin/umount $mount_point 2>&1\n";
    system("/bin/umount $mount_point 2>&1");
    print "RUNNING THIS: $remount_cmd 2>&1\n";
    system("$remount_cmd 2>&1");
    print "Well, that's the best I can do... Let's see if it works now...\n</pre>\n";
}








More information about the OLUG mailing list