Friday, December 26, 2008

Automounting SAMBA Shares

note: This article is intended for a technical audience -- you should use caution when modifying a production system -- caveat emptor.

This is more of a collaboration of other people's posts, with some additions of my own for performance-related issues.

Basically, I manage a bunch of WD MyBook Network Drive (World Edition) boxes for various people -- typically, these are hooked up via SMB shares to various types of Linux install for redundant network backups over the LAN.

After various hacks, from mounting as part of a cronjob to modifying /etc/rc.local -- I decided to attempt automatic fstab mounting under the christmas break and figured i'd document my findings here.

First off, to automatically mount the filesystems on the MyBook -- you need to add lines similar to the following to your /etc/fstab file.


//[SMB SHARE]/[sharename] /media/[mountpoint] cifs credentials=/root/.smbcredentials,rw,iocharset=utf8,uid=[username],gid=groupname,file_mode=0664,dir_mode=0775 0 0


(note: Blogger has wordwrapped this post, but this should be one line when copied to your /etc/fstab file.)

Where:


  • SMB SHARE -- Is the NETBIOS name or IP address of the MyBook.

  • sharename -- Is the name of the share you need to mount (personally, I like to make at least shares based on the usernames using the box).




  • credentials=/root/.smbcredentials -- Is a plaintext file containing the username and password of the user you have created on the MyBook.




  • iocharset=utf8 -- Specifies that all files written or read from the device should be in the UTF-8 character set.

  • rw -- Specifies that access to the share should be read-write.

  • uid=username,gid=groupname -- Specifies the username and groupname on the local Linux machine.

  • file_mode=0664,dir_mode=0775 -- Specifies the octal permissions of the files written on the MyBook.




  • 0 0
  • -- Means fsck will not attempt to check the filesystem under any circumstances, this is always advisable when mounting SMB shares.


After you have edited your /etc/fstab file, you need to make your credentials file -- this file specifies the name and password of the user on the MyBook.

This file needs two lines, with a trailing blank line -- and should usually be placed in the /root or /etc/samba directory and have 0600 permissions.

An example of this file is:


username=winuser
password=winpassword


Save this file and change it's permissions, then alter your /etc/fstab file to point to it's location.

Once you've done this, you should be able to have your shares automatically mounted by your Linux box (after either rebooting or running mount -a as root).

One particular quirk you might find using this method -- is unmounting errors -- these occur because the shutdown routine (by default) shuts down the network devices (specifically, those machines running NetworkManager to control network resources) before unmounting any mounted network shares (ie. what we're trying to achieve here).

These errors usually halt the shutdown of your machine (usually meaning you have to power off using the power button, which can damage your filesystem).

To fix this, you can run the following as root:


ln -s /etc/init.d/umountnfs.sh /etc/rc0.d/K15umountnfs.sh
ln -s /etc/init.d/umountnfs.sh /etc/rc6.d/K15umountnfs.sh


Which will alter your system to unmount the network-attached shares before NetworkManager has a chance to shutdown the network devices.

No comments: