Linux Kernel

🌟 Demystifying Linux Kernel Development with Multipass 🌟

As I explore the fascinating world of Linux Kernel Development, I’ve been experimenting with writing and building custom kernel modules. Along the way, I discovered a streamlined way to set up and experiment using Multipass, a lightweight VM manager for Linux, Windows and macOS.

https://github.com/canonical/multipass

πŸ’‘ Here’s an Example and Steps to Get Started with Kernel Modules Using Multipass:

This example is based on MacBook Pro (chip: Apple M1 Pro).

Setting Up Multipass on macOS

Prerequisites

Ensure that Homebrew is installed on your macOS before proceeding. For instructions, visit Homebrew.

Installation

1. Install Multipass Run the following command to install Multipass using Homebrew:

1brew install --cask multipass

2. Verify Installation Check if Multipass was installed successfully:

1multipass version

3. Explore Available Commands View the list of available commands:

1multipass help

Creating an Instance for the Demo

1. Create a Demo Instance Launch an instance named primary:

1multipass launch 24.04 --name primary

2. Connect to the Instance Access the shell of the primary instance:

1multipass shell primary

Configuring SSH Access

1. Modify SSH Configuration Open the SSH configuration file:

1sudo vim /etc/ssh/sshd_config

Update the KbdInteractiveAuthentication property to yes:

1KbdInteractiveAuthentication yes

2. Reload Daemon and Restart SSH Service Reload the system daemon:

1sudo systemctl daemon-reload

Restart the SSH service:

1sudo service ssh restart
2sudo systemctl restart ssh

3. Set User Password Set a password for the default ubuntu user. For this example, the password will be demo123:

1sudo passwd ubuntu

Linux Device Drivers

This guide outlines the steps to develop, build, and manage a simple Linux kernel module using an example "Hello World" module.

Setup Environment

  1. Connect to Multipass Instance Connect to the intance if not done
1multipass shell primary
  1. Create a Directory for Module Development Create a directory to store the module files:
1mkdir ldd
  1. Exit Current Instance To disconnect from the current instance:
1exit
  1. Connect to Multipass Ubuntu Using VSCode Remote SSH Use the following SSH parameters:
  1. Install Required Utilities Update the system and install the necessary build tools and kernel headers:
1sudo apt update
2sudo apt upgrade
3sudo apt install -y build-essential linux-headers-$(uname - r) kmod

Create a "Hello World" Kernel Module

  1. Write the Source Code Create a file named helloworld.c with the following content:
 1#include <linux/kernel.h>
 2#include <linux/module.h>
 3#include <linux/init.h>
 4
 5MODULE_LICENSE("GPL");
 6MODULE_AUTHOR("Coumarane COUPPANE");
 7MODULE_DESCRIPTION("Simple Hello World From HelloWorld Module");
 8MODULE_VERSION("1.0.0");
 9
10static int __init helios_init(void)
11{
12    printk(KERN_ALERT "Hello world from HelloWorld module!! \n");
13    return 0;
14}
15
16static void __exit helios_exit(void)
17{
18    printk(KERN_ALERT "Destructing HelloWorld Module \n");
19}
20
21module_init(helios_init);
22module_exit(helios_exit);
  1. Create a Makefile Create a Makefile in the same directory with the following content:
 1obj-m += helios.o
 2
 3KDIR := /lib/modules/$(shell uname -r)/build
 4PWD := $(shell pwd)
 5
 6all:
 7	$(MAKE) -C $(KDIR) M=$(PWD) modules
 8
 9clean:
10	$(MAKE) -C $(KDIR) M=$(PWD) clean

Build and Manage the Module

  1. Build the Module Run the following command in the module directory:
1make
  1. Load the Module Insert the module into the kernel:
1sudo insmod helios.ko
  1. Check Logs View kernel messages related to the module:
1sudo dmesg | tail
  1. Remove the Module Unload the module from the kernel:
1sudo rmmod helios.ko