目录
1.环境搭建
1.1 操作系统(Ubuntu 22.04.4 Server LTS)
Linux system 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
1.2 工具安装
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc g++ make cmake automake flex bison bc bzip2 vim qemu-system-x86
sudo apt-get install libncurses-dev libssl-dev libelf-dev
1.3 下载Linux kernel源码
wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v6.x/linux-6.9.tar.xz
1.4 下载busybox源码
wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2
2.编译
2.1 编译linux kernel
cd linux-6.9
#此处的-j$(nproc)意思是,按照核心数去同时进行几个编译任务,可以加快编译速度
make -j$(nproc)
2.2 制作根文件系统(debootstrap)
#sudo debootstrap --arch [平台] [发行版本代号] [构建目录] [镜像地址]
sudo debootstrap --arch=amd64 --foreign focal rootfs http://mirrors.ustc.edu.cn/ubuntu/
dd if=/dev/zero of=linux_rootfs.ext4 bs=1M count=2048
mkfs.ext4 rootfs.ext4
mkdir -p tmpfs
sudo mount -t ext4 rootfs.ext4 tmpfs/ -o loop
sudo cp -af rootfs/* tmpfs/
sudo umount tmpfs
sudo chmod 777 rootfs.ext4
2.3制作根文件系统(busybox)
2.3.1 编译
- make menuconfig
Busybox Settings--->
Build Options--->
[*]Build BusyBox as a static binary(no shared libs)
- make
- make install
2.3.2 制作
mkdir -p dev proc sys etc/init.d
sudo mknod dev/console c 5 1
sudo mknod dev/null c 1 3
cat << 'EOF' > etc/init.d/rcS
#!/bin/sh
# Mount necessary filesystems
mount -t proc none /proc
mount -t sysfs none /sys
# Create device nodes
mdev -s
# Start a shell on the console
exec /bin/sh
EOF
chmod +x etc/init.d/rcS
cat << 'EOF' > etc/inittab
::sysinit:/etc/init.d/rcS
::respawn:/bin/sh
::askfirst:/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
EOF
dd if=/dev/zero of=linux_rootfs.ext4 bs=1M count=2048
mkfs.ext4 rootfs.ext4
mkdir -p tmpfs
sudo mount -t ext4 rootfs.ext4 tmpfs/ -o loop
sudo cp -af _install/* tmpfs/
sudo umount tmpfs
sudo chmod 777 rootfs.ext4
3.运行和调试
3.1 运行(debootstrap)
qemu-system-x86_64 -kernel ./linux-6.9/arch/x86_64/boot/bzImage \
-hda rootfs.ext4 \
-append "noinitrd root=/dev/sda rootfstype=ext4 rw console=ttyS0" \
-m 512M \
-smp 4 \
-nographic
- kernel 指定内核路径
- hda 指定rootfs.ext4路径
- append 附加到内核的命令
- m 指定内存大小
- smp 指定CPU核心数
- nographic 非界面方式启动
3.2 替换现有内核
3.2.1 安装模块
- linux编译完后执行
make install
和make modules_install
- 修改Linux启动信息
sudo vim /etc/default/grub
把GRUB_TIMEOUT_STYLE
屏蔽,GRUB_TIMEOUT
设置为10秒
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
GRUB_DEFAULT=0
#GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX=""
# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"
# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console
# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480
# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true
# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"
# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"
保存后更新启动信息
sudo update-grub
reboot
开机选择要启动的内核版本即可,如下图所示
4.常见问题
4.1 编译linux kernel时遇
到No rule to make target debian/canonical-certs.pem, needed by certs/x509_certificate_list
vim .config
- 找到
CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"
, 修改为CONFIG_SYSTEM_TRUSTED_KEYS=""
; - 找到
CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"
, 修改为CONFIG_SYSTEM_REVOCATION_KEYS=""
;
make -j$(nproc)