Skip to contentSkip to conversation

cold-plugged usb flash drive not handled correctly in embedded

DISCUSSION Wolfgang Wegner 5 participants 12 messages Aug 20, 2010 – Aug 23, 2010

lore
Share & tools
lore
#0 lore raw

Hi list,

I am having a problem with udev on an embedded system and
am asking for advice on how to track this down. The system is
an ARM (kirkwood) platform with a busybox-based init. I cross-
compiled udev-161, made a simple udev start script (basically
ripped off from debian and stripped from most of the config
stuff) and a very basic rules file in
/etc/udev/rules.d/50-udev.rules:
ACTION="add", KERNEL="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk", OPTIONS="last_rule" ACTION="remove", KERNEL="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usbdisk"

It does not matter if I leave the standard rules in /libexec/rules.d/ or if I remove them altogether, in either case the USB flash drive is correctly mounted when plugged into the running system but not when it is already present when the system boots up.

I enabled debugging in udev, but have to admit I am a bit overwhelmed by all the logging then. Here some parts which seem to show that I did not make an obvious mistake:

udev reads correct rules file:
7.953380 [578] parse_file: reading '/etc/udev/rules.d/50-udev.rules' as rules file

Triggered by udevadm trigger, the device is correctly enumerated:
18.530148 [586] udev_device_new_from_syspath: device 0x40770 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sda/sda1' 18.530268 [586] udev_rules_apply_to_event: LINK 'block/8:1' //libexec/rules.d/50-udev-default.rules:3
18.530343 [586] udev_rules_apply_to_event: GROUP 0 //libexec/rules.d/50-udev-default.rules:72
18.530490 [586] udev_device_new_from_syspath: device 0x3eb40 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sda' 18.530894 [586] udev_device_read_db: device 0x3eb40 filled with db file data

But the mount is not executed.

When checking what would have to be done for this device manually, the actions are correctly shown:

udevadm_test: DEVPATH=/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sda/sda1 [...]
udevadm_test: /dev/sda1: UUID56-17AE
udevadm_test: run: '/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/sda1 /mnt/usbdisk'

And removing the device also triggers the unmount:

405.042924 [623] udev_device_read_db: device 0x2b068 filled with db file data
405.043050 [623] udev_rules_apply_to_event: RUN '/bin/umount -l /mnt/usbdisk' /etc/udev/rules.d/50-udev.rules:8

while re-inserting finally also runs the mount:

417.490164 [623] udev_device_new_from_syspath: device 0x30148 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host3/target3:0:0/3:0:0:0/block/sda/sda1' 417.490282 [623] udev_rules_apply_to_event: LINK 'block/8:1' //libexec/rules.d/50-udev-default.rules:3
417.490358 [623] udev_rules_apply_to_event: GROUP 0 //libexec/rules.d/50-udev-default.rules:72
417.490408 [623] udev_rules_apply_to_event: RUN '/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk' /etc/udev/rules.d/50-udev.rules:6 417.490554 [623] udev_device_new_from_syspath: device 0x304a0 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host3/target3:0:0/3:0:0:0/block/sda' 417.490987 [623] udev_device_read_db: device 0x304a0 filled with db file data

Can anybody point me to where I have to look for the problem? Which additional information is needed - if any - to give a hint?

Thank you and best regards,
Wolfgang

#1 lore raw

Re: cold-plugged usb flash drive not handled correctly in embedded system

Quoted 2 lines · Wolfgang Wegner

/etc/udev/rules.d/50-udev.rules:
ACTION="add", KERNEL="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk", OPTIONS="last_rule"

udev 161 has no "last_rule" thing anymore.

You mount all devices, the possibly built-in ,and the plugged-in devices at the same place? :)

Quoted 5 lines

It does not matter if I leave the standard rules in /libexec/rules.d/ or if I remove them altogether, in either case the USB flash drive is correctly mounted when plugged into the running system but not when it is already present when the system boots up.

The event for the device happens during early boot where you have no chance to mount. Do you run 'udevadm trigger' at bootup? It will re-generate all events so that they can be handled when userspace is running. It would need --action­d for your current rule to match.

Kay

#2 lore raw
In reply to Wolfgang Wegner

Hi,

