Debian Kernel Compilation – 2 ways – make-kpkg (new) and make (old)

KERNEL COMPILATION IN DEBIAN
##############################
##############################

There now exists 2 ways to compile thine kernel.
Thine old way thine finest of ways
Thine new way makes thine deb file.

Im not funny… The end…

“I hope this will be informative for you and thank you for watching”

#################################################
FOR 3.x KERNELS – MAKES DEB FILES WITH MAKE-KPKG
#################################################

The current way to compile the kernel makes a deb file in debian/ubuntu. The advantage with the newer method is that the kernel is compiled into 2 .deb files that can easily be installed. And also deleted/removed/uninstalled with dpkg or apt-get remove.

http://www.howtoforge.com/kernel_compilation_debian_etch

http://www.tecmint.com/kernel-compilation-in-debian-linux/

http://syslint.com/syslint/how-to-compile-linux-kernel-in-debian-ubuntu-the-easy-way/

# NOTE YOU DONT HAVE TO BE ROOT, BUT I USUALLY AM
sudo -i
# OR "su - root" OR "su -"  OR" su"

# need build tools
apt-get install build-essential
# Need these tools for kpkg (fakeroot emulates root access and doesnt harm system, but perfect for kernel compilation - kernel-package has make-kpkg):
apt-get install fakeroot kernel-package
# need this tool for ncurses (menus in command line)
apt-get install libncurses5-dev
# Lets do everything from this directory
mkdir /root/kernel-compiling
cd /root/kernel-compiling
# Check on kernel.org for latest kernel and download it with wget
wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.12.tar.xz
# extract with "tar -xf linux*" or you can be more specific (in the end if doesnt matter how you extract it)
tar -xvJf linux-3.12.tar.xz
# It makes the linux folder go into it and thats where you will compile it
cd linux-3.12
# Type "uname -r" and see your current config file (copy the current used config file from /boot to current directory but name it as .config in current dir)
cp /boot/config-`uname –r`.config

# Now lets edit the kernel settings (what modules you want installed - this is a whole article of itself on knowing what to select)
# easiest method is to select what you currently have (thats why we copied .config here)
# What to do in menu config:
# Open menuconfig below, then it will open up a CLI GUI (with ncurses library - in case you were curious) then select the LOAD button and hit enter twice (it will open up .config file)
# Now select the SAVE button and it will properly save the .config file (and make any minor adjustments that might exist between the old .config file and current versions configuration in .config)
# Now select EXIT button
# So simply its LOAD->SAVE->EXIT
make menuconfig

# This prepares the directory (cleans garbage up)
make-kpkg clean

# Now to make the compilation speed or else it could take hours, lets utilize our many cores.
# By default CONCURRENCY_LEVEL is not set so it will use 1 core of CPU
# run "nproc" or "cat /proc/cpuinfo"  or "lsusb" and count your cores
# Set the level like so: CONCURRENCY_LEVEL=number of cores + 1
# If you have 2 cores put CONCURRENCY_LEVEL=3, if you have 4 cores then put a level of 5. Just add 1
export CONCURRENCY_LEVEL=3
# NOTE: Setting CONCURRENCY_LEVEL is optional its just so that compilation isnt as long

# Now run this command to generate the two .deb files of your finished and compiled kernel
# NOTE: change "-customerkernel" to "-whatever" just dont forget the dash in the front as it appears as a seperator
# NOTE: fakeroot is like "su" or "sudo" but you dont need to know the root password - it provide an emulated root environment for your commands. it makes a temporary fake root environment for the program ran (make-kpkg compiling kernel) in this case, so that it gets all of the needed things a root system would need (in an emulated fashion, that works). Its good if your a dev and not a root user and need to compile the kernel (compilation requires root level privs, so this takes care of it)
fakeroot make-kpkg --append-to-version "-customkernel" --revision "1" --initrd kernel_image kernel_headers

# SIDENOTE: this process might take up alot of space. My linux-#.# folder grew to 12GiB with linux-4.2-rc6 & a regular linuxmint config file. Also the final deb files were 7.1 MiB for the headers, 'linux-headers-4.2.0-rc6-infotinks_1_amd64.deb', and 48 MiB for the kernel, 'linux-image-4.2.0-rc6-infotinks_1_amd64.deb'.

