Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 54

Configuring Kdump for Kernel Crash Dumps

Recommended resources

Introduction

In our last lesson, we explored the powerful world of live kernel debugging with KGDB and KDB. We learned how to halt a running kernel, set breakpoints, and inspect its state interactively. This is an invaluable skill, but it relies on being able to connect to a live system and keep it paused, which isn't always possible on a production device or for crashes that happen too quickly.

This lesson addresses that limitation by introducing you to post-mortem debugging. We will learn how to configure a system to act like an aircraft's "black box," automatically saving a complete snapshot of the kernel's memory at the exact moment of a crash. This snapshot, called a crash dump, can then be analyzed offline to find the root cause.

Today, you will learn to configure the kdump service to capture a kernel crash dump upon a panic. We will cover the underlying mechanism, the necessary system configuration, and how to trigger a test crash to verify your setup.


1. The Kdump Mechanism: Two Kernels are Better Than One

When a kernel panics, it's in an unstable state. It cannot be trusted to perform complex operations like writing its own memory image to a disk file. The solution is to use a second, clean kernel to perform the capture. This is the core idea behind kdump.

The process relies on kexec, a feature that allows one kernel to boot directly into another, bypassing the BIOS/firmware and hardware initialization. kdump uses kexec in a specific way:

  1. Reservation: When the primary (or "production") kernel boots, it reserves a small, protected region of RAM using the crashkernel boot parameter.
  2. Pre-loading: A second, minimal "capture" kernel and a special initramfs are loaded into this reserved memory. This capture kernel sits dormant while the production kernel runs the system.
  3. Panic!: When the production kernel panics, instead of halting, it triggers a kexec into the pre-loaded capture kernel.
  4. Capture: The capture kernel boots in a clean state. Its main job is to access the memory of the crashed production kernel (which is now just static data in RAM) and save it to a file. This file is often called vmcore.
  5. Reboot: Once the dump is saved, the capture kernel reboots the machine back into the normal production kernel.

This two-kernel dance is the foundation of reliable crash dumping on Linux.

Kdump Mechanism for Kernel Crash Dumps
This diagram illustrates the kdump process. The production kernel runs normally. Upon a crash, the system boots into the dedicated capture kernel, which then saves the memory contents of the crashed kernel to a dump file before rebooting.

Let's look at the components and flow in more detail.

kdump: usage and internals

This presentation provides an excellent overview of the kdump mechanism. We'll focus on the first few slides to solidify our understanding of the core concepts.

Please review slides 2-5 and 9-11 of this PDF presentation. Slides 2-5 ('Overview' and 'Kernel: kexec_load()'): Focus on the relationship between kexec and kdump, and note the KEXEC_ON_CRASH flag, which is the kernel-level switch that enables this whole process. Slides 9-11 ('Kdump: revisit', 'The Primary Kernel', 'The Capture Kernel'): These slides summarize the two-kernel concept and introduce crashkernel=, elfcorehdr, and /proc/vmcore.


2. Configuring Your System for Kdump

Setting up kdump involves a clear, sequential process of configuring the kernel, bootloader, and user-space services. Let's walk through it step-by-step.

Step 1: Kernel Configuration

First, you must ensure your kernel is built with the necessary features. For most modern distributions, these are enabled by default, but if you're building a custom kernel for an embedded device, you must verify them.

Oops! Debugging Kernel Panics

This Linux Journal article provides a practical checklist of the kernel configuration options required for kdump.

Read the first section of the article, down to the list of CONFIG_ options. Pay attention to what each option enables.

Here's a summary of the essential kernel options:

  • CONFIG_KEXEC=y: Enables the core kexec system call.
  • CONFIG_CRASH_DUMP=y: Enables the special kexec_load() functionality for a crash kernel.
  • CONFIG_PROC_VMCORE=y: This is crucial for the capture kernel. It creates the /proc/vmcore pseudo-file, which provides an ELF-formatted view into the old kernel's memory.
  • CONFIG_DEBUG_INFO=y: Just as with KGDB, this includes DWARF debugging symbols in the vmlinux file, which is essential for analyzing the vmcore later.
  • CONFIG_MAGIC_SYSRQ=y: Allows you to trigger a crash manually for testing purposes.

Step 2: Reserving Memory with crashkernel

This is the most critical step. You must tell the production kernel to reserve memory for the capture kernel at boot time. This is done via a kernel command-line parameter in your bootloader configuration (e.g., grub.cfg).

The parameter is crashkernel=size. For example, crashkernel=256M.

Oops! Debugging Kernel Panics

Let's dive into the specifics of the crashkernel parameter and how to apply it.

Read the section starting from 'Now, open this file: /etc/default/grub.d/kdump-tools.default' down to 'And, reboot the system.' This shows a concrete example of how to modify the bootloader configuration on a Debian-based system and then update it.

After modifying your bootloader configuration and rebooting, you can verify that the memory was reserved:




# Check the kernel command line that was actually used
dmesg | grep "Command line"




# Look for the kernel message confirming the reservation
dmesg | grep "Reserving"



# Expected output: [ 0.000000] Reserving 256MB of memory at <address> for crashkernel




# You can also inspect the physical memory map
cat /proc/iomem | grep "Crash kernel"

Step 3: Installing and Configuring User-space Tools

With the kernel and bootloader prepared, the final step is to configure the user-space kdump service. This is typically handled by a package like kdump-tools (on Debian/Ubuntu) or kexec-tools (on Fedora/RHEL).




# On Debian/Ubuntu
sudo apt-get install kdump-tools crash




# On Fedora/RHEL
sudo dnf install kexec-tools crash

The main configuration file is usually /etc/kdump.conf (or /etc/sysconfig/kdump on older systems). This file tells the kdump service what to do when it runs inside the capture kernel environment.

