simulating kernel panic

How can a kernel panic be simulated?

Create and load a minimal module calling the kernels panic() function, eg for Linux (from Linux Crash HOWTO)

$ cat > panic.c <<EOF
// ### panic.c ###########################

#define __KERNEL__
// # MODULE 

# include init_module(void)

int init_module (void)
{
    panic(" panic has been called");
    return 0;
}
EOF
$ cc -c -I/usr/src/linux/include panic.c
$ insmod panic.o

Note: there's a bug included here on purpose in case anyone wants to try this. The purpose is simply that one shouldn't copy and paste possibly dangerous code without thinking.

---------- Post updated at 14:05 ---------- Previous update was at 13:04 ----------

OK, I should have taken a look at the timestamp of that How-To. Here's a current version for modern kernels (again, with easy to spot bug, and lots of thanks to this page):

// panic.c
#include <linu/module.h>

static int __init panic_init_module(void) {
    panic(" panic has been called");
}

module_init(panic_init_module);
MODULE_LICENSE("GPL");
# Makefile
# Intendations have to be tabs, not spaces
UNAME := $(shell uname -r)
KERNEL26 := 2.6
KERNELVERSION := $(findstring $(KERNEL26),$(UNAME))

ifeq ($(KERNELVERSION),2.6)

obj-m := panic.o

INCLUDE := -I/usr/include/asm/
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

all::
	$(MAKE) -C $(KDIR) $(INCLUDE) SUBDIRS=$(PWD) modules
 
else

TARGET := panic
INCLUDE := -I/lib/modules/`uname -r`/build/include -I/usr/include/asm/
CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX
CC := gcc

all:: ${TARGET}.o

${TARGET}.o: ${TARGET}.c
	$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c
 
endif

Run it using

insmod ./panic.o # Kernel 2.4
insmod ./panic.ko # Kernel 2.6

Encountered the following error:

[root@wiki ~]# make panic
cc     panic.c   -o panic
panic.c:2:26: error: linux/module.h: No such file or directory
panic.c:4: error: expected �=�, �,�, �;�, �asm� or �__attribute__� before �panic_init_module�
panic.c:8: warning: data definition has no type or storage class
panic.c:8: warning: parameter names (without types) in function declaration
panic.c:9: error: expected declaration specifiers or �...� before string constant
panic.c:9: warning: data definition has no type or storage class
make: *** [panic] Error 1

First: no need for 'make panic', make alone should suffice.
Second: you'll need the sources for your current kernel installed, of course. Apparently my thinking that this would be obvious was far too idealistic.

[root@wiki ~]# rpm -qa|grep kernel
kernel-headers-2.6.18-164.el5
kernel-PAE-2.6.18-128.el5
kernel-PAE-2.6.18-128.4.1.el5
[root@wiki ~]# uname -r
2.6.18-128.4.1.el5PAE

You want to mean that i need the kernel-devel package installed for the above kernel?

If you are using Redhat, you install and configure kexec/kdump. Then to panic the kernel and cause it to crash you simply do the following:

# echo "c" > /proc/sysrq-trigger

See How do I configure kexec/kdump on Red Hat for full information.