Building a kernel

From Paul's Linux Wiki
Jump to: navigation, search

Kernel Build and Installation

  1. Get the required tools
    sudo apt-get install git-core kernel-package fakeroot build-essential libncurses5-dev
    
  2. Change to the directory where you want to clone the git tree
    mkdir kernel
    cd kernel
    
  3. Clone the mainline kernel git tree For the latest kernel:
    git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
    

    or for the latest stable one:

    git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
    
  4. Change to linux directory
    cd lin<tab key>
    
  5. Copy the kernel config file from your existing system to the kernel tree (Note: in this case: use back ticks ..not single quotes!)
    cp /boot/config-`uname -r` .config
    
  6. Bring the config file up to date
    yes '' | make oldconfig
    
  7. (optional) If you need to make any kernel config changes, do the following and save your changes when prompted
    make menuconfig
    

    Note that Ubuntu kernels build with debugging information on, which makes the resulting kernel modules (*.ko files) much larger than they would otherwise be. (linux-image*.deb will be 200-300 MB instead of 20-30 MB). To turn this off, go into "Kernel hacking"; then, under "Kernel debugging", turn OFF "Compile the kernel with debug info". Cf. bug 90283

  8. Edit scripts/setlocalversion and comment out the lines as shown below. If this is not done, make-kpkg will fail to build the packages with an error "package linux-image-[kernel version]-custom+ not in control info".
    sed -rie 's/echo "\+"/#echo "\+"/' scripts/setlocalversion
    

    In addition, if you want to build from a git non-HEAD version, also comment out the lines extra plus at about line 168

        -       if test "${LOCALVERSION+set}" != "set"; then
        -               scm=$(scm_version --short)
        -               res="$res${scm:++}"
        -       fi
        +       #if test "${LOCALVERSION+set}" != "set"; then
        +       #       scm=$(scm_version --short)
        +       #       res="$res${scm:++}"
        +       #fi
        +       dummystr=""
    

    (Don't forget to add that 'dummystr' line!

  9. Clean the kernel source directory:
    make-kpkg clean
    
  10. Build the linux-image and linux-header .deb files. This process takes a lot of time.
    CONCURRENCY_LEVEL=`getconf _NPROCESSORS_ONLN` fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers
    

    With this command the package names will be something like linux-image-3.5.0-custom and linux-headers-3.5.0-custom and in that case the version will be 3.5.0-custom-10.00.Custom. You may change the string custom into something else by changing the --append-to-version option. You may also change the default 10.00.Custom into something else by using the --revision option.

  11. Change to one directory level up (this is where the linux-image and linux-header .deb files were put):
    cd ..
    
  12. Now install the .deb files. In this example, the files are probably linux-image-3.5.0-rc2-custom_3.5.0-custom-10.00.Custom_amd64.deb and linux-headers-3.5.0-custom_3.5.0-custom-10.00.Custom_amd64.deb You may receive warnings about '/lib/firmware/3.5.0-custom/' - this is expected and will only be problematic if the driver you are trying to test requires firmware. Note: If you got a later version, these names may differ: do a ls in this directory to see the proper filenames and then use those instead.
    sudo dpkg -i linux-image-3.5.0-custom_3.5.0-custom-10.00.Custom_amd64.deb 
    sudo dpkg -i linux-headers-3.5.0-custom_3.5.0-custom-10.00.Custom_amd64.deb
    
  13. You now need to make the new kernel bootable. Get your module names:
    cd /lib/modules 
    ls
    

    See how your new module is called here, and use that below (in our case: 3.5.0-custom)

    sudo update-initramfs -ck 3.5.0-custom 
    sudo update-grub
    
  14. You are now ready to boot into your new kernel. Just make sure you select the new kernel when you boot. You may have to make changes to the grub menu in order to make it display your boot options:
    cd /etc/default/ 
    gksu gedit /etc/default/grub 
    

    set: GRUB_TIMEOUT=-1

    sudo update-grub 
    sudo reboot
    
  15. After reboot, use the following command to check if you're running with your new kernel:
    uname -a
    

Miscellaneous

Tip: Make the build cmds into aliases in .bashrc. For example add this:

alias mk1='CONCURRENCY_LEVEL=`getconf _NPROCESSORS_ONLN` fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers'
alias mk2='cd ..;sudo dpkg -i linux-image-3.5.0-custom_3.5.0-custom-10.00.Custom_amd64.deb'
alias mk3='sudo dpkg -i linux-headers-3.5.0-custom_3.5.0-custom-10.00.Custom_amd64.deb'

You have to logout and log back in to activate this .bashrc


Example of debug statement added to kernel modul source:

printk( KERN_INFO "PAUL: File: %s, line: %u\n", __FILE__, __LINE__);


Example of cmd to display raw USB stick contents:

sudo dd if=/dev/sdb1 bs=512 count=2 2> /dev/null | hexdump -Cv

(Note sdb1 is the first partition, sdb is the whole drive)