Compile and Install Kernel.org Kernel to Custom Volume

compilingkernellinux

I want to compile and install a kernel.org kernel on a custom HDD volume, say /dev/sda5, instead of being merged with my current Ubuntu's directories.

I can find information about configuration and compilation process all over the web, but there's no track of how to put the kernel on a custom volume (different than the booted distro you're using at the moment of compile). What I'm asking for is like how we can install 2 different distros on 2 different volumes on 1 HDD, now think of my custom kernel as another distro.

Best Answer

You can compile a kernel anywhere you like, including your home directory. The only time directories outside the build tree are modified is when you make one of the install* targets. So, to install a kernel you'd do the obvious:

cd $SOME_DIRECTORY
tar -xjvf linux-$VERSION.tar.bz2
cd linux-$VERSION
make mrproper menuconfig
# Configure the kernel here.
# Now build it using all your CPU threads in parallel.
make -j$(grep -c processor /proc/cpuinfo) bzImage modules

After you configure the kernel, it'll be built. At this point, you'll have a kernel binary (vmlinux) and a bootable kernel image under arch/$YOUR_ARCHITECTURE/boot/bzImage.

If you're building a monolithic kernel, you're done. Copy the uncompressed file (vmlinux) or compressed file (bzImage) to your intended volume, configure the boot manager if you need to, and off you go.

If you need to install modules, and assuming you've mounted your target volume on /mnt, you could say:

INSTALL_MOD_PATH=/mnt \
INSTALL_PATH=/mnt/boot \
make modules_install

This will copy the kernel image to /mnt/boot and the modules to /mnt/lib/modules/$VERSION.

Please note, I'm oversimplifying this. If you need help building the kernel manually, you should read some of the documents in the kernel source tree's Documentation/ subdirectory. The README file also tells you how to build and install it in detail.

Booting the kernel is a different story, though. Most modern distributions use a initial RAMdisk image which contains a ton of drivers for the hardware needed to bring up the rest of the kernel (block devices, filesystems, networking, etc). This process won't make this image. Depending on what you need to do (what do you need to do?), you can use an existing one or make a new one using your distribution's toolchain. You should check the documentation on update-initramfs.

There are other issues too, though. Using the standard toolchain you can't compile a kernel for a different architecture or sub-architecture. Note that in some cases, even kernels compiled on a particular type of x86 box won't work on certain other types of x86 boxes. It all depends on the combination of sub-architectures and the kernel config. Compiling across architectures (e.g. building an ARM kernel on an x86 machine) is altogether out of the question unless you install an appropriate cross-compilation toolchain.

If you're trying to rescue another installation or computer, however, a rescue disk might come in handier than building a custom kernel like that.

One more thing: if you're trying to build a kernel for another computer which boots, is the same architecture as the one you're compiling on, and runs a Debian or Debian-like OS (Ubuntu counts), you could install the kernel-package package (sudo aptitude install kernel-package). Then unpack the kernel, cd to the root of the source tree, and say:

CONCURRENCY_LEVEL=$(grep -c processor /proc/cpuinfo) \
sudo make-kpkg --initrd binary-arch

This will apply necessary patches, configure the kernel, build it and package it as a .deb package (a few packages, actually). All you need to do is install it on your target system and you're done.

Related Question