A better way of managing device nodes

Creating device nodes statically with mknod is quite hard work and inflexible. There are other ways to create device nodes automatically on demand:

  • devtmpfs: This is a pseudo filesystem that you mount over /dev at boot time. The kernel populates it with device nodes for all the devices that the kernel currently knows about and creates nodes for new devices as they are detected at runtime. The nodes are owned by root and have default permissions of 0600. Some well-known device nodes, such as /dev/null and /dev/random, override the default to 0666 (see struct memdev in drivers/char/mem.c).
  • mdev: This is a BusyBox applet that is used to populate a directory with device nodes and to create new nodes as needed. There is a configuration file, /etc/mdev.conf, which contains rules for ownership and the mode of the nodes.
  • udev: This is now part of systemd and is the solution you will find on desktop Linux and some embedded devices. It is very flexible and a good choice for higher end embedded devices.

Tip

Although both mdev and udev create the device nodes themselves, it is more usual to let devtmpfs do that job and use mdev/udev as a layer on top to implement the policy for setting ownership and permissions.

An example using devtmpfs

If you have booted up one of the earlier ramdisk examples, trying out devtmpfs is as simple as entering this command:

# mount -t devtmpfs devtmpfs /dev

You should see that /dev is full of device nodes. For a permanent fix, add this to /etc/init.d/rcS:

#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev

In point of fact, kernel initialization does this automatically unless you have supplied an initramfs ramdisk as we have done! To see the code, look in the init/do_mounts.c, function prepare_namespace().

An example using mdev

While mdev is a bit more complex to set up, it does allow you to modify the permissions of device nodes as they are created. Firstly, there is a startup phase, selected by the -s option, when mdev scans the /sys directory looking for information about current devices and populates the /dev directory with the corresponding nodes.

If you want to keep track of new devices coming on line and create nodes for them as well, you need to make mdev a hotplug client by writing to /proc/sys/kernel/hotplug. These additions to /etc/init.d/rcS will achieve all of that:

#!/bin/sh
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

The default mode is 660 and ownership is root:root. You can change that by adding rules in /etc/mdev.conf. For example, to give the null, random, and urandom devices their correct modes, you would add this to /etc/mdev.conf:

null     root:root 666
random   root:root 444
urandom  root:root 444

The format is documented in the BusyBox source code in docs/mdev.txt and there are more examples in the directory named examples.

Are static device nodes so bad after all?

Statically created device nodes do have one advantage: they don't take any time during boot to create, whereas the other methods do. If minimizing boot time is a priority, using statically-created device nodes will save a measurable amount of time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.14.93