Installing Linux Kernel Modules - linux-kernel

I usually only work on the FreeBSD kernel and it is a bit different. I get up to the stage of making and installing the modules, but how do you actually boot the kernel that you just built?

Take a look here. Essentially it's something along the lines of make menuconfig, make all, make install, make install_modules, make install_firmware, and then tweaks in grub or lilo config.

Depends on how your system is configured, but you want to copy the bzImage file somewhere your boot loader can see it, then update configs (if necessary). On my gentoo box:
#pwd is /usr/src/linux
cp arch/i386/boot/bzImage /boot/default-kernel

Related

Modifications not taken into account when recompiling linux kernel sources

I want to use the writeprotect mode of the userfaultfd feature, but it is implemented only from Linux 5.11.
I have ubuntu 20 with kernel 5.4 so I installed version 5.11 of Linux from sources, but when I reboot on the installed version, and try to use the writeprotect mode in a c user program the macros (e.g., UFFDIO_WRITEPROTECT) still appear not to be defined. And I verified that userfaultfd is not a module (doing lsmod).
In the other hand, if I make a modification for example in the scheduler just for a test (let's say in kernel/sched/core.c:context_switch) the modification are taken into account because this is in-core, but modifications for user space are not, I'm confused.
The userfaultfd.c file is in the fs/ directory of the kernel.
So please is it something that I'm missing in the compilation process (make menuconfig, make, make modules_install, make) ?
I was wrongly interpreting the error I had.
The modifications are indeed taken into account when I boot on the newly compiled kernel.
But while searching an understanding of what happens when booting on a compiled kernel, I got this:
When booting on a kernel compiled from sources, the libraries in /usr/include are not modified on the disk
If you want to overwrite them by the one modified, you should do this:
make deb-pkg //from the kernel sources directory
cd ..
sudo dpkg -i *.deb
This will install the new kernel and overwrite the headers

Unable to install a kernel even though it compiled successfully

I'm trying to install a linux-Kernel version 4.9.228 in my ubuntu 20.04.
The kernel compiled successfully, without any errors. I also executed the command:
sudo make modules_install install
When I rebooted my system, to check whether it made correct entry, I checked the files under the /boot/ directory:
System.map-4.9.228
vmlinuz-4.9.228
initrd.img-4.9.228
config-4.9.228
I don't know why but when I ran uname -r. It hasn't updated my version. It still says 5.4.0-37-generic.
Please guide me if I am missing something.
Note: The sole purpose of doing all this, is to learn how to add a system call and how to compile a kernel. I'm open to suggestions, if you got any. Thanks.
You might have successfully installed a different kernel version, but in order to boot into it, you need to tell the bootloader to do so.
I'm going to assume that you are using GRUB2 as bootloader, since it's standard on most distributions including Ubuntu. In order to boot the new kernel, you have multiple options.
Option 1
This is the safest and recommended option.
Edit the GRUB configuration file /etc/default/grub (you need to be root, so use something like sudo vi or sudo nano) and make sure that you have GRUB_TIMEOUT=N where N is a number of seconds. If you don't, then just add it in a new line. You want N to be at least 5 or 10 so that you have time to select the correct kernel version when GRUB starts.
Additionally, make sure that you do not have any of the following lines (you shouldn't have them, but if you do, remove or comment them):
GRUB_DISABLE_RECOVERY="true"
GRUB_DISABLE_SUBMENU=y
GRUB_HIDDEN_TIMEOUT=N
GRUB_TIMEOUT_STYLE=hidden
Save the changes to /etc/default/grub and run sudo update-grub. This will detect currently installed kernels and add them to the bootable list when you start the computer and enter GRUB.
Reboot your PC, when GRUB starts you will see a list of options. If you don't do anything, your default kernel will boot, otherwise you can use the arrow keys and the ENTER key to navigate to "Advanced options for Ubuntu", where you will find the list of available kernels and you will be able to select the one you prefer.
It should look something like this:
Option 2
Use only as fallback if option 1 does not work (it should, but you never know). This is not as clean as option 1 because it changes the default kernel, and changes are also probably going to be overridden by an upgrade of your current kernel (which technically does exactly this to update the default version).
Make the kernel you want the default one by changing the two symlinks /initrd.img and /vmilunuz to point to the right versions of the kernel and initrd image. This can be done either manually or through the linux-update-symlinks command.
$ linux-version list --paths
... grab the correct VERSION and PATH ...
$ sudo linux-update-symlinks install VERSION PATH
Run sudo update-grub to let GRUB detect the changes.
Reboot your PC.
Option 3
Manually boot into the kernel you want from the GRUB command line. This is an advanced option and I'm listing it only as a last resort, you should never need to do this unless you have completely broken your system configuration.
Reboot the PC into GRUB, and as soon as you see it, press C to enter the GRUB command line.
Follow this answer from now on.
In any case, remember that playing around with kernel development on your own machine is not a good idea if you are not sure what you are doing. I recommend you use a virtual machine to experiment with the kernel, that way if anything goes wrong you can just throw it away and create a new one.

Buildroot linux kernel clean build

I am using buildroot to build linux firmware. How can I do a clean rebuild the linux kernel only (without having to build the whole thing which take an hour)?
I tried -
make linux-rebuild
but that does not do a clean.
I have also tried
make linux-reconfigure && make linux-rebuild
but that does not work either.
If you want to remove all of the changes in sources of the linux kernel be in buildroot directory (cd buildroot) and do following:
make linux-dirclean
make linux-rebuild
The first command will remove output/build/linux* directory and the second one will fetch and rebuild from scratch the kernel.
I've found in the buildroot /dl folder a copy of the kernel repo is stored in there as a tar.gz file. If you do not delete that file, buildroot will not pull any kernel updates.
after reading a lot through buildroot manual and trail and error - this is what you need to do for cleaning of linux only -
make clean linux

Adding only one driver to linux kernel

I know that on the internet I can find same information about "adding one driver to linux kernel" but I can not get it working.
I need to compile an ADV7800driver. It is based on adv7180 driver code.
I put my driver file (adv7800.c) into /linux_source_folder/drivers/media/platform/mxc/capture folder. I also add
adv7800_converter-objs := adv7800.o
obj-$(CONFIG_MXC_ADV7800_CONVERTER)+=adv7800_converter.o
in makefile in the same folder and add information in Kconfigfile.
Then I go back to /linux_source_folder and do sudo make menuconfig and set adv7800 as a module and save config. Then sudo make defconfig, then sudo make -j4 modules (now waiting about 2 hours) and then sudo make modules install.
As a result I can see every module which I configure in menuconfig but I can not see my own module (only .c file exists).
How can I do this correctly and how can I build only this one module without building others (to same much time) ?
I do not remember exactly what happens in terms of instructions executed, but the idea of defconfig is to set a default configuration for a given architecture/platform.
If, as you said, you run sudo make defconfig after you configure your module to be compiled, most likely you loose your configuration. The defconfig should be executed first (once) and then you customize the configuration.
Regarding the compilation of a single module, I point you to an old answer
How to "make" existing Linux kernel module driver after modifying the driver source code.
One note: you should not use sudo to compile

Kernel recompiled with same config but: cpufreq: no nforce2 chipset error

I need to compile an old kernel 2.6.23 (downloaded from linuxkernels.com) in order to use it with a real time patch (it's a long story...), I installed ubuntu 10.04 which has a kernel 2.6.32-43-generic-pae.
I decided to simple copy ubuntu configuration:
cp cp /boot/config-2.6.32-43-generic-pae /usr/src/linux-2.6.23/.config
I recompile the kernel:
make menuconfig
make
make install
make modules_install
mkinitramfs -o /boot/initrd.img-2.6.23-MYVER 2.6.23-MYVER
note that in the config I make this edit: I remove the module versioning support under loadable modules section. (this step is required by the patch).
At start, I get the title error:
cpufreq: no nforce2 chipset error
how it possible, since I copied a working configuration? Maybe is because of that only flag I disable?
Not sure, but when I copy an existing .config, I run "make oldconfig" first to make sure I'm all sync'd up. Then I run make menuconfig if I want to interactively review/change any settings.

Resources