linux内核编译与虚拟机启动

本文将针对AArch64平台进行linux内核编译,并使用qemu虚拟机运行linux内核1

工具链准备

为了编译linux内核,需要准备相关的构建工具链,我们要编译AArch64平台的,所以需要安装aarch64-linux-gnu-编译工具链

1
sudo apt install gcc-aarch64-linux-gnu

编译busybox(根文件系统)

busybox是一个广泛应用于嵌入式linux的工具箱,集成了一些很常用的linux命令,我们首先需要编译一个busybox做准备,然后作为rootfs进行加载,从而使用户迅速方便地构建相对完整、功能丰富的文件系统。点击此处下载对应busybox版本,下载解压后,进入对应的目录下,然后执行如下指令

1
2
3
mkdir build
make O=build ARCH=arm64 defconfig
make O=build ARCH=arm64 menuconfig

在弹出的配置菜单中,在[settings]里修改如下配置:

1
2
3
[*] Don't use /usr
[*] Build static binary (no shared libs)
(aarch64-linux-gnu-) Cross compiler prefix

运行如下命令:

1
2
make O=build # -j16
make O=build install

进入build/_install目录下,该目录结构为:

1
2
3
4
5
6
7
$ tree -L 1 .
.
├── bin
├── linuxrc -> bin/busybox
└── sbin

2 directories, 1 file

创建一些空的目录作为rootfs中的目录

1
mkdir -pv {etc,proc,sys,usr/{bin,sbin}}

创建一个init文件并执行chmod +x init,该文件将会在系统启动后执行:

1
2
3
4
5
6
7
8
#!/bin/sh

mount -t proc none /proc
mount -t sysfs none /sys

echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"

exec /bin/sh

此时build/_install目录如下:

1
2
3
4
5
6
7
8
9
10
11
12
$ tree -L 1 .
.
├── bin
├── etc
├── init
├── linuxrc -> bin/busybox
├── proc
├── sbin
├── sys
└── usr

6 directories, 2 files

将上述目录与文件进行打包:

1
find . -print0 | cpio --null -ov --format=newc | gzip > ../initramfs.cpio.gz

生成的cpio映像位于build目录下,实际就是将initramfs打包成了cpio格式的压缩包。

编译最小配置的linux内核

kernel下载适当版本的内核源码并解压,然后执行如下命令:

1
2
3
4
5
cd linux-5.10.127   # 这里选择长期支持版本的5.10内核
mkdir build

make O=build ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- allnoconfig
make O=build ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

这会首先初始化一个最小的配置(allnoconfig),然后打开配置菜单。在配置菜单中做以下修改:

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
-> General setup
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support

-> General setup
-> Configure standard kernel features
[*] Enable support for printk

-> Executable file formats / Emulations
[*] Kernel support for ELF binaries
[*] Kernel support for scripts starting with #!

-> Device Drivers
-> Generic Driver Options
[*] Maintain a devtmpfs filesystem to mount at /dev
[*] Automount devtmpfs at /dev, after the kernel mounted the rootfs

-> Device Drivers
-> Character devices
[*] Enable TTY

-> Device Drivers
-> Character devices
-> Serial drivers
[*] ARM AMBA PL010 serial port support
[*] Support for console on AMBA serial port
[*] ARM AMBA PL011 serial port support
[*] Support for console on AMBA serial port

-> File systems
-> Pseudo filesystems
[*] /proc file system support
[*] sysfs file system support

完成后保存并退出,再运行:

1
make O=build ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- # -j16

编译完成后有两个东西比较有用,一个是build/vmlinux,ELF格式的内核,可以在GDB中加载调试信息。另一个是build/arch/arm64/boot/Image,是可以加载的内核镜像文件。

qemu启动

在ubuntu上执行如下命令安装对应架构的qemu

1
apt install qemu-system-arm

创建一个qemu文件夹,将上面编译好的Imageinitramfs.cpio.gz拷贝到qemu文件夹下,然后执行如下脚本:

1
2
3
4
5
6
qemu-system-aarch64 \
-machine virt -cpu cortex-a72 -smp 4 -m 2G \ # 4个cortex-a72 CPU,2GB内存
-kernel ./Image \
-append "console=ttyAMA0" \
-initrd ./initramfs.cpio.gz \
-nographic

系统启动非常快,用了不到1秒中

1
2
3
4
5
6
7
8
Booting Linux on physical CPU 0x0000000000 [0x410fd083]
Linux version 5.10.127 (root@hecs-198689) (aarch64-linux-gnu-gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #1 SMP Wed Jun 29 21:48:32 CST 2022
... 省略若干启动信息
Run /init as init process

Boot took 0.51 seconds

/bin/sh: can't access tty; job control turned off

启动后可以看到位于我们打包的busybox文件夹下:

1
2
3
/ # ls
bin etc linuxrc root sys
dev init proc sbin usr

可以执行一些简单的命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/ # top
Mem: 13660K used, 2042812K free, 0K shrd, 0K buff, 3592K cached
CPU: 0.0% usr 0.0% sys 0.0% nic 99.9% idle 0.0% io 0.0% irq 0.0% sirq
Load average: 0.00 0.00 0.00 1/55 59
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
59 1 0 R 2284 0.1 1 0.0 top
1 0 0 S 2284 0.1 0 0.0 /bin/sh
14 2 0 SW 0 0.0 1 0.0 [migration/1]
34 2 0 SW 0 0.0 5 0.0 [migration/5]

/ # free
total used free shared buff/cache available
Mem: 2056472 9728 2042568 0 4176 2033024
Swap: 0 0 0

使用ctrl a + x可以关闭qemu

参考文献

0%