2026年6月3日编译Linux Kernel和BusyBox
感谢BIlibili大佬(@于小乐_Unics)的视频:【教程】带你编译内核,手搓自己的Linux发行版!
运行环境
BASH
root@xiaoman-Laptop:~# cat /etc/os-release
PRETTY_NAME="Ubuntu 26.04 LTS"
NAME="Ubuntu"
VERSION_ID="26.04"
VERSION="26.04 LTS (Resolute Raccoon)"
VERSION_CODENAME=resolute
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=resolute
LOGO=ubuntu-logo
root@xiaoman-Laptop:~#
本次采用WSL的方式进行编译,关于WSL的安装与调试不在本篇文章范围内,请自行查找网络或者询问AI
编译前的检查工作
警告
本文章所有操作均在ROOT环境下进行,请不要在您的生产环境尝试,关于您的一意孤行我方不负任何责任。
一: 安装Ubuntu
POWERSHELL
PS D:\WSL> wsl --install
正在下载: Ubuntu
正在安装: Ubuntu
已成功安装分发。可以通过 “wsl.exe -d Ubuntu” 启动它
正在启动 Ubuntu...
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。NAT 模式下的 WSL 不支持 localhost 代理。
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: xiaoman1221
New password:
Retype new password:
passwd: password updated successfully
usermod: no changes
Help improve Ubuntu!
Help us improve Ubuntu features and compatibility by sharing system reports with Canonical.
Reports are sent anonymously and do not contain any personal data.
For legal details, please visit: https://ubuntu.com/legal/systems-information-notice
We will save your answer to Windows and will only ask you once.
Would you like to opt-in to platform metrics collection (Y/n)? To see an example of the data collected, enter 'e'.
[Y/n/e]: n
wsl: Failed to start the systemd user session for 'xiaoman1221'. See journalctl for more details.
xiaoman1221@xiaoman-Laptop:/mnt/d/WSL$
二: 替换APT源为清华大学源(参考本站或者清华大学源相关说明)
这里不过多赘述
三: 安装必要包:
BASH
# update
apt clean && apt update && apt upgrade -y
# install
apt install -y vim wget curl git bash-c*
apt install -y tree bzip2 lbzip2 make gcc libncurses-dev pkg-config flex bison libelf-dev libssl-dev
apt install -y dosfstools grub2-common grub-efi-amd64-bin qemu-system-x86
四:创建一个文件夹,别给文件搞得乱七八槽的
BASH
root@xiaoman-Laptop:~# mkdir -p workspace/Linux/{Kernel,BusyBox,mnt}
root@xiaoman-Laptop:~# tree ./workspace/
./workspace/
└── Linux
├── BusyBox
├── mnt
└── Kernel
4 directories, 0 files
root@xiaoman-Laptop:~#
五: 下载相关源代码
Kernel 官方网站
BusyBox 官方网站
BASH
## Kernel 源代码下载
# 进入文件夹
cd /root/workspace/Linux/Kernel/
# 下载
wget https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.11.tar.xz
# 解压
tar -xf linux-7.0.11.tar.xz
## BusyBox 源代码下载
# 进入文件夹
cd /root/workspace/Linux/BusyBox/
# 下载
wget https://busybox.net/downloads/busybox-1.38.0.tar.bz2
# 解压
tar -xf busybox-1.38.0.tar.bz2
目前我们拥有的文件夹结构如下:
BASH
root@xiaoman-Laptop:~/workspace/Linux# tree -L 2
.
├── mnt
├── BusyBox
│ ├── busybox-1.38.0
│ └── busybox-1.38.0.tar.bz2
└── Kernel
├── linux-7.0.11
└── linux-7.0.11.tar.xz
开始编译内核
BASH
# 进入文件夹
cd /root/workspace/Linux/Kernel/linux-7.0.11
# 使用默认配置
make defconfig
# 开始编译
make -j 32 # 32代表线程数,一般为你CPU核心数量的两倍,按自己的情况修改,如果出现报错请适当降低数字,稳定第一
# 开始编译后服务器变卡属于正常现象,此步骤时间很长,取决你机器的速度
# 显示:Kernel: arch/x86/boot/bzImage is ready (#1)
# 代表成功
编译成功的产物:
BASH
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11# cd arch/
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch# ls
Kconfig alpha arc arm arm64 csky hexagon loongarch m68k microblaze mips nios2 openrisc parisc powerpc riscv s390 sh sparc um x86 x86_64 xtensa
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch# cd x86
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch/x86# ls
Kbuild Kconfig.assembler Kconfig.cpufeatures Makefile Makefile_32.cpu built-in.a configs entry hyperv include kvm math-emu modules.order pci power ras tools video xen
Kconfig Kconfig.cpu Kconfig.debug Makefile.um boot coco crypto events ia32 kernel lib mm net platform purgatory realmode um virt
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch/x86# pwd
/root/workspace/Linux/Kernel/linux-7.0.11/arch/x86
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch/x86#
这里留意:/root/workspace/Linux/Kernel/linux-7.0.11/arch/x86/boot/bzImage
BASH
root@xiaoman-Laptop:~/workspace/Linux/Kernel/linux-7.0.11/arch/x86/boot# file bzImage
bzImage: Linux kernel x86 boot executable, bzImage, version 7.0.11 (root@xiaoman-Laptop) #1 SMP PREEMPT_DYNAMIC Wed Jun 3 06:04:04 CST 2026, RO-rootFS, Normal VGA, setup size 512*39, syssize 0xe0f40, jump 0x26c 0x8cd88ec0fc8cd239 instruction, protocol 2.15, from protected-mode code at offset 0x2c4 0xdfe51e bytes gzip compressed, relocatable, handover offset 0xe0bf80, legacy 64-bit entry point, can be above 4G, 32-bit EFI handoff entry point, 64-bit EFI handoff entry point, EFI kexec boot support, xloadflags bit 5, max cmdline size 2047, init_size 0x2ad0000
这个就是主要的文件
开始编译BusyBox
BASH
# 创建一个编译后产物的文件夹,我们不是要在本机安装,要将相关文件存入单独的文件
mkdir /root/workspace/Linux/BusyBox/Build
# 进入文件夹
cd /root/workspace/Linux/BusyBox/busybox-1.38.0
# 进入交互式配置模式
make menuconfig
# 选择相关选项
# 1. 选择 Settings ---> [*] Build static binary (no shared libs) 此步骤意为编译为静态库文件,无需链接动态库
# 2. 选择 Networking Utilities ---> [ ] tc (8.3 kb) 取消勾选tc套件
# 3. Do you wish to save your new configuration? YES 保存刚才的设置
# 开始编译
make -j 32 # 32代表线程数,一般为你CPU核心数量的两倍,按自己的情况修改,如果出现报错请适当降低数字,稳定第一
# 开始编译后服务器变卡属于正常现象,此步骤时间很长,取决你机器的速度
# 显示:Final link with: m resolv
# 代表成功
# 生成二进制文件到指定目录
make install CONFIG_PREFIX=/root/workspace/Linux/BusyBox/Build
# 显示如下信息表示成功
# --------------------------------------------------
# You will probably need to make your busybox binary
# setuid root to ensure all configured applets will
# work properly.
# --------------------------------------------------
编译成功的产物:
BASH
root@xiaoman-Laptop:~/workspace/Linux/BusyBox# tree /root/workspace/Linux/BusyBox/Build/
/root/workspace/Linux/BusyBox/Build/
├── bin
│ ├── arch -> busybox
│ ├── ...
├── linuxrc -> bin/busybox
├── sbin
│ ├── ...
│ └── zcip -> ../bin/busybox
└── usr
├── bin
│ ├── ...
│ └── yes -> ../../bin/busybox
└── sbin
├── add-shell -> ../../bin/busybox
├── ...
└── udhcpd -> ../../bin/busybox
6 directories, 408 files
至此所有二进制文件全部完成
创建img镜像
BASH
# 进入目录
cd /root/workspace/Linux
# 创建一块大小为512的img块文件
dd if=/dev/zero of=output.img bs=1M count=512
# 将刚创建按的文件挂载至操作系统,通常为/dev/loop*
losetup -f -P ./output.img
# 分区
gdisk /dev/loop0
# Command (? for help): n
# Partition number (1-128, default 1):
# First sector (34-1048542, default = 2048) or {+-}size{KMGTP}:
# Last sector (2048-1048542, default = 1046527) or {+-}size{KMGTP}: +50M
# Current type is 8300 (Linux filesystem)
# Hex code or GUID (L to show codes, Enter = 8300): EF00
# Changed type of partition to 'EFI system partition'
# Command (? for help): n
# Partition number (2-128, default 2):
# First sector (34-1048542, default = 104448) or {+-}size{KMGTP}:
# Last sector (104448-1048542, default = 1046527) or {+-}size{KMGTP}:
# Current type is 8300 (Linux filesystem)
# Hex code or GUID (L to show codes, Enter = 8300):
# Changed type of partition to 'Linux filesystem'
# Command (? for help): p
# Disk /dev/loop0: 1048576 sectors, 512.0 MiB
# Sector size (logical/physical): 512/512 bytes
# Disk identifier (GUID): 10C146D1-656B-465F-A3DF-1B6B5C5229DC
# Partition table holds up to 128 entries
# Main partition table begins at sector 2 and ends at sector 33
# First usable sector is 34, last usable sector is 1048542
# Partitions will be aligned on 2048-sector boundaries
# Total free space is 4029 sectors (2.0 MiB)
# Number Start (sector) End (sector) Size Code Name
# 1 2048 104447 50.0 MiB EF00 EFI system partition
# 2 104448 1046527 460.0 MiB 8300 Linux filesystem
# Command (? for help): w
# Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
# PARTITIONS!!
# Do you want to proceed? (Y/N): Y
# OK; writing new GUID partition table (GPT) to /dev/loop0.
# The operation has completed successfully.
# 查看
lsblk
# NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
# loop0 7:0 0 512M 0 loop
# ├─loop0p1 259:2 0 50M 0 part
# └─loop0p2 259:3 0 460M 0 part
# 格式化
# EFI分区
mkfs.fat -F32 /dev/loop0p1
# 数据分区
mkfs.ext4 /dev/loop0p2
Grub引导生成
前置工作: 获取UUID和PARTUUID
BASH
blkid
# /dev/loop0p2: UUID="5bcc0e2e-b607-446e-b616-f7e057971338" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="7a92b03f-19af-4941-afc0-be126d599608"
# 记录下列信息(你要记住你的,而不是我的)
## UUID="5bcc0e2e-b607-446e-b616-f7e057971338"
## PARTUUID="7a92b03f-19af-4941-afc0-be126d599608"
生成基础文件
BASH
# 挂载EFI分区至mnt文件夹(前文创建的)
mount /dev/loop0p1 /root/workspace/Linux/mnt/
# 开始初始化
grub-install --target=x86_64-efi --efi-directory=/root/workspace/Linux/mnt --recheck --removable
## 出现下列提示则成功!
# Installing for x86_64-efi platform.
# Installation finished. No error reported.
修改grub配置文件:grub.cfg
BASH
vim /root/workspace/Linux/mnt/EFI/BOOT/grub.cfg
## 将UUID改成你的UUID,hd后面的数字改成0
# search.fs_uuid 5bcc0e2e-b607-446e-b616-f7e057971338 root hd0
# set prefix=($root)'/boot/grub'
# configfile $prefix/grub.cfg
# 卸载
umount /root/workspace/Linux/mnt
主系统文件创建
BASH
## 挂载数据分区
mount /dev/loop0p2 /root/workspace/Linux/mnt/
## 创建所有Linux必须的文件夹
mkdir -p /root/workspace/Linux/mnt/{dev,etc,proc,root,tmp,run,var/log,boot/grub}
# root@xiaoman-Laptop:~/workspace/Linux/mnt# tree
#.
# ├── boot
# │ └── grub
# ├── dev
# ├── etc
# ├── lost+found
# ├── root
# ├── run
# ├── tmp
# ├── proc
# └── var
# └── log
# 11 directories, 0 files
## 复制内核文件到文件系统
cp /root/workspace/Linux/Kernel/linux-7.0.11/arch/x86/boot/bzImage /root/workspace/Linux/mnt/boot/
## 复制BusyBox产物到文件系统
cp -r /root/workspace/Linux/BusyBox/Build/* /root/workspace/Linux/mnt/
## 创建init脚本
vim /root/workspace/Linux/mnt/boot/init
## 脚本内容
#!/bin/sh
mount -t sysfs none /sys
mount -t proc none /proc
mount -t devtmpfs devtmpfs /dev
printf "\n >>> Powered By xiaoman1221 <<<\n\n"
sleep 3
printf "\033[H\033[J"
exec /bin/sh
## 赋予init文件执行权限
chmod +x /root/workspace/Linux/mnt/boot/init
## 创建grub.cfg
vim /root/workspace/Linux/mnt/boot/grub/grub.cfg
## 脚本内容
menuentry "Build With xiaoman1221" {
insmod part_gpt
insmod fat
insmod ext2
insmod normal
search --no-floppy --fs-uuid --set=root 5bcc0e2e-b607-446e-b616-f7e057971338
linux /boot/bzImage root=PARTUUID=7a92b03f-19af-4941-afc0-be126d599608 rw init=/boot/init rootdelay=3 console=ttyS0
initrd /boot/init
}
至此:所有的文件全部处理完成,开始卸载所有文件系统并进入qemu测试
BASH
## 安全卸载并同步缓存
umount /root/workspace/Linux/mnt
losetup -d /dev/loop0
sync
## 使用QEMU启动测试
qemu-system-x86_64 \
-m 2G \
-smp 2 \
-bios /usr/share/ovmf/OVMF.fd \
-drive file=output.img,format=raw \
-net none \
-nographic
结果:
BASH
~ # pwd
/
~ # ls
bin etc proc sbin var
boot linuxrc root tmp
dev lost+found run usr
~ #
全文结束
本文是原创文章,采用 CC BY-NC-SA 4.0 协议,完整转载请注明来自 小满1221
评论
隐私政策
0/500
滚动到此处加载评论...