Quoted 5 lines · Kay Sievers
Quoted 2 lines · Wolfgang Wegner

/etc/udev/rules.d/50-udev.rules:
ACTION="add", KERNEL="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk", OPTIONS="last_rule"

udev 161 has no "last_rule" thing anymore.

ok, but I guess this does not change anything, especially in the case when I use this as the only rule. ;-)

Quoted 2 lines

You mount all devices, the possibly built-in ,and the plugged-in devices at the same place? :)

Yes, currently I do, because only one USB device is "supported".

Quoted 10 lines
Quoted 5 lines

It does not matter if I leave the standard rules in /libexec/rules.d/ or if I remove them altogether, in either case the USB flash drive is correctly mounted when plugged into the running system but not when it is already present when the system boots up.

The event for the device happens during early boot where you have no chance to mount. Do you run 'udevadm trigger' at bootup? It will re-generate all events so that they can be handled when userspace is running. It would need --action­d for your current rule to match.

The busybox start script (/etc/init.d/rcS) runs my udev start script (/etc/init.d/start_udev.sh) which in turn runs udevadm trigger as one of the last steps:

[...]
udevd --daemon --debug

mkdir -p /dev/.udev/queue/ /dev/.udev/rules.d/
create_dev_root_rule /dev/.udev/

udevadm trigger

create_dev_makedev

udevadm settle

exit 0

Again, I have to admit I do not really understand what create_dev_makedev is for (copied from debian), but I am quite sure it can do no harm...

From my understanding, this should generate all the events -
and I see the event is there, because of this:
18.530148 [586] udev_device_new_from_syspath: device 0x40770 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sda/sda1' 18.530268 [586] udev_rules_apply_to_event: LINK 'block/8:1' //libexec/rules.d/50-udev-default.rules:3
18.530343 [586] udev_rules_apply_to_event: GROUP 0 //libexec/rules.d/50-udev-default.rules:72
18.530490 [586] udev_device_new_from_syspath: device 0x3eb40 has devpath '/devices/platform/orion-ehci.0/usb1/1-1/1-1:1.0/host2/target2:0:0/2:0:0:0/block/sda' 18.530894 [586] udev_device_read_db: device 0x3eb40 filled with db file data

All I am missing is the "RUN" line here.

BTW, amazing response time! :-)

Regards,
Wolfgang

#3 lore raw
In reply to Wolfgang Wegner
Quoted 8 lines · Kay Sievers
Quoted 2 lines · Wolfgang Wegner

/etc/udev/rules.d/50-udev.rules:
ACTION="add", KERNEL="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk", OPTIONS="last_rule"

udev 161 has no "last_rule" thing anymore.

You mount all devices, the possibly built-in ,and the plugged-in devices at the same place? :)

sorry, I should have made this clear: there is no built-in sd* device, all internal filesystems reside in UBIFS. (But I agree that I should make a real mount script for the release version, just in case somebody comes up with an USB hub or a multi card reader. ;-) )

Regards,
Wolfgang

#4 lore raw
In reply to Wolfgang Wegner
Quoted 12 lines · Wolfgang Wegner

Hi list,

I am having a problem with udev on an embedded system and
am asking for advice on how to track this down. The system is
an ARM (kirkwood) platform with a busybox-based init. I cross-
compiled udev-161, made a simple udev start script (basically
ripped off from debian and stripped from most of the config
stuff) and a very basic rules file in
/etc/udev/rules.d/50-udev.rules:
ACTION="add", KERNEL="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,flush,quiet,nodev,nosuid,exec,noatime,dmask0,fmask1 /dev/%k /mnt/usbdisk", OPTIONS="last_rule" ACTION="remove", KERNEL="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usbdisk"

Quoted 5 lines

It does not matter if I leave the standard rules in /libexec/rules.d/ or if I remove them altogether, in either case the USB flash drive is correctly mounted when plugged into the running system but not when it is already present when the system boots up.

Perhaps because you do not have a 'mount' executable when the device is found during the boot process?

thanks,

greg k-h

#5 lore raw

Re: cold-plugged usb flash drive not handled correctly in embedded system

In reply to Wolfgang Wegner
Quoted 10 lines · Wolfgang Wegner
Quoted 4 lines · Kay Sievers

