From 00e921ec1075368b4a249980ff8ac12165fab118 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Wed, 10 Nov 2021 01:14:16 -0500 Subject: [PATCH] A hello world Linux kernel module --- .gitignore | 8 ++++++++ Makefile | 7 +++++++ README.md | 11 +++++++++++ Vagrantfile | 6 ++++++ build.sh | 13 +++++++++++++ helloworld.c | 21 +++++++++++++++++++++ 6 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 Vagrantfile create mode 100644 build.sh create mode 100644 helloworld.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c1d5ed7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.cmd +*.ko +*.mod* +*.o +.helloworld.o.d +.vagrant +Module.symvers +modules.order diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3e9d04f --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +obj-m += helloworld.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..a83453b --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +## kernmod + +A hello world Linux kernel module and build environment in Debian GNU/Linux. + + +### Public Domain Dedication +Copyright (C) 2021 Kris Lamoureux + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..40ebe0f --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,6 @@ +Vagrant.configure("2") do |config| + config.vm.box = "debian/bullseye64" + config.vm.synced_folder ".", "/vagrant" + config.vm.network "private_network", type: "dhcp" + config.vm.provision "shell", path: "build.sh" +end diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..594558d --- /dev/null +++ b/build.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +export DEBIAN_FRONTEND=noninteractive +apt-get update +apt-get install build-essential linux-headers-$(uname -r) -y + +cd /vagrant +make +modinfo helloworld.ko +insmod helloworld.ko +cat /proc/modules | grep helloworld +rmmod helloworld +cat /var/log/messages | grep helloworld diff --git a/helloworld.c b/helloworld.c new file mode 100644 index 0000000..f6613e8 --- /dev/null +++ b/helloworld.c @@ -0,0 +1,21 @@ +#include +#include +#include + +#define DRIVER_AUTHOR "Kris Lamoureux" +#define DRIVER_DESC "Hello world kernel module" + +int init_module(void) +{ + printk(KERN_INFO "helloworld: hello, world\n"); + return 0; +} + +void cleanup_module(void) +{ + printk(KERN_INFO "helloworld: goodbye, world\n"); +} + +MODULE_LICENSE("Dual BSD/GPL"); +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC);