Compiling a kernel module for openvz VM? - c

I have an openvz machine which I am root on, it is a virtual machine I am ssh to:
>uname -a
Linux molo 2.6.32-042stab084.25 #1 SMP Wed Feb 12 16:04:42 MSK 2014 x86_64 x86_64 x86_64 GNU/Linux
I am trying to build an hello world kernel module:
hello.c:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);
Makefile:
obj-m = hello.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
And when I am trying to compile hello.c:
#make
make -C /lib/modules/2.6.32-042stab084.25/build M=/local/my_modules modules
make: *** /lib/modules/2.6.32-042stab084.25/build: No such file or directory. Stop.
make: *** [all] Error 2
That is the kernel version uname -r reports
#uname -r
2.6.32-042stab084.25
The following didn't help too:
$sudo apt-get install "linux-headers-$(uname -r)"
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package linux-headers-2.6.32-042stab084.25
E: Couldn't find any package by regex 'linux-headers-2.6.32-042stab084.25'
Here is the /lib/modules directory:
ls /lib/modules/2.6.32-042stab084.25/
modules.alias modules.ccwmap modules.dep.bin modules.ieee1394map modules.isapnpmap modules.pcimap modules.softdep modules.symbols.bin
modules.alias.bin modules.dep modules.devname modules.inputmap modules.ofmap modules.seriomap modules.symbols modules.usbmap

Download and install linux-headers package from openvz page (more on http://openvz.org/Installation_on_Debian)
1) Add source
cat << EOF > /etc/apt/sources.list.d/openvz-rhel6.list
deb http://download.openvz.org/debian wheezy main
# deb http://download.openvz.org/debian wheezy-test main
EOF
2) Install
wget http://ftp.openvz.org/debian/archive.key
sudo apt-key add archive.key
sudo apt-get update
sudo apt-get install "linux-headers-$(uname -r)"
Remember that you can only do this from the "Host", not from inside of a OpenVZ VPS. Meaning that loading kernel drivers to kernel is only permitted from the Host and would affect all containers on that Host.

Your kernel doesn't appear to be one of the ones provided by Canonical, usually official kernels end with a version number or with the generic or other suffix.
Usually you find the package with the kernel headers under pool/main/l as you can see here in the case of the security repository for Ubuntu Saucy.
You should ask the question to the person that provided the kernel that you are using, there is not that much you can do without more information unless you are willing to go for an officially supported kernel.

Related

Cannot use a Makefile to comple kernel files in Ubuntu Linux on WSL

I have some C code and a Makefile for that C code. The C program is called simple.c and the the makefile is just called Makefile.
Here are the contents of simple.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Kernel Module\n");
return 0;
}
/* This function is called when the module is removed. */
void simple_exit(void)
{
printk(KERN_INFO "Removing Kernel Module\n");
}
/* Macros for registering module entry and exit points. */
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");
And this is the content of the Makefile:
obj-m += simple.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I have both of them in the same directory and I use the instruction make to compile it however it does not work and I get this error message:
make -C /lib/modules/5.15.79.1-microsoft-standard-WSL2/build M=/home/eddie modules make[1]: *** /lib/modules/5.15.79.1-microsoft-standard-WSL2/build: No such file or directory. Stop. make: *** [Makefile:3: all] Error 2
I have tried to install some things I have seen online for linux kernal but they do not seem to work.
I have tried the following:
sudo apt install linux-tools-virtual hwdata
sudo apt-get install linux-headers-$(uname -r)
It's important to note that these all do sucessfully install. When I use lsmod to list all current kernal modules nothing is listed.
Am I doing anything wrong? Missing a command to install something? I am using Ubuntu Linux subsystem for linux on windows 11

Linux Kernel Module Development "module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1"

