Chapter 3 Devices
Device Files
(3.1)
Device files are sometimes called device nodes. Device files are in the /dev directory. To identify a device use ls -l. When you see a b, c, p or s the file is a device. These lettes stand for block, character, pipe and socket respectively.
Block device
Programs access data from a block device in fixed chunks. A block device's total size is fixed and easy to index. Processes have random access to any block in the device with the help of the kernel.
Character device
Character devices work with data streams. You can only read chars from or write chars to character devices. They don't have a size. Printers directly attached to your computer are represented by character devices. During character device interaction, the kernel cannot back up and reexamine the data stream after it has passed data to a device or process.
Pipe device
Named pipes are like character devices, with another process at the other end of the I/O stream instead of kernel driver.
Socket device
Sockets are often found outside the /dev directory. Socket files represent Unix domain sockets.
The sysfs Device Path
(3.2)
2 Problems:
- The name of the device doesn't say a lot
- The kernel assigns devices in the order in which they are found, so a device may have a different name between reboots.
The sysfs interface is an uniform view based on their actual hardware attributes. The base path for devices is /sys/devices. An example for a SATA hard disk (/dev/sda) is /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda.
Both paths have different purposes. The /dev file is there so that the user processes can use the device, whereas the /sys/devices path is used to view information and manage the device.
/sys/block should contain all of the block devices, those are however symbolic links. Run ls -l /sys/block to reveal the true sysfs paths.
udevadm info --query=all --name=/dev/sda can be used to show the path and other attributes.
dd and Devices
(3.3)
The program dd is useful when working with block and character devices. The sole function is to read from an input file or stream and write to an output file or stream (possibly with some encoding).
dd copies data in blocks of a fixed size. An example is dd if=/dev/zero of=new_file bs=1024 count=1. This will copy a 1024-byte block from /dev/zero (a continuous stream of zero bytes) to a new_file.
Note the lack of dash characters. That is because dd is based on an old IBM JCL style. Use the option=value method instead.
Device Name Summary
(3.4)
Ways to find the name of a device:
- using
udevd(udevadm) - in /sys directory
- guess with output of the
dmesg - output of
mountcommand (if already visible) cat /prov/devicesto see block and character devices (each line has a number=major number and a name)
Note: Only the first is reliable.
Hard Disks: /dev/sd*
- Entire disks = /dev/sda & /dev/sdb
- Partitions = /dev/sda1 & /dev/sda2
The sd stands for SCSI (=Small Computer System Interface) disk. Therefore you can use lsscsi to list the SCSI devices on your system. Optical drives will be labeled /dev/sr0, these are read only. To (re)write one will use generic devices such as /dev/sg0.
Terminals are devices for moving characters between a user process and an I/O device. Examples are /dev/tty1 (first virtual console) and /dev/pts/0 (first pseduoterminal device). The /dev/pts directory itself is a dedicated filesystem.
ALT-F1 (and F2, F3...) can be used to switch between consoles. Another command to switch to tty1 for example is
chvt 1.
Creating Device Files
In modern Linux systems, you do not create your own device files; this is done with devtmpfs and udev.
However it is interesting (and might help you) to know how it was once done. The mknod command creates a device. Make sure you know the name, and both its major and minor numbers. To create /dev/sda1 one should use: mknod /dev/sda1 b 8 2. The b 8 2 specifies a block device with a major number 8 and a minor number 2. c or p can be used to create character or named pipe devices.
udev
(3.5)
The Linux kernel sends notifications to a user-space process (udevd) upon detecting a new device on the system. Afterwards its this user-space process that examines the device's characteristics and creates a device file. This was the theory! In practice there is a problem. Device files are necessary early in the boot procedure therefore udevd had to start early.
The devtmpfs filesystem was developed in response to that problem. The kernel creates device files as necessary but it also notifies udevd that a new device is available. After that udevd does not create the device files, but it does perform device initialization and process notification. Udevd creates symbolic links in /dev to further identify devices.
udevd operation and configuration
The udevd daemon operates as follows:
- The kernel sends udevd a notification event, called a uevent, through an internal network link.
- udevd loads all of the attributes in the uevent.
- udevd parses its rules, and it takes actions or sets more attributes based on those rules.
The rules files are in the /lib/udev/rules.d and /etc/udev/rules.d directories.
The udevadm program is an administration tool for udevd. For example reloading udevd rules and triggering events. But you can search, explore and monitor system devices as well. An example is udevadm info --query=all --name=/dev/sda.
To monitor uevents with udevadm use udevadm monitor. Note that there are two copies of each message in this output because the deafult beahvior is to print both the incoming message from the kernel and the message that udevd sends. Use --kernel or --udev if you only want one entry. Use --property if you want to see the whole incoming uevent including attributes.
You can also filter events by subsystem. For example, to seeonly kernel messages pertaining to changes in the SCSI subsystem use: udevadm monitor --kernel --subsystem-match=scsi.
Extra (slides + notities)
(Slides)
Virtuele en (pseudo-)terminals zijn te vinden in /dev/tty* en /dev/pts/*.
tput init
File descriptoren zijn t vinden in /proc/pid/fd/*. Het openen van een nieuwe descriptor gebeurt als volgt: exec #< ....
Lezen van bestand f zonder file descriptoren read < f ; echo $x. Via file descriptoren exec #< f ; read x <&# ; echo $x.
Het maken van device files kan via mknod of mkfifo (voor named pipes).
TODO sysfs device path TODO udevd overview/summary, rest staat hierboven vrij uitgebreid