Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

devices

  • The udev service enables user space programs to automatically configure and use new devices.
  • The kernel presents many IO interfaces for devices as files to user space processes
  • device files are in the /dev directory
  • to identify a device and view it’s properties, use ls -l. Note the first character of each line in the output below. If you see b (block), c (character), p (pipe), or s (socket), the file is a device.
    • ryan:notes/  |main βœ“|$ ls -l /dev | head
      total 0
      crw-------   1 root root       10,   107 Jan  6 15:14 acpi_thermal_rel
      crw-r--r--   1 root root       10,   235 Jan  6 15:14 autofs
      drwxr-xr-x   2 root root             260 Jan  6 15:14 block/
      crw-rw----   1 root disk       10,   234 Jan  6 15:14 btrfs-control
      drwxr-xr-x   3 root root              60 Jan  6 15:14 bus/
      drwxr-xr-x   2 root root            5960 Jan  7 08:29 char/
      crw--w----   1 root tty         5,     1 Jan  6 15:14 console
      lrwxrwxrwx   1 root root              11 Jan  6 15:14 core -> /proc/kcore
      drwxr-xr-x  10 root root             220 Jan  6 15:14 cpu/
      

device types

  • block device = hard disks. Data is read in chunks
  • character device = data is read in streams (monitors, printers, etc.)
  • pipes = like character devices, except another process is at the end of the IO stream
  • socket = special purpose interfaces that are typically used for inter-process communications

sysfs

  • The sysfs filesystem is a pseudo-filesystem which provides an interface to kernel data structures.
  • The sysfs filesystem is commonly mounted at /sys.
  • Many of the files in the sysfs filesystem are read-only, but some files are writable, allowing kernel variables to be changed. To avoid redundancy, symbolic links are heavily used to connect entries across the filesystem tree.
ryan:~/ $ ll /sys/
total 4
   1 0 dr-xr-xr-x  13 root root    0 Jan  6 15:14 ./
   2 4 drwxr-xr-x  20 root root 4096 Dec 27 19:39 ../
8359 0 drwxr-xr-x   2 root root    0 Jan  6 15:14 block/
   8 0 drwxr-xr-x  54 root root    0 Jan  6 15:14 bus/
  10 0 drwxr-xr-x  87 root root    0 Jan  6 15:14 class/
   5 0 drwxr-xr-x   4 root root    0 Jan  6 15:14 dev/
   4 0 drwxr-xr-x  29 root root    0 Jan  6 15:14 devices/
  11 0 drwxr-xr-x   6 root root    0 Jan  6 15:14 firmware/
   2 0 drwxr-xr-x  10 root root    0 Jan  6 15:14 fs/
  12 0 drwxr-xr-x   2 root root    0 Jan  6 15:14 hypervisor/
5235 0 drwxr-xr-x  17 root root    0 Jan  6 15:14 kernel/
6394 0 drwxr-xr-x 349 root root    0 Jan  6 15:14 module/
5247 0 drwxr-xr-x   3 root root    0 Jan  6 15:14 power/

  • The most important directories within /sys are:
    • block = contains info for every block device attached to the system
    • bus = contains a directory for every bus type in the kernel
    • hypervisor =
    • class
    • devices
    • kernel
    • firmware
    • module
    • power

https://docs.kernel.org/filesystems/sysfs.html https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt

Hard Disks

  • Most block devices attached to a Linux system will have a device name with a prefix of /dev/sd*

    • Example: /dev/sda
    • The β€˜sd’ portion stands for SCSI Disk
  • To list the SCSI devices on your system, use a tool that walks the SCSI device paths, such as lsscsi

    • lsscsi is not commonly installed by default

udevd

  • udevd is responsible for creating device files for attached devices
  • The process is commonly systemd-udevd
  • The kernel will send a notification to this process upon detecting a new device attached to the system. Udevd will then create a file in user-land for the device.
    • This caused problems because some devices need to be available very early in the boot process, so devtmpfs was created
    • devtmpfs filesytem is used by the kernel to create device files as necessary, but it also notifies udevd that a new device is available. Upon receiving this signal, udevd does not create a new device file, but it does perform device initialization along with setting permissions and notifying other processes that new devices are available.