mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-27 12:04:27 +01:00
Buildroot QEMU works. Nuff said.
This commit is contained in:
25
README.md
25
README.md
@@ -1,9 +1,32 @@
|
|||||||
# Linux Kernel Module Cheat
|
# Linux Kernel Module Cheat
|
||||||
|
|
||||||
|
Run one command, get into QEMU Buildroot BusyBox with several minimal Linux kernel module examples. Tested in Ubuntu 14.04.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
|
||||||
|
./run
|
||||||
|
|
||||||
|
First build will take a while (GCC, Linux kernel).
|
||||||
|
|
||||||
|
QEMU opens up, and you can run:
|
||||||
|
|
||||||
|
root
|
||||||
|
insmod /hello.ko
|
||||||
|
insmod /hello2.ko
|
||||||
|
rmmod hello
|
||||||
|
rmmod hello2
|
||||||
|
|
||||||
|
Each module comes from a C file under `kernel_module/package/kernel_module/src/`.
|
||||||
|
|
||||||
|
The Linux kernel version can be found with:
|
||||||
|
|
||||||
|
grep BR2_LINUX_KERNEL_VERSION buildroot/.config
|
||||||
|
|
||||||
1. [Introduction](introduction.md)
|
1. [Introduction](introduction.md)
|
||||||
1. [Build](build.md)
|
1. [Build](build.md)
|
||||||
1. [kmod](kmod.md)
|
1. [kmod](kmod.md)
|
||||||
1. Examples
|
1. Examples
|
||||||
1. [Host](host/)
|
1. [Host](host/)
|
||||||
1. Buildroot
|
1. Buildroot
|
||||||
1. [hello](hello.c)
|
1. [hello](kernel_module/package/kernel_module/src/hello.c)
|
||||||
|
1. [hello2](kernel_module/package/kernel_module/src/hello2.c)
|
||||||
|
|||||||
1
kernel_module/Config.in
Normal file
1
kernel_module/Config.in
Normal file
@@ -0,0 +1 @@
|
|||||||
|
source "$BR2_EXTERNAL_KERNEL_MODULE_PATH/package/kernel_module/Config.in"
|
||||||
1
kernel_module/external.desc
Normal file
1
kernel_module/external.desc
Normal file
@@ -0,0 +1 @@
|
|||||||
|
name: KERNEL_MODULE
|
||||||
1
kernel_module/external.mk
Normal file
1
kernel_module/external.mk
Normal file
@@ -0,0 +1 @@
|
|||||||
|
include $(sort $(wildcard $(BR2_EXTERNAL_KERNEL_MODULE_PATH)/package/*/*.mk))
|
||||||
5
kernel_module/package/kernel_module/Config.in
Normal file
5
kernel_module/package/kernel_module/Config.in
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
config BR2_PACKAGE_KERNEL_MODULE
|
||||||
|
bool "kernel_module"
|
||||||
|
depends on BR2_LINUX_KERNEL
|
||||||
|
help
|
||||||
|
Linux Kernel Module Cheat.
|
||||||
20
kernel_module/package/kernel_module/kernel_module.mk
Normal file
20
kernel_module/package/kernel_module/kernel_module.mk
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
################################################################################
|
||||||
|
#
|
||||||
|
# kernel_module
|
||||||
|
#
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
KERNEL_MODULE_VERSION = 1.0
|
||||||
|
KERNEL_MODULE_SITE = $(BR2_EXTERNAL_KERNEL_MODULE_PATH)/package/kernel_module/src
|
||||||
|
KERNEL_MODULE_SITE_METHOD = local
|
||||||
|
|
||||||
|
define KERNEL_MODULE_BUILD_CMDS
|
||||||
|
$(MAKE) -C '$(@D)' LINUX_DIR='$(LINUX_DIR)' PWD='$(@D)' CC='$(TARGET_CC)' LD='$(TARGET_LD)'
|
||||||
|
endef
|
||||||
|
|
||||||
|
define KERNEL_MODULE_INSTALL_TARGET_CMDS
|
||||||
|
$(INSTALL) -D -m 0755 $(@D)/*.ko '$(TARGET_DIR)'
|
||||||
|
endef
|
||||||
|
|
||||||
|
$(eval $(kernel-module))
|
||||||
|
$(eval $(generic-package))
|
||||||
10
kernel_module/package/kernel_module/src/Makefile
Normal file
10
kernel_module/package/kernel_module/src/Makefile
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
obj-m += hello.o hello2.o
|
||||||
|
ccflags-y := -Wno-declaration-after-statement -std=gnu99
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' modules
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) -C '$(LINUX_DIR)' M='$(PWD)' clean
|
||||||
17
kernel_module/package/kernel_module/src/hello.c
Normal file
17
kernel_module/package/kernel_module/src/hello.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
Hello world module.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
|
||||||
|
int init_module(void)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "hello init\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_module(void)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "hello cleanup\n");
|
||||||
|
}
|
||||||
19
kernel_module/package/kernel_module/src/hello2.c
Normal file
19
kernel_module/package/kernel_module/src/hello2.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
Hello world module 2.
|
||||||
|
|
||||||
|
Mostly to check that our build infrastructure can handle more than one module!
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/kernel.h>
|
||||||
|
|
||||||
|
int init_module(void)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "hello2 init\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_module(void)
|
||||||
|
{
|
||||||
|
printk(KERN_INFO "hello2 cleanup\n");
|
||||||
|
}
|
||||||
10
run
10
run
@@ -1,10 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -e
|
set -e
|
||||||
cd buildroot
|
cd buildroot
|
||||||
make BR2_EXTERNAL="$(pwd)/.." qemu_x86_64_defconfig
|
make BR2_EXTERNAL="$(pwd)/../kernel_module" qemu_x86_64_defconfig
|
||||||
echo '
|
echo 'BR2_PACKAGE_KERNEL_MODULE=y' >> .config
|
||||||
BR2_TOOLCHAIN_EXTERNAL=y
|
# kernel_module-reconfigure rebuilds the modules if they were modified.
|
||||||
BR2_PACKAGE_KERNEL_MODULE=y
|
env -u LD_LIBRARY_PATH make BR2_JLEVEL="$(($(nproc) - 2))" kernel_module-reconfigure all
|
||||||
' >> .config
|
|
||||||
make BR2_JLEVEL="$(($(nproc) - 2))"
|
|
||||||
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2,if=virtio,format=raw -append root=/dev/vda -net nic,model=virtio -net user
|
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2,if=virtio,format=raw -append root=/dev/vda -net nic,model=virtio -net user
|
||||||
|
|||||||
Reference in New Issue
Block a user