Wednesday, March 20, 2013

Using autofs to mount NTFS USB disks

Sometimes when I restore my KDE session, I find an issue when auto starting applications who will read my USB disks straight away - music players, download managers, etc. Yesterday I implemented autofs to solve this, and works like a charm!

First of all, install the package:

$ sudo apt-get install autofs

In this example I'm going to setup my USB disk for torrents, so next time Ktorrent auto starts with KDE it won't be complaining about missing torrent files. My USB disk is using ntfs, with block device /dev/sdb1. I will auto mount it on /usb/torrents.

The main configuration file for autofs is the file /etc/auto.master (it might change depending on Linux distributions). Additional files are /etc/auto.smb (for cifs), /etc/auto.misc (iso9660, etc), /etc/auto.net (nfs).  We add this line to /etc/auto.master:

#cat auto.master
[...]
/usb /etc/auto.misc.torrents  -timeout=300

The name auto.misc.torrents is up to you, and time is in seconds.

Now, we create the file /etc/auto.misc.torrents:


# cat /etc/auto.misc.torrents 
torrents   -fstype=ntfs-3g            :/dev/sdb1

We start the service with service start autofs and we enter in the folder /usb:

#cd /usb
#ls
# <we see nothing>

But, if we enter in the folder torrents:

#cd torrents
#ls
asterisk.tar.bz2   burning  yoj      reaver.tar.bz2  smokeping-2.6.9  
#


The torrents folder will get auto mounted when we try to access it.

*There's a failure in autofs package, when mounting ntfs-3g. Basically it tries to pass the '-n' parameter which is not accepted. See below the output from automounter:


handle_packet: type = 3
handle_packet_missing_indirect: token 333, name torrents, request pid 2236
attempting to mount entry /usb/torrents
lookup_mount: lookup(file): looking up torrents
lookup_mount: lookup(file): torrents -> -fstype=ntfs            :/dev/sdb1
parse_mount: parse(sun): expanded entry: -fstype=ntfs            :/dev/sdb1
parse_mount: parse(sun): gathered options: timeout=399,fstype=ntfs
parse_mount: parse(sun): dequote(":/dev/sdb1") -> :/dev/sdb1
parse_mount: parse(sun): core of entry: options=timeout=399,fstype=ntfs, loc=:/dev/sdb1
sun_mount: parse(sun): mounting root /usb, mountpoint torrents, what /dev/sdb1, fstype ntfs, options 
do_mount: /dev/sdb1 /usb/torrents type ntfs options  using module generic
mount_mount: mount(generic): calling mkdir_path /usb/torrents
mount_mount: mount(generic): calling mount -t ntfs /dev/sdb1 /usb/torrents
spawn_mount: mtab link detected, passing -n to mount
>> ntfs-3g: Unknown option '-n'.
>> ntfs-3g 2010.3.6 integrated FUSE 27 - Third Generation NTFS Driver
>>              Configuration type 1, XATTRS are on, POSIX ACLS are off
>> Copyright (C) 2005-2007 Yura Pakhuchiy
>> Copyright (C) 2006-2009 Szabolcs Szakacsits
>> Copyright (C) 2007-2010 Jean-Pierre Andre
>> Copyright (C) 2009 Erik Larsson
>> Usage:    ntfs-3g [-o option[,...]] <device|image_file> <mount_point>
>> Options:  ro (read-only mount), remove_hiberfile, uid=, gid=,
>>           umask=, fmask=, dmask=, streams_interface=.
>>           Please see the details in the manual (type: man ntfs-3g).
>> Example: ntfs-3g /dev/sda1 /mnt/windows
>> Ntfs-3g news, support and information:  http://ntfs-3g.org
mount(generic): failed to mount /dev/sdb1 (type ntfs) on /usb/torrents
dev_ioctl_send_fail: token = 333
failed to mount /usb/torrents
handle_packet: type = 3
handle_packet_missing_indirect: token 334, name torrents, request pid 2236
dev_ioctl_send_fail: token = 334


I've seen some decent patches around, but being lazy as I am I just did the following to work around it:


root@Nova:/usr/bin# mv ntfs-3g ntfs-3g.orig
root@Nova:/usr/bin# vim ntfs-3g

#!/bin/sh
line=`echo $@ | sed -e 's/-n//g'`
/usr/bin/ntfs-3g.orig $line
root@Nova:/usr/bin# chmod +x ntfs-3g

root@Nova:/usr/bin# chmod 655 file

It is not ideal... but works for me :)

No comments:

Post a Comment