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.
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 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)
cd ..
truncate -s 5G disk.raw
mkfs.ext4 disk.raw
mkdir mnt
sudo mount disk.raw mnt
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
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.