Skip to content

Instantly share code, notes, and snippets.

@wojtekka
Created November 13, 2025 20:47
Show Gist options
  • Select an option

  • Save wojtekka/d08612f6a108507cbc8e67e737e92700 to your computer and use it in GitHub Desktop.

Select an option

Save wojtekka/d08612f6a108507cbc8e67e737e92700 to your computer and use it in GitHub Desktop.
Building out-of-tree Linux kernel module
cat > Kbuild << EOF
obj-m := foo.o
EOF
cat > Makefile << EOF
KDIR ?= /lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
EOF
cat > foo.c << EOF
#include <linux/module.h>
static int foo_init(void)
{
printk("foo_init\n");
return 0;
}
static void foo_exit(void)
{
printk("foo_exit\n");
}
module_init(foo_init);
module_exit(foo_exit);
MODULE_AUTHOR("John Doe <john@doe>");
MODULE_DESCRIPTION("Foo module");
MODULE_LICENSE("GPL v2");
EOF
make
sudo insmod foo.ko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment