In this post, I will tackle several low-level Linux components; including the /proc filesystem, dmesg, SysRq (commands when the system hangs or crashes), and watchdog timers. I will use these commands in debugging, and recovery.

Surveying the Linux Kernel

  1. printk is the kernel's function for code to print messages; it is like printf in C.
  2. Those messages are sent to a RAM buffer and sometimes to the system console. You can look at the contents of the RAM buffer using the command called dmesg.
  3. The ones that are important enough are shown in the console.
  4. The logging daemon may send to a file or elsewhere.
  5. Use journalctl -k with -f to follow as new log messages are added.
  6. The proc and sysfs file systems are virtual file systems.
  7. Their contents are not stored on disk - made up when you ask for them.
  8. Each file and directory entry has an associated function in the kernel that produces the contents on demand. You can look at the file under proc, then the kernel generates its contents. So if you look at the file, it's always up-to-date information. Some of these files are writable.
  9. Since proc is writeable, you can tell the kernel something like " Change this variable to this new value. And that's often for some sort of things called tunables.
  10. All the files have some functions the kernel is associated with them that get called when you use that file.

What is /proc?

  1. It is the psuedo file system for Linux
  2. The proc filesystem is mounted on /proc at boot
  3. proc gets its name from "process"
  4. proc contains lots of process information and lots more

What is /proc: Process Info

  1. Each process has a directory named with its PID
  2. So if you go to /proc, you will see a lot of process ID numbers
  3. It will contain informmation about:
    • memory usage of the process
    • what program its running
    • what files it has open
  4. There can be hundred of files and directorues per process
  5. Threads have entried under directory /task

LAB - Inspecting Kernel messages and /proc

  1. Find a proc file for the kernel command line

    As seen here, bootloader pass this command to the kernel at boot time

    grace@grace-ubuntu:~$ cat /proc/cmdline 
    BOOT_IMAGE=/vmlinuz-6.8.0-47-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv ro
  2. Use dmesg and grep, do you see the kernel reporting the kernel command line?

     

    race@grace-ubuntu:~$ sudo dmesg | grep vmlinuz
    [    0.000000] Kernel command line: BOOT_IMAGE=/vmlinuz-6.8.0-47-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv ro
    [    0.000000] Unknown kernel command line parameters "BOOT_IMAGE=/vmlinuz-6.8.0-47-generic", will be passed to user space.
    [    0.688543]     BOOT_IMAGE=/vmlinuz-6.8.0-47-generic
    
    • If not, can you determine if the boot messages from the kernel were lost?
    • Does your system have a log file that recorded the boot messages?

      grace@grace-ubuntu:~$ journalctl  -k | grep vmlinuz
      May 02 01:08:21 grace-ubuntu kernel: Kernel command line: BOOT_IMAGE=/vmlinuz-6.8.0-47-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv ro
      May 02 01:08:21 grace-ubuntu kernel: Unknown kernel command line parameters "BOOT_IMAGE=/vmlinuz-6.8.0-47-generic", will be passed to user space.
      May 02 01:08:21 grace-ubuntu kernel:     BOOT_IMAGE=/vmlinuz-6.8.0-47-generic
      
      
    • You can grep for  BOOT_IMAGE under /var/log to look

      grace@grace-ubuntu:~$ grep -r BOOT_IMAGE= /var/log
      /var/log/syslog.1:2025-04-29T01:08:19.013791+00:00 grace-ubuntu kernel: Kernel command line: BOOT_IMAGE=/vmlinuz-6.8.0-47-generic root=/dev/mapper/ubuntu--vg-ubuntu--lv ro
      /var/log/syslog.1:2025-04-29T01:08:19.013792+00:00 grace-ubuntu kernel: Unknown kernel command line parameters "BOOT_IMAGE=/vmlinuz-6.8.0-47-generic", will be passed to user space.
      /var/log/syslog.1:2025-04-29T01:08:19.014609+00:00 grace-ubuntu kernel:     BOOT_IMAGE=/vmlinuz-6.8.0-47-generic
      grep: /var/log/syslog.1: binary file matches
      grep: /var/log/gdm3: Permission denied
      grep: /var/log/journal/239eb21f7f464fcc8ee798e79551352d/system@1f5e831359a04d01ae2179ec537dfbde-0000000000003dad-00062493a5992b91.journal: binary file matches
      grep: /var/log/journal/239eb21f7f464fcc8ee798e79551352d/system@00062492a2cee612-58be7e2437a5b50a.journal~: binary file matches
      grep: /var/log/journal/239eb21f7f464fcc8ee798e79551352d/system@62de8a3135ed49d9bbf1013ab94059cd-000000000000bd60-0006341cc94d4ca2.journal: binary file matches
      
  3. According to /proc/meminfo, what is the total amount of RAM in your system

    Answer: 4G

    grace@grace-ubuntu:~$ cat /proc/meminfo 
    MemTotal:        3995472 kB
    MemFree:         2245756 kB
    MemAvailable:    2878148 kB
    Buffers:           48232 kB
    Cached:           822352 kB
    SwapCached:            0 kB
    Active:          1203308 kB
    Inactive:         183388 kB
    Active(anon):     625860 kB
    Inactive(anon):        0 kB
    Active(file):     577448 kB
    Inactive(file):   183388 kB
    Unevictable:       88268 kB
    Mlocked:           26240 kB
    SwapTotal:       3994620 kB
    SwapFree:        3994620 kB
    Zswap:                 0 kB
    Zswapped:              0 kB
    Dirty:                 0 kB
    Writeback:             0 kB
    AnonPages:        604436 kB
    Mapped:           283408 kB
    Shmem:            102292 kB
    KReclaimable:      39016 kB
    Slab:             171936 kB
    SReclaimable:      39016 kB
    SUnreclaim:       132920 kB
    KernelStack:        9904 kB
    ShadowCallStack:    2500 kB
    PageTables:        17760 kB
    SecPageTables:         0 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:     5992356 kB
    Committed_AS:    4621344 kB
    VmallocTotal:   133141626880 kB
    VmallocUsed:       31816 kB
    VmallocChunk:          0 kB
    Percpu:             5280 kB
    HardwareCorrupted:     0 kB
    AnonHugePages:         0 kB
    ShmemHugePages:        0 kB
    ShmemPmdMapped:        0 kB
    FileHugePages:         0 kB
    FilePmdMapped:         0 kB
    CmaTotal:          32768 kB
    CmaFree:           31104 kB
    HugePages_Total:       0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB
    Hugetlb:               0 kB
    grace@grace-ubuntu:~$ 

 


