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:
/tmp/
that I can use both
before and after /
has been mounted?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.
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.
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).
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.
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.
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- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With