2010-07-19

FreeBSD - How to compile custom kernel

Normally, the generic kernel that comes with default FreeBSD installations are good enough for most installations. But some machines that I worked on are having more then 4gb of memory. Be default, the i386 processor architecture only recognizes 4gb or less memory. Any random access memory larger then 4gb, the kernel will need to enable the feature PAE (Physical Address Extension) in order to make use of the processor's extended physical address space, from 32-bin to 36-bit. After compiling PAE kernel feature, the 4gb limit will increase to 64gb.

Most of the time, I re-compile the kernel because of the server hardware comes with more then 4gb of memory. But recently I'm setting up a firewall that need to do traffic shaping, through QoS (Quality of Service). PF's ALTQ feature will do the traffic shaping, but by default, it is not included in the default kernel.

This post will show how to compile custom kernel in FreeBSD so that it includes the ALTQ feature :

Preparation :
  • Command to check what kernel versions are loader currently :
    uname -a
  • Processor architecture based kernel config files are stored in
    /usr/src/sys/(arch)/conf/
    where (arch) can be i386, alpha, amd64, ia64, powerpc, sparc64, or pc98.
  • Kernel options are stored in the file /usr/src/sys/conf/NOTES
  • Make sure that the kernel source exist in /usr/src. If not, please refer to the post "FreeBSD - How to setup & configure jail with ezjail", "caveat" section

Custom kernel compilations starts here :
  1. Backup the current bootable kernel :
    cp -r /boot/kernel /boot/kernel.BOOTABLE
  2. Change directory into the kernel config files directory :
    cd /usr/src/sys/i386/conf/
  3. Create a new file (e.g. CUSTOMKERNEL) and put the kernel options in it. As an example, this post is compiling ALTQ (Quality of Service) function into kernel so below are the kernel options needed :
    include GENERIC
    ident kernel_with_ALTQ
    
    options         ALTQ
    options         ALTQ_CBQ        # Class Bases Queuing (CBQ)
    options         ALTQ_RED        # Random Early Detection (RED)
    options         ALTQ_RIO        # RED In/Out
    options         ALTQ_HFSC       # Hierarchical Packet Scheduler (HFSC)
    options         ALTQ_PRIQ       # Priority Queuing (PRIQ) 
  4. Compile & build the customer kernel using the kernel option file (CUSTOMKERNEL) we've just created :
    cd /usr/src; make buildkernel KERNCONF=CUSTOMKERNEL
  5. Install the kernel and config files into proper place :
    cd /usr/src; make installkernel KERNCONF=CUSTOMKERNEL

Reboot the box so that the new kernel will be loaded. In case the new kernel does not boot, this is how to revert to old kernel (that we just backup) :
  1. Reboot the box. When the Boot Menu appears, choose option 6, "Escape to a loader prompt".
  2. When dropped to the "Ok" prompt, execute
    boot /boot/kernel.BOOTABLE/kernel
It should now boot into the old kernel. Either fix the problem, or re-compile & install the kernel but uses the "GENERIC" kernel option file instead.

By the way, in order to compile PAE kernel option into a custom kernel, just include
options            PAE
into the kernel option file. Reboot the box and it will recognize memory above 4gb. Easy huh :p

Adios !!!

No comments: