sudo apt update && sudo apt upgrade -y
I used these commands to fetch the latest packages, and then upgrade to them.
sudo apt install build-essential libncurses-dev libssl-dev libelf-dev bison flex -y
This shell command was used to get the build-essential including the gcc and the Makefile, necessary for the completion of this exercise. The others are also important environment tools that are needed for the completion of the assignment.
cd;wget -P ~/ <https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.11.9.tar.xz>
Install the kernel version we will be working on in the home directory. wget is used to fetch files from the web, while the -P ~/ is used to instruct wget to store the fetched files in the home directory.
cd; tar -xvf ~/linux-5.11.9.tar.xz
xvf is used to specify to do the following: -Extract files from the drive -Verbose to list every file being extracted -File, telling the tar that the next thing typed is the file argument.
uname -r
In this case, we run this command to check what the current kernel we are working in, it is expected to be different to the kernel we just installed as we are still running our original kernel. Next, I proceeded to make a directory named hello, and inside that, I made a file called hello.c, this file will test a dummy call in my new kernel.
#include <linux/kernel.h>
#include <linux/syscalls.h>
SYSCALL_DEFINE0(hello)
{
printk("Hello, from the kernel );
return 0;
}
SYSCALL_DEFINE0(hello) is a kernel macro to define a new system call. The 0 indicates that it takes 0 arguments. printk() is the kernel's version of printf. It can print messages to the kernel's log buffer. dmesg command allows us to view these messages.
Now, we need to make a Makefile to compile these:
emacs Makefile
With the following code in it:
obj-y := hello.o
This code tells us to compile the hello.c into hello.o and link it to the kernel.
Keeping this in mind, we need to tell the main kernel Makefile to look inside the hello directory. We need to make sure that the original kernel is preserved for normal functionality, hence we run this command:
cp Makefile Makefile.orig
We did this to preserve our original Makefile as we are editing the compilation of our kernel as it is. I then proceeded to add: