For decent Linux Kernel development I needed a virtual machine and a Kernel I can control easily. Since I use Gentoo for almost every system it was a clear choice I want to use Gentoo again, even though it might be a bit time consuming to set the VM up. To install Gentoo Linux on your daily hardware check our other post: https://www.blog.cocacoding.com/installing-gentoo-encrypted-on-a-modern-uefi-notebook/
For the start I will run Gentoo inside VirtualBox to set it up, but I choose settings that allow me to run it in QEMU later.
So lets install Gentoo. First, I get an installation medium. I use the Gentoo minimal CD: https://www.gentoo.org/downloads/
Table of Contents
Setting up the Kernel development Virtual Machine:
We set up the VM by creating it in VirtualBox and add a hard drive in qcow format QCOW (QEMU Copy-on-Write), I choose 128 GB disk space and added 4 Cores plus 16 GiB RAM to the machine. We boot the image (install-amd64-minimal-*.iso) as live CD and start the VM. Inside the VM you will be asked for the key map settings.
Setting up the empty Linux machine
We begin with the installation of our Gentoo VM. Run inside the terminal:
fdisk /dev/sda
Then create the partitions accordingly. I chose two primary partitions, the first with 500 MB for /boot and the remaining space for / (root file system)
Then we create a file system:
mkfs.ext4 /dev/sda1
mkfs.ext4 /dev/sda2
Next, we need to mount the directories to a mount point of our choice. I chose /mnt/gentoo
, just like described in the handbook.
mount /dev/sda2 /mnt/gentoo
mkdir /mnt/gentoo/boot
mount /dev/sda1 /mnt/gentoo/boot
Now we can begin with the stage3 download:
links startpage.com
Search for “gentoo amd64 stage3” and follow the a site. Alternatively just go to http://distfiles.gentoo.org/releases/amd64/autobuilds I search for the hardened stage3 since I am interested in a hardened user land and tool chain and usually there are no reasons not to use it. We download the stage3 file we found for our architecture amd64 by clicking enter on the file in our command line web browser “links”.
Now you should have all the stage3 files: The stage3 archive, the content file, and a DIGEST file we use for signature verification. Import the PGP key from the signatures page keys https://www.gentoo.org/downloads/signatures/
gpg --keyserver hkps://hkps.pool.sks-keyservers.net --recv-keys 0xBB572E0E2D182910
Then we can check if the files are valid:
gpg --verify *DIGEST.asc
If we get a “Good signature from Gentoo Linux Release Engineering […]” then it looks pretty good. Then we check the files:
sha512sum -c *DIGEST.ASC
If the stage3-amd64-hardened-*.tar.xz is okay we unpack it:
cd /mnt/gentoo
tar xpvf stage3-*.tar.bz2 --xattrs-include='*.*' --numeric-owner
Enter the new system
No we have a gentoo system on our hard drive. This is good because now we are able to change into the system:
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
mount --types proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"
We are now in our future gentoo system. We need to remount /boot
(Which was on /boot
on the outer system) to /boot (/mnt/gentoo/boot
from the outer system’s view). We also update our system:
mount /dev/sda2 /boot
emerge-webrsync
And we update the complete system:
emerge -av --deep --with-bdeps=y --newuse --update @world
Then I install some standard software:
emerge -av gentoolkit genlop genkernel grub lvm2 cryptsetup pciutils gentoo-sources eix
Configuration
Set the host name to your host name:
nano /etc/conf.d/hostname
Change the key map if you want to:
nano /etc/conf.d/keymaps
Configure timezone and locales:
echo "Europe/Brussels" > /etc/timezone
emerge --config sys-libs/timezone-data
nano -w /etc/locale.gen
locale-gen
echo "LANG="de_DE.UTF-8" > /etc/env.d/02locale
echo "LC_COLLATE="C" >> /etc/env.d/02locale
env-update && source /etc/profile && export PS1="(chroot) ${PS1}"
Change the login and make ssh appear during startup:
passwd root
rc-update add sshd default
And then add networking support, I choose DHCP and config my DHCP server for static IPs, if you need another setup check out the gentoo networking manual in the wiki.
nano /etc/conf./net
Add the Line:
config_eth0="dhcp".
Then add it as a init service:
cd /etc/init.d
ln -s net.lo net.eth0
rc-update add net.eth0 default
Then we install some basic services:
emerge -av fcron metalog
Next we need to adjust the virtual machine for kernel development.
Kernel Debugging Configuration
First we need to get the sources directly from the kernel development branch:
mkdir ~/src
cd ~/src
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
ln -s linux-stable linux
# Get the releases
cd linux
git pull --tags origin
# Use the kernel config from live cd or use your own template
zcat /proc/config.gz > config_file
make menuconfig
Load the file config_file
from menuconfig
and update it. I want to change the Kernel to support our virtual hardware, so according to the gentoo wiki page I enable all these features:
<*> Bus options (PCI etc.) --->
[*] Mark VGA/VBE/EFI FB as generic system framebuffer
<*> Device Drivers --->
<*> Serial ATA and Parallel ATA drivers (libata) --->
[*] AHCI SATA support
[*] ATA SFF support (for legacy IDE and PATA)
[*] ATA BMDMA support
[*] Intel ESB, ICH, PIIX3, PIIX4 PATA/SATA support
<*> Network device support --->
<*> Ethernet driver support --->
[*] Intel devices
[*] Intel(R) PRO/1000 Gigabit Ethernet support
<*> Input device support --->
<*> Keyboards --->
[*] AT keyboard
<*> Mice --->
[*] PS/2 mouse
<*> Graphics support --->
<*> Direct Rendering Manager (XFree86 4.1.0 and higher DRI support) --->
[*] Enable legacy fbdev support for your modesetting driver
<*> Virtio GPU driver
<*> Frame buffer Devices --->
<*> Support for frame buffer devices --->
[*] Enable Firmware EDID
[*] Simple framebuffer support
<*> Console display driver support --->
[*] Framebuffer Console support
[*] Map the console to the primary display device
<*> Sound card support --->
<*> Advanced Linux Sound Architecture --->
<*> PCI sound devices --->
[*] Intel/SiS/nVidia/AMD/ALi AC97 Controller
<*> USB support --->
[*] xHCI HCD (USB 3.0) support
[*] EHCI HCD (USB 2.0) support
Additionally for the debugging when doing Linux Kernel development:
CONFIG_DEBUG_KERNEL=y
CONFIG_DEBUG_INFO=y
Then your should be able to build your kernel with genkernel
or using make and make sure you have mounted /boot
properly:
# Use make
make -j8 all
make modules_install
make install
# or use genkernel
genkernel --kernel-config=config_file --kernel-dir=src --install --symlink initramfs
Configure the boot loader:
nano /etc/default/grub
Change the Linux command line to this to avoid renaming eth0 to some crazy device name (although unique identification fixes some problems this is still not useful if you have only one interface, for instance):
GRUB_CMDLINE_LINUX_DEFAULT=net.ifnames=0
Then install grub:
grub-install /dev/sda
grub-mkconfig -o /etc/grub/grub.cfg
Now you should be able to reboot and start your next kernel development project.
Further Lecture:
[1] https://wiki.gentoo.org/wiki/Genkernel
[2] https://github.com/rafaelnp/llkdd/wiki/Configuring,-compiling-and-installing-the-Linux-kernel
[3] https://www.andreasch.com/2019/01/28/Linux-kernel-debugging/