Linux Devices
The goal is to be able to extract information about the devices on a system in order to understand a few rudimentary operations.
Device Files
Manipulating most nodes on a Unix system is easy because the kernel presents many of the device I/O interfaces to user processes as files.
Block device
Applications access data from a block device in a fixed chunks. Disks can be easily split up into blocks of data.
Character device
These type of devices work with data streams. You can read only read characters from or write characters to character devices.
Pipe device
Similar to character devices, with another process at the other end of the I/O stream instead of a kernel driver.
Socket device
Special-purpose interfaces that are frequently used for interprocess communication.
Not all devices have device files, because the block and character device I/O interfaces are not appropriate in all cases.
The sysfs Device Path
The traditional Unix /dev directory is a convenient way for user processes to reference and interface with devices supported by the kernel. The linux kernel offers the sysfs interface through a system of files and directories.
dd and Devices
Command dd function is to read from an input file or stream and write to an output file or stream. dd copies data in blocks of a fixed size.
- if=file : input file
- of=file : output file
- bs=size : block size
- ibs=size, obs=size : input and output block sizes
- count=num : total number of blocks to copy
- skip=num : Skip past the first num blocks in the input file or stream, and do not copy to the output
Device Name Summary
- Query udevd using udevadm
- Look for the device in the /sys directory
- Guess the name from the output of the journalctl -k command
- For a disk device that is already visible to the system, you check the mount command
- Run cat /proc/devices to see the block and character devices for which you system currently has drivers
Hard Disks: /dev/sd*
Virtual Disks: /dev/xvd, /dev/vd
Non-Volatile Memory Devices: /dev/nvme*
Device Mapper: /dev/dm-, /dev/mapper/
CD and DVD Drives: /dev/dr*
PATA Hard Disks: /dev/hd*
Terminals: /dev/tty, /dev/pts/, and /dev/tty
Serial Ports: /dev/ttyS, /dev/ttyUSB, /dev/ttyACM*
Parallel Ports: /dev/lp0 and /dev/lp1
Audio Devices: /dev/snd/*, /dev/dsp, /dev/audio, and more
udev
The linux kernel can send notifications to a user-space process called udevd upon detecting a new device on the system
devtempfs
The kernel creates devices files as necessary, but it also notifies udevd that a new device is available. Upon receiving this signal, udevd does not create the device files, but it does perform device initialization along with setting permissions and notifying other processes that new devices are available.