Setting up autofs

From the Forum

This article was taken from the forum, and it was felt it would be interesting for the more experienced user. The article is one way to allow automatic filesharing of Samba shares, on 2 servers, in this example named 'Cedar' & 'Walnut'.

I thought I might contribute a howto on setting up autofs, which allows automatic mounting and unmounting of filesystems. Other howtos on the Internet were helpful but somewhat confusing for me. So here's my setup using two Linux samba servers (one Ubuntu and one PCLOS) and my local PCLOS desktop.

This assumes you have samba running on a server(s) and that you have it configured correctly. The important things from the smb.conf file concerning the shares on my "Cedar" samba server, for example, are:

[Cedar_Share]
path = /home/cedar_share
comment = Shared Directory on Cedar
browseable = yes
public = no
writable = yes
create mask = 0765
valid users = username1 username2
[Cedar_Data]
path = /mnt/data
comment = External Drive on Cedar
browseable = yes
public = no
writable = yes
create mask = 0765
valid users = username1 username2

The above assumes you want to allow the samba user to write to the shares. On the local PCLOS machine you also need to have the files:

/etc/samba/auth.walnut.username1
/etc/samba/auth.cedar.username1

which contain the samba username and password for the user who will be automounting the samba shares (or local partitions) with autofs.

The samba auth.host.username files can be created by the PCLOS Control Center/Mount Points/Set Samba mount points. However, if you have it write to the etc/fstab file, be sure to comment them out or delete them from the fstab file afterward.

Now install autofs via Synaptic (autosmb is not needed)

Below are how I have my auto.* files set up. You can configure however you like. Each file points to another auto.file or to the share that it will mount.

The auto.master file (below) points to two files:

auto.misc and auto.hosts.

The initial mount points, /mnt/local and /mnt/hosts) need to be created, however no other directories do. The timeout option will unmount the shares in 30 seconds, and ghost images of the mounted directories will be kept after the mount goes away.

# Start File: /etc/autofs/auto.master
/mnt/local   /etc/autofs/auto.misc    --timeout=30 --ghost
/mnt/hosts   /etc/autofs/auto.hosts    --timeout=30 --ghost
# End file /etc/autofs/auto.master

The auto.hosts file points to two more files, one for each server (cedar and walnut). The hostname mount points (Cedar and Walnut) will be automatically created by autofs under /mnt/hosts when autofs mounts a share.

# Start file: /etc/autofs/auto.hosts
Cedar   -fstype=autofs   file:/etc/autofs/auto.cedar
Walnut   -fstype=autofs   file:/etc/autofs/auto.walnut
# End file /etc/autofs/auto.hosts

The auto.walnut file tells autofs how and where to mount the shares from the walnut server. The mount points data and www will be automatically created by autofs under /mnt/hosts/Walnut.

# Start File: /etc/autofs/auto.walnut
data-fstype=smbfs,credentials=/etc/samba/auth.walnut.username1,uid=501 ://walnut/data2
www-fstype=smbfs,credentials=/etc/samba/auth.walnut.username1,uid=501 ://walnut/www
# End file: /etc/autofs/auto.walnut
The auto.cedar file is much like the auto.walnut file...
# Start File: /etc/autofs/auto.cedar
Data -fstype=smbfs,credentials=/etc/samba/auth.cedar.username1,uid=501 ://cedar/Cedar_Data
Share -fstype=smbfs,credentials=/etc/samba/auth.cedar.username1uid=501 ://cedar/Cedar_Share
# End File: /etc/autofs/auto.cedar

The auto.misc file will mount a local partition. The mount point "backup" will be automatically created under /mnt/local.

# Start File: /etc/autofs/auto.misc
backup -fstype=ext2 :/dev/hdc1
# End File: /etc/autofs/auto.misc

Now start the autofs daemon as root from the command line with

/etc/init.d/autofs start

Stop or restart with:

/etc/init.d/autofs stop
/etc/init.d/autofs restart

Using Konqueror, I can now navigate into the directory shares I just set up. Autofs will quickly mount them.

If I go into each of the shares, do "df" at the command line, The mounted filesystems look like:

Filesystem            Size Used Avail Use% Mounted on
//cedar/Cedar_Data     15G 903M   14G   7% /mnt/hosts/Cedar/Data
//cedar/Cedar_Share   6.4G 164M  6.2G   3% /mnt/hosts/Cedar/Share
//walnut/data2         56G  22G   34G  39% /mnt/hosts/Walnut/data
//walnut/www          7.4G 3.2G  4.3G  43% /mnt/hosts/Walnut/www
/dev/hdc1              57G 5.1G   49G  10% /mnt/local/backup

If Konqueror is closed, and there is no longer any activity in those directories, they will be unmounted. The mounts can also be activated from the command line or from any application. For example, for the command line.

touch /mnt/local/backup/testfile.txt 

would automatically mount the partition and create testfile.txt in that directory. Then 30 seconds later the partition would be unmounted. Thats it.

Act Now!

Top