Bob Sammers
Bob Sammers

Reputation: 3270

udev: device connected at boot time

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?

Upvotes: 6

Views: 4672

Answers (3)

user1984964
user1984964

Reputation: 51

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.

Upvotes: 2

rodrigo
rodrigo

Reputation: 98328

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.

Upvotes: 1

bdonlan
bdonlan

Reputation: 231063

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.

Upvotes: 6

Related Questions