389 字
2 分鐘
Build Linux kernel
Build Linux kernel
download kernel
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"menuconfig for debug
make mrproper && make menuconfigKernel 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
export CC='ccache gcc'export HOSTCC='ccache gcc'
make all -j$(nproc)make scripts_gdb- if you want to boot new kernel in PC
export INSTALLKERNEL=installkernelKERNEL_NAME=smthPRESET_NAME=smthsudo 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 dkmssudo dkms autoinstall -k $(make kernelrelease)
# install initramfs via mkinitcpiosudo mkinitcpio -p linux-$PRESET_NAME -k $(make kernelrelease)Build rootfs via busybox
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 _installmkdir -p lib lib64 proc sys etc etc/init.d
# write init scripttouch etc/init.d/rcSchmod +x etc/init.d/rcS(cat <<EOF#!/bin/sh# Mount the /proc and /sys filesystemsmount -t proc none /procmount -t sysfs none /sysmount -t devtmpfs none /devmount -t debugfs none /sys/kernel/debug# Populate /dev/sbin/mdev -sEOF) > etc/init.d/rcS
# pack all busybox file into rootfs via cpio and gzipfind . | cpio -o --format=newc | gzip > ../../rootfs.gzBoot using QEMU
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=noneTracing and debug kernel
gdb vmlinux -ex 'target remote:1234' -ex 'b start_kernel' -ex 'c'Build rootfs via pacstrap
mkdir rootfssudo chown root:root rootfssudo pacstrap -c ./rootfs base systemd bash
dd if=/dev/zero of=arch.img bs=1M count=20480mkfs.ext4 arch.img
mkdir mntsudo mount arch.img mntsudo cp -a rootfs/. mnt/sudo umount mntBoot
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/分享文章
生成精美分享圖或複製連結,與更多人分享本文。
繼續閱讀
換條路線
從其他文章中穩定抽取
最後更新於 ,距今已過 307 天
部分內容可能已過時
評論