2010-07-26

FreeBSD - How to allow normal user to mount USB drive

Recently, I've got a few external hard disk and one of them is formatted into file system FAT32 (msdosfs or vfat), because the files are to be interchange between Linux, OSX & FreeBSD. After the files are save from OSX, the external hard disk cannot be mounted onto FreeBSD or PC-BSD as a normal user. Since these are just some media files and frequently need to be interchange between these 2 OS, I'm quite reluctant to mount it using root or sudo. After all, root or sudo are suppose to be used for system administration.

On the FreeBSD box, some system value & devfs needed to be change in order for the external USB hard disk can be mounted. As for PC-BSD, the "hald" failed to mount the USB external hard disk because this USB external hard disk FAT32 partition is too big. Some changes needs to be done on "hald" but being a command line geek, I'd rather meddle these in command line. By the way, the error message pops out on the "Dolpine" (KDE) file manager is :
"An error occurred while accessing '160gb', the system responded:
org.freedesktop.Hal.Device.Volume.UnknownFailure: mount_msdosfs:
/dev/msdosfs/160gb: Disk too big, try '-o large' mount option : Invalid argument"

Again, "If doubt, refer de FreeBSD Handbook" shed light. These steps are to be executed as root or equivalent privilege. Also, the normal user that is intended to mount the external hard disk, need to be part of the group "wheel". Below are the steps that I've taken to allow normal user to mount a external USB drive :

Part I - Preparation

  1. After the external USB hard disk is plug in, check /dev/msdosfs for the external hard disk label.
  2. Make a directory in /media/ that will act as the mount point for this USB external hard disk. Of cause, the directory have to be granted permission for the normal user to access.
    mkdir /media/mountpoint
    chown normaluser:normaluser /media/mountpoint
  3. Insert an entry into file system table so that the next time we'll just need to specify the mount point to the "mount" command. Rather then spelling out every parameter & value to mount the USB external hard disk.
    vi /etc/fstab
    then insert the below into it :
    /dev/msdosfs/hdd-label-name /media/new-mountpoint msdosfs rw,large,noauto 0 0
Note : The option, "large", specified in /etc/fstab might take up a lot of memory. Here is why (taken from "man mount_msdosfs") :
Support file systems larger than 128 gigabytes at the
expense of 32 bytes of kernel memory for each file on
disk. This memory will not be reclaimed until the file
system has been unmounted.


Part II - On a FreeBSD box

  1. Change the system value below so that normal user are allow to mount device :
    sysctl vfs.usermount=1
  2. Execute the command below so that the changes are permanent and will retain after reboot :
    echo vfs.usermount=1 >> /etc/sysctl.conf
  3. Add the below rules set into /etc/devfs.rules so that devfs change the permission of the device file automatically when the external USB hard disk is plug in :
    [userrules=5]
    add path 'da[0-9]*' mode 0660 group wheel
    add path 'msdosfs/160gb' mode 0660 group wheel
  4. Restart the daemon "devfs" so that the above rules set are loaded,
    /etc/init.d/devfs restart
  5. Tell rc.conf to load the rule set every time when the system is rebooted,
    echo "devfs_system_ruleset="userrules" >> /etc/rc.conf

Part II - On a PC-BSD box

  1. Similarly, add the below lines into the file /etc/devfs.rules so that devfs will change the permission automatically :
    [userrules=5]
    add path 'da[0-9]*' mode 0660 group wheel
    add path 'msdosfs/160gb' mode 0660 group wheel
  2. Restart the daemon "devfs" so that the above rules set are loaded,
    /etc/init.d/devfs restart
  3. Tell rc.conf to load the rule set every time when the system is rebooted,
    echo "devfs_system_ruleset="userrules" >> /etc/rc.conf

Arrivederci !!!

No comments: