0%

编译 linux 内核和 busybox 文件系统

编译 linux 内核和 busybox 文件系统

1 前期准备

1.1 下载、解压 linux 内核源码

1
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.9.229.tar.gz

压缩包体积较大,下载用时可能会很长,请耐心等待

  • 解压
1
tar -xzf ./linux-4.9.229.tar.gz

1.2 linux内核源码目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ tree linux-4.9.229 -L 1
linux-4.9.229
├── COPYING
├── CREDITS
├── Documentation
├── Kbuild
├── Kconfig
├── MAINTAINERS
├── Makefile
├── README
├── REPORTING-BUGS
├── arch
├── block
├── certs
├── crypto
├── drivers
├── firmware
├── fs
├── include
├── init
├── ipc
├── kernel
├── lib
├── mm
├── net
├── samples
├── scripts
├── security
├── sound
├── tools
├── usr
└── virt

22 directories, 8 files

1.2.1 arch

1
2
3
4
5
6
7
8
9
10
$ tree linux-4.9.229/arch/ -L 1
linux-4.9.229/arch/
├── Kconfig
├── alpha
├── arc
├── arm
├── arm64
├── avr32
├── blackfin
......
  • 在arch目录下存放着用于支持不同体系结构的代码,其中包括常见的x86、arm64等

1.2.2 Documentation

  • 内核参数、配置、特性的技术说明文档

1.2.3 init

  • 内核启动相关的代码

1.2.4 block

  • 块设备相关代码

1.2.5 drivers

  • 不同外部设备的驱动代码,占据内核代码的很大一部分

1.2.6 ipc

  • 进程间通信相关的代码

1.2.7 security

  • 安全相关的代码

1.2.8 net

  • 协议栈相关的代码

1.2.9 sound

  • 声音相关的代码

1.2.10 fs

  • 文件系统相关的代码

1.2.11 kernel

  • 进程管理、调度等核心代码

1.2.12 mm

  • 内存管理相关的代码

2 编译 linux 内核

2.1 指定硬件体系架构

1
$ export ARCH=x86

2.2 配置 board config

1
2
3
4
$ make x86_64_defconfig
#
# configuration written to .config
#
  • 内核的编译系统会根据 .config 文件中的配置去编译linux内核

2.3 配置 kernel

1
$ make menuconfig
  • 这一步如果出现报错:fatal error: curses.h: No such file or directory,需要安装依赖库,
1
$ sudo apt install libncurses5-dev
  • 选中如下配置,让内核支持ramdisk驱动:
1
2
3
4
5
6
7
8
9
10
11
General setup  --->

----> [*] Initial RAM filesystem and RAM disk (initramfs/initrd) support

Device Drivers --->

[*] Block devices --->

<*> RAM block device support

(65536) Default RAM disk size (kbytes)

2.4 编译

1
$ make
  • 编译成功后内核位于:./arch/x86_64/boot/bzImage

3 编译 busybox

  • busybox 是一个集成了三百多个最常用Linux命令和工具的软件

3.1 下载、解压 busybox

1
$ wget https://busybox.net/downloads/busybox-1.30.0.tar.bz2
  • 解压
1
$ tar xf busybox-1.30.0.tar.bz2
  • 切换目录
1
$ cd busybox-1.30.0/

3.2 配置 busybox 源码

  • 将 busybox 配置为静态链接,这样 busybox 在运行的时候就不需要额外的动态链接库
1
2
3
4
5
$ make menuconfig

Busybox Settings --->
Build Options --->
[*] Build BusyBox as a static binary (no shared libs)

3.3 编译、安装

1
$ make && make install 

3.4 补充一些必要的文件或目录

3.4.1 切换目录

1
$ cd _install/

3.4.2 创建文件夹

1
2
3
$ mkdir etc dev mnt
$ mkdir -p proc sys tmp mnt
$ mkdir -p etc/init.d/
  • init.d 目录包含系统许多服务的启动和停止脚本

3.4.3 配置自动挂载

1
2
3
4
$ vim etc/fstab
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs   defaults 0 0
sysfs /sys sysfs defaults 0 0
  • 系统开机时会主动读取 /etc/fstab 这个文件中的内容,根据文件里面的配置挂载磁盘,这样我们只需要将磁盘的挂载信息写入这个文件中就不需要每次开机启动之后手动进行挂载了

3.4.4 配置开机启动脚本

1
2
3
4
5
6
7
8
9
$ vim etc/init.d/rcS
echo -e "Welcome to tinyLinux"
/bin/mount -a
echo -e "Remounting the root filesystem"
mount -o remount,rw /
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
  • 修改文件权限,
1
$ chmod 755 etc/init.d/rcS 

3.4.5 配置系统初始化脚本

1
2
3
4
5
$ vim etc/inittab
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
  • 修改文件权限,
1
chmod 755 etc/inittab 

3.4.6 创建设备文件

1
2
3
4
$ cd dev
$ mknod console c 5 1
$ mknod null c 1 3
$ mknod tty1 c 4 1
  • 这样就实现了一个最小的、完整的,可以被内核启动的文件系统