udev: device connected at boot time
Asked Answered
G

3

6

I'm using udev to detect USB drive connection and disconnection on my Ubuntu 10.04 LTS x64 server. Everything works fine when USB devices are connected while the machine is running, but if one is already present at boot time, my script does not complete, apparently because mkdir /tmp/blah doesn't work.

If I subsequently type sudo udevadm trigger at the terminal, everything is okay.

I'm assuming that at the point that udev first evaluates connected devices against its rules, the root filesystem has not been mounted.

My questions are therefore:

  1. Have I correctly identified the problem?
  2. Is there a standard way to solve it - i.e. is there an alterative to /tmp/ that I can use both before and after / has been mounted?
Gregarine answered 23/8, 2011 at 15:57 Comment(2)
Excellent question for unix.stackexchange.comBots
You might be right... that or serverfault. I'm writing a bash script though: my head's in programming mode so I came here, perhaps without thinking it through thoroughly!Gregarine
I
6

The root filesystem is mounted, but is read-only at the time. /dev/shm (an in-memory filesystem) should be available; newer linux distributions may also have a /run ramdisk. You can also pick a permanent directory somewhere, mount a tmpfs over it in your script, and do your work there.

Inevitable answered 23/8, 2011 at 16:53 Comment(2)
Moving the processing from '/tmp/' to '/dev/shm/' solves the problem - thanks very much for your help. Presumably this file system is persistent while the machine is up?Gregarine
Yes, but it's an in-memory filesystem, so don't use too much space there.Inevitable
B
2

One solution to this problem is to write a script that's called by your udev rules that immediately detaches, and waits for some event to occur to ensure the system is "booted enough" to create mount points, etc. to mount your devices. The person who answered the following post (http://superuser.com/questions/53978/ubuntu-automatically-mount-external-drives-to-media-label-on-boot-without-a-u) wrote a script that checks if "httpd" is running before continuing on. I'm sure there are probably other "better" ways to do this too.

Brackely answered 16/1, 2013 at 19:59 Comment(0)
A
1

1- I don't know, even in the initramfs, before the root filesystem is mounted, there is a writable /tmp directory.

True, when the real root is mounted this /tmp will be discarded and the final /tmp will be empty. Are you sure that the mkdir /tmp/blah command is failing? Or do you assume that because it is not there when you look for it?

2- In Ubuntu (I don't know of other distros) you have a hidden directory in /dev/.initramfs for these kind of needs. Since /dev is a tmpfs (or devtmpfs) mountpoint preserved in final root filesystem you will still have it there.

Acarpous answered 23/8, 2011 at 16:58 Comment(3)
udev rules in the root filesystem will be run with the root filesystem's /tmp; this may be prior to remounting the root filesystem read-write.Inevitable
You are correct - I haven't seen the mkdir command failing (its output is not logged, currently), but the next command (a mount) fails, reporting that the directory mkdir is supposed to create is not presentGregarine
@Inevitable that may be it, read-only. In this case, configuring the /tmp as a tmpfs should do the trick. The /dev/.initramfs trick should also be valid.Acarpous

© 2022 - 2024 — McMap. All rights reserved.