Wednesday, January 25, 2006

Loading Kernel Modules in FreeBSD

Like Linux, the FreeBSD kernel supports the loading and unloading of modules. This allows an administrator to add or remove driver support without having to recompile the kernel or reboot the system. The possible modules are the files ending with the .ko extension in /boot/kernel.

To list the currently loaded modules:

root@freebsd# kldstat
Id Refs Address Size Name
1 7 0xc0400000 63070c kernel
2 16 0xc0a31000 568dc acpi.ko
3 1 0xc14de000 15000 linux.ko
root@freebsd#

If you're curious as to the meaning of each of the columns, see man 2 kldstat. Note that the usage and output is similar to Linux's lsmod command.

Linux also provides the insmod and rmmod commands to load and unload modules. The FreeBSD equivalents are kldload and kldunload.

For example, to load USB scanner support:

#kldload uscanner.ko

To remove it when you are finished

#kldunload uscanner.ko

Loading something that is already statically compiled into the kernel produces this error message:

root@freebsd# kldload acpi.ko
kldload: can't load acpi.ko: File exists

If you don't know what a module does, ask whatis. Suppose that I'm curious about the module if_pcn.ko. I won't include the .ko in my query. I also won't include the if_; it categorizes the module as an interface type. (Similarly, snd_ represents the sound category.) That leaves pcn, making this command:

root@freebsd# whatis pcn
pcn(4) - AMD PCnet/PCI Fast Ethernet device driver

I think my NIC might fall into that category. man 4 pcn gives the actual NIC models covered by this particular kernel module.

Check this for more info on Freebsd Basics.