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?
Bob Sammers
  • 3,080
  • 27
  • 33
  • Excellent question for http://unix.stackexchange.com/ – Luc M Aug 23 '11 at 16:52
  • 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! – Bob Sammers Aug 23 '11 at 17:18

3 Answers3

5

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.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 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? – Bob Sammers Aug 23 '11 at 17:09
  • Yes, but it's an in-memory filesystem, so don't use too much space there. – bdonlan Aug 23 '11 at 17:11
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.

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.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 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. – bdonlan Aug 23 '11 at 17:11
  • 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 present – Bob Sammers Aug 23 '11 at 17:13
  • @bdonlan 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. – rodrigo Aug 23 '11 at 18:02