Compiling 2.6.21 Kernel Modules from Source for Amazon EC2

Though it is not possible to compile and run your own kernels on Amazon EC2, it is possible to compile and run your own kernel modules and bundle them with your EC2 images. The trick is that the kernel modules need to be compiled with exactly the same kernel source and compiler versions as were used to build the original kernel.

The steps below were used to build the updated 2.6.21 kernel modules for the Ubuntu and Debian AMIs listed at https://alestic.com

Run an instance of the Fedora 8 AMI from Amazon:

  • 32-bit: ami-f51aff9c
  • 64-bit: ami-f21aff9b

Upgrade and install required packages:

yum upgrade -y
yum install -y rpmdevtools sparse

Download kernel source, patch, and configure:

arch=x86_64  # for 64-bit (large, xlarge)
arch=i686    # for 32-bit (small, medium)

curl -LO \
  http://kojipkgs.fedoraproject.org/packages/kernel-xen-2.6/2.6.21.7/2.fc8/src/kernel-xen-2.6-2.6.21.7-2.fc8.src.rpm

rpm -ivh kernel-xen-2.6-2.6.21.7-2.fc8.src.rpm 2>&1 |
  egrep -v '(user|group) mockbuild does not exist'

cd /usr/src/redhat/SPECS/

rpmbuild -bp --target=$arch kernel.spec

mv /usr/src/redhat/BUILD/kernel-2.6.21/linux-2.6.21.$arch \
   /usr/src/linux-$(uname -r)

cd /usr/src/linux-$(uname -r)/
mv .config .config.orig
cp /boot/config-$(uname -r) .config
echo "-2.fc8xen" > localversion-ec2

Build and install the kernel modules:

time make modules # 70 minutes

make INSTALL_MOD_STRIP=1 modules_install

Download, build, and install the latest fuse kernel module source:

curl -L -O http://downloads.sourceforge.net/fuse/fuse-2.7.3.tar.gz
tar xvzf fuse-2.7.3.tar.gz
cd fuse-2.7.3
./configure --enable-kernel-module
make
make INSTALL_MOD_STRIP=1 install

Package up the new kernel modules:

tar cvzf /tmp/ec2-kernel-modules-2.6.21.$arch.tar.gz \
  /lib/modules/2.6.21.7-2.fc8xen/kernel/*

Copy the kernel modules to where you are building your new AMI instance and unpack:

cd /
tar xvf /tmp/ec2-kernel-modules-2.6.21.$arch.tar.gz
depmod -a

If you are using the ec2ubuntu-build-ami script, you can specify the URL of the kernel modules with the --modules option

Article originally published here: http://groups.google.com/group/ec2ubuntu/web/compiling-2-6-21-kernel-modules-from-source-for-amazon-ec2