Simple custom linux kernel drivers
####kthreads.c: single suicidal kernel thread
-
Compile the task initialization and cleanup into a module. This will produce a .ko file.
$ cd drivers/kthreads && make
-
Insert the module into the kernel
$ sudo insmod kthreads.ko
-
Check that the task is running and watch the task lifecycle
$ ps -ef | grep erin_thread $ tail -f /var/log/{messages,kernel,dmesg,syslog}
####erin.c: static msg producer and sink character device
-
Compile the driver into a module, which can be loaded as a character device. This will produce a .ko file.
$ cd drivers/character_devices/simple && make
-
Make a new device node
$ sudo mknod /dev/erin c 250 0
$ sudo chmod a+r+w /dev/erin -
Insert a module into the linux kernel
$ sudo insmod erin.ko
-
Check that the module is loaded
$ cat /proc/modules | grep erin
-
Check that the module is listed as a character device
$ cat /proc/devices | grep erin
-
Watch what the device is doing
$ tail -f /var/log/{messages,kernel,dmesg,syslog}
-
Try using it
$ cat /dev/erin
$ echo "hi" > /dev/erin
####block.c: a block device backed by simple kernel buffer
-
Compile
$ cd drivers/block_devices/simple && make
-
Insert the module into the kernel
$ sudo insmod block.ko
-
Add a new partition the disk
$ fdisk /dev/sbd0
> n (new partition)
> p (primary partition)
> 1 (first partition number)
> (use defaults)
> w (write changes) -
Make a filesystem at the first partition
$ mkfs /dev/sbd0p1
-
Mount the filesystem
$ mount /dev/sdb0p1 /mnt
-
Create a file in the filesystem, and read from it, and check the size.
$ echo "Hi erin" > /mnt/file1
$ cat /mnt/file1
$ ls /mnt -
Cleanup
$ unmount /mnt
... If something goes wrong, and your kernel doesn't have CONFIG_MODULE_FORCE_UNLOAD set, you have no choice but to reboot.