I have the source code of a hello world kernel module that works in Ubuntu 20 in a laptop.Now I am trying to compile the same code in Ubuntu 20 but inside WSL2. For that I am using this:
make -C /sys/modules/$(shell uname -r)/build M=$(PWD) modules
The problem is that /lib/modules
is empty. It seems that WSL2 does not bring anything in /lib/modules/4.19.104-microsoft-standard/build
I tried getting the headers using:
sudo apt search linux-headers-`uname -r`Sorting... DoneFull Text Search... Done
But nothing get's populated in the modules folder
Is there anything I need to do in order that folder contains all required modules?
[EDIT]
Getting closer thanks to @HannahJ.
I am doing:
> sudo make -C /home/<user>/WSL2-Linux-Kernel M=$(pwd) modulesSL2-Linux-Kernel M=$(pwd) modulesmake: Entering directory '/home/<user>/WSL2-Linux-Kernel' CC [M] /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.o Building modules, stage 2. MODPOST 1 modules CC /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.mod.o LD [M] /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.komake: Leaving directory '/home/<user>/WSL2-Linux-Kernel'
At the end, I get the lkm_example.ko
file created.
After that:
> sudo insmod lkm_example.koinsmod: ERROR: could not insert module lkm_example.ko: Invalid module format> dmesg[200617.480635] lkm_example: no symbol version for module_layout[200617.480656] lkm_example: loading out-of-tree module taints kernel.[200617.481542] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000074f1d70f, val ffffffffc0000158> sudo modinfo lkm_example.kofilename: /home/<user>/containers-assembly-permissionsdemo/demo-2/lkm_example.koversion: 0.01description: A simple example Linux module.author: Carlos Garcialicense: GPLsrcversion: F8B272146BAA2381B6332DEdepends:retpoline: Yname: lkm_examplevermagic: 4.19.84-microsoft-standard+ SMP mod_unload modversions
This is my Makefile
obj-m += lkm_example.oall: make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) modulesclean: make -C /home/<usr>/WSL2-Linux-Kernel M=$(PWD) cleantest: # We put a — in front of the rmmod command to tell make to ignore # an error in case the module isn’t loaded. -sudo rmmod lkm_example # Clear the kernel log without echo sudo dmesg -C # Insert the module sudo insmod lkm_example.ko # Display the kernel log dmesgunload: sudo rm /dev/lkm_example sudo rmmod lkm_example
[Edit2]This is my kernel module:
#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <asm/uaccess.h> #include <linux/init_task.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Carlos Garcia"); MODULE_DESCRIPTION("A simple example Linux module."); MODULE_VERSION("0.01"); /* Prototypes for device functions */ static int device_open(struct inode *, struct file *); static int device_release(struct inode *, struct file *); static ssize_t device_read(struct file *, char *, size_t, loff_t *); static ssize_t device_write(struct file *, const char *, size_t, loff_t *); static int major_num; static int device_open_count = 0; static char msg_buffer[MSG_BUFFER_LEN]; static char *msg_ptr; /* This structure points to all of the device functions */ static struct file_operations file_ops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; /* When a process reads from our device, this gets called. */ static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset) { ... } /* Called when a process tries to write to our device */ static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t *offset) { ... } /* Called when a process opens our device */ static int device_open(struct inode *inode, struct file *file) { ... try_module_get(THIS_MODULE); } /* Called when a process closes our device */ static int device_release(struct inode *inode, struct file *file) { ... module_put(THIS_MODULE); } static int __init lkm_example_init(void) { ... major_num = register_chrdev(0, "lkm_example", &file_ops); if (major_num < 0) { printk(KERN_ALERT "Could not register device: % d\n", major_num); return major_num; } else { printk(KERN_INFO "lkm_example module loaded with device major number % d\n", major_num); return 0; } } static void __exit lkm_example_exit(void) { /* Remember — we have to clean up after ourselves. Unregister the character device. */ unregister_chrdev(major_num, DEVICE_NAME); printk(KERN_INFO "Goodbye, World !\n"); } /* Register module functions */ module_init(lkm_example_init); module_exit(lkm_example_exit);