需要下载busybox和linux内核,我下载的是busybox-1.35.0.tar.bz2
和linux-5.10.tar.xz
,分别通过tar -xvf busybox-1.35.0.tar.bz2
和tar -xvf linux-5.10.tar.xz
解压。(linux内核linux-5.10和linux-5.17.9经过测试都是可以的,但是linux-5.19在使用中存在一些问题,目前还不清楚问题的根本原因)
https://busybox.net/downloads/
http://mirrors.aliyun.com/linux-kernel/?spm=a2c6h.25603864.0.0.54434823gkVvPG
https://mirrors.edge.kernel.org/pub/linux/kernel/
首先先解压下载的busybox,我自己下载的是当前最新版本的busybox-1.35.0,然后按照下面的步骤去制作最小文件系统即可。
1. cd busybox-1.35.0
2. make ARCH=arm64 defconfig
3. make ARCH=arm64 menuconfig
这会打开一个配置菜单,在配置菜单中修改以下几个选项,然后保存退出。
Busybox Settings ---> Build Options ---> Build BusyBox as a static binary (no shared libs) ---> yes
Busybox Settings ---> Build Options ---> (aarch64-linux-gnu-) Cross compiler prefix
在执行下面的步骤之后会生成_install
文件夹。
make
make install
$ ls
bin linuxrc sbin usr
cd _install
mkdir -pv {home,root,lib,etc/init.d,proc,sys,home,dev,usr/{lib,bin,sbin}}
vim _install/etc/profile
#!/bin/sh
export HOSTNAME=jimmy
export USER=root
export HOME=/home
export PS1="[$USER@$HOSTNAME \W]\# "
PATH=/bin:/sbin:/usr/bin:/usr/sbin
LD_LIBRARY_PATH=/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
vim _install/etc/init.d/rcS
mkdir -p /sys
mkdir -p /tmp
mkdir -p /proc
mkdir -p /mnt
/bin/mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
vim _install/etc/fstab
proc /proc proc defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev tmpfs defaults 0 0
debugfs /sys/kernel/debug debugfs defaults 0 0
vim _install/etc/inittab
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/umount -a -r
cd _install/dev
sudo mknod console c 5 1
sudo mknod null c 1 3
我下载的linux-5.0
的linux内核linux-5.10.tar.xz
,之前使用的linux-5.19.tar.xz
的内核,但是在使用的时候有一些问题,把内核切换到linux-5.10.tar.xz
之后测试是OK。
tar -xvf linux-5.10.tar.xz
cd linux-5.10
sudo cp -fr ../busybox-1.35.0/_install _install_arm64
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make defconfig
make menuconfig
menuconfig
按照下面的方式去配置
-> General setup
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
(_install_arm64) Initramfs source file (s)
...
-> Device Drivers
[*] PCI support
[*] Block devices
[*] Virtio block driver
[*] Virtio drivers
[*] PCI driver for virtio devices
[*] Support for legacy virtio draft 0.9.X and older devices
[*] Virtio balloon driver
[*] Virtio input driver
[*] Platform bus driver for memory mapped virtio devices
[*] Memory mapped virtio devices parameter parsing
make all -j16
sudo dd if=/dev/zero of=rootfs.ext4 bs=1M count=8192
sudo mkfs.ext4 rootfs.ext4
sudo mkdir -p tmpfs
sudo mount -t ext4 rootfs.ext4 tmpfs/ -o loop
sudo cp -af _install_arm64/* tmpfs/
sudo umount tmpfs
sudo rm -rf tmpfs
sudo chmod 777 rootfs.ext4
qemu-system-aarch64 -machine virt -cpu cortex-a57 -machine type=virt -m 1024 -smp 4 -kernel arch/arm64/boot/Image --append "noinitrd root=/dev/vda rw console=ttyAMA0 loglevel=8" -drive if=none,file=rootfs.ext4,id=hd0 -device virtio-blk-device,drive=hd0 -netdev user,id=eth0 -device virtio-net-device,netdev=eth0 -nographic
[ 1.909965] TERM=linux
[ 2.076950] 9pnet_virtio: no channels available for device kmod_mount
mount: mounting kmod_mount on /mnt failed: No such file or directory
Please press Enter to activate this console.
[root@Tester ]#
[root@Tester ]#
[root@Tester ]# ls
bin home lost+found root test.txt var
dev lib mnt sbin tmp
etc linuxrc proc sys usr
[root@Tester ]#