Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a failed kernel module

Tags:

linux-kernel

This situation always bother me:

I wrote a kernel module, and sometimes it has a bug(dereference a NULL pointer). After I insmod hello.ko, it shows some kernel errors. Then I change the code, and try to remove the module and install it again. The question is: I don't know how to remove the kernel module.

$ rmmod hello

ERROR: module hello in use

$ rmmod -f hello

ERROR: removing hello: device or resource busy

I always reboot the machine in order to remove the module, which takes too long. Does anyone have a better solution for this? Thanks for any inputs.

like image 444
Fengwei Zhang Avatar asked Oct 05 '11 15:10

Fengwei Zhang


People also ask

How do I force a kernel module to uninstall?

NAME rmmod - Simple program to remove a module from the Linux Kernel SYNOPSIS rmmod [-f] [-s] [-v] [modulename] DESCRIPTION rmmod is a trivial program to remove a module (when module unloading support is provided) from the kernel. Most users will want to use modprobe(8) with the -r option instead.

How do I manually remove a kernel?

First, boot into a new kernel. List all other older kernel using the dpkg command. Note down system disk space usage by running the df -H command. Delete all unused old kernels, run: sudo apt --purge autoremove.

What command can be used to add and remove modules in a kernel?

The Linux kernel has a modular design. Functionality is extendible with modules or drivers. Use the modprobe command to add or remove modules on Linux. The command works intelligently and adds any dependent modules automatically.

Where are the kernel module stored?

They are located in /lib/modules or /usr/lib/modules and have had the extension . ko ("kernel object") since version 2.6 (previous versions used the .o extension). The lsmod command lists the loaded kernel modules.


1 Answers

Use a virtual machine.

Once you make a NULL dereference or other such mistake, you've put the kernel into an unknown state. Even if you did manage to remove the module (which is unlikely to be possible; a kernel OOPS kills the calling thread, so it'll never have a chance to reduce the reference count - the module will never be removable) there may still be corruption left behind, and your new, 'fixed' module is just as likely to be in trouble.

Much better to just use a fast-to-reboot virtual machine - perhaps with a snapshot, to make restoration even faster.

like image 113
bdonlan Avatar answered Oct 11 '22 08:10

bdonlan