[GUIDE][HOW_TO] BUILD a LINUX KERNEL FROM SOURCE [UBUNTU]

Search This thread

#buzz

Senior Member
May 23, 2014
80
373
/android/works
This is a general open source linux development thread!

Android's kernel is a derivative of linux's kernel. Its good to know how to build both of these kernels. You might be already familiar with building kernels for various devices from sources. So I have made a new thread for guiding people on how to compile linux kernel from source (example taken as ubuntu kernel).

Requirements:
  • Any linux os x64 bit(example here: ubuntu 14.04)
  • Git (sudo apt-get install git)
  • Minimum of 4GB RAM and some reasonable linux-swap

To get the currently running kernel image, type the following:
Code:
apt-get source linux-image-$(uname -r)

Now we need to obtain Ubuntu Kernel Sources from its repositories. Make a new directory and inside it, initialise the git and clone the repository.
Code:
git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release>.git
<release> : Type in the required source. It can be lucid, precise, trusty, utopic etc.

Setting up the build environment. There are lots of tools and packages that are very much essential for building a kernel. These tools can be downloaded as a whole bundle and can be installed easily. Here's the code to set it up:
Code:
sudo apt-get build-dep linux-image-$(uname -r)

NOTE: The above comand can be executed only after you obtain the currently running kernel image. I have already given the code to obtain it above.

Now, change directory to the root of the kernel and type the following:
Code:
chmod -R a+x *
The above code will set the required permissions for building and executing the kernel.

Now, run these two commands:
Code:
fakeroot debian/rules clean
fakeroot debian/rules editconfigs

The first command cleans up the code automatically.
The slightly tricky part is with the second line of the code. When you execute it, you will have to edit a series of menuconfigs. To make changes to the configuration file we need to edit the configuration file. The kernel developers have created a script to edit kernel configurations which has to be called through the debian/rules makefile, unfortunately you will have to go through all the flavors for this script to work properly. The script will ask you if you want to edit the particular configuration. You should not make changes to any of the configurations until you see your wanted flavour configuration
We have now covered about 70% of progress. The rest is building the kernel and testing it.

Building the kernel is quite easy. Change your working directory to the root of the kernel source tree and then type the following commands:
Code:
fakeroot debian/rules clean
fakeroot debian/rules binary-headers binary-generic
If the build is successful, a set of three .deb binary package files will be produced in the directory above the build root directory. For example after building a kernel with version "3.13.-0.35" on an amd64 system, these three .deb packages would be produced:
Code:
cd ..
ls *.deb
linux-headers-3.13.0-35_3.13.0-35.37_all.deb
linux-headers-3.13.0-35-generic_3.13.0-35.37_amd64.deb
linux-image-3.13.0-35-generic_3.13.0-35.37_amd64.deb
Testing the new kernel
Install the three-package set (on your build system, or on a different target system) with dpkg -i and then reboot:
Code:
sudo dpkg -i linux*3.13.0-35.37*.deb
sudo reboot
Guys, I hope I have made an easy tutorial. You are always welcome to ask doubts (even on PM). Thank You.
 
Last edited:

#buzz

Senior Member
May 23, 2014
80
373
/android/works
Specific Hardware/Architecture

Creating a new config

I’ll be using the method of creating a new flavour, this adds a bit more work but this way you can always compile the original kernels.

We’ll use the generic flavour as the base for our own flavour being i7, this extension needs to be in small caps.
Code:
cp debian.master/config/amd64/config.flavour.generic debian.master/config/amd64/config.flavour.i7
fakeroot debian/rules clean
debian/rules updateconfigs

To make changes to the configuration file we need to edit the configuration file. The kernel developers have created a script to edit kernel configurations which has to be called through the debian/rules makefile, unfortunately you will have to go through all the flavours for this script to work properly.
Code:
debian/rules editconfigs

The script will ask you if you want to edit the particular configuration. You should not make changes to any of the configurations until you see the i7 configuration
Code:
Do you want to edit config: amd64/config.flavour.i7? [Y/n]