The /proc pseudo filesystem

Key Commands:

  • ps aux: Lists all running processes with detailed information.

  • cat /proc/<PID>/cmdline: Shows the command that started the process.

  • ls /proc/<PID>/fd: Lists file descriptors for the process.

  • cat /proc/<PID>/status: Provides detailed information about the process, including memory usage and thread count.

  • ls /proc/<PID>/ns: Lists namespaces used by the process.

  • ls /proc/<PID>/net: Contains network-related configuration for the process.

  • cat /proc/<PID>/stat: Provides detailed information about the process.

  • cat /proc/<PID>/net/dev: Shows network device statistics.
     

Key Points:
 

  • The /proc directory is a pseudo file system that provides an interface to kernel data structures.

  • It contains various files and directories that offer detailed information about processes and system resources.

    • /proc/nnm - kernel keeps info for every running process

    • /proc/sys - kernel tunables organized by different kernel interfaces

    • /proc/*- many files that contain status info about a running system

  • Commands like ps aux, cat /proc/<PID>/cmdline, and ls /proc/<PID>/fd help you explore the state and behavior of processes in detail.

  • The /proc files offer deeper insights into system information directly from the kernel, which is useful for troubleshooting and optimization


Using /proc to get detailed system information

  • mount | grep proc: Shows how the proc pseudo file system is mounted.

  • cat /proc/cpuinfo: Provides detailed information about the CPU, including address sizes, flags, model name, and number of cores.

  • cat /proc/meminfo: Contains detailed information about memory usage.

  • cat /proc/cmdline: Displays the command line used to boot the system.

  • cat /proc/vmstat: Provides information about virtual memory usage.

  • cat /proc/modules: Lists currently loaded kernel modules and their memory addresses.

  • cat /proc/filesystems: Shows supported file systems.

  • cat /proc/swaps: Gives information about swap space in use.

  • cat /proc/mounts: Lists mounted file systems.


Key Points:
 

  • The /proc directory is a pseudo file system providing an interface to kernel data structures.

  • Commands like lsmod, lscpu, and swapon -a provide similar information but in a more readable format.

  • The /proc files offer deeper insights into system information directly from the kernel.
     


Reading process information on /proc

Some important key take aways

  • /proc/ Directory: This directory contains detailed information about the current state of processes. Each running process has a corresponding directory named by its PID (Process ID).

  • ps aux: This command lists all running processes with detailed information, including user, PID, CPU and memory usage, and the command that started the process.

  • cmdline: This file within a process's /proc/PID directory shows the exact command line that was used to start the process.

  • fd Directory: This directory contains file descriptors for the process, such as standard input (stdin), standard output (stdout), and standard error (stderr).

  • ns Directory: This directory lists the namespaces used by the process, which are isolated environments provided by the kernel.

  • net Directory: This directory contains network-related configuration for the process, including details about network interfaces and connections.

  • stat: This file provides detailed but hard-to-read information about the process, such as CPU usage, memory usage, and process state.

  • status: This file offers detailed information about the process, including memory usage, thread count, and other status details.
     

Commands

  • Command: ps aux

    • Description: Lists all running processes with detailed information.

    • Sample Output:
      USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
      root 1 0.0 0.1 22528 1024 ? Ss 10:00 0:00 /sbin/init

  • Command: cat /proc/3285/cmdline

    • Description: Displays the command line that started the process with PID 3285.

    • Sample Output:
      /usr/sbin/cron

  • Command: ls /proc/3285/fd

    • Description: Lists file descriptors for the process with PID 3285.

    • Sample Output:

      0 -> /dev/null
      1 -> socket:[38145]
      2 -> socket:[38145]


Tuning the kernel through /proc/sys

Commands:

  • View all tunables:
    bash
    sysctl -A

  • Change a tunable value temporarily:
    bash
    echo <new_value> > /proc/sys//

    Example:
    bash
    echo 60 > /proc/sys/vm/swappiness

  • Check the current value of a tunable:
    bash
    cat /proc/sys//

    Example:
    bash
    cat /proc/sys/vm/swappiness

Make a tunable value persistent:
Edit the /etc/sysctl.conf file or create a drop-in file in /etc/sysctl.d/ with the following format:
bash
vm.swappiness = 60

Key Points:

  • Temporary vs. Persistent Changes:

    • Temporary changes can be made using the echo command.

    • Persistent changes should be added to /etc/sysctl.conf or a drop-in file in /etc/sysctl.d/.

  • Dotted Notation:

    • Use dotted notation (e.g., vm.swappiness) in the /etc/sysctl.conf file.

  • Testing Changes:

    • Test changes in runtime first using the echo command. If the change works as expected, make it persistent.

  • Systemd Integration:

    • Persistent settings are processed by the systemd-sysctl.service unit file during boot.


Testing Critical Features with sysrq

Some of the use cases of sysrq are:

  1. Safe Reboot of a Frozen System (REISUB)
  2. Useful for debugging kernel issues. If crash dump is set up (like kdump), it will trigger a dump for analysis.
  3. Flushes data to disk—helps prevent data loss during a system hang or before forced power off.
  4. Force-kills everything except init. Can help recover control in some out-of-memory (OOM) situations.
  5. Prevents filesystem corruption before a forced reboot.
  6. Immediate Reboot or Shutdown
    • SysRq + B: Reboot
    • SysRq + O: Power off (on some systems)
  • sysrq Interface: Provides advanced operations on the kernel through the /proc/sysrq file.

  • Help Command: Use echo h > /proc/sysrq-trigger to display help information with dmesg.

  • Crash System: Use echo c > /proc/sysrq-trigger to crash the system.

  • Out-of-Memory Killer: Use echo f > /proc/sysrq-trigger to trigger the out-of-memory killer. - when there is no more memory, it will skill some of the memory to prevent the system from becoming fully unresponsive or crashing. The OOM killer makes sure that the critical system will still be functional even under low memory conditions.

  • Kill All Processes: Use echo i > /proc/sysrq-trigger to kill all processes.

  • System Reset: Use echo b > /proc/sysrq-trigger to reset the system.

  • Show Blocked Tasks: Use echo w > /proc/sysrq-trigger to show blocked tasks.

  • Show Memory Usage: Use echo m > /proc/sysrq-trigger to display detailed memory usage.


Using Watchdogs

  1. Use to reset the system if a serious problem is detected -> useful as when system hangs it will hang forever.
  2. At the lowest level a kernel module must be loaded to implement watchdog.
  3. This can be a hardware-related module that interfaces ipmi, or the softdog software-based watchdog.
  4. Ensure the watchdog service is enabled and started: systemctl enable --now watchdog.
  5. This daemon writes to /dev/watchdog once a minute by default (configure in /etc/watchdog.conf).
  6. If the daemon stops doint that, the system is considered to be failing, and wathcdog will reset the system.
    • dnf install watchdog
    • systemctl enable --now watchdog
    • modprobe -i softdog
    • echo c > /proc/sysrq-trigger

Laboratory:

  1. Install watchdog:

    apt install watchdog
  2. Enable watchdog for it to persist after reboot.

    systemctl enable --now watchdog
  3. AS we have no hardware to work with IPMI. We need to enable kernel module softdog. This module helps to reset the system if a serious problem is detected, preventing the system from hanging indefinitely. It is particularly useful in virtual machines where hardware watchdogs are not available.

    modprobe -i softdog
  4. Will execute sysrq command:

    echo c > /proc/sysrq/trigger
  5. The sysrq will make your system hang. However, because, we configured watchdog, it will trigger an automatic reboot to prevent your system from hanging.

 


Linux Kernel /sys

  1. The sysfs file system is mounted on /sys at boot
  2. sysfs is for kernel object info
  3. In particular, it is for hardware info ( for example, PCI device info)
  4. Underneath /sys were tens of thousands of entries

debugfs, trace under /sys

  1. /sys has debugfs filesystem mounted under /sys/kernel/debug
  2. /sys provides powerful tracing in /sys/kernel.tracing

Module Information

  1. /sys/module contains information about all of the currently loaded Linux kernel loadable modules
  2. Wether they were dyanamically loaded or statically linked into the kernel image

Device Information

  1. Information about block and character device files in /sys/dev
  2. Information about block devices in /sys/block
  3. Classes of devices in /sys/class
    • Like /sys/class/net

Challenges

sysfs

  1. From which files does lspci get its info

    User strace -e openat lspci to see

    grace@grace-ubuntu:~$ strace -e openat lspci
    openat(AT_FDCWD, "/sys/bus/pci/devices", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/config", O_RDONLY) = 3
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/vendor", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/device", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/class", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/revision", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/subsystem_vendor", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:05.0/subsystem_device", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/config", O_RDONLY) = 3
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/vendor", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/device", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/class", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/revision", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/subsystem_vendor", O_RDONLY) = 4
    openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:02.0/subsystem_device", O_RDONLY) = 4
    

     

  2. According to /sys/block, do you have a block device (disk) sda? If so, do you have device files for partitions of sda/vda? How many?

    grace@grace-ubuntu:~$ cd /sys/block/
    grace@grace-ubuntu:/sys/block$ ls
    dm-0   loop1   loop11  loop3  loop5  loop7  loop9
    loop0  loop10  loop2   loop4  loop6  loop8  vda

    It has 3 partitions named vda1, vda2 and vda 3

    grace@grace-ubuntu:/sys/block/vda$ ls
    alignment_offset  discard_alignment  hidden     power      size       vda1
    bdi               diskseq            holders    queue      slaves     vda2
    cache_type        events             inflight   range      stat       vda3
    capability        events_async       integrity  removable  subsystem
    dev               events_poll_msecs  mq         ro         trace
    device            ext_range          partscan   serial     uevent
    
  3. Using strace, does the command fdisk -l(run it as root( open any files under sys/block under /sys/dev/block?

    grace@grace-ubuntu:/sys/block$ sudo strace -e openat -o /tmp/strace.out fdisk -l
    .....
    .....
    
    Disk /dev/vda: 64 GiB, 68719476736 bytes, 134217728 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 7E7313D8-FC4B-4345-A5A9-2B8DF715664D
    
    Device       Start       End   Sectors  Size Type
    /dev/vda1     2048   2203647   2201600    1G EFI System
    /dev/vda2  2203648   6397951   4194304    2G Linux filesystem
    /dev/vda3  6397952 134215679 127817728 60.9G Linux filesystem
    
    
    Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 30.47 GiB, 32719765504 bytes, 63905792 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    grace@grace-ubuntu:/sys/block$ grep sys/block /tmp/strace.out 
    openat(AT_FDCWD, "/sys/block/loop0/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop1/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop2/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop3/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop4/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop5/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop6/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop7/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/vda/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/vda1/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/vda1/device/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/vda2/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/vda2/device/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/vda3/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/vda3/device/dev", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
    openat(AT_FDCWD, "/sys/block/dm-0/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/dm-0/dm/name", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop8/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop9/dev", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/block/loop10/dev", O_RDONLY|O_CLOEXEC) = 4
    

    It opens  files under /sys/dev/block

    grace@grace-ubuntu:/sys/block/vda$ grep sys/dev/block /tmp/strace.out 
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:0", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 4
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 5
    openat(AT_FDCWD, "/sys/dev/block/7:1", O_RDONLY|O_CLOEXEC) = 5