The event for the device happens during early boot where you have no chance to mount. Do you run 'udevadm trigger' at bootup? It will re-generate all events so that they can be handled when userspace is running. It would need --action­d for your current rule to match.

The busybox start script (/etc/init.d/rcS) runs my udev start script (/etc/init.d/start_udev.sh) which in turn runs udevadm trigger as one of the last steps:

Quoted 7 lines

[...]
udevd --daemon --debug

mkdir -p /dev/.udev/queue/ /dev/.udev/rules.d/
create_dev_root_rule /dev/.udev/

udevadm trigger

As mentioned, add --action­d. Or make the rule match on 'change' too.

Kay

#6 lore raw
In reply to Wolfgang Wegner
Quoted 3 lines · Greg KH

Perhaps because you do not have a 'mount' executable when the device is found during the boot process?

/bin/mount is called (successfully) by busybox' init script at the very beginning, before udev is started.

Thanks for the hint, though.

Regards,
Wolfgang

#7 lore raw
In reply to Wolfgang Wegner
Quoted 14 lines · Kay Sievers
Quoted 13 lines · Wolfgang Wegner
Quoted 4 lines · Kay Sievers

The event for the device happens during early boot where you have no chance to mount. Do you run 'udevadm trigger' at bootup? It will re-generate all events so that they can be handled when userspace is running. It would need --action­d for your current rule to match.

The busybox start script (/etc/init.d/rcS) runs my udev start script (/etc/init.d/start_udev.sh) which in turn runs udevadm trigger as one of the last steps:

[...]
udevd --daemon --debug

Quoted 6 lines
Quoted 4 lines

mkdir -p /dev/.udev/queue/ /dev/.udev/rules.d/
create_dev_root_rule /dev/.udev/

udevadm trigger

As mentioned, add --action­d. Or make the rule match on 'change' too.

Thanks, that helped!

I was reading the manpage on my debian box, which is using udev-125 and thus the documentation is horribly outdated in some parts, i.e. I was still under the impression --action­d was the default.

Thanks for the help, now everything works like a charm and I can think about the proper mount script/command.

Best regards,
Wolfgang

#8 lore raw
In reply to Wolfgang Wegner
Quoted 2 lines · Wolfgang Wegner

Thanks for the help, now everything works like a charm and I can think about the proper mount script/command.

You may wish to look at the latest debian version of the usbmount package, which uses udev and invokes scripts. (IIRC)

Karl <[email protected]>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

#9 lore raw

Re: cold-plugged usb flash drive not handled correctly in embedded system

In reply to Wolfgang Wegner
Quoted 1 line · Wolfgang Wegner

ACTION="remove", KERNEL="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usbdisk"

Hmm. You are aware that this won't save you, right? :-)

If someone writes a file into /mnt/usbdisk/... somewhere, and then unplugs the USB device, this umount call will *not* flush the write out to disk, and the file will most likely be either lost, or corrupt.

The remove event gets handed to udev *after* the device is already gone. (And for that matter, the USB hub doesn't provide an event to the host system until the device is already gone. Once it's gone, you can't delay its removal, or start any USB transactions, or anything like that:
there's no way to cleanly unmount the FS, or flush out writes, unless the user tells you beforehand that they're going to be removing the device.)

#10 lore raw
In reply to Wolfgang Wegner
Quoted 8 lines · Karl O. Pinc
Quoted 2 lines · Wolfgang Wegner

Thanks for the help, now everything works like a charm and I can think about the proper mount script/command.

You may wish to look at the latest debian version of the usbmount package, which uses udev and invokes scripts. (IIRC)

Thanks, I will give it a look!

Regards,
Wolfgang

#11 lore raw
In reply to Wolfgang Wegner
Quoted 4 lines · Bryan Kadzban
Quoted 1 line · Wolfgang Wegner

ACTION="remove", KERNEL="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usbdisk"

Hmm. You are aware that this won't save you, right? :-)

Yes, I am - once more you got me for simply copying something without a deeper look into it. The scripts/commands were simply copied from either a script formerly used with mdev and from some udev example I found on the internet. I can only guess that the intention was to not block udev with the umount in case some data would have needed to be flushed out. As long as the device does not work wireless, it is clear there can be no transaction after the unplug = remove event. :-)

Regards,
Wolfgang