Make your changes, save the configuration and then keep going until the script ends.

When you’re done, make a backup of the config flavor file.
Code:
cp debian.master/config/amd64/config.flavour.i7 ../.
Now we need to clean up the git tree in order to get ready for compilation.
Code:
git reset --hard
git clean -df
Getting ready for compilation

Because we are going to be creating a new flavour based on a existing flavour (generic in my case) we need to create some extra files. During compilation the process checks the previous release for some settings, as we’re creating a local flavour it doesn’t exist in the source, so we’re creating it.

To see the previous release we use:
Code:
ls debian.master/abi

cp debian.master/abi/3.0.0-12.20/amd64/generic debian.master/abi/3.0.0-12.20/amd64/i7
cp debian.master/abi/3.0.0-12.20/amd64/generic.modules debian.master/abi/3.0.0-12.20/amd64/i7.modules

Copy our flavored configuration file back.
Code:
cp ../config.flavour.i7 debian.master/config/amd64/

We need to edit some files:
File: debian.master/etc/getabis

Search for the line:
Code:
getall amd64 generic server virtual

Change it in:
Code:
getall amd64 generic server virtual i7

File: debian.master/rules.d/amd64.mk

Search for the line:
Code:
flavours        = generic server virtual

Change it in:
Code:
flavours        = generic server virtual i7

File: debian.master/control.d/vars.i7

This files does not exist and in order to make the compilation process aware of our own flavor we want to compile we need to create it.
Code:
cp debian.master/control.d/vars.generic debian.master/control.d/vars.i7

You can edit the file and make it your own.
Code:
arch="i386 amd64"
supported="i7 Processor"
target="Geared toward i7 desktop systems."
desc="x86/x86_64"
bootloader="grub-pc | grub-efi-amd64 | grub-efi-ia32 | grub | lilo (>= 19.1)"
provides="kvm-api-4, redhat-cluster-modules, ivtv-modules, ndiswrapper-modules-1.9"

We need to commit our changes in the git repository.
Code:
git add .
git commit -a -m "i7 Modifications"

The text after -m is the message you add to your commit.
Compilation

It’s finally time for compiling, to keep our newly created branch in pristine condition we will do the compilation in a separate branch. We keep our branch clean as this will help later on when we want to update our new branch to a newer kernel.
Code:
git checkout -b work
fakeroot debian/rules clean
All the packages will be created in the directory /d1/development/kernel/ubuntu/oneiric
Create independent packages:
Code:
skipabi=true skipmodule=true fakeroot debian/rules binary-indep

The above statement will create the following deb files:
Code:
linux-doc_3.0.0-13.21_all.deb
linux-headers-3.0.0-13_3.0.0-13.21_all.deb
linux-source-3.0.0_3.0.0-13.21_all.deb
linux-tools-common_3.0.0-13.21_all.deb

Create the tools package:
Code:
skipabi=true skipmodule=true fakeroot debian/rules binary-perarch

The above statement will create the following deb file:
Code:
linux-tools-3.0.0-13_3.0.0-13.21_amd64.deb

Create the flavour depended files:
Code:
skipabi=true skipmodule=true fakeroot debian/rules binary-i7

The above statement will create the following deb files:
Code:
linux-headers-3.0.0-13-i7_3.0.0-13.21_amd64.deb
linux-image-3.0.0-13-i7_3.0.0-13.21_amd64.deb
Installation

After the compilation is finished we’ll have the above packages in the parent directory.

