1 minute, 15 seconds
Automatically Import zfs zpool On USB Disk Attach With devd on FreeBSD

I had a situation where zpool on external USB disk would not get imported automatically at boot time. but manual zpool import extwd worked fine. I guessed rc script which imports known zpools at boot time runs before USB disk gets attached, so I looked for mechanism which would trigger import on attach. In FreeBSD this can be done with devd - device state change daemon.

As described in devd.conf manpage, the devd daemon provides a way to have userland programs run when certain kernel events happen. I took a look at existing files in /etc/devd/and found out rtlbtw.conf triggers on USB device attach, so I took one record from it as starting point.

Now I needed to find out values for vendor and product of my USB disk:

usbconfig dump_device_desc

There they were in the output:

ugen0.3: <Elements Portable (WDBUZG) Western Digital Technologies, Inc.> at usbus0, cfg=0 md=HOST spd=SUPER (5.0Gbps) pwr=ON (224mA)

  bLength = 0x0012 
  bDescriptorType = 0x0001 
  bcdUSB = 0x0300 
  bDeviceClass = 0x0000  <Probed by interface class>
  bDeviceSubClass = 0x0000 
  bDeviceProtocol = 0x0000 
  bMaxPacketSize0 = 0x0009 
  idVendor = 0x1058 
  idProduct = 0x1078 
  bcdDevice = 0x1065 
  iManufacturer = 0x0002  <Western Digital>
  iProduct = 0x0003  <Elements 1078>
  iSerialNumber = 0x0001  <575842314141343150444E35>
  bNumConfigurations = 0x0001 

Based on above information I created conf file in /etc/devd/ with the following content:

#/etc/devd/extwd.conf
notify 100 {
    match "system"      "USB";
    match "subsystem"   "DEVICE";
    match "type"        "ATTACH";
    match "vendor"      "0x1058";
    match "product"     "0x1078";
    action "/sbin/zpool import extwd";
};

After reboot my extwd zpool got mounted automatically.