# How does loading modules work?

## Kobboi

Since I started with Linux and Gentoo, my first and only distro, I have always compiled custom kernels without initrd. Only recently have I started experimenting with more general kernels and modules for stuff I personally didn't even have. So now some questions pop up. Let's start with this one:

I'm using a genkernel kernel and initramfs. I understand that in order to access disks and perhaps network devices, modules are present in the initramfs. Then the system completes the boot procedure. If I then do an lsmod, I still see all kinds of modules that don't interest me, like SATA chipsets I don't have and PCMCIA modules for a desktop. Is it normal that they are still there after boot?

----------

## Tux12Fun

I think this is normal because you use a Kenel witch is build with genkernel.

If you compile your own kernel without genkernel you can decide what you need.

Genkernel builds everything to have the modules if kenel needs it. The system can't detect

witch modules are needed for your hardware an so everything is loaded.

I use a own build kernel via menuconfig. Thats the best way for me.

I hope, my information is correct.

----------

## poly_poly-man

modules are kernel code. Once they are loaded, they get attached to the end of the executable that is the running kernel. At that point they act just like kernel code (except they can be removed).

It is safe to have more modules loaded than you need - the only downside is memory consumption, which is small on modern machines (makes me worry on poly-p-ux, tho.)

When you have an initrd (initramfs), all the modules on that are loaded (and can be unloaded, if you don't need them). Basically, the genkernel-generated kernel itself is very small, and enables EVERYTHING as a module. This is not usually the best of behaviors, but guarantees a kernel compatible across almost every machine of the same architecture.

What you want to do is to enable only what you need, and make what you need to boot in-kernel, and otherwise make it a modules (some argue otherwise, but alsa argues for this method).

----------

## Kobboi

In fact, what I want to do really is to be compatible with a range of machines, so I have no problem with compiling things as modules, for every possible hardware device. What I expected was that modules would only be loaded if the hardware was present, not that I have 20 SATA chipset modules that I don't need in a running system. On the other hand, stuff that I do have, a certain hardware monitoring chip, is NOT loaded automatically. Why create an initramfs then, if the modules remain in memory anyway (if I don't unload them). I'd better compile a huge kernel then, so I don't have the initramfs overhead.

Am I wrong in thinking/wanting that things would be a little bit more selfconfiguring?

----------

## poly_poly-man

 *Kobboi wrote:*   

> In fact, what I want to do really is to be compatible with a range of machines, so I have no problem with compiling things as modules, for every possible hardware device. What I expected was that modules would only be loaded if the hardware was present, not that I have 20 SATA chipset modules that I don't need in a running system. On the other hand, stuff that I do have, a certain hardware monitoring chip, is NOT loaded automatically. Why create an initramfs then, if the modules remain in memory anyway (if I don't unload them). I'd better compile a huge kernel then, so I don't have the initramfs overhead.
> 
> Am I wrong in thinking/wanting that things would be a little bit more selfconfiguring?

 

Anything in the initrd will load automatically - usually only disk access drivers and such will be here.

Beyond that, look into udev and a few others for auto-loading modules.

----------

## Kobboi

But why  :Sad:  Maybe I'm wrong but when I create only a kernel with module support for every SATA PCI hardware, only the module that corresponds to hardware that I have gets loaded (by udev?). I read somewhere that mdev is a udev replacement for use in an initramfs. Why isn't it as intelligent as udev?

----------

## Kobboi

Anybody care to shed some light as to why every module in an initramfs is loaded? Can't a system detect what to load?

----------

## Kobboi

Are sys-apps/hotplug and sys-apps/coldplug recommended packages? They ebuild versions give the impression that they represent outdated technology.

----------

## poly_poly-man

 *Kobboi wrote:*   

> Anybody care to shed some light as to why every module in an initramfs is loaded? Can't a system detect what to load?

  theortically if you put a smarter loader into an initrd - keep in mind you have very little room, tho.

----------

## jcat

I think you'll need to discuss this with a Dev for a real answer.  It's obviously by design.  

Maybe loading everything, thus ensuring absolutely assuring maximum compatibility, is preferable than trusting everything is detected correctly initially.  

There could be many reasons I guess.  If you want to speak to a Dev about it, try IRC, or post a bug and see what response you get   :Wink: 

Cheers,

jcat

----------

## prizident

this behavior is normal for gentoo initrd as it uses mdev which is not as intelligent as udev is. I know that some other distros use udev in initrd

----------

## Kobboi

What does it do then, apart from loading all modules it encounters? Create corresponding device nodes?

----------

