Skip to content

Instantly share code, notes, and snippets.

@denis2glez
Last active November 20, 2021 13:14
Show Gist options
  • Save denis2glez/38bc66697ecf7e8b2674d2f3019f3997 to your computer and use it in GitHub Desktop.
Save denis2glez/38bc66697ecf7e8b2674d2f3019f3997 to your computer and use it in GitHub Desktop.
Build and test a Linux kernel

Build and test a Linux kernel

This method of compiling the kernel is the traditional method common to all distributions. Although Arch Linux is used this time, you can use the equivalent tools in your distribution.

Prerequisites

Install the base-devel package group which contains necessary packages such as make and gcc, plus the arch-install-scripts and qemu packages needed to create and test a minimal system.

sudo pacman -S base-devel arch-install-scripts qemu

Clone and build the kernel

Clone and compile a Linux kernel

git clone --depth=1 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
make x86_64_defconfig
make kvm_guest.config
make -j $(nproc)

Create and mount a file system

cd ..
truncate -s 5G disk.raw
mkfs.ext4 disk.raw

mkdir mnt
sudo mount disk.raw mnt

Setup an Arch system

Let's transform that formatted partition into a functional system

sudo pacstrap mnt base base-devel
sudo arch-chroot mnt

Then unlock the root account (be aware of it!), setting a new root password

[guest/]# sudo passwd root
[guest/]# exit
sudo umount mnt

Use QEMU to test the built kernel

qemu-system-x86_64 -hda disk.raw \
    -cpu host -smp 4 -m 8G -nographic \
    -kernel linux/arch/x86/boot/bzImage \
    -append "root=/dev/sda rw console=ttyS0 loglevel=5" \
    --enable-kvm

After login, verify that you are actually running the compiled kernel

[guest/]# uname -r

and you are ready to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment