2

My teacher gives me a linux kernel vmlinuz-3.17.2 and a rootfs.ext2 which can be loaded to qemu. And he asks me to build a simplest kernel module which prints a hello world as homework.

  • Firstly, I download the kernel source and run make oldconfig
  • Secondly, I make the config to be PREEMPT and without modversions (according to uname -a of vmlinuz running in qemu) , then make prepare
  • Thirdly, I compile the kernel mod and copy hello.ko in rootfs.ext2
  • Finally, In qemu, I run insmod hello.ko which exit without any prompt and echo $? returns 0.

However, I can't see anything in dmesg or /var/log/messages Is there anything wrong? How can I do with this? There is also nothing to be printed when I run rmmod hello.ko successfully.

My log level is 7 4 1 7

I have make my hello.c as follows:

#include <linux/init.h>
#include <linux/module.h>

static int __init hello_init(void)
{
    pr_info("Hello World");
    return -1; 
// I changed this to -1 deliberately, Because It seems that the code is not executed.
}

static void __exit hello_exit(void)
{
    printk(KERN_ERR "Goodbye, cruel world\n");
}
MODULE_LICENSE("GPL");

module_init(hello_init);
module_exit(hello_exit);
0

2 Answers 2

2

Buildroot

Buildroot is the easiest way to do it:

Tested on Ubuntu 16.04.

Sign up to request clarification or add additional context in comments.

Comments

0

The proper way to do this is to use modprobe instead of insmod (though for your example, insmod would be sufficient considering that the module does not have any dependencies)

The problem is that modprobe expects modules to be in /lib/modules, however when you compile your modules (using make modules) and install them (using make modules_install) the modules get installed to your host system's /lib/modules directory rather than the hard disk image which QEMU uses.

The solution to this problem is to supply the variable INSTALL_MOD_PATH to make while you're installing the modules.

Hence, the steps are:

  1. Find out where your hard disk image is, its given as an argument to qemu, usually via -drive file=<img> or -hda=<img>
  2. Mount it: sudo mount <img> /mnt
  3. In your kernel source directory run the following commands:
make modules -j `nproc` 
sudo make modules_install -j `nproc` INSTALL_MOD_PATH=/mnt

After doing the above, you should be able to load all your compiled modules the next time you start your QEMU instance.

Note: If you wish to compile just a single module, you can use the command make modules M=<path to module directory> -j `nproc`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.