Hey guys and welcome back. So in the previous nugget we happened to mention that the kernel
was modular, meaning that it relied on modules. So let's now dig in and discuss a little bit
more about this subject. Now one thing to say that off the bat is that generally speaking you're
not going to have to worry about loading particular modules with the kernel, this is going to be an
automatic process. However there may be times where you want to manually intervene and take
particular control of a particular module i.e to load that module or to remove that module and as such
it is important that we do have a strong understanding of what that process would actually look like. So
let's dive in and discuss some things we have to know then shall we. So in the previous nugget I
happened to mention that if we go into the lib directory and then go into the modules directory
within here we're going to have a folder for all of our kernels. Simply put each kernel has its own
sub directory so this kernel here has its own sub directory whereas this one here has another one
with its modules so on so forth and we learned the command we can use to identify the kernel
running on our system is the uname command with the dash r flag to try to remember that command.
So let's go into the folder or the sub directory of the kernel we are currently using and if we do
an ls in fact let me long list this you're actually going to see we have a directory called kernel
right here if i go into this directory and i list again we can see a whole bunch of sub directories
relating to modules for particular features such as drivers network modules so on so forth. So if
we wanted to see modules related to the file system we would go into the fs directory for file system
and we do an ls we have some modules right here which end in .ko and as we can see here we actually
have further directories relating to our file system so i could go into say for example ntfs
to an ls and we can see the ntfs file system module for the kernel right here again with that .ko
extension. Now the reason why you want to know the location of these particular modules is that if
you want to load in a particular module certain tools will require you to specify the module's
particular location so you have to be able to identify it. Now one thing to say is that when
we want to copy a particular module there is a very specific command that we have to be aware of
and that is called the depth mod command let me just clear my screen that will be depth mod so
depth mod now what this command actually does it ultimately looks into the lib modules directory
which we just saw and then the directory for that particular kernel number so that would be if i
actually move back a few let me see the directory here looks in lib modules and then the subdirectory
for the particular kernel and then it's going to ultimately grab all of the modules within the kernel
directory within this folder so everything within here and it is going to generate what is called a
modules .depth file now this file happens to be really quite important we do want to be remembering
this one for sure and what it does it basically gives us a list of all the kernel modules and
those module dependencies a dependency meaning that a module may actually rely on another module
to operate so pretty much everything we actually need i.e all the modules and the modules which
rely upon those modules will actually be specified within this modules .depth file that has been
generated now one thing i really really want to highlight with respect to the depth mod command
is that this command is only going to execute with respect to the current kernel in play so you
know how i do have other kernels on my system again if i go back we do an ls we can see these
three different kernel options the only one i happen to be running right now is 5.13.0.28 so
it's only going to act within this directory right here and probe for the modules within
this directory and its subsequent subdirectories now when a module happens to be loaded into memory
we actually have a bunch of commands that we can do to ultimately check and manage these modules the
first one we can do is the ls mod command this is going to allow us to list i.e ls the modules to
check this out if i happen to run this command and i say ls mod we're going to get a whole bunch
of output now if i scroll up to the very top here we can actually see the module name on the left
hand side right here and the size of that particular module as well as any dependencies i.e what is this
actually used by so we can see here btrfs actually uses the raid6pq module whereas the hfs module
no additional modules are actually reliant upon this module and i suppose i should actually mention
is that the size here we're actually looking at this is in bytes just for clarity there now if you
want to be able to remove or unload a particular module you're going to have to make sure that this
particular module has the value zero in the third column that means that it's not being used by something
else therefore it can be successfully removed whereas if we try to remove this module right here
we can see it's been used by vbox video therefore we're not going to be able to do such a thing now
if i want to be able to manually remove a particular module i can use the rm mod commander again
these commands are quite appropriately named rm typically in linux is for remove such as rm a
particular file and we want to remove a particular module so rm mod the name itself is quite descriptive
once you actually understand the linux jargon so if i examine some of the output right here take
for example this one right here pata acpi what i can do is actually remove this then okay so first
thing i will do is i will say ls mod that gives us a listing and what i'm going to do is i will
actually grip for pata so we can clearly see it's loaded but if i happen to say rm mod and then say
pata acpi and notice one thing i should have actually pointed out when we are making these
kernel changes this is going to require super user privileges so what i'll actually do is i will just
become the root user type in the old passwords there we go so i'm now the root user let's try
this command again say ls mod grip pata it's still loaded if i now say rm mod and then give the name
of the module pata acpi if i hit enter this module will now be removed if i actually check again
notice the output of the ls mod command we actually don't have any output for pata acpi it has been
successfully removed now if i want to actually insert a particular module we have a command called
insmod so insmod insert module again really quite easy to remember once you understand the jargon
the difference here is that if i just happen to say insmod you know like pata acpi notice it's
saying there is no such file or directory we have to be able to identify where about this particular
module is so what i can do though is i can clear the screen and i can try to clear the screen by not
mashing the keyboard and i'll use our trusty fine command we learned about this command in the lpik
one examination so what i will do here is i will search for absolutely everything which we can see
here but i'll stop this right now and instead i will actually grip for what i'm looking for so
i'll grip for pata acpi notice we actually see the output here we need to pick the one which
corresponds to our particular kernel so i'll say uname dash r so it will be in this one right here
5 13028 folder and i can use this with the insmod command so i can say insmod and i will just copy
this location right here paste it in again with the dot ko extension this is what we have for
our modules for the kernel if i now hit enter this should now reload this module back into the kernel
and again if i happen to run my ls mod command and to enter notice this module has been successfully
loaded once again and in fact if i just run the ls mod command on itself this module should
actually be at the very top of the output you see that right here because it was the last one loaded
in so if you want to load in a particular module manually you can use the insmod command this is
kind of deemed the more difficult way because again we have to manually identify the particular
location of the module itself but we do have a more modern approach and in my opinion a much
better approach and we can do this using the mod probe command this is deemed the much easier
and simpler way to handle such a task so previously we were removing and re-adding the pata acpi
module we can do the same thing using mod probe so what i will do is i'll say mod probe and i will
say dash r this is for removing and now i just give the name of this particular module and that
should it be now removed if i do an ls mod it will no longer be there again if i grip for pata
it is no longer there now this time when i want to reload this module using the mod probe command
i don't have to specify an absolute path i can just say mod probe pata acpi and hit enter
and if i check my ls mod notice it has been loaded in once again now why is it the mod probe command
is able to almost magically and automatically handle this process is that mod probe is getting
this information from whereabouts well it was the information we talked about when we run the
dep mod command what does that command actually generate it generates a modules dot d e b file
in which case this ultimately tells us all of our modules and our module dependencies so mod probe
is looking into this file and just by simply giving the module name it can solve all the other issues
itself directly so way way way easier than having to manually provide full pass yourself but like i
say you are way more likely to never actually have to need to use these tools because the kernel will
handle the loading and removing of these modules on the fly automatically but in the event that maybe
a particular module is causing an error maybe you have to manually intervene and remove and maybe
reload that module or perhaps you just want to free up additional memory by proactively going in
and removing modules which have been automatically loaded which you have deemed unnecessary for the
operation of your system in which case being able to understand these tools are really going to be
advantageous to you as you manage your linux system okay doc so that is us for our introduction into
the modules of the kernel and i hope this has been informative for you and i'd like to thank you for viewing