# Now install them, first install the linux-image and then the linux-headers (since we are using dpkg, you can use apt-get remove or dpkgs to remove the kernels later). Note that it dumps the output deb files in the parent folder. Also you need to be root to install the kernel of course:
cd ..
dpkg -i linux-image-3.12.0-customkernel_1_i386.deb linux-headers-3.12.0-customkernel_1_i386.deb
# NOTE: first install the kernel image, then install the headers

# Run "update-grub" just in case (it should be run automatically when you installed the linux-image, the kernel image), it looks thru your /boot folder for legit kernels (that have the image, and initramfs) and then it makes a grub entry for each one for you. Grub is the screen before booting that lets you pick which OS you want to load, or which kernel version of the OS (if you have several kernel versions you can pick which kernel there). Usually you can enter grub by hitting ESCAPE when the computer boots up, right after it POSTs.
update-grub

# thats it should of updated grub and everything, test out new kernel with a reboot "sudo shutdown -r now" or "reboot -f"
reboot -f

# IF PROBLEM: If there is a problem with the new kernel, reboot the system and boot into the older kernel (press ESCAPE until grub comes up and select the older kernel normal or recovery boot - it doesnt matter). Then remove the problem kernel and tackle google to see where the problem was (sometimes you will see the errors during the compilation, or even during your installs with "dpkg -i". It might hint at some errors like certain modules that didnt compile well & point you at the logs that you need to see about.)
# Here is an example of how you remove a bad kernel, after you boot into another kernel:
# dpkg -l | egrep -i "linux-image|linux-headers"
# dpkg --remove linux-headers-3.12.0-customekernel_1_i386.deb
# dpkg --remove linux-image-3.12.0-customkernel_1_i386.deb
# update-grub
# NOTE: first uninstall the headers, then the kernel image. Backwards order of installation.

# DONT FORGET TO CLEAN UP compilation directory when your done (that things could be many gigs):
rm -rf /root/kernel-compiling

 

###########################
FOR KERNEL 2.6 – WITH MAKE
############################

The older ways of kernel building essentially you compiled up the kernel and everything in boot. This method works with any system if you know what your doing so not just debian.

http://www.cyberciti.biz/tips/compiling-linux-kernel-26.html
http://voices.yahoo.com/compiling-linux-26-kernel-scratch-340428.html
http://www.linuxforums.org/forum/kernel/55612-mini-howto-compile-linux-kernel-2-6-a.html

# lets download the kernel to /tmp it will only temporarily be there
cd /tmp
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-x.y.z.tar.bz2
# extract it to /usr/src (you will have a linux folder there with latest kernel
tar -xjvf linux-2.6.25.tar.bz2 -C /usr/src
# go into that folder that you just extracted (where x,y,z is a number of the kernel version)
cd /usr/src/linux-x.y.z
# make sure you have the right tools to compile
apt-get install build-essential
apt-get install gcc
apt-get install libncurses5-dev

# Select one of the following ways to select your options
make menuconfig # Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
make xconfig # X windows (Qt) based configuration tool, works best under KDE desktop
make gconfig # X windows (Gtk) based configuration tool, works best under Gnome Dekstop.

# Start compiling to create a compressed kernel image, enter:
make
# Start compiling to kernel modules:
make modules
# Install kernel modules (become a root user, use su command):
su -
make modules_install

# To install to boot System.map, config and vmlinuz & grub:
make install
# Or manually install it (copy files from compile directory to /boot):
cp System.map /boot/System.map-x.y.z-comment
cp .config /boot/config-x.y.z-comment
cp arch/x86/boot/bzImage /boot/vmlinuz-x.y.z-comment
# note those 3 files need to match in the x.y.z-comment part (x.y.z refers to the version of kernel, comment can be anything like the word "custom" or "whocares")

# Note for next section realize that the top 3 files and the bottom initrd have to match on the x.y.z-comment part (for update-grub program to work)
# System.map-x.y.z-comment
# config-x.y.z-comment
# vmlinuz-x.y.z-comment
# initrd-x.y.z-comment

# If 2.x Kernel:
# Lets make the bootup environment initrd:
cd /boot
mkinitrd -o initrd.img-2.6.25 2.6.25
# Initrd images contains device driver which needed to load rest of the operating system later on. Not all computer requires initrd, but it is safe to create one.
# Make sure to rename the file to "initrd.img-x.y.z-comment"
# mv /boot/initrd.img-x.y.z /boot/initrd.img-x.y.z-comment

