How to make devd automount USB storage devices?

http://gezeiten.org/post/2011/01/Xfce-4.8-on-BSD-flavors

This seems to be a trend. Which is fine, because desktop environments and file managers never should have handled device recognition in the first place.

Keeping with the trend, I'd like to have devd mount whatever I add to a suitable directory under /media. Unfortunately the devd (8) and devd.conf (5) manpages seem to have gone over my head whenever I try to read them.

I've figured something like
Code:
attach 0 {
match "system" "USB";
match "subsystem" "INTERFACE";
match "type" "attach";
[...]
}

but I've got no idea what goes in the dots.
 
Are you running Xfce 4.8?

This might help you:

[thread=23105]http://forums.freebsd.org/showthread.php?t=23105[/thread]

I haven't researched how to make an Xfce interface to it though...
 
aragon said:
Are you running Xfce 4.8?

This might help you:

[thread=130483]http://forums.freebsd.org/showthread.php?p=130483[/thread]

Actually I'm running fluxbox, and that's kind of a brilliant thing you've got there. I just linked the Xfce thing because I couldn't think of any other way to explain what I wanted to do.
 
Cool. Been a while since I last played with Fluxbox, but it probably has a clever way of interfacing with what I made. Let us know how you get it working. Would like to add it to what I've done for Openbox. :)
 
I'm just going to post my xfce auto-mounting code here because it's kind of semi-functional and I'm tired of working on it. Automounting works. It's unmounting that's more difficult. This should all be considered experimental.

First, a script to detect filesystem type and mount or unmount them:

/usr/local/libexec/xfce-mount
Code:
#!/bin/sh

# mount/unmount a removable storage device
# Warren Block, 2011-04-10

# modify /usr/local/etc/gamin/gaminrc, set to poll user Desktop directory
# poll /usr/home/wblock/Desktop/*

cmd=$1          # (mount/umount)
username=$2
devnode=$3

# $0 mount username /dev/da0s1
# $0 umount username

deskdev="/home/$username/Desktop/removable-storage"

# return a file type string for a devnode
fstype () {
  case `file -s $1 | egrep -o 'FAT|NTFS|EXT2|ISO 9660|Unix Fast File system' | tr ' ' _` in
    FAT*)
      printf "msdosfs" ;;
    NTFS*)
      printf "ntfs" ;;
    EXT2*)
      printf "ext2fs" ;;
    ISO_9660*)
      printf "cd9660" ;;
    Unix_Fast_File_system*)
      printf "ufs" ;;
  esac
}

mount_it () {
  if [ ! -c "$devnode" ]; then
    echo "$devnode does not exist"
    return
  fi
  if [ ! -d "$deskdev" ]; then
    mkdir $deskdev
    chown $username:operator $deskdev
    chmod 770 $deskdev
  fi
  fs=`fstype $devnode`
  mount -t $fs $devnode $deskdev
}

umount_it () {
  if `mount | grep -q $deskdev`; then
    umount $deskdev
  fi
  # only remove mount directory if it exists and is empty
  if [ -d $deskdev -a ! "`ls -A $deskdev`" ]; then
    rm -rf "$deskdev"
  fi
}

case $cmd in
  mount*)
    mount_it ;;
  umount*)
    umount_it ;;
esac

Configure devd(8) to run that script:
/etc/devd.conf
Code:
...
# WB
# automount USB storage devices
notify 20 {
        match "system" "DEVFS";
        match "type" "CREATE";
        match "cdev" "da[0-9]s1$";
        action "su wblock -c '/usr/local/libexec/xfce-mount mount wblock /dev/$cdev'";
};

# WB
# unmount USB storage devices
notify 20 {
        match "system" "DEVFS";
        match "type" "DESTROY";
        match "cdev" "da[0-9]s1$";
        action "su wblock -c '/usr/local/libexec/xfce-mount umount wblock /dev/$cdev'";
};

As noted above, devel/gamin has to be set to poll the mountpoint instead of opening it for read in an infuriating fashion that prevents unmounting.
/usr/local/etc/gamin/gaminrc
Code:
poll /usr/home/wblock/Desktop/*

User must be a member of wheel and usermount must be enabled:
/etc/sysctl.conf
Code:
vfs.usermount=1

Finally, add an "Unmount" menu option in Thunar so there will be a menu entry to unmount the device before unplugging it:
Start Thunar.
Edit/Configure custom actions...
Click + to add a new one.
Name: Unmount
Description: Unmount a volume
Command: /sbin/umount %f
(Note: do not quote that %f, it does not work. I don't know why, but hope xfce quotes or escapes arguments internally.)
Click the Appearance Conditions tab.
File Pattern: removable-storage
Select (check) Directories, Okay, Close.

To use:

Plug in the USB drive or memory card. An icon for "removable-storage" will appear on the desktop. Use as normal. When done, open the desktop in Thunar, right-click removable-storage and select Unmount. Disconnect the storage device and the icon will disappear.

Problems:

It only looks for the first slice. Disks with multiple slices or bsdlabels are not handled.

Despite the polling setting, I still see gam_server occasionally opening the removable-storage mountpoint for read, and preventing an unmount. Deinstalling gamin solves that, but icons aren't auto-redrawn on the desktop and probably elsewhere. Pressing F5 does that. Might be something that could be triggered with a dbus message from the mount script, don't know.

Trying to unmount from the desktop right-click context menu instead of within Thunar does not work. No idea why, maybe just to be annoying.
 
aragon said:
Clever file system detection method... I might have to steal it. :)

Thanks! It needs more work to avoid stating filesystem names in two places, but that was deferred until after getting the basic functionality (mount/unmount) working...

I looked at your xmountd, but haven't tried it yet. Looks to be much more ambitious than mine.
 
Back
Top