write me a linux kernel module in C that purposefully panics the system via kdump
- User
write me a linux kernel module in C that purposefully panics the system via kdump
- Assistant
write me a linux kernel module in C that purposefully panics the system via kdump
Certainly! Below is an example of a Linux kernel module written in C that purposefully triggers a kernel panic. This can be useful for testing kdump configurations to ensure that your system correctly captures kernel crash dumps.
Important Warning: Running this module will crash your system. Do not run this on a production or critical system. Ensure that you have saved all work and are prepared for the system to reboot.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Kernel module that triggers a panic for kdump testing");
MODULE_VERSION("1.0");
static int __init panic_kdump_init(void)
{
printk(KERN_INFO "panic_kdump_test: Module loaded. Triggering kernel panic...\n");
panic("panic_kdump_test: Purposeful panic for kdump testing.\n");
return 0;
}
static void __exit panic_kdump_exit(void)
{
printk(KERN_INFO "panic_kdump_test: Module unloaded.\n");
}
module_init(panic_kdump_init);
module_exit(panic_kdump_exit);
Save the code above into a file named panic_kdump_test.c.
Prepare your build environment:
sudo apt-get install build-essential linux-headers-$(uname -r)
(Commands may vary depending on your Linux distribution.)Create a Makefile in the same directory with the following content:
obj-m += panic_kdump_test.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Build the kernel module by running:
make
Load the module (This will trigger a kernel panic):
sudo insmod panic_kdump_test.ko
Verify kdump:
/var/crash).Clean up (after rebooting):
make clean
panic() function is used to cause a kernel panic with a custom message.By triggering a kernel panic, you can test how your system handles crashes and whether kdump correctly captures and saves the crash dump for analysis.
Disclaimer: Use this code responsibly and at your own risk. The author and provider of this code are not responsible for any damage or data loss resulting from its use.
Not yet included in a published release.