diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b9ab2a0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "buildroot"] + path = buildroot + url = https://github.com/buildroot/buildroot diff --git a/Config.in b/Config.in new file mode 100644 index 0000000..9a757c5 --- /dev/null +++ b/Config.in @@ -0,0 +1,6 @@ +config BR2_PACKAGE_KERNEL_MODULE + bool "kernel_module" + help + Kernel module hello world. + + http://example.com/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fd934d1 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +obj-m += hello.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 diff --git a/README.md b/README.md index 9155383..7d3974c 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,6 @@ 1. [Build](build.md) 1. [kmod](kmod.md) 1. Examples - 1. [Host](host/) - -TODO: use Buildroot as in this answer http://stackoverflow.com/a/38813400/895245 to install and test modules on QEMU. + 1. [Host](host/) + 1. Buildroot + 1. [hello](hello.c) diff --git a/buildroot b/buildroot new file mode 160000 index 0000000..083c073 --- /dev/null +++ b/buildroot @@ -0,0 +1 @@ +Subproject commit 083c0735e924e0d534328095a944ae159f661219 diff --git a/external.desc b/external.desc new file mode 100644 index 0000000..74da067 --- /dev/null +++ b/external.desc @@ -0,0 +1 @@ +name: KERNEL_MODULE diff --git a/external.mk b/external.mk new file mode 100644 index 0000000..843df5c --- /dev/null +++ b/external.mk @@ -0,0 +1,20 @@ +################################################################################ +# +# kernel_module +# +################################################################################ + +KERNEL_MODULE_VERSION = 1.0 +KERNEL_MODULE_SITE = .. +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)/kernel_module.ko' '$(TARGET_DIR)/kernel_module.ko' +endef + +$(eval $(kernel-module)) +$(eval $(generic-package)) diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..0156ed1 --- /dev/null +++ b/hello.c @@ -0,0 +1,13 @@ +#include +#include + +int init_module(void) +{ + printk(KERN_INFO "hello init\n"); + return 0; +} + +void cleanup_module(void) +{ + printk(KERN_INFO "hello cleanup\n"); +} diff --git a/run b/run new file mode 100755 index 0000000..c492369 --- /dev/null +++ b/run @@ -0,0 +1,10 @@ +#!/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))" +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