Pune Kernel Meetup Assignment.
An assignment was given at Pune Kernel Meetup-January-2018, for people to get started with linux kernel.
The assignment was to print “Hello World” as the first line in the kernel boot up.
So here is the solution to this problem that I could find.
The Solution.
Here are the steps to print “Hello World” as the first line in the kernel bootup.
Get the kernel source tree.
Find the entry point of the kernel.
Put printk(“Hello World”); at the very beginning of the entry point.
Compile the kernel.
Install the kernel.
Run the kernel.
Check if the “Hello World” message is there in the beginning of kernel log.
How to do that?
Get the kernel source tree.
To get the kernel source tree you need git.
You can install git using these commands.
On Debian based systems.
$ sudo apt-get install git
On rpm based systems.
# for rhel/centos
$ sudo yum install git
# for fedora 21 and above
$ sudo dnf install git
Once you have git installed, you can now clone the kernel.
To clone the kernel, run these commands.
$ mkdir kernel
$ cd kernel
$ git clone -b staging-testing git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git
After this you will have Greg Kroah-Hartman’s staging tree repository cloned into kernel directory.
Find the entry point of the kernel.
Once you have cloned the kernel. It’s time to find the entry point (main function) of the kernel.
If you go to the kernel source tree and check out what directories are there, it is quite easy to guess which directory may contain the main function of the kernel.
[groot@localhost ~]$ cd kernel/staging/
[groot@localhost staging]$ ls
arch COPYING Documentation fs ipc kernel MAINTAINERS Module.symvers samples sound virt
block CREDITS drivers include Kbuild lib Makefile net scripts tools
certs crypto firmware init Kconfig LICENSES mm README security usr
[groot@localhost staging]$ ls init/
built-in.a do_mounts.c do_mounts_initrd.c do_mounts_rd.c init_task.c main.c modules.builtin noinitramfs.c
calibrate.c do_mounts.h do_mounts_md.c initramfs.c Kconfig Makefile modules.order version.
You can see there is a main.c in the kernel/staging/init directory.
There is no main function in this main.c but, there is a function called start_kernel which is the entry point of the kernel.
You just need to put your printk statement at the start of this function to display your message as soon as the kernel boots up.
Compile Kernel (on Fedora).
Dependencies
To compile the kernel you will have to install some packages.
On fedora you can do it using the following command.
$ sudo dnf group install "C Development Tools and Libraries"
$ sudo dnf install elfutils-libelf-devel
Get the config
You can use your existing kernel config for the kernel you are now going to compile. You can do that by running,
$ cd ~/kernel/staging/
$ cp /boot/config-`uname -r`* .config
If you want to make any changes in the config you can use
$ make menuconfig
If you want to save the current config before you make any changes, you can use.
$ make olddefconfig
Build the kernel
To build the kernel
$ make
Once this is done, you will have vmlinux file in the kernel/staging directory. This is your kernel that you’ve just built.
Install kernel
To install this kernel
$ sudo make modules_install install
Run the kernel.
Now you have the kernel installed, but the os won’t run this kernel right away when you reboot your system. For that you will have to put a menu entry in the grub (bootloader).
On Fedora you can do this using
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
This command adds a menu entrie in the /boot/grub2/grub.cfg for each of the kernels available in the /boot directory.
Once all this is done, you are all set to start using your new kernel. Just reboot the system and select your kernel entry in the grub.
Check if the “Hello World” message.
After booting your new kernel, go to terminal and run
$ dmesg | grep <your message>
This will show the line in the kernel log where your message is printed on the kernel.