logo

Linux - /dev and udev

In some Unix-like systems, most device files are managed as part of a virtual file system traditionally mounted at /dev.

Devices

Normal Disks (/dev/hda1)

By convention:

  • /dev/hd{x}: IDE controllers devices. E.g. /dev/hda, /dev/hdb, /dev/hdc.
  • /dev/sd{x}: SCSI and SATA controllers devices. (Hard disk and SATA SSD) E.g. /dev/sda, /dev/sdb, /dev/sdc.
  • /dev/nvme{disk}n{namespace}p{partition}: NVMe SSD. E.g. /dev/nvme0n1p6 equivalent to /dev/sda6.

Configurable: The device names are determined by the udev configuration /lib/udev/rules.d/60-persistent-storage.rules.

Diskless instances: use local ram (tmpfs) or remote HDD / SSD (accessible via iscsi).

Network Block Device (/dev/nb0)

Every time the client computer wants to read, e.g., /dev/nb0, it sends a request over TCP to the server, which will reply with the data read.

This can be used for stations with low disk space (or even diskless) to borrow disk space from another computer.

Unlike NFS, it is possible to put any filesystem on it, etc. It should even be possible to use NBD as a root filesystem, but it requires a user-level program to be in the initrd to start. It also allows you to run block-device in user land (making server and client physically the same computer, communicating using loopback).

Loopback (/dev/loop1)

/dev/loop* are loop devices making plain files accessible as block devices (virtual devices to mount image files).

Loopback device: a virtual device that can be used like any other media device. The loopback filesystem associates a file on another filesystem as a complete device.

E.g. Ubuntu Snaps.

LVM / Device Mapper (/dev/mapper)

Logical Volumes are "dynamic partitions", i.e. they can be created / resized / deleted while the Linux system is running.

Logical Volume Manager (LVM) is a device mapper framework; can have their root file systems on a logical volume.

device-mapper is in the kernel, LVM2 tools are in the userspace.

The device mapper is a framework provided by the Linux kernel for mapping physical block devices onto higher-level virtual block devices. It forms the foundation of the logical volume manager (LVM), software RAIDs and dm-crypt disk encryption, and offers additional features such as file system snapshots.

The entries in /dev/mapper are LVM logical volumes. You can think of these as Linux's native partition type. Linux can also use other partition types, such as PC (MBR or GPT) partitions.

Note that the Linux device mapper is used for other things besides LVM (such as dm-crypt disk encryption), so files in /dev/mapper aren't necessarily LVM logical volumes.

Other features:

  • Logical Volumes can extend over more than one disk.
  • Can create snapshots and revert to a previous snapshot.

Ramdisk (e.g. /dev/ram0)

Ramdisk: is a block of random-access memory (primary storage or volatile memory) that a computer's software is treating as if the memory were a disk drive.

The device does not refer to any physical hardware, but to a portion of memory that is set aside for the purpose.

/boot/efi

The /boot/efi system partition is simply the boot partition created when the computers mother board runs UEFI rather than BIOS.

Controlling daemon

Monitors hardware addition and removal at run time, making corresponding changes to the device file system

  • BSD: devfs, in kernel space
  • Linux: udev, in user space

Check by df:

Linux:

Filesystem ... Mounted on
udev       ... /dev

BSD:

Filesystem ... Mounted on
devfs          /dev

Mount / Umount

Unix systems have a single directory tree. Mounting is the act of associating a storage device to a particular location in the directory tree, which makes a filesystem available to the system.

What is udev?

udev = userspace /dev: /dev used to be static, udev is for plugable devices running in userspace.

To be able to deal with peripheral devices that are hotplug-capable in a user-friendly way, a part of handling all of these hotplug-capable hardware devices was handed over from the kernel to a daemon running in user-space. Running in user space serves security and stability purposes.

udev primarily manages device nodes in the /dev directory. Unlike traditional Unix systems, where the device nodes in the /dev directory have been a static set of files, the Linux udev device manager dynamically provides only the nodes for the devices actually present on a system.

$ ps -e | grep udevd
    304 ?        00:00:00 systemd-udevd

udev is event driven: udev handles all user space events raised when hardware devices are added into the system or removed from it, including firmware loading as required by certain devices.