Buildroot QEMU works. Nuff said.

This commit is contained in:
Ciro Santilli
2017-05-11 07:28:35 +01:00
parent 338756a029
commit e06171b483
10 changed files with 102 additions and 7 deletions

View File

@@ -1,9 +1,32 @@
# 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. [Build](build.md)
1. [kmod](kmod.md)
1. Examples
1. [Host](host/)
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
View File

@@ -0,0 +1 @@
source "$BR2_EXTERNAL_KERNEL_MODULE_PATH/package/kernel_module/Config.in"

View File

@@ -0,0 +1 @@
name: KERNEL_MODULE

View File

@@ -0,0 +1 @@
include $(sort $(wildcard $(BR2_EXTERNAL_KERNEL_MODULE_PATH)/package/*/*.mk))

View File

@@ -0,0 +1,5 @@
config BR2_PACKAGE_KERNEL_MODULE
bool "kernel_module"
depends on BR2_LINUX_KERNEL
help
Linux Kernel Module Cheat.

View 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))

View 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

View 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");
}

View 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
View File

@@ -1,10 +1,8 @@
#!/usr/bin/env bash
set -e
cd buildroot
make BR2_EXTERNAL="$(pwd)/.." qemu_x86_64_defconfig
echo '
BR2_TOOLCHAIN_EXTERNAL=y
BR2_PACKAGE_KERNEL_MODULE=y
' >> .config
make BR2_JLEVEL="$(($(nproc) - 2))"
make BR2_EXTERNAL="$(pwd)/../kernel_module" qemu_x86_64_defconfig
echo 'BR2_PACKAGE_KERNEL_MODULE=y' >> .config
# kernel_module-reconfigure rebuilds the modules if they were modified.
env -u LD_LIBRARY_PATH make BR2_JLEVEL="$(($(nproc) - 2))" kernel_module-reconfigure all
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