# Custom Kernel / Driver Modules



## cpplain (Jun 11, 2010)

I'm building a custom kernel following section 8 of the Handbook, "Configuring the FreeBSD Kernel." Section 8.4, "Kernel Drivers, Subsystems, and Modules," briefly describes dynamically loadable kernel modules but doesn't go into much detail on the advantages/disadvantages of loaded modules versus drivers compiled into the kernel.

That leaves me to ask:

1. Is there any benefit to removing all drivers from the kernel that can be loaded via modules, using loader.conf?

2. What about the inverse? Is there any benefit to compiling drivers into the kernel versus loading modules?

Christopher


----------



## zeiz (Jun 11, 2010)

The less drivers in kernel the less time it probes devices on boot = advantage IMO.
The more modules to build the longer build time = disadvantage IMO.
Some exceptions: sound drivers are not in kernel config file and must be build anyway; zfs must be as module; may be some more - forgot.

There are tons of drivers in kernel config file. For myself I remove all the drivers for devices I don't have. For example I have only 1 NIC so all the others I remove. I also don't have SCSI (but I don't remove scsi bus otherwise usb stick fails to mount). I don't have floppy (fdc) atapi floppy, tape devices, media changers ...some more.
But I add *device atapicam* to kernel config file (it's not there by default) to play audio CDs.
In general it must be file NOTES in the same directory as GENERIC - take a look.


----------



## SIFE (Jun 11, 2010)

as Mr zeiz said remove all drivers you don't need like nic cards ,wireless support ,IDE support if you don't have also remove emulation for old BSD's if you don't need .
now days LPT port is no more used so you can remove it ,also remove kernel options you don't need like IP6 .

my kernel config in attachment .
to modules you can set what you will need in /etc/make.conf like that :

```
MODULES_OVERRIDE=zfs sis linux linprocfs procfs acpi\
sound/sound sound/driver/ich msdosfs accf_http\
opensolaris krpc
```


----------



## phoenix (Jun 11, 2010)

If you will be adding/changing hardware in the system down the road, then it's best to always build all the modules.  That way, it's just a simple *kldload modulename* to enable support while the system is running, and a simple edit of /boot/loader.conf to enable support at boot.

Remove all the drivers you won't be needing from the kernel config, though, to make the binary kernel nice and small.

If you know for sure, 100%, that you will never be changing hardware, then you can skip building the modules.  But I recommend against this.    Just in case.


----------



## cpplain (Jun 11, 2010)

I've removed unused drivers from the kernel as suggested. Though, I'm still unclear on whether it is better to load needed drivers via modules, using loader.conf, or to compile the drivers into the kernel.

For example:

I need em for my NIC. I have compiled this into my custom kernel. But, would it be better to leave this out of the kernel and load via loader.conf? Why or why not?

Christopher


----------



## alys (Jun 11, 2010)

if you have some module, which you exactly need to load at boot, it is better to include it in the core. if you module implement some optional functionality, it is better to have it as module.
Modules are used to reduce the boot image(to reduce memory usage and boot time), but if you exactly need to load this module at the boot - this slightly increases the boot time. because kernel needs to load it separately and dynamically link this code to the kernel. If code is already in your kernel - than it is linked by linker during kernel build.


----------



## alys (Jun 11, 2010)

advice - do not have the paranoia about modules. Use them as wide as possible. Include in the kernel the code you absolutely need at every boot.
Keep your kernel as small as possible. And as much as possible modules.


----------



## hapvbk (Apr 8, 2011)

Hi there,

This is an old topic but I have some problem while doing the same thing. I pose my question here and hope that you guys have a solution for it. 

I follow exactly the handbook: add 
	
	



```
if_ath_load="YES"
```
 to the loader.conf. After reboot, I used *kldstat* to check the loaded module but there is only one module loaded (kernel) and the if_ath is not loaded. 

Then I tried *kldload if_ath*, the error message is the file exists. I tried to unload it using *kldunload*, it is said the file not found. I don't know what I have done wrong and how to solve this problem. 


Did anyone experience the same thing like I did? Any suggestion or solution is highly appreciated. 


Thanks so much.

HAPV


----------



## Bunyan (Apr 8, 2011)

> Then I tried kldload if_ath, the error message is the file exists.


The support for that network interface is built into the GENERIC kernel.

```
# Wireless NIC cards
device		ath		# Atheros pci/cardbus NIC's
```
.


----------



## hapvbk (Apr 8, 2011)

Hi Bunyan,

Thanks so much for replying. Do you mean that we have to use the name "ath" for loading/unloading the kernel? 

In /boot/default/loader.conf, I see that they use the name if_ath:


```
if_ath_load="NO"          # Atheros IEEE 802.11 wireless NICs
```


HAPV


----------



## SirDice (Apr 8, 2011)

hapvbk said:
			
		

> Do you mean that we have to use the name "ath" for loading/unloading the kernel?


No, it means loading the module in loader.conf is useless because the module is already build into the kernel. Look at the GENERIC kernel config file to see what's already loaded. Things that are build into the kernel cannot be loaded/unloaded, hence kldstat won't show them.


----------



## DutchDaemon (Apr 8, 2011)

Use *kldstat -v*.


----------



## hapvbk (Apr 8, 2011)

SirDice said:
			
		

> No, it means loading the module in loader.conf is useless because the module is already build into the kernel. Look at the GENERIC kernel config file to see what's already loaded. Things that are build into the kernel cannot be loaded/unloaded, hence kldstat won't show them.



Hi SirDice, 

If we want to load/unload dynamically the ath driver, can we: 
1. exclude the compilation of that driver into the kernel by remove the line "device   ath" in the config file (GENERIC or a customized one).
2. compile the kernel and the driver separately, and add the command to loader.conf to load the driver. 

The idea is, I want to customize the rate adaptation in the ath driver and try to avoid compiling the whole kernel, as well as to reboot the OS each time we have st to test. 

Thanks a lot. 


HAPV


----------



## hapvbk (Apr 8, 2011)

DutchDaemon said:
			
		

> Use *kldstat -v*.



Thanks DutchDaemon. 

I used *kldstat -v* and see the list of modules loaded into the kernel. I am still looking for a way to load/unload any of them dynamically. 


HAPV


----------



## phoenix (Apr 8, 2011)

If the driver is listed in /usr/src/sys/<arch>/conf/<kernel_config_file>, then the driver is compiled directly into the kernel, and cannot be loaded/unloaded using *kldload*/*kldunload*.

If you want to play around with drivers, then don't list them in the kernel config file, recompile the kernel, install the new kernel, reboot, and use the kld* tools.


----------



## hapvbk (Apr 8, 2011)

phoenix said:
			
		

> If the driver is listed in /usr/src/sys/<arch>/conf/<kernel_config_file>, then the driver is compiled directly into the kernel, and cannot be loaded/unloaded using kldload/kldunload.
> 
> If you want to play around with drivers, then don't list them in the kernel config file, recompile the kernel, install the new kernel, reboot, and use the kld* tools.



Thanks Phoenix so much for that confirmation. I am going to that direction and let you know if it works.


----------



## hapvbk (Apr 10, 2011)

Hi there,

I'd like to update my progress. It's not a big thing but it may help the newbies like me. 

- Remove the ath driver in the kernel compilation by comment the devices and options related to *ath* driver: open the config file in /sys/i386/conf/, find and comment (put the # at the beginning of the line) the following lines: 


```
#device         ath             # Atheros pci/cardbus NIC's
#device         ath_hal         # pci/cardbus chip support
#options        AH_SUPPORT_AR5416       # enable AR5416 tx/rx descriptors     
#device         ath_rate_sample    # SampleRate tx rate control for ath
```

- Compile the kernel: `# make buildkernel KERNCONF=<CONFIG FILE>` and then `# make instalkernel KERNCONF=<CONFIG FILE>` 

- Open /boot/loader.conf and add: 
	
	



```
if_ath_load="YES"
```
, save and then reboot. 

The new kernel will have the ath driver build and loaded separately. Now, if we type kldstat, we will see the *if_ath* module loaded besides the kernel. We can use kldload(8)/kldunload(8) to load/unload the ath driver. 

If we customize the ath driver and want to test it, edit the /etc/make.conf file and add: *MODULES_OVERRIDE = ath*. Rebuild the kernel. Now, only the ath driver is recompiled. After installing the new ath driver by `# make installkernel` unload and reload the if_ath module to have the new ath driver loaded. 

Thanks everybody for your help. 

HAPVBK


----------

