Navigation Menu

Skip to content

edahlgren/custom_drivers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

custom_drivers

Simple custom linux kernel drivers

####kthreads.c: single suicidal kernel thread

  1. Compile the task initialization and cleanup into a module. This will produce a .ko file.

    $ cd drivers/kthreads && make
  2. Insert the module into the kernel

    $ sudo insmod kthreads.ko
  3. 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

  1. 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
  2. Make a new device node

    $ sudo mknod /dev/erin c 250 0
    $ sudo chmod a+r+w /dev/erin
  3. Insert a module into the linux kernel

    $ sudo insmod erin.ko
  4. Check that the module is loaded

    $ cat /proc/modules | grep erin
  5. Check that the module is listed as a character device

    $ cat /proc/devices | grep erin
  6. Watch what the device is doing

    $ tail -f /var/log/{messages,kernel,dmesg,syslog}
  7. Try using it

    $ cat /dev/erin
    $ echo "hi" > /dev/erin

####block.c: a block device backed by simple kernel buffer

  1. Compile

    $ cd drivers/block_devices/simple && make
  2. Insert the module into the kernel

    $ sudo insmod block.ko
  3. Add a new partition the disk

    $ fdisk /dev/sbd0
    > n (new partition)
    > p (primary partition)
    > 1 (first partition number)
    > (use defaults)
    > w (write changes)
  4. Make a filesystem at the first partition

    $ mkfs /dev/sbd0p1
  5. Mount the filesystem

    $ mount /dev/sdb0p1 /mnt
  6. Create a file in the filesystem, and read from it, and check the size.

    $ echo "Hi erin" > /mnt/file1
    $ cat /mnt/file1
    $ ls /mnt
  7. 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.

About

Simple custom linux kernel drivers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages