Setting up environment for kernel module development

In this post I am going to document my config and steps to setup environment for developing kernel modules. My current system is MBP 2018 with MacOS Mojave, the environment I am going to setup is for linux kernel dev so this works for most of the OSes. Before going into the details I want to mention that the IDE, toolkit, etc., I mention are my preferences if you have any better setup please let me know in the comments. With that said, let’s dive-in and get the work done.

Virtual Machine When coming to developing kernel related software its always suggested to use virtual environments. Well I dont want QEMU for this setup as I am not going to compile the kernel so I am going with my favourite and easy to use virtualization software vagrant. If you have virtualbox you can skip the next step.

$ brew cask install virtualbox

Installing vagrant and running a linux VM. (Run the commands from vagrant up in the desired directory)

$ brew cask install vagrant
$ vagrant init ubuntu/bionic64
$ vagrant up

Once the VM is initialized and running you can login to shell via vagrant ssh.

Kernel module package Linux distros provide the commands modprobe, insmod and depmod within a package.

$ sudo apt-get install build-essential kmod

Headers Before you can build anything you’ll need to install the header files for your kernel. On GNU/Linux:

On Ubuntu:

$ sudo apt-get update
$ apt-cache search linux-headers-$(uname -r)

Once we find the required header we should install the required version.

$ sudo apt-get install kmod linux-headers-5.0.0-050000-generic

This steps are enough to get started with, If I make any more additions as I progress I will update the blog. If you want to setup the kernel dev setup with vscode check this repo. I am going to use VSCODE remote ssh for now.