From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Mdev and the translation is 7% complete.
Outdated translations are marked like this.
Other languages:

本文文档介绍了如何在 Linux 中用 mdev 替换 udev,从而允许单独的 /usr 分区,而不需要 initramfs。 作者使用 IceWM 作为窗口管理器的 Gentoo Linux。这里的说明经过一些小调整后应该适用于其它发行版。

mdev is a udev replacement from Busybox, it populates and updates /dev (official docs). Replacing eudev or udev is non-trivial, and is probably most adapted to embedded systems.

mdev 可以在我的系统上工作吗?

The mdev application is definitely suitable as long as the system does not use a full-fledged desktop environment. Note that a desktop environment is not required to run AbiWord, Firefox, GIMP, Gnumeric, etc. However, KOffice applications like KMail seem to pull in most of KDE as a dependency. In general, when using KDE or GNOME, mdev is not suitable. Also using LVM might be troublesome.

使用默认的 Gentoo 特定配置文件时它会工作得很好:

  • default/linux/x86/13.0
  • default/linux/amd64/13.0


user $eselect profile list
Current /etc/portage/make.profile symlink:
Recent versions of evdev (as provided through x11-drivers/xf86-input-evdev) and www-client/chromium require udev. They will not build without it.

There is one more sanity check that Gentoo users can run to check for udev dependency.


Get a general overview which packages might depend on udev. The output could look similar to the one shown below:

user $equery d udev
 * These packages depend on udev:
media-libs/mesa-9.0.1 (gbm ? virtual/udev)
sys-apps/hwids-20130329 (udev ? >=virtual/udev-197-r1)
sys-apps/util-linux-2.22.2 (udev ? virtual/udev)
virtual/dev-manager-0 (virtual/udev)
x11-base/xorg-server-1.13.4 (udev ? >=virtual/udev-150)
x11-drivers/xf86-video-intel-2.20.13 (udev ? virtual/udev)
x11-libs/cairo-1.10.2-r3 (drm ? >=virtual/udev-136)

Add the following line to the /etc/portage/package.mask or /etc/portage/package.mask/mdev file (which can be created if it does not exist yet):

文件 /etc/portage/package.mask/mdevMask away udev

Disable the udev USE flag globally in /etc/portage/make.conf:

root #euse -D udev

Rebuild all packages with the new -udev USE flag:

root #emerge -uDNvp @world

If the only error that comes up is that Portage is not able to re-install udev as required by virtual/dev-manager, proceed to the next stage. Otherwise, sys-fs/udev might be an hard dependency of one or more packages installed on the system.

Software requiring extra steps

ATI/AMD binary blobs

Some ATI/AMD Radeon cards require binary blobs. The Gentoo sys-firmware/radeon-ucode package pulls in all the binary blobs available at to support many different models.

With all the binary blobs in the library directory, the kernel needs udev to figure out which one of the many binary blobs to load. If all but one of the binary blobs are removed, leaving only the correct one in the library directory, the binary blob is loaded automatically.

Replacing udev with mdev

This is a critical step which might result in a non-bootable Linux system. Only proceed if the above approach has not revealed any additional udev dependencies.

Setting up the kernel for devtmpfs

Set up the kernel to support and automount a devtmpfs filesystem at /dev:

内核 Enabling devtmpfs
Device Drivers --->
    Generic Driver Options --->
        [*] Maintain a devtmpfs filesystem to mount at /dev
        [*] Automount devtmpfs at /dev, after the kernel mounted the rootfs

Once the changes have been made, rebuild the kernel. Do NOT reboot yet.

Emerging busybox

Make sure that the mdev USE flag is set for sys-apps/busybox. The static USE flag is probably also a good idea. In the /etc/portage/package.use or /etc/portage/package.use/mdev file, add the follwoing line:

文件 /etc/portage/package.use/mdev
sys-apps/busybox static mdev

Now, (re)install busybox:

root #emerge --ask --oneshot busybox

Mounting devpts

The devpts filesystem exhibits non-standard behavior. It does not automount, at bootup, or with mount -a. An explicit mount devpts command is required. The standard solution for udev-based systems is to run rc-update add udev-mount sysinit as root, and have the udev-mount script do the mounting at startup. An mdev-based system will probably not have udev installed. An alternative way to do this at bootup is to include the command mount devpts in a shell script in /etc/local.d/. In this example, the file will be named /etc/local.d/000.start.

Another side-effect of not using udev-mount is that /dev/shm is only writable by root. The command chmod 1777 /dev/shm is required to restore the standard behavior. This command will also be run from the script at startup.

文件 /etc/local.d/000.startMount of devpts at bootup and change /dev/shm permissions
mount devpts
chmod 1777 /dev/shm
Scripts used in /etc/local.d/
  • must be executable
  • must have the extension ".start" in order to run at startup
  • must have the extension ".stop" in order to run at shutdown

Replace the udev service

Remove udev from the services list and replace it with mdev:

root #rc-update del udev sysinit
root #rc-update add mdev sysinit


Reboot to the new kernel. The system should now be running using mdev.


Remove udev from the system:

root #emerge --ask --depclean --verbose sys-fs/udev sys-fs/eudev

In the /etc/portage/package.mask file (or a file in this directory), add sys-fs/udev and sys-fs/eudev.

This now results in a completely udev-free machine.

Setting up a USB printer running under CUPS

Make 'find' available at early boot-time

root #cd /bin
root #ln -s /bin/busybox find

Rebuild the kernel

For libusb (and hence CUPS) to see the USB ports, set CONFIG_USB_DEVICE_CLASS=y and CONFIG_USB_DEVICEFS=y in the kernel configuration. Also enable CONFIG_USB_PRINTER=y, despite the injunction in the cups-1.5.2 ebuild to disable it. When using make menuconfig, these items are found here:

内核 Enabling generation of USB devices at boot time
Device Drivers --->
    [*] Support for Host-side USB
      [*] USB device filesystem [DEPRECATED]
      [*] USB device class-devices [DEPRECATED]
    [*] USB Printer support
Don't worry about the deprecation warnings.

Configure the new kernel into the bootloader and reboot into it

Verify the presence of the new device nodes

A hierarchy of device nodes should be present under /dev/bus/usb/. When switching on the printer, a node for it should occur at /dev/lp0. lsusb should run successfully, and this should display the printer's details.

Configure the printer in CUPS

Configure CUPS in the usual way. CUPS should find the printer, and will give it a URI like parallel:/dev/lp0.


Linux modules missing at boot sequence

If Linux modules are required and not loaded on boot after migration, execute one or more of the following solutions:

  • Add Linux modules to *.conf files in /etc/modules-load.d/
  • Compile modules into the Linux kernel, without the need of loading them on demand.
  • Write a custom module loader:
文件 /sbin/hotplug
test -n "$MODALIAS" && modprobe "$MODALIAS";
exec /sbin/mdev

Install it as the hotplug handler instead of mdev. Note that the earlier this gets executed, the better. Consider naming this script /sbin/hotplug, since that is the default value of /proc/sys/kernel/hotplug (or change the default value in the kernel configuration).


This set of instructions was originally written by Walter Dnes and hosted at his personal website. It was imported to the Gentoo wiki with some editing by Michael Mol per discussion on the gentoo-user mailing list.
  • mdev unlike udev does not support auto-modules loading. Create files ending with .conf in /etc/modules-load.d/ and put all the modules there that should be loaded (nvidia, wl, etc.) one per line. Customize options via files ending with .conf in /etc/modprobe.d (see man 5 modprobe.d for syntax). It might be necessary to move the module configuration to this location.
  • mdev -s does not create /dev/mapper nodes. Either manually create them or use dmsetup mknodes from lvm2. It is a good idea to add it after mdev -s in the init script.
  • Use mouse and keyboard drivers for xorg inputs. Evdev needs udev to be built. Mousedrv (for the mouse driver) may conflict with the synaptic driver when both are loaded.
  • The Kernel configuration option CONFIG_INPUT_EVDEV not only provides the keyboard and mouse as input device events, it will provide lid and button events to acpid as well.

See also

  • Mdev/Automount USB‎ — describes how to implement automounting of USB devices on a machine using mdev as the device manager.

External resources