Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 537
Bob Sammers Avatar asked Aug 23 '11 15:08

Bob Sammers


People also ask

What is a udev device?

udev is a generic device manager running as a daemon on a Linux system and listening (via a netlink socket) to uevents the kernel sends out if a new device is initialized or a device is removed from the system.

Why do I need udev?

It allows you to identify devices based on their properties, like vendor ID and device ID, dynamically. udev runs in userspace (as opposed to devfs which was executed in kernel space). udev allows for rules that specify what name is given to a device, regardless of which port it is plugged into.

Does udev run as root?

Generally, unless otherwise specified, programs started by something are run as the same user as that something. In this case, that's systemd-udevd , and it runs as root, so the programs are run as root (which is why you need to use sudo to run as another user).

What is udev service in Ubuntu?

udev supplies the system software with device events, manages permissions of device nodes and may create additional symlinks in the /dev directory, or renames network interfaces. The kernel usually just assigns unpredictable device names based on the order of discovery.


3 Answers

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.

like image 159
bdonlan Avatar answered Sep 30 '22 18:09

bdonlan


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.

like image 21
user1984964 Avatar answered Sep 30 '22 16:09

user1984964


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.

like image 22
rodrigo Avatar answered Sep 30 '22 18:09

rodrigo