17

On Ubuntu 14.04, I found that when I don't plug in my external wireless adapter, its module rt2800usb is still shown in lsmod.

  1. when does automatically loading a driver module happen? Is it when the device is connected to the computer, or when the OS boots?

  2. when does automatically unloading a driver module happen? Is it when the device is disconnected to the computer, or when the OS shuts down?

2 Answers 2

15

When the kernel detects a new device, it runs the program modprobe and passes it a name that identifies the device. Most devices are identified through registered numbers for a vendor and model, e.g. PCI or USB identifiers. The modprobe program consults the module alias table /lib/modules/VERSION/modules.alias to find the name of the file that contains the driver for that particular device. A similar principle applies to drivers for things that are not hardware devices, such as filesystems and cryptographic algorithms. For more details, see Debian does not detect serial PCI card after reboot

Once modprobe has identified which module file (.ko) contains the requested driver, it loads the module file into the kernel: the module code is dynamically loaded into the kernel. If the module is loaded successfully, it will then appear in the listing from lsmod.

The automatic loading of modules happen when the kernel detects new hotpluggable hardware, e.g. when you connect a USB peripheral. The operating system also does a pass of enumerating all the hardware that's present on the system early during startup, in order to load drivers for peripherals that are present at boot time.

It's also possible to manually request the loading of a module with the modprobe or insmod command. Most distributions include a startup script that loads the modules listed in /etc/modules. Another way for modules to be loaded is if they're a dependency of a module: if module A depends on module B, then modprobe A loads B before loading A.

Once a module is loaded, it remains loaded until explicitly unloaded, even if all devices using that driver have been disconnected. A long time ago, there was a mechanism to automatically unload unused modules, but it was removed, if I remember correctly, when udev came onto the scene. I suspect that automatic module unloading is not a common feature because the systems that would tend to need it are mostly desktop PCs that have lots of memory anyway (on the scale of driver code).

4
  • Thanks. I haven't modified /etc/modules. rt2800usb is in the output of lsmod, and does that mean that I connected its device to my computer before since booting?
    – Tim
    Apr 22, 2015 at 2:31
  • 1
    @Tim If the module is loaded, and you didn't load it explicitly, and it isn't listed in /etc/modules, then yes, presumably the reason the module is loaded is because the device was present at some point. Apr 22, 2015 at 11:15
  • The operating system also does a pass of enumerating all the hardware that's present on the system early during startup - How exactly is this done? What does the init script invoke to achieve this? Dec 27, 2021 at 18:02
  • @AxelFontaine I think it's udevadm trigger on a sysvinit system, and I guess that or some equivalent with systemd. Dec 27, 2021 at 19:01
5

Modules are loaded when the system boots via the Initial RAM Disk a.k.a the initrd. The Rationale Section states:

Many Linux distributions ship a single, generic Linux kernel image – one that the distribution's developers create specifically to boot on a wide variety of hardware. The device drivers for this generic kernel image are included as loadable kernel modules because statically compiling many drivers into one kernel causes the kernel image to be much larger, perhaps too large to boot on computers with limited memory. This then raises the problem of detecting and loading the modules necessary to mount the root file system at boot time, or for that matter, deducing where or what the root file system is.

Ubuntu like many other distributions, chooses to load every device driver into this initrd, regardless of whether the driver is needed or not, and also regardless if the device is present on the system or not. As Giles pointed out, the whole thing is loaded into RAM, and then the used modules are detected at startup, and the unused ones are removed from RAM. Using this approach ensures that Ubuntu will always start on any system regardless of setup. Ubuntu is mimicking a monolithic kernel using microkernel constructs. See The Reason This Works


  1. The module rt2800usb will always be loaded at bootup because the module was included in the initramfs that Gilles referred to. The initramfs is a successor of the initrd, therefore it will always be shown by lsmod. Note that you can insert a newly compiled module into the kernel by using modprobe followed by the module name.

As a test reboot your system with your wireless adapter unplugged. If all goes well the module will not be listed in lsmods output because during bootup the detection process started by the initramfs and the init sstem did not find the device during probing, and the module was removed from RAM.

  1. To remove a module while a system is running, you can use commands such as rmmod, or modprobe -r followed by the module name. At the next boot the module will be reloaded. See Above. In most cases a module is not removed dynamically, as this would disable hotplugging, i.e once a module is removed the device using it cannot be detected again when replugged in.

In order to remove a module from lsmod, you must remove it from the initramfs image created by recompiling the kernel without the chosen module and then rebuilding the image. Doing this disables all detection of said module.

8
  • 3
    You're confusing loading a file into RAM as part of a RAM disk and loading (i.e. dynamically linking) a module into the running kernel. Modules are loaded temporarily into memory — not into the kernel — from the initrd (technically an initramfs nowadays), but they are then removed from memory after the real root is mounted. Modules are only loaded into the kernel when a device using them is detected (with a few exceptions). Apr 21, 2015 at 22:41
  • Whilst I agree here, he was talking about unloading and loading one module which cannot be done unless he chooses to reconfigure the Ubuntu RAM Disk, which is not recommended because Ubuntu chooses to load all the modules into RAM every kernel update. All modules are loaded every time, they are just not all used.
    – eyoung100
    Apr 21, 2015 at 22:48
  • 2
    No, the question is about loading and unloading a module into the kernel. Neither your original answer nor your revised answer addresses that. The initramfs is irrelevant (or at most peripherally relevant) to this question. Apr 21, 2015 at 23:02
  • @Gilles Is this better??
    – eyoung100
    Apr 21, 2015 at 23:36
  • 1
    @eyoung100, I agree with Gilles. A discussion of initramfs is not relevant to the question. Modules are generally loaded by enumerating devices in /sys and loading the drivers for devices that are actually found in the system. This happens whether the device is present at boot or hot plugged later. udev has much more to do with it than initramfs/initrd does, and whether or not all, most, or only some modules are copied into the initramfs (a configuration option in /etc/initramfs-tools/initramfs.conf) is not especially relevant.
    – Celada
    Apr 21, 2015 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .