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
printkis the kernel's function for code to print messages; it is likeprintfin C.- 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. - The ones that are important enough are shown in the console.
- The logging daemon may send to a file or elsewhere.
- Use
journalctl -kwith-fto follow as new log messages are added. - The proc and sysfs file systems are virtual file systems.
- Their contents are not stored on disk - made up when you ask for them.
- 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.
- 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.
- All the files have some functions the kernel is associated with them that get called when you use that file.
What is /proc?
- It is the psuedo file system for Linux
- The proc filesystem is mounted on /proc at boot
- proc gets its name from "process"
- proc contains lots of process information and lots more
What is /proc: Process Info
- Each process has a directory named with its PID
- So if you go to /proc, you will see a lot of process ID numbers
- It will contain informmation about:
- memory usage of the process
- what program its running
- what files it has open
- There can be hundred of files and directorues per process
- Threads have entried under directory /task
LAB - Inspecting Kernel messages and /proc
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 roUse 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-genericYou 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
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
/procdirectory 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, andls /proc/<PID>/fdhelp you explore the state and behavior of processes in detail.The
/procfiles 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 auxDescription: 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/cmdlineDescription: Displays the command line that started the process with PID 3285.
Sample Output:
/usr/sbin/cron
Command:
ls /proc/3285/fdDescription: 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 -AChange a tunable value temporarily:
bash
echo <new_value> > /proc/sys//
Example:
bash
echo 60 > /proc/sys/vm/swappinessCheck 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
echocommand.Persistent changes should be added to
/etc/sysctl.confor a drop-in file in/etc/sysctl.d/.
Dotted Notation:
Use dotted notation (e.g.,
vm.swappiness) in the/etc/sysctl.conffile.
Testing Changes:
Test changes in runtime first using the
echocommand. If the change works as expected, make it persistent.
Systemd Integration:
Persistent settings are processed by the
systemd-sysctl.serviceunit file during boot.
Testing Critical Features with sysrq
Some of the use cases of sysrq are:
- Safe Reboot of a Frozen System (REISUB)
- Useful for debugging kernel issues. If crash dump is set up (like
kdump), it will trigger a dump for analysis. - Flushes data to diskāhelps prevent data loss during a system hang or before forced power off.
- Force-kills everything except
init. Can help recover control in some out-of-memory (OOM) situations. - Prevents filesystem corruption before a forced reboot.
- Immediate Reboot or Shutdown
SysRq + B: RebootSysRq + O: Power off (on some systems)
sysrq Interface: Provides advanced operations on the kernel through the
/proc/sysrqfile.Help Command: Use
echo h > /proc/sysrq-triggerto display help information withdmesg.
Crash System: Use
echo c > /proc/sysrq-triggerto crash the system.Out-of-Memory Killer: Use
echo f > /proc/sysrq-triggerto 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-triggerto kill all processes.System Reset: Use
echo b > /proc/sysrq-triggerto reset the system.Show Blocked Tasks: Use
echo w > /proc/sysrq-triggerto show blocked tasks.
Show Memory Usage: Use
echo m > /proc/sysrq-triggerto display detailed memory usage.
Using Watchdogs
- Use to reset the system if a serious problem is detected -> useful as when system hangs it will hang forever.
- At the lowest level a kernel module must be loaded to implement watchdog.
- This can be a hardware-related module that interfaces ipmi, or the softdog software-based watchdog.
- Ensure the watchdog service is enabled and started: systemctl enable --now watchdog.
- This daemon writes to /dev/watchdog once a minute by default (configure in /etc/watchdog.conf).
- 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:
Install watchdog:
apt install watchdogEnable watchdog for it to persist after reboot.
systemctl enable --now watchdogAS 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 softdogWill execute sysrq command:
echo c > /proc/sysrq/trigger- 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
- The sysfs file system is mounted on /sys at boot
- sysfs is for kernel object info
- In particular, it is for hardware info ( for example, PCI device info)
- Underneath /sys were tens of thousands of entries
debugfs, trace under /sys
/syshas debugfs filesystem mounted under/sys/kernel/debug/sysprovides powerful tracing in/sys/kernel.tracing
Module Information
/sys/modulecontains information about all of the currently loaded Linux kernel loadable modules- Wether they were dyanamically loaded or statically linked into the kernel image
Device Information
- Information about block and character device files in
/sys/dev - Information about block devices in
/sys/block - Classes of devices in
/sys/class- Like
/sys/class/net
- Like
Challenges
sysfs
From which files does lspci get its info
User
strace -e openat lspcito seegrace@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) = 4According 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 vdaIt 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 ueventUsing strace, does the command fdisk -l(run it as root( open any files under
sys/blockunder/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 bytesgrace@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) = 4It 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
No Comments