Using devtmpfs

We have one working and simple filesystem working. It is time to take a look on the /dev directory, and see which options do we have.

File devices are a special kind of files, usually found under /dev. If you want to write  to your serial port, open /dev/ttyS0, if you want to access the first framebuffer, go to /dev/fb0, etc. You can also setup permissions, for example, to protect the serial port to be abused from users, or to make a device readonly. And we can create symlinks, so we know that /dev/logitech will point to the correct webcam device.

How we create all this stuff?

The first option  is to use mknod for each device. This is very odd and hard to maintain, but if you are looking for some quick config, or a very fast boot, or a small kernel, you can choose this. With mknod, you specify a pair of magic numbers (driver specific) to create the device. So, you must know all thre devices that are present or that will be present in the future, and the magic numbers. That is quite a mess.

Busybox has a file with all the devices that you want to create when you make the final image. Check  system / device_table.txt . It has the device name, major/minor numbers, and permissions. For example, you can create the serial port and give it permissions to everybody. You can create more things like symlinks, dirs, etc, not only devices…

Tee most sophisticated way, and the usual one that you will find in your distro, is udev. This is not ‘unix traditional’, but it is the standard now in Linux. It works in userspace (safer) and for example, it will trigger actions when you plug devices (and you can create symlinks, etc). But if you have an embedded device, maybe this one is too much.

Nowadays, the kernel uses devtmpfs to handle device files. It will populate  /dev with the default devices and with default permissions (root 0600). It will mount it very early, so, you can do things like init=/bin/sh, and you can create even smaller images.

There were something similar before called devfs, but it had many issues and it is now obsolete. The new version (about 2009) is like ‘devfs done right’.

Busybox has a project called mdev. It is like a micro udev. It will handle things like load kernel modules when you plug an usb camera, etc.

You dont need devtmpfs to run mdev. Calling mdev -s will check /sys and will create the correct devices (slow). If you dont use devtmpfs, you need to call mdev -s in your /bin/init script to populate /dev

So, when the kernel detects a new device, it calls  /proc/sys/kernel/hotplug. To enable mdev :

echo /sbin/mdev > /proc/sys/kernel/hotplug

with some time, I will write a simple mdev.conf with same permissions, and some scripts to automount usb devices under /media

 

Leave a comment