kdump: usage and internals

The 'kdump: usage and internals' presentation gives a clear breakdown of the kdump configuration files.

Review slides 29-31 ('Kdump: The Fedora way'). Although the file paths are for Fedora, the options are conceptually the same across distributions. Focus on the purpose of: path: The directory where vmcore is saved. core_collector: The program used to copy and compress the dump (e.g., makedumpfile). default: The action to take after the dump completes (e.g., reboot).

A minimal /etc/kdump.conf might look like this:




# Path to save the crash dump
path /var/crash




# Use makedumpfile to compress the data and exclude unused pages
core_collector makedumpfile -l --message-level 1 -d 31




# Action to take after saving the dump
default reboot

After configuring, enable and start the service:

sudo systemctl enable kdump.service
sudo systemctl start kdump.service

3. Verifying and Testing the Kdump Setup

How do you know if it's working? First, you can check the status of the kdump service and see if the crash kernel is loaded.

Oops! Debugging Kernel Panics

The Linux Journal article provides a fantastic set of commands to verify a complete kdump setup.

Read the section from 'Your kdump service should be running:' down to the output of the 'sudo kdump-config show' command. Execute these commands on your own test system if you have one set up.

Key verification commands:

  • systemctl status kdump.service: Should show the service is active.
  • cat /sys/kernel/kexec_crash_loaded: Should return 1.
  • kdump-config show (on Debian/Ubuntu): Gives a comprehensive status report.

The final test is to trigger a panic and see if a dump is captured. The safest way to do this is with the Magic SysRq key.




# First, ensure SysRq is enabled
echo 1 | sudo tee /proc/sys/kernel/sysrq




# Trigger a manual kernel panic
echo c | sudo tee /proc/sysrq-trigger

What to expect:

  1. Your system will instantly freeze and display a kernel panic message on the console.
  2. The kexec mechanism will boot into the capture kernel. You may see boot messages from this second kernel.
  3. The capture kernel will run the kdump scripts, which will use makedumpfile to save the vmcore to the path you specified in /etc/kdump.conf.
  4. The machine will reboot.
  5. After rebooting, you can navigate to /var/crash (or your configured path) and find a new directory named with the date and time of the crash, containing the vmcore file.

Congratulations! You have successfully captured a kernel crash dump.


4. Interview Scenario

Interviewer: "We have a fleet of embedded devices in the field that are mission-critical. Recently, a few of them have started to reboot unexpectedly. We suspect a kernel panic. We cannot attach a live debugger. Your task is to provide a clear, step-by-step procedure for our field technicians to enable post-mortem debugging on these devices so they can capture the crash information for you to analyze. What procedure do you give them?"

Click for Analysis

Your Analysis:

"The goal is to enable kdump to automatically capture a vmcore file upon the next panic. I would provide the field technician with the following simplified, step-by-step instructions:

  1. Objective: We are going to configure the device to save a 'black box' recording of the kernel's state when it crashes. This will allow our engineering team to diagnose the root cause.

  2. Step 1: Reserve Memory for the Capture System.

    • Log into the device's console.
    • You will need to edit the bootloader's configuration file. This is typically located at /boot/grub/grub.cfg or a similar path.
    • Find the line that starts with linux /boot/vmlinuz-....
    • At the end of this line, add the following parameter: crashkernel=256M. This tells the system to set aside 256MB of memory for the crash capture utility.
    • Save the file and reboot the device for this change to take effect.
  3. Step 2: Install the Crash Capture Tools.

    • Once the device reboots, log back in.
    • Install the necessary software package by running: sudo apt-get update && sudo apt-get install kdump-tools.
  4. Step 3: Configure the Dump Location.

    • The system needs to know where to save the crash file. We'll edit the configuration file: sudo nano /etc/kdump.conf.
    • Find the line that starts with #path. Remove the # and ensure it reads path /var/crash. This sets the save location.
    • Find the line that starts with #default. Remove the # and ensure it reads default reboot. This will make the device automatically reboot after saving the crash data.
    • Save and close the file.
  5. Step 4: Start the Capture Service.

    • Finally, we need to enable and start the service that waits for a crash. Run this command: sudo systemctl enable --now kdump.service.
  6. Confirmation:

    • To confirm the setup is ready, run cat /sys/kernel/kexec_crash_loaded. If it returns 1, the system is armed and ready to capture the next crash.
  7. Final Instruction: Let the device run normally. After the next unexpected reboot occurs, please check the /var/crash directory. There should be a new folder containing the crash data. Please compress this folder and send it to the engineering team."

This procedure is clear, provides context for each step, and includes a verification command, making it suitable for a technician who may not be a kernel expert.


Conclusion

You have now bridged the gap between live debugging and post-mortem analysis. By configuring kdump, you've created a safety net that ensures even the most elusive crashes leave behind the evidence needed for diagnosis.

Key Takeaways:

  • Kdump uses a two-kernel approach: A small, pre-loaded capture kernel boots upon the production kernel's panic to safely save its memory.
  • The crashkernel= boot parameter is non-negotiable: It carves out the protected memory required for the capture kernel to live in.
  • Configuration involves three stages: Enabling kernel CONFIG options, setting the crashkernel= boot parameter, and configuring the user-space kdump service.
  • Testing is crucial: Always trigger a test panic with sysrq to ensure your configuration works before deploying it to a critical system.

Preview of the Next Lesson:

We've successfully captured a vmcore file. But this file is a raw, multi-gigabyte binary dump of memory. How do we turn it into useful information? In our next lesson, "Analyze a kernel crash dump using the 'crash' utility to perform a post-mortem root cause analysis," we will learn how to load this vmcore file into the powerful crash utility and begin dissecting it to find the bug that caused the panic.