Build and run the Linux kernel on QEMU
内核开发者Kuniyuki Iwashima有一篇文章Run the Linux kernel on QEMU,讲解了如何构建Linux Kernel并在QEMU下运行,本篇文章是一篇实操验证,并给出了一些注意事项。本文发行版本使用的是Ubuntu 22.04,工作目录为~/Linux-kernel,因此有些命令和原文有所不同。
下载Linux kernel源码
原文中是直接从<git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git>下载,实测速率会比较慢,可以选择其他国内的同步源,比如https://mirrors.tuna.tsinghua.edu.cn/git/linux.git。另外git默认会下载文件的全部记录,如果只是编译验证,其实只下载最近3次的即可,因此命令可以是:
git clone –depth 3 https://mirrors.tuna.tsinghua.edu.cn/git/linux.git
如果出现下载错误,则使用如下命令解决,之后重新下载。
git config –global http.version HTTP/1.1
git config –global http.postBuffer 524288000
git config –global fetch.parallel 1
编译Linux Kernel
执行配置
make x86_64_defconfig
安装必要的组件
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
编译执行
make -j 4
顺利的话大概需要10分钟左右编译完成,4核CPU16G内存的主机。
构建buildroot rootfs
构建准备
git clone –depth 1 git://git.buildroot.net/buildroot
使用上述命令会下载失败,因为git.buildroot.net目前无法使用,因此使用如下命令直接下载buildroot的稳定发布版本。
wget https://buildroot.net/downloads/buildroot-2026.08.2.tar.xz
下载后解压
tar Jxvf buildroot-2026.08.2.tar.xz
安装必要的组件
sudo apt install -y build-essential sed make binutils diffutils gcc g++ bash patch gzip bzip2 perl tar cpio unzip rsync file bc findutils wget libncurses5-dev libssl-dev flex bison python3 python3-pip device-tree-compiler git
配置buildroot,make menuconfig中需要选择在rootfs中支持的软件包,根据需要选择。
cd buildroot-2026.08.2/
make menuconfig
构建
开始构建,大概需要1-2个小时
make -j 4
遇到了elfutils下载文件时间很长的问题,使用手动方式下载下属文件(或者换个电脑环境下载),然后放到dl中指定目录下解决,具体是哪个目录可以看编译出错的提示。
https://sourceware.org/elfutils/ftp/0.195/elfutils-0.195.tar.bz2
下述命令只下载不编译,可以查看哪些下载出错了,以保证所有依赖都下载完成。
make source
如果在make menuconfig过程中选择的组件比较多,则大概率在生成ext4文件镜像的时候提示出错,需要在make menuconfig中修改ext文件的大小,从默认的60M修改为512M。 路径为:Filesystem images → ext2/3/4 root filesystem → ext2/3/4 root filesystem size(最后这个可能有些命名差异,找60M大小的那个选项,修改为512M)
完成修改后,重新make -j 4编译,可以看到output/images/目录下生成了rootfs.ext4文件。
修改必要的文件
挂载rootfs
sudo mkdir /mnt/buildroot
sudo mount -o loop ~/Linux-kernel/buildroot-2026.08.2/output/images/rootfs.ext4 /mnt/buildroot/
修改dhcp配置,依然自动分配IP地址。
vim /mnt/buildroot/etc/network/interfaces
| echo -e ‘\nauto eth0\niface eth0 inet dhcp’ | sudo tee -a /mnt/buildroot/etc/network/interfaces |
修改ssh配置,以便root无密码登录
sudo sed -i -e ‘s/#PermitRootLogin prohibit-password/PermitRootLogin yes/’ -e ‘s/#PermitEmptyPasswords no/PermitEmptyPasswords yes/’ /mnt/buildroot/etc/ssh/sshd_config
卸载rootfs
sudo umount /mnt/buildroot
启动系统
可以通过下述命令直接启动系统
qemu-system-x86_64 -boot c -m 2048M -kernel ~/Linux-kernel/linux/arch/x86/boot/bzImage -hda ~/Linux-kernel/buildroot-2026.08.2/output/images/rootfs.ext4 -append “root=/dev/sda rw console=ttyS0,115200 nokaslr” -serial stdio -display none -nic user,hostfwd=tcp::10022-:22
将ctrl+c传递给虚拟机内部,qemu退出使用”先按 Ctrl+a,松开后再按 x”,这样qemu中的系统就可以正常响应ctrl+c组合键了
qemu-system-x86_64 -boot c -m 2048M -kernel ~/Linux-kernel/linux/arch/x86/boot/bzImage -hda ~/Linux-kernel/buildroot-2026.08.2/output/images/rootfs.ext4 -append “root=/dev/sda rw console=ttyS0,115200 nokaslr” -serial mon:stdio -display none -nic user,hostfwd=tcp::10022-:22
移除dropbear组件
完成上述工作后,qemu启动后可以进入系统,但是通过ssh root@localhost -p 10022命令登录总是提示输入root密码,而我root已经设置免密登录了,后来才发现是在make menuconfig中dropbear和openssh都选择了,导致dropbear启动后openssh无法启动,去掉dropbear就好了。
make menuconfig中去掉dropbear的选择,并保存退出。
删除dropbear相关文件
rm -f output/target/etc/init.d/S50dropbear
find output/target -name “dropbear” -exec rm -fv {} \;
重新编译rootfs
make -j 4
因为rootfs中的文件都恢复成默认的,所以需要重新修改相关文件,再启动qemu模拟器。
最后解决这个问题的过程中,AI给了很多建议来修改openssh的配置以及虚拟机的配置,但是大的方向是错误的,因为根本原因是启动了dropbear而不是openssh,所以怎么改openssh的配置都解决不了问题。AI并不知道make menuconfig中同时配置了dropbear和openssh的这个信息,如果前期有这个输入,那么他大概率可以推断出是这个配置导致的,而不是一直在错误的指导,但是这一步是没有经过AI的,所以他不知道。这其实和人类有些类似,即使你去请教其他人,直觉上也会去排查openssh的问题,而不是怀疑dropbear引入的问题,除非通过top查看进程信息,或者openssh的日志信息,这就需要经验和判断了。