Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Build kernel from source

Cách đặt tên

• Major: Phiên bản nâng cấp chính


• Minor: Phiên bản nâng cấp phụ
• Patchlevel: Đối với stable ver, khi các lỗi quan trọng được fix
Build kernel from source
• Step 1: Obtain Linux kernel and extract Linux kernel
kernel.org kernel source tree: Torvalds/linux
• Step 2: Config by make [x|g|menu]config

• Step 3: Build the kernel’s loadable modules and Device Tree Blobs ->
create compressed kernel image file ([b|z|u]image), uncompressed
kernel image (vmlinux), system.map, kernel module objects & any
configured DTB files.
Build kernel from source
• Step 4: Install the just built kernel

• Step 5: Setup the GRUB bootloader and the initramfs (or initrd) image
under /boot (x86_64)
• This creates and install initramfs (or initrd) image under /boot
• It updates the bootloader configuration file to boot the new kernel

• Step 6: Customize the GRUB bootloader


Step 1: Obtain kernel from source
• Download from https://www.kernel.org

• Clone Linus source tree

• Extract kernel source tree


tar xf ~/Downloads/linux-5.4.tar.xz --directory=${HOME}/kernels/
export LLKD_SRC=${HOME}/kernels/linux-5.4
Architecture of kernel source tree
• Top-level files: README, COPYING, MAINTAINERS and Makefile
• Major directories:
• kernel/: core kernel subsystem, deals with process/thread life cycle, CPU
scheduling, locking, timers, interrupts, etc
• mm/: core memory management code
• fs/: implements the kernel Virtual FileSystem Switch (VFS) and individuals
filesystem drivers
• block/: block IO code, implementing page cache, generic block IO layer, IO
schedulers, etc
• net/: network protocol stack
Architecture of kernel source tree (cont.)
• ipc/: Inter-process communication subsystem code, which covers messages
queues, shared memory, semaphores, etc
• sound/: audio subsystem code
• virt/: Kernel Virtual Machine code implemented here
• Infrastructure/misc:
• arch/: arch-specific code
• crypto/: kernel level implementation of ciphers
• include/: contains arch-independent kernel headers
• init/: arch-independent kernel initialization code
• lib/: closet equivalent to a library for the kernel

Step 2: Configure the Linux Kernel
• Kbuild system
• Config symbol

• Kconfig files

• Makefiles

• The .config file


Popular methods
• Config for typical embedded system

• Using distribution config

• Via localmodconfig
-> lsmod > /tmp/lsmod.now
make LSMOD=/tmp/lsmod.now localmodconfig
make menuconfig
• [.]: in-kernel feature, Boolean state
• <.> tri-state feature
• <*>: on, feature compiled and built in
• <M>: module, feature compiled and built in as loadable kernel module
• <>: off

• {.}: a dependency exists for this config option


• -*-: a dependency requires this item to be compiled.
Verify change
• grep “string_option” .config: hiển thị giá trị hiện tại của trường giá trị
trong .config

• Scripts/diffconfig .config.old .config


Step 3: build the kernel image and modules
• time make –j[n]

With n being the number of processor cores, checked by nproc


Step 4: Installing the kernel modules
• Find modules built:
$find . –name “*.ko”

$sudo make modules_install

Câu hỏi: Các module được cài đặt vào folder nào?
Step 5: Bootloader setup
• For x86[64]
• Generating the initramfs (or initrd) image

• Setup GRUB bootloader

• Sudo make install

• For embedded system:


• U-Boot
What is initramfs
• For systems that do not know in advance the boot disk, exact
filesystem type

• Initramfs allows to run user space applications (or scripts) before the
actual root filesystem has been mounted. E.g:
• Setup a console font
• Print a custom welcome message on the console device
• Load up kernel modules as require
Boot process on x86
• BIOS loads up the first sector of the first bootable disk into RAM
(stage one bootloader), next loads the stage two bootloader into RAM
• Load the GRUB (Grand Unified Bootloader) bootloader into memory
• GRUB bootloader
• Perform low-level hardware initialization
• Loads the compressed kernel image (vmlinuz) and initramfs image into RAM
• Jump to the kernel entry point
• The Linux kernel initialize the hardware and software environment.
Boot process on x86(cont.)
• The kernel locate and mount the initramfs as a temporary root
filesystem in RAM (RAMdisk), which perform the loading of required
kernel modules.
• The RAMdisk is then unmounted, let the real root filesystem replace
and initialize the system.
• System initialization proceeds, the kernel invokes the first user space
process (/sbin/init) with PID = 1
Exercises
• Redo the process and turn on those options in menuconfig -> Kernel
Hacking

• CONFIG_KASAN
Exercises
CONFIG_DEBUG_SHIRQ

CONFIG_SCHED_STACK_END_CHECK

CONFIG_PROVE_LOCKING

CONFIG_LOCK_STAT
CONFIG_DEBUG_ATOMIC_SLEEP
CONFIG_STACKTRACE
CONFIG_DEBUG_BUGVERBOSE
CONFIG_FTRACE
CONFIG_BUG_ON_DATA_CORRUPTION
CONFIG_KGDB (kernel GDB; optional)
CONFIG_UBSAN
CONFIG_EARLY_PRINTK
CONFIG_DEBUG_BOOT_PARAMS
CONFIG_UNWINDER_FRAME_POINTER (selects FRAME_POINTER and
CONFIG_STACK_VALIDATION)

You might also like