I am currently trying to develop a simple linux kernel module. It should just log something, its 1:1 copied from the internet.
I have the following files:
lkm_example.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Robert W. Oliver II");
MODULE_DESCRIPTION("A simple example Linux module.");
MODULE_VERSION("0.01");
static int __init lkm_example_init(void) {
printk(KERN_INFO "Hello, World!\n");
return 0;
}
static void __exit lkm_example_exit(void) {
printk(KERN_INFO "Goodbye, World!\n");
}
module_init(lkm_example_init);
module_exit(lkm_example_exit);
Makefile:
obj-m += lkm_example.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
I also did the following:
sudo apt-get install build-essential linux-headers-`uname -r`
For compilation i used:
stbau#kernel-dev-vm:~/src/lkm_example$ sudo make
make -C /lib/modules/5.13.0-39-generic/build M=/home/stbau/src/lkm_example modules
make[1]: Entering directory '/usr/src/linux-headers-5.13.0-39-generic'
CC [M] /home/stbau/src/lkm_example/lkm_example.o
MODPOST /home/stbau/src/lkm_example/Module.symvers
CC [M] /home/stbau/src/lkm_example/lkm_example.mod.o
LD [M] /home/stbau/src/lkm_example/lkm_example.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.13.0-39-generic'
Executing with insmod:
stbau#kernel-dev-vm:~/src/lkm_example$ sudo insmod lkm_example.ko
insmod: ERROR: could not insert module lkm_example.ko: Invalid module format
The dmesg log gives the following error:
[ 49.272618] lkm_example: module verification failed: signature and/or required key missing - tainting kernel
[ 49.272630] module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 1, loc 0000000054f3f1c5, val ffffffffc0a0a000
I am using the following kernel:
stbau#kernel-dev-vm:~/src/lkm_example$ uname -a
Linux kernel-dev-vm 5.13.0-39-generic #44-Ubuntu SMP Thu Mar 24 15:35:05 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
As you can see in the dmesg log, i only get an error and not the messages i expected. I have no idea what i did wrong/what is missing.
I think the problem is that the module is not signed. I tried signing it using the sign-file but i was not able to generate a private/public key file.
Re-Installing the kernel headers worked for me.
I used the following commands:
sudo apt update && sudo apt upgrade
sudo apt remove --purge linux-headers-*
sudo apt autoremove && sudo apt autoclean
sudo apt install linux-headers-generic

insmod error in kernel module programming

I am just starting with modular programming.
Above are my two files:
hello.c
#include <linux/init.h>
#include <linux/module.h>
static int hello_init(void)
{
printk(KERN_ALERT "TEST: Hello world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "TEST: Good Bye");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
obj-m += hello.o
KDIR = /usr/src/linux-headers-3.13.0-46-generic
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order
And here's my terminal output showing error in insmod command, kindly help.
anubhav#anubhav-Inspiron-3421:~/Desktop/os$ make
make -C /usr/src/linux-headers-3.13.0-46-generic SUBDIRS=/home/anubhav/Desktop/os modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-46-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-46-generic'
anubhav#anubhav-Inspiron-3421:~/Desktop/os$ insmod hello.ko
insmod: ERROR: could not insert module hello.ko: Operation not permitted
If you have secure boot enabled, the newer kernels won't allow inserting arbitrary kernel modules. So, either you can disable secure boot in your BIOS or you need to sign the kernel modules you want to install.
Steps to securely sign your kernel modules:
Create a X509 certificate that can be imported in firmware
Enrolling the public key just created
Sign the module you want to install
Install the module
You need to be root to do steps 2 & 4. The detailed process is described in a nice Ubuntu blog.
As isowen mentioned only root can load or unload the module.
You see the print in hello_init() when you do insmod hello and you see the print in hello_exit() when you do rmmod hello.
execute cat /proc/sys/kernel/modules_disabled and if you see the result
1 then execute echo 'kernel.modules_disabled=1' >> /etc/sysctl.d/99-custom.conf then reboot and try again. ;) BR nu11secur1ty

Loadable kernel module not compiles correctly on different computers

I'm trying to make loadable kernel module for ARM achitecture. Just for example I'm made simple hello.c
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);
And created Makefile
obj-m := hello.o
PWD := $(shell pwd)
ARCH=arm
CROSS_COMPILE_LINARO=/home/cooperok/mk908/gcc-linaro-arm-linux-gnueabihf-4.7-2013.01-20130125_linux/bin/arm-linux-gnueabihf-
KERNEL_ROCKCHIP=/home/cooperok/mk908/kernel/
default:
make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE_LINARO) -C $(KERNEL_ROCKCHIP) M=$(PWD) modules
clean:
make CROSS_COMPILE=$(CROSS_COMPILE_LINARO) -C $(KERNEL_ROCKCHIP) M=$(PWD) clean
Cross compiler and kernel sources are the same on both computers. When I'm running make on first machine compiled hello.ko file is successfully installed with insmod command, but when I'm compiling module on second machine and trying to install that module I'm getting error "insmod: error inserting 'hello.ko': -1 Invalid module format"
As I understand module is compiling with crosscompiler which I'm specified in CROSS_COMPILE (arm-linux-gnueabihf-gcc and others) and only it will make module without any specific libraries installed on computers, is it?
First computer have 64bit OS (Ubuntu 12.04), and second 32bit OS (Linux Mint 12), this is main difference, so make util is different. But is it realy main reason why module correctly compiling only on 64bit OS? If so is it problem in make, or it's really deals with different libraries?

