How does the Linux kernel know which drivers to load at boot?

18,087

Solution 1

How does the Linux kernel know which drivers to load at boot?

The kernel generates events for devices on e.g. the PCI bus when they are plugged (either hot or cold; events are queued until userspace runs AFAIR). udev will receive these events and do modprobe calls which include the PID/VID (product/vendor IDs) of the device(s); this is usually a string with some * in it. modprobe will then calculate the intersection of the set expressed by udev's load request wildcard and the set of aliases of kernel modules (themselves being possibly wildcards).

Since USB/Firewire/etc. controllers are usually attached to the PCI bus, that's how your HCI driver gets loaded. That is how things recurse down; loading is then done with USB/Firewire PID/VIDs of course.

Network protocol modules (e.g. ipv6) are however not dealt with through udev; instead, when a program calls socket(AF_INET6, ...) the kernel directly calls modprobe (more precisely: whatever is in /proc/sys/kernel/modprobe) with a non-wildcarded alias, net-pf-10 in case of IPv6, because AF_INET6 happens to have value 10. modprobe then loads ipv6.ko, because that is what has the net-pf-10 alias.

Similarly for filesystems, attempting to mount -t foo will cause the kernel to also call modprobe (again, via ____call_usermodehelper), this time with foo as argument.

Accessing device nodes (e.g. /dev/loop0, provided it already exists) has the same strategy if loop.ko is not already loaded. The kernel here requests block-major-7-0 (because loop0 usually has (7,0), cf. ls -l), and loop.ko has the fitting block-major-7-* alias.

Solution 2

Greg Kroah give an excellent example on how to find exactly which drivers you need for you kernel. Kindly Greg gives his book away for free online

http://files.kroah.com/lkn/

A quote from Greg's books

I'm especially proud of the chapter on how to figure out how to configure
a custom kernel based on the hardware running on your machine. This is an
essential task for anyone wanting to wring out the best possible speed and
control of your hardware. 
Share:
18,087

Related videos on Youtube

izzy
Author by

izzy

Updated on June 04, 2022

Comments

  • izzy
    izzy almost 2 years

    I'd like to know this for the first boot and for subsequent boots.

    I'm compiling my own kernel and want it to be as lean as possible. I want to build the .config file by hand (mainly as a learning experience), so I need to know everything that can be excluded. I know a possible solution is to look at my current distros list of loaded drivers. However, I'm curious about how my distro discovered what drivers to load initially.

    TIA.

  • izzy
    izzy over 12 years
    Thanks for your answer Adrian. I'm actually working through chapter 7 of the book right now. Greg Kroah details the process of discovering what modules are currently loaded by the running kernel -- which is very valuable. What I'm curious about is how did the OS know to load those modules in the first place?
  • Adrian Cornish
    Adrian Cornish over 12 years
    ASAIK Brute force generally - it tries loading it - if it doesnt work the hardware is probably not there.