2

Is there any proper way to build a minimal kernel for FreeBSD? The FreeBSD Handbook has the lack of information about this. By default /boot/kernel directory has the pretty big size - around 450MB. I want to minimize kernel fingerprint and remove all unnecessary kernel modules and options. Should I use "NO_MODULES" option in /etc/make.conf? Or use C compilation flags?

3

There are a number of things you can do to reduce the size and number of files in /boot/kernel.

Possibly the best space saving is to be had by setting WITHOUT_KERNEL_SYMBOLS in /etc/src.conf (if this file doesn't already exist, just create it), and the next time you installkernel, the debug symbol files won't be installed. It's safe to delete them now, if you need the space immediately (rm /boot/kernel/*.symbols)

There are a few make.conf settings that control what modules are built:

  • NO_MODULES - disable building of modules completely
  • MODULES_OVERRIDE - specify the modules you want to build
  • WITHOUT_MODULES - list of modules that should not be built

The NO_MODULES option is probably a bit too heavy-handed, so a judicious combination of the other two is a better choice. If you know exactly which modules you want, you can simply set them in MODULES_OVERRIDE. Note that WITHOUT_MODULES is evaluated after MODULES_OVERRIDE, so any module named in both lists will not be built.

If you really want to suppress building of all modules, you can use NO_MODULES, and ensure that all required drivers and modules are statically compiled into the kernel. Each driver's manpage shows the appropriate lines to add to your kernel config file, so you should be able to figure out what you need.

If you still find that space is a problem, or if you just want to strip down the kernel as much as possible, you can edit your kernel config to remove any devices and subsystems your machine doesn't support, or which you are sure you won't want to use. The build system is pretty sensible, and if you inadvertently remove a module required by one still active in the config, you will get a failed build and an error message explaining what went wrong.

Although it can be extremely tedious, the best approach is to take small steps, removing one or two things at a time and ensuring that the resultant configuration both builds and boots correctly. Whatever you do, though, I highly recommend you make a copy of /usr/src/sys/<arch>/config/GENERIC, and edit the copy. If you ever get so muddled that the only recourse is to start from the default config, you'll be glad you've still got the GENERIC file on your system!

In order to build your custom kernel, you can either pass the name of the config on the command line as make KERNCONF=MYKERNCONF buildkernel, or you can set KERNCONF in /etc/make.conf. Make sure you place the custom config file in /usr/src/sys/<arch>/config and the build system will be able to find it.

1

"Proper" kernel is a relative notion and depends on your needs. People who need to ask about building kernel have no business building one. By the way how did you come up with the 450 MB size of FreeBSD kernel

root@neill-backup:~ # uname -a
FreeBSD neill-backup.int.autonlab.org 10.1-RELEASE FreeBSD 10.1-RELEASE #0 r274401: Tue Nov 11 21:02:49 UTC 2014     root@releng1.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC  amd64

here

root@neill-backup:~ # du -h /boot/kernel/kernel
 20M    /boot/kernel/kernel
  • Yeah, but /boot/kernel have size around 450MB. – powerthrash Dec 29 '14 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.