How to auto-detect a USB drive?

Roland Smith rsmith at
Wed Mar 11 10:43:07 PDT 2009

On Wed, Mar 11, 2009 at 08:10:46AM -0700, Peter Steele wrote:
> >My question to you would be: What exactly do you mean by 
> >"automatically detect"? The drive *is* automatically detected. 
> >Should it be mounted afterwards? 
> Yeah, I guess my wording was a little vague. I know that the system
> automatically detects when a USB drive is inserted, and creates the
> appropriate entries under /dev. I want to follow this up with having
> the drive automatically mounted, and I'll then examine the USB drive
> for specific files I expect to be present, and possible run something
> that's installed on the disk, then unmount the disk. We want to use
> this approach to deploy our software on large clusters of machines
> that may not have an IP identity.

The easiest way (I think) is to use devd, in combination with a USB
drive that has a unique label.

Say you have a USB drive formatted with a UFS filesystem. You use
'tunefs -L' to give this drive a unique label,
e.g. 'FOO'. Then you can put the following in /etc/devd.conf

notify 1000 {
        match   "system"                "DEVFS";
        match   "subsystem"             "CDEV";
        match   "cdev"                  "/dev/ufs/FOO";
        action  "sh /usr/local/sbin/autorun /dev/ufs/FOO &";

Where the script /usr/local/sbin/autorun would contain the required actions
(mount, check, run, umount, beep).

The reason for using a labeled device is that you might not want to run
the script on any old USB drive! If you want to use a FAT formatted
drive you should use /dev/msdosfs/FOO for the path.

Last week I submitted a patch to get this system and subsystem
documented in the manual page of devd.conf. It should be in CURRENT now,
and will be MFC'd to STABLE in a week or so.

[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url :

More information about the freebsd-questions mailing list