Saturday, November 14, 2009

A Fast Way to Build Linux Kernel

  1. apt-get source linux-image-`uname -r`. This command downloads the Linux kernel source code of your current version.
  2. make defconfig. Default configuration is much more compact than the old configuration used in Ubuntu or other Linux distributions. Because I only need to change the scheduler, this compact configuration is more than enough for me.
  3. Usually, the default configuration allows you to boot into the system. If it doesn't, the most likely reason is lack of device drivers in the kernel. The drivers you need to compile depends on the hardware system you are using. For example, I need to enable CONFIG_FUSION for my virtual machine. "lscpi" servers as a good clue to figure out which drivers are needed.
  4. make menuconfig, and enable the network device driver. Check your Ethernet controller using lspci, and enable the corresponding configuration option in the kernel configuration. For example, my controller is PCnet32 LANCE, so I need to enable CONFIG_PCNET32. Another way to figure out which option to enable is to view the file /etc/udev/rules.d/70-persistent-net.rules. Again in my case, I can see "pcnet32" at the end of the device description. My desktop has the device type e1000e, so I would enable CONFIG_E1000E instead. You can find these configurations in the path Device driver -> Network device support in menuconfig. For instance, Ethernet (10 or 100Mbit) -> AMD PCnet32 PCI support is for CONFIG_PCNET32, and Ethernet (1000 Mbit) -> Intel(R) PRO/1000 PCI-Express Gigabit Ethernet support is for CONFIG_E1000E.
  5. Refine your kernel configuration (optional). You can disable unnecessary modules further in "make menuconfig", such as support for Mac devices.
  6. Pick a name for your new kernel (optional). Also in menuconfig, General setup -> Local version.
  7. make -j?. ? is one plus the number of cores your machine has. It takes only 4 minutes on my virtual machine.
  8. sudo make modules_install
  9. sudo make install
  10. Update your GRUB menu list (/boot/grub/menu.lst). e.g:
    • title Ubuntu 8.04.1, kernel 2.6.24.6-custom
    • root (hd0, 0)
    • kernel /boot/vmlinuz-2.6.24.6-custom root=/dev/sda1 ro
  11. sudo reboot

No comments: