qemu-for-kernel-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseQEMU for Kernel Development
QEMU 内核开发
Purpose
目的
Guide agents through QEMU-based kernel and driver development workflows: building/booting custom kernels, attaching virtio block/network, using Buildroot or minimal initramfs, and rapid module test cycles — focused on kernel dev vs general .
skills/virtualization/qemu-kvm指导 Agent 完成基于 QEMU 的内核与驱动开发工作流:构建/启动自定义内核、接入 virtio 块设备/网络、使用 Buildroot 或最小 initramfs,以及进行快速的模块测试循环——重点聚焦内核开发,而非通用的 。
skills/virtualization/qemu-kvmWhen to Use
使用场景
- Test kernel patch without bare metal
- Develop driver against virtio or emulated DT platform
- CI kernel boot smoke test
- NFS or 9p root for fast edit-compile-test
- 无需裸机即可测试内核补丁
- 针对 virtio 或模拟 DT 平台开发驱动
- CI 内核启动冒烟测试
- 使用 NFS 或 9p root 快速执行编辑-编译-测试流程
Workflow
工作流
1. Minimal direct kernel boot (arm64 virt)
1. 最小化直接内核启动(arm64 virt)
bash
qemu-system-aarch64 \
-machine virt -cpu cortex-a57 -smp 4 -m 2G \
-kernel arch/arm64/boot/Image \
-append "console=ttyAMA0 root=/dev/vda rw" \
-drive if=virtio,file=rootfs.ext4,format=raw \
-netdev user,id=net0 -device virtio-net-device,netdev=net0 \
-nographicx86_64:
bash
qemu-system-x86_64 -enable-kvm -m 4G -smp 4 \
-kernel arch/x86/boot/bzImage \
-append "console=ttyS0 root=/dev/vda rw" \
-drive file=rootfs.img,format=raw,if=virtio \
-nographicbash
qemu-system-aarch64 \
-machine virt -cpu cortex-a57 -smp 4 -m 2G \
-kernel arch/arm64/boot/Image \
-append "console=ttyAMA0 root=/dev/vda rw" \
-drive if=virtio,file=rootfs.ext4,format=raw \
-netdev user,id=net0 -device virtio-net-device,netdev=net0 \
-nographicx86_64:
bash
qemu-system-x86_64 -enable-kvm -m 4G -smp 4 \
-kernel arch/x86/boot/bzImage \
-append "console=ttyS0 root=/dev/vda rw" \
-drive file=rootfs.img,format=raw,if=virtio \
-nographic2. Buildroot rootfs loop
2. Buildroot rootfs 循环
bash
cd buildroot
make qemu_aarch64_virt_defconfig
makebash
cd buildroot
make qemu_aarch64_virt_defconfig
makeoutput/images/Image, rootfs.ext4
output/images/Image, rootfs.ext4
Integrate custom kernel: set `BR2_LINUX_KERNEL_CUSTOM_VERSION` or external tree.
集成自定义内核:设置 `BR2_LINUX_KERNEL_CUSTOM_VERSION` 或使用外部内核树。3. Out-of-tree module test
3. 树外模块测试
bash
undefinedbash
undefinedon host — cross build
on host — cross build
make -C $KERNEL_SRC M=$PWD modules
make -C $KERNEL_SRC M=$PWD modules
in guest or via initramfs
in guest or via initramfs
insmod mydriver.ko
dmesg | tail
Share tree with 9p:
```bash
-fsdev local,id=dev,path=$PWD,security_model=none \
-device virtio-9p-pci,fsdev=dev,mount_tag=hostshareinsmod mydriver.ko
dmesg | tail
通过 9p 共享目录树:
```bash
-fsdev local,id=dev,path=$PWD,security_model=none \
-device virtio-9p-pci,fsdev=dev,mount_tag=hostshareguest: mount -t 9p -o trans=virtio hostshare /mnt
guest: mount -t 9p -o trans=virtio hostshare /mnt
undefinedundefined4. GDB stub for kernel
4. 内核 GDB stub
bash
qemu-system-aarch64 ... -s -S
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernelUse for live kernel debugging after boot.
CONFIG_KGDBbash
qemu-system-aarch64 ... -s -S
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernel使用 ,可在内核启动后进行实时内核调试。
CONFIG_KGDB5. Device tree overrides
5. 设备树覆盖
bash
qemu-system-aarch64 -machine virt -dtb myboard.dtb ...virt machine provides virtio devices; platform drivers can target QEMU models.
-devicebash
qemu-system-aarch64 -machine virt -dtb myboard.dtb ...virt machine 提供 virtio 设备;平台驱动可以针对 QEMU 的 模型进行开发。
-device6. Agent usage
6. Agent 用法
/qemu-for-kernel-development Boot arm64 virt with custom Image and virtio rootfs for driver CI/qemu-for-kernel-development Boot arm64 virt with custom Image and virtio rootfs for driver CICommon Problems
常见问题
| Symptom | Cause | Fix |
|---|---|---|
| Kernel panic no root | Wrong | |
| No console output | Missing | |
| Module vermagic error | Built against wrong tree | Same |
| Slow without KVM | TCG emulation | |
| DTB mismatch | Wrong machine | Use |
| 症状 | 原因 | 修复 |
|---|---|---|
| 内核 panic,找不到 root | | 对 virtio blk 使用 |
| 无控制台输出 | 命令行缺少 | |
| 模块 vermagic 错误 | 针对错误的内核树构建 | 与已启动的镜像使用相同的 |
| 未使用 KVM 时速度缓慢 | TCG 模拟 | 在 x86 host 上使用 |
| DTB 不匹配 | machine 不匹配 | 使用通过内核 |
Related Skills
相关 Skill
- — KVM, VFIO, libvirt
skills/virtualization/qemu-kvm - — bare-metal MCU QEMU
skills/qemu/qemu-embedded-simulation - — KUnit, kselftest
skills/kernel/kernel-testing - — DT for virt/platform
skills/kernel-dev/device-tree - — remote GDB basics
skills/debuggers/gdb
- — KVM、VFIO、libvirt
skills/virtualization/qemu-kvm - — 裸机 MCU QEMU
skills/qemu/qemu-embedded-simulation - — KUnit、kselftest
skills/kernel/kernel-testing - — 用于 virt/platform 的 DT
skills/kernel-dev/device-tree - — 远程 GDB 基础
skills/debuggers/gdb