Friday, March 19, 2010

Configuring and Tuning FreeBSD System

Tuning the kernel of a relatively less known operating system often creates this "wow" factor. You would be seen as a technical expert but also a real geek, and this comment actually came from one of my colleagues - a Network Consultant...

Fact is, the FreeBSD kernel (or any operating system) is essentially a resource manager. By resources I am referring to CPU cycles, hard-disk spaces, memory etc. There is a great introductory video posted on youtube about FreeBSD kernel internals. [http://www.youtube.com/watch?v=nwbqBdghh6E]


There are two main "tools" when it comes to configuring and tuning the kernel. The kernel's configuration file and "sysctl".

Firstly the "GENERIC" kernel configuration file is located under the "/usr/src/sys/i386/conf/" directory. This configuration file basically contains modules/devices being loaded into the system. It is good practice to copy this "GENERIC" file to another file named, say "MyKERNEL" for editing purposes.

Removing unnecessary modules reduces resource consumption, hence resulting in faster reboot time and smoother operations. Using my second-hand Compaq as an example, I have removed the following devices from my kernel configuration file:

#cpu I486_CPU
#cpu I586_CPU
#options SMP # Symmetric MultiProcessor Kernel
#device apic # I/O APIC
#device fdc # Floppy disk
#device atapifd # ATAPI floppy drives
#device atapist # ATAPI tape drives

and everything under...
# PCI Ethernet NICs.
# PCI Ethernet NICs that use the common MII bus controller code.
# USB Ethernet, requires miibus
# FireWire support


Also, this configuration file contains "options". These are some of the interesting ones:
makeoptions COPTFLAGS="-O2 -pipe -funroll-loops -ffast-math" #Optimize C compiler
options DEVICE_POLLING # Device Polling
options HZ=2000 # "top" shows reduced interrupts from NIC
options IPFIREWALL # NAT Service
options IPFIREWALL_VERBOSE # Syslog logging
options IPDIVERT # Socket Divert

There is a chapter on configuring BSD kernel in the FreeBSD handbook, [http://www.freebsd.org/doc/handbook/kernelconfig.html].


To compile and install the kernel, use the following commands:
# /usr/src
# make buildkernel KERNCONF=MyKERNEL && make installkernel KERNCONF=MyKERNEL
# shutdown -r now
... where "MyKERNEL" is the name of the kernel configuration text file you edit.

Speaking from experiences, these were the two biggest mistakes I made. I once tried to copy a FreeBSD 7.0 kernel configuration text file and tried compiling it on a FreeBSD 8.0 system. The result was lots of "parameters unknown/not found" error messages. Just stick with the GENERIC configuration file which comes with installation. In case if you have forgotten to install the source code, you can always use "sysinstall" to do a post-installation of all packages to re-add the source code. Another mistake I made was to install ports (and compile code) during kernel compilation. It happened when I was working till 1am during weekday and I wished to speed up the process. Patience is a virtual...


Secondly, "sysctl" command is also useful for tuning kernel parameters as recommended in the FreeBSD handbook.
[http://www.freebsd.org/doc/handbook/configtuning-kernel-limits.html]

I admit I have not tried all of these options, as my experience with "sysctl" evolves around tuning parameters at run-time e.g. limiting icmp responses (net.inet.icmp.icmplim) and switching off the "beep" sound (hw.syscons.bell). Of course, you can always save these parameters into the "/etc/sysctl.conf" file such that they remain constant after system restart.


I suppose the best way to become familiar with any system is to get your hands dirty, so to speak. So if you'd like to learn more, please give it a go and good luck with your Unix experience!

No comments:

Post a Comment