Linux device driver ed 3 code compilation errors and Linux header files

I'm trying to get the examples from Linux Device Drivers, ed 3 (ldd3) working before I start working with the book so that I can have a set of working examples that I can use.... I'm getting the following errors (seeing error in Debian squeeze and also Crunchbang Linux):
inp.c:33:20: error: [u]asm/io.h:[/u] No such file or directory
when I looked at the makefile I found this (which I think is the problem):
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
INCLUDEDIR = $(KERNELDIR)/include
contents of /lib/modules/uname -r/build which is a link to /usr/src/linux-headers-2.6.39-bpo.2-486/
$ uname -r
2.6.39-bpo.2-486
$ ls /lib/modules/`uname -r`/build
arch include Makefile Module.symvers scripts
$ ls /lib/modules/`uname -r`/build/include
config generated linux
The directory that make is looking does not have the required files. I found the files required under /usr/src/linux-headers-2.6.39-bpo.2-common/ and the missing asm/io.h file # /usr/src/linux-headers-2.6.39-bpo.2-common/include/asm-generic/
$ ls /usr/src/linux-headers-2.6.39-bpo.2-common/
arch include Kbuild Makefile scripts
$ ls /usr/src/linux-headers-2.6.39-bpo.2-common/include/
acpi crypto Kbuild linux media net rdma scsi staging trace xen
asm-generic drm keys math-emu mtd pcmcia rxrpc sound target video
Do I have to install any package to get the files in that directory... I've already installed linux-headers-uname -r package (in both Debian and Crunchbang)... In gnewsense I found the files in /lib/modules/$(shell uname -r)/build... but it was a older kernel... so not sure if the directory structure under linux change... or is it distro specific... please let me know how do I get the compilation going.... I'm not very good with Makefiles so how can I change the makefile so that it will look for the header files in the other directories....
Thanks,
asp5
First things first, LDD3 is quite old and I wouldn't be surprised if header files might have moved around. Javier Martinez Canillas has updated the LDD3 sources for more modern kernels.
When investigating this a little further, I found that the asm symlink has been broken for a while. (io.h appears to have stored in asm-generic/ for some reason.) In case you're curious, I filed a bug report at Ubuntu for the broken symlinks.
For whatever it's worth, I can build a module referencing either <asm/io.h> or <asm-generic/io.h> (though as Hasturkun reminds me, you shouldn't use asm-generic directly):
$ cat > Makefile
obj-m = foo.o
$ cat > foo.c
#include <linux/module.h>
#include <linux/sched.h>
#include <asm/io.h>
int init_module() { return 0; }
void cleanup_module() { }
$ make -C /lib/modules/`uname -r`/build M=$PWD
make: Entering directory `/usr/src/linux-headers-2.6.38-12-generic'
LD /home/sarnold/tmp/module/built-in.o
CC [M] /home/sarnold/tmp/module/foo.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/sarnold/tmp/module/foo.mod.o
LD [M] /home/sarnold/tmp/module/foo.ko
make: Leaving directory `/usr/src/linux-headers-2.6.38-12-generic'
$ cat > foo.c
#include <linux/module.h>
#include <linux/sched.h>
#include <asm-generic/io.h>
int init_module() { return 0; }
void cleanup_module() { }
$ make -C /lib/modules/`uname -r`/build M=$PWD
make: Entering directory `/usr/src/linux-headers-2.6.38-12-generic'
CC [M] /home/sarnold/tmp/module/foo.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/sarnold/tmp/module/foo.mod.o
LD [M] /home/sarnold/tmp/module/foo.ko
make: Leaving directory `/usr/src/linux-headers-2.6.38-12-generic'
$

Resources