Making windows(smb) shares mounting convenient with autofs

Working with windows shares in Linux is kinda painful, especially if you are a console geek and don’t like all that magic Gnome and KDE network applets do for you during connection to some smb server(what even worse these connections are only “visible” in GUI not in console).

I just needed something as low-level as possible and at the same time simple and convenient. Using smbmount with friends(smbumount, smbclient) is a possible way to go but I’m too lazy to type it each time and remember all its options ;)

After some googling around it turned out that autofs could be the right tool. I only had to configure it properly… It took several hours of heavy googling, reading scarce blog posts, hacking with autofs and tearing lots of my hear before I finally got more or less acceptable solution for this problem.

In short, the solution below allows you to dynamically mount any smb server by simply typing its name in “cd” shell command, e.g:

~ $ cd /auto/smbbrowse/bird   #"bird" is the name of the smb server
bird $ ls
books  print$

The idea on how to achieve this is not mine actually, it was borrowed from “How to setup autofs” blog post. However the original script only worked with Linux smb servers and could not connect to Windows shares, that’s why I hacked it a little(I added default guest auth connection settings and meta characters quoting).

Ok, let’s finally configure autofs step by step for Ubuntu(I’m using Feisty, hopefully it should work for other releases as well).

  1. Install smbfs with all dependencies:
    # apt-get install smbfs
    
  2. Install autofs:
    # apt-get install autofs
    
  3. Install perl Filesys::SmbClient package:
    apt-get install libfilesys-smbclient-perl
    
  4. Edit /etc/auto.master file, here’s my version:
    # This is an automounter map and it has the following format
    # key [ -mount-options-separated-by-comma ] location
    # For details of the format look at autofs(5).
    
    /auto/smbbrowse /etc/auto.smbbrowse --timeout=60
    
  5. Now create /etc/auto.smbbrowse script(don’t forget to chmod +x it!):
    #!/usr/bin/perl
    # auto.smbbrowse -- by greenfly (greenfly@greenfly.org), a bit
    # hacked by pachanga(http://efiquest.org) ;)
    # This script will output the proper autofs configuration
    # based on the servername it is passed.
    
    # on debian: apt-get install libfilesys-smbclient-perl
    # otherwise: perl -MCPAN -e 'install Filesys::SmbClient'
    use Filesys::SmbClient; 
    
    # you may want to tweak this line a bit, e.g I'm assuming the Russian codepage is used which
    # can be different in your case
    my $mount_options = "-fstype=smbfs,username=guest,password=,fmask=666,iocharset=utf8,codepage=cp866";
    
    my $server = shift(@ARGV);
    
    my $smb = new Filesys::SmbClient(username  => "guest",
                                     password  => "");
    
    # get list of file shares
    my @shares = browse("smb://$server");
    
    # start the output
    print "$mount_options\t";
    
    foreach(@shares)
    {
       s/ /\\ /g;
       print "/$_ //$server/$_ ";
    }
    print "\n";
    
    #------------------------------------------------------------------------------
    # Method that browse content of $rep and output the list of file shares
    #------------------------------------------------------------------------------
    sub browse
    {
       my ($rep) = @_;
       chop($rep) if ($rep=~/\/$/);
       return undef if (!$rep);
    
    # Read directory
       my $D = $smb->opendir($rep) || die "Can't read $rep:$!\n";
       my @f = $smb->readdir_struct($D);
       $smb->close($D);
    # Sort file by name
       @f = sort { $a->[1] cmp $b->[1] } @f;
    
       my @ftemp;
       foreach $f (@f)
       {
    # filter out everything that isn't a file share and apply quoting
          if($f->[0] == 3){ push @ftemp, quotemeta $f->[1]; }
       }
       @f = @ftemp;
    
       return @f;
    }
    
  6. Finally start autofs daemon:
    # /etc/init.d/autofs start
    
  7. Enjoy! ;) Try mounting any smb share in your LAN using the following command:
    $ cd /auto/smbbrowse/name_of_the_server
    

Of course /etc/auto.smbbrowse is not ideal, for example it doesn’t support authentication and it has Russian codepage hardcoded. I think this can be easily fixed by introducing some /etc/auto.smbbrowse.conf configuration file with necessary settings, e.g:

# for each specific server:
# server:username:password[:options]
craft:smbuser:mypass
*:Guest:Guess:,gid=disk,dmask=0775,fmask=0774

Actually there’s an alternative auto.smb script which uses this approach with external configuration file but I experienced some problems when trying to use it(simply put, it didn’t work at all and I was lazy to find out why).

You might be also wondering on how to discover all smb servers in your LAN. Piece of cake - use nbtscan utility, for example:

$ sudo nbtscan 192.168.0.0/24

P.S. There’s also a 10(!) pages “Browsing the Windows network with your linux machine with automatic mounting and discovery of all hosts and shares” tutorial but I personally found it way too complicated and I’m pretty happy with auto.smbbrowse solution I described above. Anyway YMMV ;)

Leave a Reply