389 字
2 分鐘
頁面瀏覽--訪問次數--
Build Linux kernel

Build Linux kernel#

download kernel#

Terminal window
if [[ $(echo $VERSION | cut -d'.' -f3) == '0' ]]; then
VERSION=$(echo $VERSION| cut -d'.' -f1,2)
fi
# Get the major version number (e.g., '6' from '6.5.3')
MAJOR_VERSION=$(echo "$VERSION" | cut -d'.' -f1)
wget "https://cdn.kernel.org/pub/linux/kernel/v${MAJOR_VERSION}.x/linux-${VERSION}.tar.xz"
tar -xf "linux-${VERSION}.tar.xz"
Terminal window
make mrproper && make menuconfig

Kernel hacking#

printk and dmesg options#

  • Enable core function if dynamic debug support
  • Enable dynamic printk() support

Compile-time checks and compiler options#

  • Provide GDB scripts for kernel debugging (CONFIG_GDB_SCRIPTS)
  • Generate readable assembler code (CONFIG_READABLE_ASM)
  • one of Debug information
  • Enable full Section mismatch analysis (IMPORTANT FOR DEBUGGING FUNCTION LIKE handle_pte_fault)

Memory Management options#

  • Multi-Gen LRU(all selected)
  • Enable userfaultfd() system call

Build linux kernel and boot using QEMU#

build linux kernel

Terminal window
export CC='ccache gcc'
export HOSTCC='ccache gcc'
make all -j$(nproc)
make scripts_gdb
  • if you want to boot new kernel in PC
Terminal window
export INSTALLKERNEL=installkernel
KERNEL_NAME=smth
PRESET_NAME=smth
Terminal window
sudo make modules_install -j$(nproc)
sudo make headers_install -j$(nproc)
sudo make install -j$(nproc)
sudo cp arch/x86_64/boot/bzImage /boot/vmlinuz-linux-$KERNEL_NAME
# install dkms
sudo dkms autoinstall -k $(make kernelrelease)
# install initramfs via mkinitcpio
sudo mkinitcpio -p linux-$PRESET_NAME -k $(make kernelrelease)

Build rootfs via busybox#

Terminal window
git clone https://git.busybox.net/busybox && cd busybox
# remember to unselect tc in Networking Utilities in menuconfig
# and use static binary (enable in Settings)
make mrproper && make menuconfig
make install -j$(nproc)
cd _install
mkdir -p lib lib64 proc sys etc etc/init.d
# write init script
touch etc/init.d/rcS
chmod +x etc/init.d/rcS
(cat <<EOF
#!/bin/sh
# Mount the /proc and /sys filesystems
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev
mount -t debugfs none /sys/kernel/debug
# Populate /dev
/sbin/mdev -s
EOF
) > etc/init.d/rcS
# pack all busybox file into rootfs via cpio and gzip
find . | cpio -o --format=newc | gzip > ../../rootfs.gz

Boot using QEMU

Terminal window
qemu-system-x86_64 \
-kernel arch/x86_64/boot/bzImage \
-nographic \
-initrd rootfs.gz \
-append "root=/dev/ram rdinit=/sbin/init console=ttyS0"
# -S -s // add these flag for gdb debug
# add this flag for share directory with host
# -virtfs local,id=shared,path=shared,mount_tag=shared,security_model=none

Tracing and debug kernel

Terminal window
gdb vmlinux -ex 'target remote:1234' -ex 'b start_kernel' -ex 'c'

Build rootfs via pacstrap#

Terminal window
mkdir rootfs
sudo chown root:root rootfs
sudo pacstrap -c ./rootfs base systemd bash
dd if=/dev/zero of=arch.img bs=1M count=20480
mkfs.ext4 arch.img
mkdir mnt
sudo mount arch.img mnt
sudo cp -a rootfs/. mnt/
sudo umount mnt

Boot

Terminal window
qemu-system-x86_64 \
-kernel arch/x86_64/boot/bzImage \
-nographic \
-drive file=arch.img,format=raw,if=virtio \
-m 1G \
-append "root=/dev/vda rw console=ttyS0"
# -cpu host -enable-kvm -smp $(nproc)
Build Linux kernel
https://c8763yee.github.io/posts/build-linux-kernel/
作者
c8763yee
發佈於
2025-12-10
許可協議
CC BY-NC-SA 4.0

分享文章

生成精美分享圖或複製連結,與更多人分享本文。

繼續閱讀

沿著主題讀

基於共同的標籤與分類

換條路線

從其他文章中穩定抽取

評論

正在載入評論...

最後更新於 ,距今已過 307 天

部分內容可能已過時