site  contact  subhomenews

USB drives kernel uevents

April 10, 2012 — BarryK
I was recently communicating with a couple of the kernel developers on one of the kernel mail-lists, about the 'usb-storage' patch that I use in Puppy kernels. Certain 'printk' lines were removed from the kernel at version 2.6.33, that the 'init' script in the initramfs needs to ascertain when all USB drives are available for use.

I developed a patch for later kernels. I raised this problem with the kernel developers and asked how to achieve the same goal without the patch. I wasn't given a solution as-such, but was informed that I should be able to glean the required information from monitoring the kernel uevents.

Uevents are status messages that the kernel generates. The usual scenario is that a daemon such as 'udevd' monitors these and is able to respond appropriately when hardware is detected, added or removed.

In the initramfs I don't use udevd, however I do have various other means for monitoring uevents in the initramfs. Anyway, first things first. I conducted an experiment to examine what uevents we need to look out for. Using Wary 5.1.2 with 2.6.32.x kernel, I modified /sbin/wait4usb to log certain information, using the 'hotplug2stdout' utility ( a uevent monitor daemon). It was a frugal HD installation, and I booted with two USB Flash drives plugged in.

This is the log just after the 'usb-storage' kernel module is loaded:
<7>usb-storage: device found at 4

<7>usb-storage: waiting for device to settle before scanning
<7>usb-storage: device found at 5
<7>usb-storage: waiting for device to settle before scanning
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6 SUBSYSTEM=scsi DEVTYPE=scsi_host SEQNUM=1165
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/scsi_host/host6 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/scsi_host/host6 SUBSYSTEM=scsi_host SEQNUM=1166
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7 SUBSYSTEM=scsi DEVTYPE=scsi_host SEQNUM=1170
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/scsi_host/host7 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/scsi_host/host7 SUBSYSTEM=scsi_host SEQNUM=1171


...the above shows what my 'usb-storage' patch is logging, as well as the relevant uevents.
...what the above indicates is that if we look for two kernel uevents for "scsi_host", that would indicate two drives are detected.

After a few seconds, the 'wait4usb' script reports this:
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0 SUBSYSTEM=scsi DEVTYPE=scsi_target SEQNUM=1173 

add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0 SUBSYSTEM=scsi DEVTYPE=scsi_device MODALIAS=scsi:t-0x00 SEQNUM=1174
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 SUBSYSTEM=scsi_disk SEQNUM=1175
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 SUBSYSTEM=scsi_device SEQNUM=1176


And then about another second later:
<7>usb-storage: device scan complete

<7>usb-storage: device scan complete
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0 SUBSYSTEM=scsi DEVTYPE=scsi_target SEQNUM=1177
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0 SUBSYSTEM=scsi DEVTYPE=scsi_device MODALIAS=scsi:t-0x00 SEQNUM=1178
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/scsi_disk/7:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/scsi_disk/7:0:0:0 SUBSYSTEM=scsi_disk SEQNUM=1179
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/scsi_device/7:0:0:0 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/scsi_device/7:0:0:0 SUBSYSTEM=scsi_device SEQNUM=1180
change@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0 ACTION=change DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0 SUBSYSTEM=scsi SDEV_MEDIA_CHANGE=1 DEVTYPE=scsi_device DRIVER=sd MODALIAS=scsi:t-0x00 SEQNUM=1181
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/block/sdc ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/block/sdc SUBSYSTEM=block MAJOR=8 MINOR=32 DEVNAME=sdc DEVTYPE=disk SEQNUM=1182
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc1 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc1 SUBSYSTEM=block MAJOR=8 MINOR=33 DEVNAME=sdc1 DEVTYPE=partition SEQNUM=1183
change@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0 ACTION=change DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0 SUBSYSTEM=scsi SDEV_MEDIA_CHANGE=1 DEVTYPE=scsi_device DRIVER=sd MODALIAS=scsi:t-0x00 SEQNUM=1185
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb SUBSYSTEM=block MAJOR=8 MINOR=16 DEVNAME=sdb DEVTYPE=disk SEQNUM=1186
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb1 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb1 SUBSYSTEM=block MAJOR=8 MINOR=17 DEVNAME=sdb1 DEVTYPE=partition SEQNUM=1187
add@/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb2 ACTION=add DEVPATH=/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb2 SUBSYSTEM=block MAJOR=8 MINOR=18 DEVNAME=sdb2 DEVTYPE=partition SEQNUM=1188


...once again, the "device scan complete" is from my patch. Now, the uevents identify all the drives and partitions.

This is good. It looks like enough information is provided to do away with my patch. I have not yet implemented a solution, thinking about it. I might modify the 'hotplug2stdout' daemon to detect when all USB drives are ready, and signal the main 'init' script.

Anyway, I have documented the test results so far, as it will probably interest other Puppy developers. Note, the original 'hotplug2stdout' C source is in my sources repo.

Comments

Uevent
Username: mavrothal
This is nice, but why not uvent itself? Is present in the main SFS anyway and is not a big package either to put in the init (~ 300k with all the rules and extras). It may even help to simplify the init script and do away with devices.

usb in kernel?
Username: 01micko
"I don't know how wary/racy kernels are configured but for Slacko-531 PAE (k2.6.39.4) and the latest beta2 (k3.1.10) USB is built in to the kernel and I removed your patch. Seems to boot ok from USB devices. I run one old P4 machine all the time from a USB HDD and boot has been 100% and I have tested numerous USB sticks. The older k2.6.37.6 has the patch IIRC. (Slacko-531 MAIN)

Re builtin usb
Username: BarryK
"01micko, My kernels now also have USB builtin. This makes the availability of the USB drives much sooner, and likely no wait is required at all. I have done some tests on this, on my latest laptop, and found no wait required. That was with USB2 Flash sticks. It may be with USB1 interfaces/drives and/or optical drives, delay may still be required. The reason that USB detection works for you without a usb-storage patch is because of above reason, but also the 'wait4usb' script has a sleep of at least 2 seconds anyway, which probably is enough for some slower interfaces. Maybe... you would need to confirm with USB1 and optical.

Re cddetect_quick
Username: BarryK
"disciple, Yes, even I had forgotten it was in there until I opened up that source package a couple of days ago. Ok, it is done, cddetect_quick-2008.tar.gz now here: http://bkhome.org/sources/alphabetical/c/


Tags: woof