Wednesday, October 10, 2012

Building Linux kernel the clean way

I like to keep my kernel sources clean and have the kernel build in a separate directory. Here is how I do it.



1. Lets create a "~/kernel" directory for holding everything
$cd ~
$mkdir ~/kernel
$cd ~/kernel
2. Download and extract the kernel source
$wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.6.tar.bz2
$tar jxvf linux-3.6.tar.bz2
3. Create a build directory
$mkdir ~/kernel/build
Now our directory structure looks like:
~/
~/kernel/
~/kernel/build/
~/kernel/linux-3.6.tar.bz2
~/kernel/linux-3.6/
4. We will create a bash function that will automatically create the build folder for us based on the folder name of the kernel source. Add the following to the ~/.bashrc file. Once this is done open a new bash terminal so that the function is available for use.
function kb {
    mkdir -p ~/kernel/build/$(basename $(pwd))
    echo ~/kernel/build/$(basename $(pwd))
}
Running $kb in your shell will create the build folder and return the path of the build folder which we will pass it to "make" in the next step.

5. To build the kernel in the "build" folder follow your normal step except...
$cd linux-3.6
$make distclean
$make defconfig O=`kb`
$make O=`kb`
You can see that a folder called "~/kernel/build/linux-3.6" has been created and all the object files are created in the build folder and our original kernel source folder is kept clean. After the compiling is done the kernel is available in the ~/kernel/build/arch/x86/boot/bzImage or whatever is your architecture folder.

Note :
The keys surrounding the `kb` in the above steps are the backtick keys which are located along with the "~" key on your keyboard.

Extra :
If you download and extract the linux-3.5.tar.bz2 and repeat the above 5th step it will create a ~/kernel/build/linux-3.5 folder and build everything there.

Next time I am going to post how you can fully automate the build with git.

Sunday, October 7, 2012

Using ketchup to manage kernel releases

We are going to use ketchup to download Linux kernel releases. Its a great tool written in python to automate a lot of stuff.

1. Install ketchup
$sudo apt-get install ketchup
$mkdir linux
$cd linux
2. Lets check the latest version of Linux kernel for the 3.x series
$ketchup -s 3
3.6
3. Lets download the 3.6 kernel release
$ketchup -r 3.6
.....
gpg: Signature made Monday 01 October 2012 05:25:16 AM IST using RSA key ID 00411886
gpg: Can't check signature: public key not found

ketchup: The GPG key seems not to be in the keyring. Please fix this and try again.
In the case potential malicious kernel code is not a problem,
you can skip the verifying by using --no-gpg.
ketchup: Tarball download failed
4. Oops ! You need to add the GPG key to your keyring for verification since each release is signed (Note that the RSA key ID 00411886 is mentioned in the message itself). Lets do that...
$gpg --recv-keys 00411886
gpg: requesting key 00411886 from hkp server keys.gnupg.net
gpg: key 00411886: public key "Linus Torvalds " imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
6. This looks good. Lets try downloading again.
$ketchup -r 3.6
....
Downloading linux-3.6.tar.sign
--2012-10-07 17:47:06--  http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.6.tar.sign
....
Downloading linux-3.6.tar.xz
--2012-10-07 17:47:06--  http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.6.tar.xz
....
7. It will download, verify, extract the kernel sources and it will also rename the directory to "linux-3.6" to match the kernel version downloaded. Great !

8. Since our original "linux" folder has been renamed to "linux-3.6" we need to back down to parent folder and go back to the renamed "linux-3.6" folder
$cd ..
$cd linux-3.6
9. Now lets try something different. Lets download Linux kernel version 3.5
$ketchup -r 3.5
Note that it only downloads the patch file that it needs to apply/revert to 3.6 that is already downloaded to roll back to 3.5 ! Also it will rename the directory to "linux-3.5". Remember to repeat the above step 8 to go to the renamed "linux-3.5" folder.