To install the files
Code:
cd ..
sudo dpkg -i linux-headers-3.0.0-13-i7_3.0.0-13.21_amd64.deb linux-headers-3.0.0-13_3.0.0-13.21_all.deb linux-image-3.0.0-13-i7_3.0.0-13.21_amd64.deb
Check your bootloader if the newly installed Ubuntu kernel is the default one, for grub check the file /boot/grub/menu.lst or if you run grub2 check /boot/grub/grub.cfg
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 17
    This is a general open source linux development thread!

    Android's kernel is a derivative of linux's kernel. Its good to know how to build both of these kernels. You might be already familiar with building kernels for various devices from sources. So I have made a new thread for guiding people on how to compile linux kernel from source (example taken as ubuntu kernel).

    Requirements:
    • Any linux os x64 bit(example here: ubuntu 14.04)
    • Git (sudo apt-get install git)
    • Minimum of 4GB RAM and some reasonable linux-swap

    To get the currently running kernel image, type the following:
    Code:
    apt-get source linux-image-$(uname -r)

    Now we need to obtain Ubuntu Kernel Sources from its repositories. Make a new directory and inside it, initialise the git and clone the repository.
    Code:
    git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release>.git
    <release> : Type in the required source. It can be lucid, precise, trusty, utopic etc.

    Setting up the build environment. There are lots of tools and packages that are very much essential for building a kernel. These tools can be downloaded as a whole bundle and can be installed easily. Here's the code to set it up:
    Code:
    sudo apt-get build-dep linux-image-$(uname -r)

    NOTE: The above comand can be executed only after you obtain the currently running kernel image. I have already given the code to obtain it above.

    Now, change directory to the root of the kernel and type the following:
    Code:
    chmod -R a+x *
    The above code will set the required permissions for building and executing the kernel.

    Now, run these two commands:
    Code:
    fakeroot debian/rules clean
    fakeroot debian/rules editconfigs

    The first command cleans up the code automatically.
    The slightly tricky part is with the second line of the code. When you execute it, you will have to edit a series of menuconfigs. To make changes to the configuration file we need to edit the configuration file. The kernel developers have created a script to edit kernel configurations which has to be called through the debian/rules makefile, unfortunately you will have to go through all the flavors for this script to work properly. The script will ask you if you want to edit the particular configuration. You should not make changes to any of the configurations until you see your wanted flavour configuration
    We have now covered about 70% of progress. The rest is building the kernel and testing it.

    Building the kernel is quite easy. Change your working directory to the root of the kernel source tree and then type the following commands:
    Code:
    fakeroot debian/rules clean
    fakeroot debian/rules binary-headers binary-generic
    If the build is successful, a set of three .deb binary package files will be produced in the directory above the build root directory. For example after building a kernel with version "3.13.-0.35" on an amd64 system, these three .deb packages would be produced:
    Code:
    cd ..
    ls *.deb
    linux-headers-3.13.0-35_3.13.0-35.37_all.deb
    linux-headers-3.13.0-35-generic_3.13.0-35.37_amd64.deb
    linux-image-3.13.0-35-generic_3.13.0-35.37_amd64.deb
    Testing the new kernel
    Install the three-package set (on your build system, or on a different target system) with dpkg -i and then reboot:
    Code:
    sudo dpkg -i linux*3.13.0-35.37*.deb
    sudo reboot
    Guys, I hope I have made an easy tutorial. You are always welcome to ask doubts (even on PM). Thank You.
    12
    Specific Hardware/Architecture

    Creating a new config

    I’ll be using the method of creating a new flavour, this adds a bit more work but this way you can always compile the original kernels.

    We’ll use the generic flavour as the base for our own flavour being i7, this extension needs to be in small caps.
    Code:
    cp debian.master/config/amd64/config.flavour.generic debian.master/config/amd64/config.flavour.i7
    fakeroot debian/rules clean
    debian/rules updateconfigs

    To make changes to the configuration file we need to edit the configuration file. The kernel developers have created a script to edit kernel configurations which has to be called through the debian/rules makefile, unfortunately you will have to go through all the flavours for this script to work properly.
    Code:
    debian/rules editconfigs

    The script will ask you if you want to edit the particular configuration. You should not make changes to any of the configurations until you see the i7 configuration
    Code:
    Do you want to edit config: amd64/config.flavour.i7? [Y/n]

    Make your changes, save the configuration and then keep going until the script ends.

    When you’re done, make a backup of the config flavor file.
    Code:
    cp debian.master/config/amd64/config.flavour.i7 ../.
    Now we need to clean up the git tree in order to get ready for compilation.
    Code:
    git reset --hard
    git clean -df
    Getting ready for compilation

    Because we are going to be creating a new flavour based on a existing flavour (generic in my case) we need to create some extra files. During compilation the process checks the previous release for some settings, as we’re creating a local flavour it doesn’t exist in the source, so we’re creating it.

    To see the previous release we use:
    Code:
    ls debian.master/abi
    
    cp debian.master/abi/3.0.0-12.20/amd64/generic debian.master/abi/3.0.0-12.20/amd64/i7
    cp debian.master/abi/3.0.0-12.20/amd64/generic.modules debian.master/abi/3.0.0-12.20/amd64/i7.modules

    Copy our flavored configuration file back.
    Code:
    cp ../config.flavour.i7 debian.master/config/amd64/

    We need to edit some files:
    File: debian.master/etc/getabis

    Search for the line:
    Code:
    getall amd64 generic server virtual

    Change it in:
    Code:
    getall amd64 generic server virtual i7

    File: debian.master/rules.d/amd64.mk

    Search for the line:
    Code:
    flavours        = generic server virtual

    Change it in:
    Code:
    flavours        = generic server virtual i7

    File: debian.master/control.d/vars.i7

    This files does not exist and in order to make the compilation process aware of our own flavor we want to compile we need to create it.
    Code:
    cp debian.master/control.d/vars.generic debian.master/control.d/vars.i7

    You can edit the file and make it your own.
    Code:
    arch="i386 amd64"
    supported="i7 Processor"
    target="Geared toward i7 desktop systems."
    desc="x86/x86_64"
    bootloader="grub-pc | grub-efi-amd64 | grub-efi-ia32 | grub | lilo (>= 19.1)"
    provides="kvm-api-4, redhat-cluster-modules, ivtv-modules, ndiswrapper-modules-1.9"

    We need to commit our changes in the git repository.
    Code:
    git add .
    git commit -a -m "i7 Modifications"

    The text after -m is the message you add to your commit.
    Compilation

    It’s finally time for compiling, to keep our newly created branch in pristine condition we will do the compilation in a separate branch. We keep our branch clean as this will help later on when we want to update our new branch to a newer kernel.
    Code:
    git checkout -b work
    fakeroot debian/rules clean
    All the packages will be created in the directory /d1/development/kernel/ubuntu/oneiric
    Create independent packages:
    Code:
    skipabi=true skipmodule=true fakeroot debian/rules binary-indep

    The above statement will create the following deb files:
    Code:
    linux-doc_3.0.0-13.21_all.deb
    linux-headers-3.0.0-13_3.0.0-13.21_all.deb
    linux-source-3.0.0_3.0.0-13.21_all.deb
    linux-tools-common_3.0.0-13.21_all.deb

    Create the tools package:
    Code:
    skipabi=true skipmodule=true fakeroot debian/rules binary-perarch

    The above statement will create the following deb file:
    Code:
    linux-tools-3.0.0-13_3.0.0-13.21_amd64.deb

    Create the flavour depended files:
    Code:
    skipabi=true skipmodule=true fakeroot debian/rules binary-i7

    The above statement will create the following deb files:
    Code:
    linux-headers-3.0.0-13-i7_3.0.0-13.21_amd64.deb
    linux-image-3.0.0-13-i7_3.0.0-13.21_amd64.deb
    Installation

    After the compilation is finished we’ll have the above packages in the parent directory.

    To install the files
    Code:
    cd ..
    sudo dpkg -i linux-headers-3.0.0-13-i7_3.0.0-13.21_amd64.deb linux-headers-3.0.0-13_3.0.0-13.21_all.deb linux-image-3.0.0-13-i7_3.0.0-13.21_amd64.deb
    Check your bootloader if the newly installed Ubuntu kernel is the default one, for grub check the file /boot/grub/menu.lst or if you run grub2 check /boot/grub/grub.cfg
    2
    nice job mate..!! :good:
    now i'm gonna try this..!! :eek:
    2
    Nice :eek: ,i can't say anything :eek:

    Thank you for the great guide!!!
    1
    thx for your info