Mounting usb drives / iso images in FreeBSD without root privileges
Mounting usb devices is a common task and many system take care of it without user’s intervention.
Under FreeBSD you can use hald (Hardware Abstraction Layer daemon) which use D-Bus objects for each device and mount your usb devices automatically, but you can also do it directly with the mount command.
Connect your device and take a look to the 10 last lines of your system console:
% dmesg | tail -n 10
You should see something like that:
da0 at umass-sim0 bus 0 target 0 lun 0
da0:
da0: 40.000MB/s transfers
da0: 7799MB (15974398 512 byte sectors: 255H 63S/T 994C)
Name of the device node and label may vary but we can see here that an usb device named JetFlash TS8GJFV10 8.07 is connected to my machine and referenced as da0.
Next step is to mount the first partition (slice) on the device to your filesystem (i.e. a directory):
# mount -t msdosfs /dev/da0s1 /mnt
The -t option specifies the type of the filesystem to mount, for a FAT flash drive use msdosfs, for a NTFS hard drive you have to specify ntfs and so on. For more informations about exotic filesystems, you should take a look at the mount(8) manpage.
The data on your drive can now be accessed in your filesystem through /mnt.
Reverting the process is quite easy:
# umount /mnt
Mounting iso files can be a bit more tricky, you have to create a virtual device attached to an iso image and mount it using the mount command:
# mdconfig -a -t vnode -f ~/file.iso -u 1
# mount -t cd9660 /dev/md1 /mnt
The first line creates a device node linked to ~/file.iso in /dev named md1.
The second line is a classic mount command specifying a cd9660 filesystem, the filesystem used by cdrom/dvdrom drives.
When you finished using your iso file, you can unmount the device, but don’t forget to destroy the virtual device:
# umount /mnt
# mdconfig -d -u 1
Ok, now you can mount usb drives or iso images but you have to perform the mount operation as root using su or sudo.
It can be useful to create a group of users able to mount usb and virtual devices:
# pw groupadd mounters
You can now add the users you want to be able to mount to this group:
# pw groupmod mounters -m lioks
Edit /etc/devfs.rules to add these new devices rules:
[localrules=10]
add path 'da*' mode 0660 group mounters
add path 'md*' mode 0660 group mounters
… and rc.conf to add this ruleset:
devfs_system_ruleset="localrules"
The last thing to do is to tune your kernel to allow any user to use the mount command:
# sysctl vfs.usermount=1
To have this done each time your start your system, add the following line to /etc/sysctl.conf:
vfs.usermount=1
Done ! Next time you will log in as an user member of the mounters group you should be able to use usb devices (and iso images) without any root privileges.
Don’t forget to make sure you have read/write permissions on the target directory :P
thanks!