C · How To

Building and Installing a Kernel

This post is about how to install a new kernel from source.

The latest source code for the Linux kernel is kept on kernel.org. You can either download the full source code as a tar ball (not recommended and will take forever to download), or you can check out the code from the read-only git repositories.

First let’s install some packages using:


sudo apt-get install vim libncurses5-dev gcc make git exuberant-ctags

I was working on the staging-next, so this post will take that as an example.


git clone -b staging-next git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging.git

Basically above command clones staging-next branch of Greg’s tree. Obviously as the size is big so this clone is going to take some time.

Now let’s set up the configuration.

When you download the source tree, it doesn’t come with a .config file. You have several options on generating a .config file. The easiest is to duplicate your current config.

Duplicate your current config using:


cp /boot/config-`uname -r`* .config

If you want to make some change in your configuration, run the following command, to to silently update any new configuration values to their default:


make olddefconfig

If you want to make changes, you can run


make menuconfig

This will popup something like this:

make menuconfig

If you simply want to use the default just save and exit out of the above menu.

Now let’s build the kernel:

Run


make

or if you have a multi-core processor (run nproc), run


make -jX

Now let’s install new kernel

Save the following script in a file where your code is present.


echo " " &&
echo "This script will attempt to install Linux Kernel 3.9 on this machine." &&
echo "Typically, your current version will be kept, and you will be able to ustilise it again later if Kernel 3.9 does not work." &&
echo " " &&
read -p "Press Enter to continue, or abort by pressing CTRL+C" nothing &&
echo " " &&
echo "Downloading Kernel 3.9 Packages" &&
echo "3 Files, 55 MB to Download" &&
echo " " &&
echo "Creating Kernel Directory in Home folder" &&
echo " " &&
mkdir -p $HOME/kernel-htu &&
cd $HOME/kernel-htu &&
echo " " &&
echo "Downloading File 1 of 3, 12 MB" &&
echo " " &&
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.9-raring/linux-headers-3.9.0-030900_3.9.0-030900.201304291257_all.deb &&
echo " " &&
if [ $(getconf LONG_BIT) = "64" ]
then
echo "64bit Detected" &&
echo " " &&
echo "Downloading File 2 of 3, 1 MB" &&
echo " " &&
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.9-raring/linux-headers-3.9.0-030900-generic_3.9.0-030900.201304291257_i386.deb &&
echo " " &&
echo "Downloading File 3 of 3, 12 MB" &&
echo " " &&
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.9-raring/linux-image-3.9.0-030900-generic_3.9.0-030900.201304291257_i386.deb
else
echo "32bit Detected" &&
echo " " &&
echo "Downloading File 2 of 3, 1 MB" &&
echo " " &&
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.9-raring/linux-headers-3.9.0-030900-generic_3.9.0-030900.201304291257_amd64.deb &&
echo " " &&
echo "Downloading File 3 of 3, 12 MB" &&
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.9-raring/linux-image-3.9.0-030900-generic_3.9.0-030900.201304291257_amd64.deb
fi &&
echo " " &&
echo "Installing Kernel" &&
echo "This step will require you password." &&
echo "This is the last step you can safely cancel at." &&
echo "Use Ctrl+C to cancel." &&
echo " " &&
sudo dpkg -i *.deb &&
echo " " &&
echo "Installation Complete" &&
echo " " &&
read -p "Press Enter to Delete the Downloads, or CTRL+C to keep them." nothing &&
echo " " &&
sudo rm -rf $HOME/kernel-htu

Set execute permissions:


sudo chmod +x kernelinstall.sh

Execute the script :


sudo ./kernelinstall.sh

Running your kernel

You need the grub menu at the time of boot, to make it always appear at the time of boot, do the following


#Open grub file using vim, you can use any editor

vim /etc/default/grub

#Either comment or remove the following line

GRUB_HIDDEN_TIMEOUT_QUIET

GRUB_HIDDEN_TIMEOUT=0

#Change GRUB_TIMEOUT to something > 0

This portion of my grub file looks something like this:


GRUB_DEFAULT=0

#GRUB_HIDDEN_TIMEOUT=0

#GRUB_HIDDEN_TIMEOUT_QUIET=true

GRUB_TIMEOUT=10 #how long grub shows the menu before it choses the kernel at the top of the list

Now after editing the file, update it using


sudo update-grub2

Now you are done. You just need to reboot.


sudo reboot

load the kernel which you installed.

Advertisements

9 thoughts on “Building and Installing a Kernel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s