Automount USB drive when connected

Frank Leonhardt frank2 at
Thu Feb 8 18:26:50 UTC 2018

On 2018-02-05 18:00, Cerebus wrote:
> I have a USB enclosure that I'd like to  be able to walk up to the
> machine and plug in  and have a series of things happen:
> 1) Disk mounts in /mnt/backup (or wherever, the key is it mounts
> without intervention)
> 2) a backup script I specify runs when the disk successfully mounts
> 3) when complete /mnt/backup unmounts
> I've got step 2 (mostly), and step 3 is trivial, but I can't get step 1 
> working
> (To be clear, this disk would never be present at startup. Basically,
> walk in, plug in drive, let the backup run, unplug the drive, and
> leave. I want this to work without requiring anyone to login)

Sorry for late reply...

You can probably do this using devd, the "state change" daemon. This can 
run arbitrary stuff when a hardware change is detected. "man devd" is a 
good place to start.

There's also the "autofs" driver, which I think relies on devd, and 
passes the request to automountd which does the business for you.

THAT SAID, when I've been in the situation you describe it has been 
having someone plug in a USB HD for an overnight backup, and unplug it 
the next morning. It was easier to have the cron job do it. One gotcha 
is that it won't mount a "dirty" FS, and that happened a lot. So the 
backup job first checked that the drive had appeared in /dev, then 
fscked the relevent partitions, then mounted it and carried on. Yes, it 
umounted it afterwards so it could safely be unplugged but....

More information about the freebsd-questions mailing list