©Jie Gao 2002–2009

Computer Notes‎ > ‎Linux/Unix Tips‎ > ‎

Setup FreeBSD's automounter (AMD) for samba shares

Using an automounter for networked file systems is a good idea, because the connection to the file system server is only needed on demand. It also eliminates the need to grant mount privilege to normal users.

I have set up a DNS-323 NAS in my home network and I want to mount its samba shares from my FreeBSD. I've already done so for my Linux system, with the help of abundant resources about setting up Linux automounter to mount samba shares. Now it comes to the turn of my FreeBSD. Again, I searched the internet for some help, and found that it is much harder to find relevant information. But in the end, I managed to have everything set up. And then, I am writing this page so that (hopefully) the next FreeBSD user who are searching the internet for this can get the answers easier.

HOWTO

First of all, you need to know how to mount a samba share in FreeBSD with the plain mount command. It sounds simple: mount -t smbfs //user@server/share /mount/point . But once you execute this command as root, you will find several things to take care:
  • By default the command asks the user for password interactively. Of course in automounter there is no chance that a password can be typed interactively. The command need to have additional options to let it suppress password prompt. Use it like: mount -t smbfs -o rw,-N //user@server/share /mount/point . The -N part in the option parameter will let the mount continue without asking a password. Unless you are using the guest user, you need to write the user's credential (username and password for servers) in /etc/nsmb.conf file. Check out the man page of nsmb.conf for details on this file. The ultimate goal is that the root user is able to mount the share without typing a password in the command line.
  • Sometimes it is helpful to use the -I option to specify the IP address or DNS name of the samba server, if there is difficulty resolving the NetBIOS name of the server. The mount command will be like: mount -t smbfs -o rw,-N,-I=192.168.1.5 //user@server/share /mount/point
  • Additional information for options is in the man page of mount_smbfs command. This command is the actual one that mounts the samba share. Note that in the mount command, any option to be passed to mount_smbfs need to be written in the value of -o option, separated by commas, with option and its value separated by "=". For example, if a command line with mount_smbfs is mount_smbfs -r -N -E gbk:cp936 //user@server/share /mount/point, the corresponding mount command line should be mount -t smbfs -o ro,-N,-E=gbk:cp936 //user@server/share /mount/point .
Then we'll go into the automounter part. The automounter of FreeBSD is named AMD (not the company that makes CPUs and GPUs). It is quite different from the automounter in Linux, in terms of the way it works and the configuration file formats. We can start off with the configuration files. AMD does not support smbfs natively. The solution here is to use the type "program" in AMD, which can let you specify the commands to mount and unmount the file systems respectively. With this, virtually any type of file system can be plugged into AMD without its native support.

Create a file named /etc/amd.conf by root, and write its content like:

[global]
auto_dir        = /.amd
log_file        = /var/log/amd.log
log_options     = error,fatal,warning
map_type        = file
search_path     = /etc

[/smb]
map_name        = amd.smb

This configuration file tells AMD about its auto mount root dir, log file location and content option, and where to search for its map files. Then the section [/smb] defines a auto mount point /smb, and its corresponding map filename amd.smb.

We mentioned the map file amd.smb. It is time to create this file also. Edit the /etc/amd.smb file by root, put the following:

share    fs:=${autodir}${path};type:=program;mount:="/sbin/mount mount -t smbfs -o rw,-N \\\/\\\/user@server/share ${fs}";

This map file tells AMD that when it needs to auto mount /smb/share, it should execute the program defined by the mount setting. Note, the command line in mount:=... looks a bit weird. Two things need to be taken care of:
  1. The program command line in mount:=... is not simply a command to execute. Instead, it has two parts. The first word in the command line is the executable file name. And then, words following are arguments for executing the command, including argv[0] (or $0). That's why there is an extra "mount" in it.
  2. The default setting of AMD will normalize slashes in its setting files. Double slashes (like the beginning of a samba share URL) will be normalized into a single slash. So extra escaping is needed ("\\\/\\\/user@server/share").
A unmount command can also be given here if the default "umount ${fs}" does not apply here (e.g. for a fuse file system).

With these two files ready, we can then enable AMD. Edit /etc/rc.conf, and add (or modify) the following lines:

amd_enable="YES"
amd_flags=""

The second line is to let AMD start without extra command line options. This will let AMD read /etc/amd.conf for its settings. Otherwise, the default flags in /etc/defaults/rc.conf make AMD ignore /etc/amd.conf .

When all file changes are made, start AMD by command: /etc/rc.d/amd start . It will be started right away. Since AMD is enabled in rc.conf, it will start automatically the next time system boots.

Then, accessing /smb/share will make AMD execute the mount command to mount it automatically. And if the mount is idle for 5 minutes, AMD automatically unmounts it.

Resources

More options available in AMD's setting files. The man page of amd and amd.conf contains many of them with explanations. But I found they are inadequate to let a completely new user of AMD set up everything correctly if the file systems to be auto-mounted are not NFS. The AMD developer's web site contains a much more detailed manual: http://www.am-utils.org.

Additional thoughts

It was a lot harder to search for relevant information for FreeBSD's auto mounter, especially if the file system to be mounted is not NFS. I think a primary reason for this is the name of the automounter. "AMD", in most cases, is not what we are discussing here. A second reason is that the development of AMD has been inactive for many years until 2 or 3 years ago it was picked up again with an official maintainer. Actually, the new AMD project is named "am-utils". But FreeBSD's documentation does not make it super clear. I only discovered it by digging into source code and CVS check in messages.