Device Files
- One of the kernel important role is to allow applications to access hardware devices
- In the Linux kernel, most devices are presented to user space applications through two different abstractions
- Character device
- Originally, an infinite stream of bytes, with no beginning, no end, no size. The pure example: a serial port.
- Used for serial ports, terminals, but also sound cards, video acquisition devices, frame buffers
- Most of the devices that are not block devices are represented as character devices by the Linux kernel
- Block device
- A device composed of fixed-sized blocks, that can be read and written to store data
- Used for hard disks, USB keys, SD cards, etc.
- Internally, the kernel identifies each device by a triplet of information
- Type (character or block)
- Major (typically the category of device)
- Minor (typically the identifier of the device)
Devices: everything is a file
- A very important Unix design decision was to represent most of the ``system objects'' as files
- It allows applications to manipulate all “system objects” with the normal file API (open, read, write, close, etc.)
- So, devices had to be represented as files to the applications
- This is done through a special artifact called a device file
- It is a special type of file, that associates a file name visible to user space applications to the triplet (type, major, minor) that the kernel understands
- All device files are by convention stored in the /dev directory
Creating device files
- Before Linux 2.6.32, on basic Linux systems, the device files had to be created manually using the mknod command
- mknod /dev/ [c|b] major minor
- Needed root privileges
- Coherency between device files and devices handled by the kernel was left to the system developer
- The devtmpfs virtual filesystem can be mounted on /dev and contains all the devices known to the kernel. The CONFIG_DEVTMPFS_MOUNT kernel configuration option makes the kernel mount it automatically at boot time, except when booting on an initramfs.