环境:Ubuntu 20.04 LTS, Linux ubuntu 5.4.0-29-generic
运行的qemu虚机为arm64。
完整详细操作命令如下:
# prepare environment. install arm64 cross compiler
apt install gcc-10-aarch64-linux-gnu
apt install build-essential
apt install gdb-multiarch
apt install libncurses5-dev
ln -s aarch64-linux-gnu-gcc-10 aarch64-linux-gnu-gcc
ln -s aarch64-linux-gnu-gcc-ar-10 aarch64-linux-gnu-gcc-ar
ln -s aarch64-linux-gnu-cpp-10 aarch64-linux-gnu-cpp
ln -s aarch64-linux-gnu-gcc-nm-10 aarch64-linux-gnu-gcc-nm
ln -s aarch64-linux-gnu-gcov-10 aarch64-linux-gnu-gcov
ln -s aarch64-linux-gnu-gcc-ranlib-10 aarch64-linux-gnu-gcc-ranlib
ln -s aarch64-linux-gnu-gcov-dump-10 aarch64-linux-gnu-gcov-dump
ln -s aarch64-linux-gnu-gcov-tool-10 aarch64-linux-gnu-gcov-tool
# get busybox and kernel source code
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.9.290.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/patch-4.9.290.xz
wget https://busybox.net/downloads/busybox-1.34.1.tar.bz2
# config and compile busybox
tar jxf busybox-1.34.1.tar.bz2
cd busybox-1.34.1/
make menuconfig
# make menuconfig setting
Settings --->
--- Build Options
[*] Build static binary (no shared libs)
Cross compiler prefix
aarch64-linux-gnu-
--- Installation Options ("make isntall" behavior)
What kind of applet lins to install (sl soft-links) --->
(./_install) Destination path for 'make isntall' (NEW)
make
make install
ls _install/
cd ..
mkdir -p qemu-arm/rootfs
cd qemu-arm/
cp ../busybox-1.34.1/_install/* rootfs/ -fr
cd rootfs/
mkdir dev etc lib proc sys tmp var root mnt
cd dev
mknod console c 5 1
mknod null c 1 3
mknod tty1 c 4 1
cd ..
# copy c run time lib
cp -a /usr/aarch64-linux-gnu/lib/*so* ./lib/
cd etc
cp -a ../../busybox-1.34.1/examples/bootfloppy/etc/* ./etc/
vim inittab
--->
::sysinit:/etc/init.d/rcS
::respawn:-/bin/sh
#tty2::askfirst:-/bin/sh
::askfirst:-/bin/sh
::ctrlaltdel:/bin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
<---
vim init.d/rcS
--->
#! /bin/sh
/bin/mount -a
/bin/mkdir -p /dev/pts
/bin/mount -t devpts devpts /dev/pts
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
<---
vim fstab
--->
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
tmpfs /dev tmpfs defaults 0 0
<---
vim profile
--->
# /etc/profile: system-wide .profile file for the Bourne shells
echo
echo -n "Processing /etc/profile... "
export HOSTNAME=aarch64
export USER=root
export HOME=/root
export PS1="[$USER@$HOSTNAME:\$PWD]\#"
export PATH=/bin:/sbin:/usr/bin/:/usr/sbin
export LD_LIBRARY_PATH=/lib:/usr/lib:$LD_LIBRARY_PATH
# no-op
echo "Done"
echo
<---
# build kernel
xz -d linux-4.9.290.tar.xz
tar xf linux-4.9.290.tar
mkdir kernel-source/
cp ../linux-4.9.290 kernel-source/ -fr
cd kernel-source/linux-4.9.290
cp ../../qemu-arm/rootfs _install -fr
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
# use default aarch64 config
make defconfig
make menuconfig
# change initramfs dir: _install
General setup --->
[*]Initial RAM filesystem and RAM disk (initramfs/initrd) support
(_install) Initramfs source file(s)
# delete default boot arg
Boot options --->
() Default kernel command string
# change address line to 48bit
Kernel Features --->
Page size(4KB) --->
Virtual address space size (48-bit) --->
# add debuginfo to kernel compile
Kernel hacking --->
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
# exit and save then make
make -j4
# run qemu virtual machine
cp arch/arm64/boot/Image.gz ../../qemu-arm/
cd ../../qemu-arm/rootfs/
qemu-system-aarch64 -machine virt -cpu cortex-a57 -nographic -smp 2 -m 2048 -kernel Image.gz -append "rdinit=/linuxrc console=ttyAMA0"
# gdb debug
# first console
qemu-system-aarch64 -machine virt -cpu cortex-a57 -nographic -smp 2 -m 2048 -kernel Image.gz -append "rdinit=/linuxrc console=ttyAMA0" -S -s
# second console
cd ../../kernel-source/linux-4.9.290/
gdb-multiarch vmlinux
target remove localhost:1234
b start_kernel
c
layout src
效果: