# Kernel modules (dynamic, static, none)



## PKraszewski (Oct 1, 2021)

Hello!
I'm looking for a definitive answer on:
* what's the difference between `device DEVICE`, `nodevice DEVICE` and _no line at all_ in kernelconfig.
* where do some defaults I didn't set come from? I didn't use any `include` in kernel config, yet modules not specified or explicitly `nodevice` *are* still built...
* what's going on in here (vanilla, unaltered, up-to-date FreeBSD 13):

```
> uname -a
FreeBSD node-01 13.0-RELEASE-p4 FreeBSD 13.0-RELEASE-p4 #0: Tue Aug 24 07:33:27 UTC 2021     root@amd64-builder.daemonology.net:/usr/obj/usr/src/amd64.amd64/sys/GENERIC  amd64

> kldload aesni
kldload: can't load aesni: module already loaded or in kernel

> ls -al /boot/kernel/aesni*
-r-xr-xr-x  2 root  wheel  67712 Apr  9 08:36 /boot/kernel/aesni.ko

> kldstat | grep aesni | wc -l
0
```
So... Is `aesni` statically linked into kernel? If yes, then why there's `aesni.ko`?  What part of kernelconfig decides, if a module is statically linked into kernel or built as .ko?

My ultimate goal is to have a fully statically linked kernel with *only* the modules that are necessary for the device. For a very specific embedded machine, that must have _zero_ unused parts.


----------



## SirDice (Oct 1, 2021)

PKraszewski said:


> what's the difference between `device DEVICE`, `nodevice DEVICE` and _no line at all_ in kernelconfig.


`device DEVICE` adds that device statically in the kernel, `nodevice DEVICE` is useful if you start by including the GENERIC config and want to remove something from it. No line at all just means that device is only built as a module. 


PKraszewski said:


> where do some defaults I didn't set come from? I didn't use any `include` in kernel config, yet modules not specified or explicitly `nodevice` *are* still built...


There's sys/<arch>/conf/ that includes devices/configuration specific to that architecture. There's also sys/conf/ that has the configuration that is common to all architectures.



PKraszewski said:


> what's going on in here (vanilla, unaltered, up-to-date FreeBSD 13)


With 13.0-RELEASE aesni(4) is statically built into the kernel. On previous versions it wasn't statically included in the kernel and had to be loaded as a module if you wanted to use it. 

It's included with the GENERIC kernel on 13.0-RELEASE:





						GENERIC « conf « amd64 « sys - src - FreeBSD source tree
					






					cgit.freebsd.org
				



Compare with the GENERIC kernel from 12.2-RELEASE for example:





						GENERIC « conf « amd64 « sys - src - FreeBSD source tree
					






					cgit.freebsd.org
				






PKraszewski said:


> If yes, then why there's `aesni.ko`?


Every module is always built, even the ones you statically include in the kernel config. 

```
LOCAL_MODULES        A list of external kernel modules that should be
                          built and installed as part of the buildkernel and
                          installkernel process.  Defaults to the list of sub-
                          directories of LOCAL_MODULES_DIR.

     LOCAL_MODULES_DIR    The directory in which to search for the kernel
                          modules specified by LOCAL_MODULES.  Each kernel
                          module should consist of a directory containing a
                          makefile.  Defaults to ${LOCALBASE}/sys/modules.
```
See build(7).


----------



## PKraszewski (Oct 1, 2021)

OK, so `device DEV`'ing what I need and *not* copying effectively unnecessary .ko files to target is my way?


----------



## zirias@ (Oct 1, 2021)

You can also avoid to build any modules at all with `NO_MODULES=yes` in /etc/src.conf (that's documented in make.conf(5) and will work there as well of course, but should still be put in src.conf(5) cause it only affects /usr/src)


----------

