首先需要编译内核:
1、获取kernel源码,www.kernel.org
2、编译内核,这里以5.11为例:
进入内核源码目录,执行
make defconfig make menuconfig(选中 kernel hacking –> Kernel debugging kernel hacking –> Compile-time checks and compiler options –> compile the kernel with debug info
make
执行完成后,会生成一个arch/x86/boot/bzImage
3,下载busybox制作文件系统,https://busybox.net/downloads/,我这里用busybox-1.33.0.tar.bz2。
进入busybox源码目录,执行
make defconfig make menuconfig
编译并安装
make CONFIG_PREFIX=/opt/kernel-debug/rootfs install
拷贝并修改启动配置文件
cp -r examples/bootfloppy/etc /opt/kernel-debug/rootfs
修改inittab文件为
::sysinit:/etc/init.d/rcS
::askfirst:/bin/ash
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
拷贝内核
cp linux-5.11/arch/x86/boot/bzImage /opt/kernel-debug/
创建磁盘和磁盘镜像文件
qemu-img create -f raw disk.raw 1024M //这里创建1G的磁盘空间,读者可根据需要自行选择
设置磁盘格式
mkfs -t ext4 ./disk.raw
挂在磁盘
sudo mount -o loop disk.raw /opt/kernel-debug/rootfs
制作根文件系统
mkdir -p /opt/kernel-debug/rootfs
cd /opt/kernel-debug/rootfs
mkdir dev sys proc
至此,内核和文件系统就做好了,下面直接执行
qemu-system-x86_64 -m 1024M -smp 4 -kernel bzImage -drive format=raw,file=disk.raw -append "init=/linuxrc root=/dev/sda"
就可以运行起来了。
(未完待续)