diff --git a/README.md b/README.md index 7b1af9e..1020d9a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Linux Kernel Module Cheat -Run one command, get into QEMU Buildroot BusyBox with several minimal Linux kernel module examples. Tested in Ubuntu 14.04. +Run one command, get into QEMU Buildroot BusyBox with several minimal Linux kernel module example tutorials. Tested in Ubuntu 14.04. Usage: @@ -30,3 +30,4 @@ The Linux kernel version can be found with: 1. [hello](kernel_module/hello.c) 1. [hello2](kernel_module/hello2.c) 1. [debugfs](kernel_module/debugfs.c) + 1. [fops](kernel_module/fops.c) diff --git a/kernel_module/debugfs.c b/kernel_module/debugfs.c index 0564839..f983156 100644 --- a/kernel_module/debugfs.c +++ b/kernel_module/debugfs.c @@ -1,17 +1,11 @@ /* Adapted from: https://github.com/chadversary/debugfs-tutorial/blob/47b3cf7ca47208c61ccb51b27aac6f9f932bfe0b/example1/debugfs_example1.c -Requires CONFIG_DEBUG_FS. +Usage: - mkdir /debugfs - mount -t debugfs none /debugfs - insmod /debugfs.ko - cd /debugfs/kernel_module_cheat - cat myfile + /debugfs.sh -Output: - - 42 +Requires `CONFIG_DEBUG_FS=y`. */ #include diff --git a/kernel_module/fops.c b/kernel_module/fops.c index 98487c2..95cd2e1 100644 --- a/kernel_module/fops.c +++ b/kernel_module/fops.c @@ -1,6 +1,10 @@ /* dmesg stuff when fops happen. +Usage: + + /fops.sh + fops define what the kernel will do on filesystem system calls on all of /dev, /proc, /sys, and consistute the main method of userland communication in drivers (syscalls being the other one). @@ -19,19 +23,19 @@ static struct dentry *dir = 0; int fop_open(struct inode *inode, struct file *file) { - printk(KERN_INFO "open"); + printk(KERN_INFO "open\n"); return 0; } ssize_t fop_read(struct file *file, char __user *buf, size_t len, loff_t *off) { - printk(KERN_INFO "read"); + printk(KERN_INFO "read\n"); return 0; } ssize_t fop_write(struct file *file, const char __user *buf, size_t len, loff_t *off) { - printk(KERN_INFO "write"); + printk(KERN_INFO "write\n"); return 0; } @@ -41,7 +45,7 @@ http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called- */ int fop_release (struct inode *inode, struct file *file) { - printk(KERN_INFO "release"); + printk(KERN_INFO "release\n"); return 0; } diff --git a/rootfs_overlay/debugfs.sh b/rootfs_overlay/debugfs.sh new file mode 100755 index 0000000..60043d7 --- /dev/null +++ b/rootfs_overlay/debugfs.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -ex +mkdir -p /debugfs +mount -t debugfs none /debugfs +insmod /debugfs.ko +cd /debugfs/kernel_module_cheat +cat myfile +# => 42 diff --git a/rootfs_overlay/etc/inittab b/rootfs_overlay/etc/inittab index debf9a4..b377665 100644 --- a/rootfs_overlay/etc/inittab +++ b/rootfs_overlay/etc/inittab @@ -1,3 +1,14 @@ # /etc/inittab +::sysinit:/bin/mount -t proc proc /proc +::sysinit:/bin/mount -o remount,rw / +::sysinit:/bin/mkdir -p /dev/pts +::sysinit:/bin/mkdir -p /dev/shm +::sysinit:/bin/mount -a +::sysinit:/bin/hostname -F /etc/hostname +::sysinit:/etc/init.d/rcS # https://unix.stackexchange.com/questions/299408/how-to-login-automatically-without-typing-root-in-buildroot-x86-64-qemu console::respawn:/bin/sh +::ctrlaltdel:/sbin/reboot +::shutdown:/etc/init.d/rcK +::shutdown:/sbin/swapoff -a +::shutdown:/bin/umount -a -r diff --git a/rootfs_overlay/fops.sh b/rootfs_overlay/fops.sh new file mode 100755 index 0000000..7a7836d --- /dev/null +++ b/rootfs_overlay/fops.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -ex +insmod /fops.ko +mkdir -p /fops +mount -t debugfs none /fops +cd /fops/kernel_module_cheat +cat fops +# => open +# => read +# => close +# TODO. +#echo a >fops