For the working of your hardware,
kernel modules are essential.
If you want to verify currently loaded kernel modules,
you can use the lsmod command.
Honestly, I don't like lsmod too much
because it's just giving you a list of kernel modules
and it doesn't give you very much meaning.
More meaningful is the lspci-k command
because that is listing kernel modules that
are loaded for PCI devices that are found on the system.
Most hardware, anyway, is on your PCI bus
and you want to know if your hardware is doing well.
Well, lspci-k is showing listed kernel modules.
When Linux is installed, the initramfs
is created to contain all kernel modules required
for hardware that was found.
And the systemd-udevd process works
as a plug-and-play manager.
It dynamically loads kernel modules
when triggered by hardware events.
To influence how systemd-udevd should load modules
and related parameters, rules are provided
in userlib-udevrules.d and custom rules
can be provided in etc-udevrules.d.
And the process of dynamically loading kernel modules
can be traced with the udev-adm-monitor command.
Alternatively, kernel modules can be loaded manually as well.
To load kernel modules manually,
use a modprobe command.
And while loading kernel modules,
kernel parameters, as shown by modinfo, can be used.
And to load a kernel module with its parameters automatically,
you can use the configuration file in etc-modprobe.d.
To verify if kernel module parameters were loaded
successfully, you can use dmessage.
dmessage is where kernel messages are stored.
And to unload kernel modules, use modprobe minus r.
Let me show you a bit of this.
So let me start with lspci minus k,
which is listing all the PCI addresses.
And for each PCI address, it's telling you
which device is there.
And it's telling you which kernel module is there,
like E1000E for the network card, for instance.
Now, as I mentioned, current Linux distributors
are pretty good at loading kernel modules dynamically
when needed.
And that is this systemd-udevd process, taking care of it.
Now, systemd-udevd can be monitored.
If you use udev-adm-monitor, you can
see exactly what is happening while you
plug in a hardware device.
Let me plug in a USB thumb drive.
So here you can see everything that
is happening in the background while a USB thumb
drive is inserted.
The file names that you see here are relative file names
to the sys directory.
The sys directory is a pseudo file system
that is used for hardware settings.
And you can see that somewhere in this procedure,
some modules are used.
So here we have the module xfat that is used.
It's used on demand because in this procedure,
the Linux kernel has detected on the USB device.
Here we have some modules as well,
by the way, that allows it to communicate with the USB device
and it automatically detects what has been loaded.
If you want to get out of here, use Control-C.
Now, in order to get an overview of all currently loaded
modules, you use lsmod.
lsmod is showing all the different modules
that are available.
Like, for instance, CD-ROM.
CD-ROM, that's an easy module to play with a little bit.
I'm going to show you modinfo on CD-ROM.
And there we can see that we have parameters.
And these parameters can be used to determine
what should happen while the module is loading.
So we have a parameter autoeject, for instance,
which is a Boolean.
So how can I set this parameter?
Well, I can use modprobe to manually load the kernel module.
And while manually loading the kernel module,
I can use modprobe CD-ROM autoclose is 1.
And that will set the parameter for me.
But if your CD-ROM module was already loaded,
then this is going to do nothing.
So the message is not giving me anything.
The message is what you normally do if you want to run it.
Now, if I use modprobe minus R on CD-ROM to unload it,
then I'm getting a problem because the module is
currently in use.
And that makes sense because my CD-ROM is activated.
So if I want to be able to unload it,
then I need to deactivate the CD-ROM.
On a physical system, that means that you
need to remove the CD-ROM from the bay.
On a virtual machine, that means that you
need to disconnect it.
I think we can do that.
Disconnecting.
And oh, guest operating system has locked the door.
Well, this is probably not a good idea.
I can try it.
And then modprobe minus R CD-ROM again.
But this is telling me that something
is happening with the CD-ROM.
I'm not really sure what is happening.
Might be mounted.
LSB-LK should tell me if it is mounted.
Oh my goodness, that's a lot of output.
And in LSB-LK, I see SR0.
SR0 is the CD-ROM not currently being mounted.
Bottom line, I can't manually unload it right here.
That's a reality of kernel modules.
And that is why it is convenient to put options
for loading your kernel modules in etcmodprobe.d.
So what was my option?
Autoclose is 1.
So I am going in etcmodprobe.d.
And in there, we can find configuration files.
And I am going to create CD-ROM.conf.
And there, we have the name of the module, CD-ROM,
where I'm using options.
Autoclose is 1.
And that makes that the next time I'm rebooting
and this module is initialized again,
it will automatically read and apply this option.
And that's about all that you need
to know about kernel module management.