# If 3.x Kernel:
update-initramfs -c -k x.y.z
# Where x.y.z is the version 
# ex: update-initramfs -c -k 3.14.2

# Updating Grub time
# First back it up
cp /boot/grub/grub.cfg /boot/grubbackup
# or if you have the older grub
cp /boot/grub/menu.lst /boot/grubbackup
# Run update-grub to update grub based on whats in /boot folder (it does so automatically)
# update-grub will look for all of the matching System.map/vmlinuz/config/initrd files with the same x.y.z-comment suffix and make a grub boot menu entry out of it
update-grub
# Or you can manually fix grub up like so:
vi /boot/grub/grub.cfg
# note if its readonly 
# chmod +w /boot/grub/grub.cfg
# then when done editting
# chmod -w /boot/grub/grub.cfg
vi /boot/grub/menu.lst

# ----GRUB 1.0: /boot/grub/menu.lst-----
# example of old grub /boot/grub/menu.lst -  newer grub is much more complicated and its simpler to just run "update-grub"
title Debian GNU/Linux, kernel 2.6.25 Default
root (hd0,0)
kernel /boot/vmlinuz root=/dev/hdb1 ro
initrd /boot/initrd.img-2.6.25
savedefault
boot

# Remember to setup correct root=/dev/hdXX device. Save and close the file. If you think editing and writing all lines by hand is too much for you, try out "update-grub" command to update the lines for each kernel in /boot/grub/menu.lst file. Just type the command "update-grub"

# ----GRUB 2.0: /boot/grub/grub.cfg-----
# example of new grub - update-grub usually generates 2 entries one for normal boot and one for recovery mode boot (note im using systemd isntead of sysvinit service manager so my init option is init=/lib/systemd/systemd - normally that init=/lib/systemd/systemd entry is missing so it launches /sbin/init, as if it said init=/sbin/init, but thats implied so you dont need it - you can configure the default init system by editing the file /etc/default/grub and running 'update-grub' instead of manually editting the complex /boot/grub/grub.cfg file - 'update-grub' scrapes thru all of the settings in /etc/default/grub and adds them to the boot-menu entries it finds for the matching System.map/vmlinuz/config/initrd files with the same x.y.z-comment suffix ):

 menuentry 'Debian GNU/Linux, with Linux 3.14.2-kostia' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.2-kostia-advanced-2888bf67-05fd-4ee0-8843-9de2b8b30803' {
                load_video
                insmod gzio
                insmod part_msdos
                insmod ext2
                set root='hd0,msdos1'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  2888bf67-05fd-4ee0-8843-9de2b8b30803
                else
                  search --no-floppy --fs-uuid --set=root 2888bf67-05fd-4ee0-8843-9de2b8b30803
                fi
                echo    'Loading Linux 3.14.2-kostia ...'
                linux   /boot/vmlinuz-3.14.2-kostia root=UUID=2888bf67-05fd-4ee0-8843-9de2b8b30803 ro   init=/lib/systemd/systemd
                echo    'Loading initial ramdisk ...'
                initrd  /boot/initrd.img-3.14.2-kostia
        }
        menuentry 'Debian GNU/Linux, with Linux 3.14.2-kostia (recovery mode)' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.14.2-kostia-recovery-2888bf67-05fd-4ee0-8843-9de2b8b30803' {
                load_video
                insmod gzio
                insmod part_msdos
                insmod ext2
                set root='hd0,msdos1'
                if [ x$feature_platform_search_hint = xy ]; then
                  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  2888bf67-05fd-4ee0-8843-9de2b8b30803
                else
                  search --no-floppy --fs-uuid --set=root 2888bf67-05fd-4ee0-8843-9de2b8b30803
                fi
                echo    'Loading Linux 3.14.2-kostia ...'
                linux   /boot/vmlinuz-3.14.2-kostia root=UUID=2888bf67-05fd-4ee0-8843-9de2b8b30803 ro single
                echo    'Loading initial ramdisk ...'
                initrd  /boot/initrd.img-3.14.2-kostia
        }


# After grub
# When done just reboot and watch the console of the output (if you were sshed in switch over to physical access on the PC or view the PC from console if you were using a VM setup)
shutdown -r now
# or
reboot

# The only thing you need to clean up is the file /tmp (your tmp should be empty upon reboot if you used tmpfs there)
# Do not erase the kernel sources from /usr/src as they are used by the system

 

Leave a Reply

Your email address will not be published. Required fields are marked *