greenfly@clover:~$ ls /var/autofs/removable/ greenfly@clover:~$ ls /var/autofs/removable/cdrom Fedora RPM-GPG-KEY TRANS.TBL GPL RPM-GPG-KEY-beta autorun README RPM-GPG-KEY-fedora dosutils README-Accessibility RPM-GPG-KEY-fedora-rawhide eula.txt RELEASE-NOTES RPM-GPG-KEY-fedora-test images RELEASE-NOTES.html RPM-GPG-KEY-rawhide isolinux greenfly@clover:~$ ls /var/autofs/removable/ cdrom greenfly@clover:~$ ls /var/autofs/removable/
Notice that the CD-ROM directory didn’t exist in /var/autofs/removable until I actually tried to access it. Then it only existed until it timed out, which, with my configuration, is 2 seconds, and then it unmounted. You can also check /var/log/syslog to watch autofs work:
Jan 27 10:05:41 clover automount[31302]: attempting to mount entry /var/autofs/removable/cdrom
Jan 27 10:05:41 clover automount[5729]: lookup(file): looking up cdrom
Jan 27 10:05:41 clover automount[5729]: lookup(file): cdrom -> -fstype=iso9660,ro,sync,nodev,nosuid :/dev/cdrom
Jan 27 10:05:41 clover automount[5729]: parse(sun): expanded entry: -fstype=iso9660,ro,sync,nodev,nosuid :/dev/cdrom
Jan 27 10:05:41 clover automount[5729]: parse(sun): dequote("fstype=iso9660,ro,sync,nodev,nosuid") -> fstype=iso9660,ro,sync,nodev,nosuid
Jan 27 10:05:41 clover automount[5729]: parse(sun): gathered options: fstype=iso9660,ro,sync,nodev,nosuid
Jan 27 10:05:41 clover automount[5729]: parse(sun): dequote("/dev/cdrom") -> /dev/cdrom
Jan 27 10:05:41 clover automount[5729]: parse(sun): core of entry: options=fstype=iso9660,ro,sync,nodev,nosuid, loc=/dev/cdrom
Jan 27 10:05:41 clover automount[5729]: parse(sun): mounting root /var/autofs/removable, mountpoint cdrom, what /dev/cdrom, fstype iso9660, options ro,sync,nodev,nosuid
Jan 27 10:05:41 clover automount[5729]: do_mount /dev/cdrom /var/autofs/removable/cdrom type iso9660 options ro,sync,nodev,nosuid using module generic
Jan 27 10:05:41 clover automount[5729]: mount(generic): calling mkdir_path /var/autofs/removable/cdrom
Jan 27 10:05:41 clover automount[5729]: mount(generic): calling mount -t iso9660 -s -o ro,sync,nodev,nosuid /dev/cdrom /var/autofs/removable/cdrom
Jan 27 10:05:43 clover kernel: ISO 9660 Extensions: Microsoft Joliet Level 3
Jan 27 10:05:43 clover kernel: ISO 9660 Extensions: RRIP_1991A
Jan 27 10:05:43 clover automount[5729]: mount(generic): mounted /dev/cdrom type iso9660 on /var/autofs/removable/cdrom
Jan 27 10:05:46 clover automount[5747]: running expiration on path /var/autofs/removable/cdrom
Jan 27 10:05:46 clover automount[5747]: expired /var/autofs/removable/cdrom
I have highlighted a few lines of interest. You can see autofs detect the access of /var/autofs/removable/cdrom, then watch it mount the filesystem, and seconds later, watch it time out and expire the mount point.
Beyond local filesystems
Autofs can manage basically any filesystem that you could normally mount by hand. This means you can use it to manage your NFS and ssh mounts. To do this, I added another line to my /etc/auto.master for smb connections:
/var/autofs/smb /etc/auto.smb --timeout=60
And then created the /etc/auto.smb file:
backup -fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000 ://gibson/backup html -fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000 ://gibson/html mp3 -fstype=smbfs,guest,uid=1000,gid=1000 ://gibson/mp3
This file sets up three mount points, /var/autofs/smb/backup, /var/autofs/smb/html, and /var/autofs/smb/mp3, respectively. Notice that I specify the mountpoints with a colon first, and then the normal //servername/share syntax used for smb. With this set up, I then restart autofs, and:
greenfly@clover:~$ ls /var/autofs/smb/ greenfly@clover:~$ ls /var/autofs/smb/backup 010901 rawtext 0211ay.jpg 1.40.2.zip 1.42.3.zip 2003 Web Analysis.xls ... url_200210.txt url_200210.xls url_200211.txt various_artists greenfly@clover:~$ ls /var/autofs/smb/ backup greenfly@clover:~$ ls /var/autofs/smb/
Again the mountpoint isn’t created until I try to access it, then it mounts the smb share, and finally times out a minute later:
Jan 27 10:34:36 clover automount[31336]: attempting to mount entry /var/autofs/smb/backup
Jan 27 10:34:36 clover automount[8930]: lookup(file): looking up backup
Jan 27 10:34:36 clover automount[8930]: lookup(file): backup -> -fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000^I://gibson/backup
Jan 27 10:34:36 clover automount[8930]: parse(sun): expanded entry: -fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000^I://gibson/backup
Jan 27 10:34:36 clover automount[8930]: parse(sun): dequote("fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000") -> fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000
Jan 27 10:34:36 clover automount[8930]: parse(sun): gathered options: fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000
Jan 27 10:34:36 clover automount[8930]: parse(sun): dequote("//gibson/backup") -> //gibson/backup
Jan 27 10:34:36 clover automount[8930]: parse(sun): core of entry: options=fstype=smbfs,username=joey,password=hacktheplanet,uid=1000,gid=1000, loc=/gibson/backup
Jan 27 10:34:36 clover automount[8930]: parse(sun): mounting root /var/autofs/smb, mountpoint backup, what //gibson/backup, fstype smbfs, options username=joey,password=hacktheplanet,uid=1000,gid=1000
Jan 27 10:34:36 clover automount[8930]: do_mount //gibson/backup /var/autofs/smb/backup type smbfs options username=joey,password=hacktheplanet,uid=1000,gid=1000 using module generic
Jan 27 10:34:36 clover automount[8930]: mount(generic): calling mkdir_path /var/autofs/smb/backup
Jan 27 10:34:36 clover automount[8930]: mount(generic): calling mount -t smbfs -s -o username=joey,password=hacktheplanet,uid=1000,gid=1000 //gibson/backup /var/autofs/smb/backup
Jan 27 10:34:37 clover automount[8930]: mount(generic): mounted //gibson/backup type smbfs on /var/autofs/smb/backup
Jan 27 10:35:44 clover automount[9058]: running expiration on path /var/autofs/smb/backup
Jan 27 10:35:44 clover automount[9058]: expired /var/autofs/smb/backup
It works in essentially same way that mounting the CD-ROM worked, only with different mount options. If you just want basic autofs mounting to work with your removable devices and network shares, this is really all you need to do. Now, if you want to push the envelope . . .
Dynamic ftp and ssh mounts with autofs and lufs
One of the things that actually inspired me to write the smb browsing script is the similar scripting that the lufs project has included for autofs. LUFS lets you mount remote ftp and ssh servers as though they were NFS or SMB fileservers on your filesystem. With their autofs tools you can simply browse around your filesystem with strategically-named folders and browse ftp.kernel.org, or even your own ssh or ftp servers.