Linux Kernel

From Blue-IT.org Wiki

Ubuntu

Kernel updates (max 2 kernels)

Thanks to zippo in an article about automate kernel management, I found this very, very good script for keeping an exact number of acutal kernels. The script takes the number of kernel as a parameter or you can specify it in the scirpt. If nothing is given, it defaults to "2". Thanks for this script.

I put it into /etc/rc.local on every machine I am using (be careful!):


NEEDS TESTING - USE WITH CARE IN PRODUCTIVE ENVIRONMENT !!! --Apos (talk) 09:46, 18 September 2015 (CEST)

#!/bin/bash
#
#       Script zur Reduzierung der installierten Linux-Kernel und -headers
#       auf eine vorgebbare Anzahl
#
#	Datei: 	/opt/kh-manager
#	Version: 1.0
#	Autor: 	Zippo
#	Datum: 	18. 6. 2013
#
#       Übergabeparameter: Anzahl der im System verbleibenden Kernel- und Header-Versionen
#
#       Achtung: Dieses Script benötigt root-Rechte bei der Ausführung!
#
################################################################################
#
#       Überprüfung des Übergabeparameters
#
if [ "$#" -lt "1" ] ; then
#       Fall wenn kein Übergabeparameter vorhanden ist:
#       Anzahl der Kernel und Header = 2
  maxkh=2
else
  maxkh=$1
#
  if [ "$maxkh" -lt "1" ] ; then
#       Fall wenn weniger als 1 Kernel im System verbleiben soll:
#       Anzahl der Kernel und Header = 1
    maxkh=1
  fi
fi
#
################################################################################
#
#       Löschung der überzähligen Header-Versionen
#
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | awk -v maxkh=$maxkh '{if(index($0,"linux-headers")>0){imax=imax+1;hname[imax]=$2}}END{for(i=1;i<=imax-2*maxkh-1;i++){print hname[i]}}' | xargs apt-get -y purge
 
#
#       Löschung der überzähligen Kernel-Versionen
#
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | awk -v maxkh=$maxkh '{if(index($0,"linux-image")>0){imax=imax+1;kname[imax]=$2}}END{for(i=1;i<=imax-maxkh-1;i++){print kname[i]}}' | xargs apt-get -y purge

update-grub

Delete all old kernels

#!/bin/bash
[ $UID==0 ] || exit 1
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
update-grub2

Debugging

sudo apt-get install lttng-tools lttng-modules-dkms

installs:

babeltrace libbabeltrace-ctf0 libbabeltrace0 liblttng-ctl0 liblttng-ust-ctl2 liburcu1 lttng-modules-dkms lttng-tools

Links

Get the kernel

apt-get install linux-source

Alternatively you can get your own kernel from kernel org

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.YOUR-KERNEL-VERSION.tar.bz2
tar xjvf linux-2.6.YOUR-KERNEL-VERSION.tar.bz2
ln -s linux-linux-source-2.6.22 linux

Install additional packages

apt-get update
apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 
# apt-get install libncurses5 cpp make build-essential

apply patches

cd /usr/src
wget http://www.kernel.org/pub/linux/kernel/v2.6/testing/patch-2.6.19-rc4.bz2
cd /usr/src/linux
bzip2 -dc /usr/src/patch-2.6.19-rc4.bz2 | patch -p1 --dry-run
bzip2 -dc /usr/src/patch-2.6.19-rc4.bz2 | patch -p1

Configure the kernel

cd /usr/src/linux
cp /boot/config-`uname -r` .config
make menuconfig

Build the custom kernel, headers and modules

Now we are ready to compile the kernel_image and kernel_headers:

make-kpkg clean
fakeroot make-kpkg --initrd \
--append-to-version=-nfs3-with-acl-support \
kernel_image kernel_headers [modules_image]**
    • If you downloaded source files for modules (e.g. nvidia, lirc etc.) which are located in /usr/src/modules, then you should add modules_image to the line above.

Install the custom kernel

cd /usr/src
dpkg -i linux-image-2.6*.deb
dpkg -i linux-headers-2.6*.deb

Check grub & reboot

vim /boot/grub/menu.lst

Build custom modules afterwards

Download the source packages, e.g.

apt-get install nvidia-legacy-kernel-source

Go to /usr/src and unpack it.

tar xzvf nvidia-legacy-kernel-source.tar.gz

Go tho the kernel directory and build the module image. Be aware that you use the same "append-to-version" information like for the custom kernel you compiled before. E.g.

cd /usr/src/linux
fakeroot make-kpkg --append_to_version=-nfs3-with-acl-support modules_image

You can also easily do

# this will configure/check the build system
m-a prepare 
# this will install your driver
m-a a-i your_driver

Module assistant

A very good tool for managing source installations is the module-assistant - a relict from the debian roots of ubuntu.

apt-get install module-assistant

Recompile a single Kernel module

This part assumes correct installed kernel-sources!

Edit Makefile

Copy the config

cp /boot/config-`uname -r` /usr/src/linux/.config

Edit the toplevel kernel Makefile and edit the correct version according to your current kernel (uname -r):

vim /usr/src/linux/Makefile
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 24
EXTRAVERSION = -2-generic

Prepare the kernel configuration

Edit the config file and change what you need to change with menuconfig

make menuconfig

Prepare the kernel

make prepare
make scripts

Compile the module(s)

Now you only need to know, where your module(s) are. The pattern is always the same:

make modules M=directory/to_modules

That's all ;)

The modules option is important. It triggers the creation of the right Module.symvers file. In some howto's you'll miss this resulting in a not usable kernel module!

If you for example want to recompile the nfs-server module, just do

make modules M=fs/nfsd

At the end you'll be prompted with a message like this

MODPOST 1 module
LD [M] fs/nfsd/nfsd.ko

Copy this module (.ko) to the according modules directory.

Here's a script if you like to compile more than one module

#!/bin/sh
#
MOD_PATH=/lib/modules/`uname -r`/kernel

for MYMODDIR in "fs/nfs" "fs/nfs" "fs/exportfs" "fs/lockd" "net/sunrpc"
do
  make modules M="$MYMODDIR"
 cp "$MYMODDIR"/*.ko "$MOD_PATH"/"$MYMODDIR"/.
done

# Do here additional work
for MYMODDIR in "net/sunrpc/auth_gss" "net/sunrpc/xprtrdma"
do
   cp "$MYMODDIR"/*.ko "$MOD_PATH"/"$MYMODDIR"/.
done

make modules M=fs/nfs_common
cp fs/nfs_common/*.ko "$MOD_PATH"/fs/nfs/.