qemu-for-kernel-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

QEMU 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-kvm
。

When 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 \
  -nographic
x86_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 \
  -nographic
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 \
  -nographic
x86_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 \
  -nographic

2. Buildroot rootfs loop

2. Buildroot rootfs 循环

bash
cd buildroot
make qemu_aarch64_virt_defconfig
make
bash
cd buildroot
make qemu_aarch64_virt_defconfig
make

output/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
undefined
bash
undefined

on 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=hostshare
insmod mydriver.ko dmesg | tail

通过 9p 共享目录树:

```bash
-fsdev local,id=dev,path=$PWD,security_model=none \
-device virtio-9p-pci,fsdev=dev,mount_tag=hostshare

guest: mount -t 9p -o trans=virtio hostshare /mnt

guest: mount -t 9p -o trans=virtio hostshare /mnt

undefined
undefined

4. GDB stub for kernel

4. 内核 GDB stub

bash
qemu-system-aarch64 ... -s -S
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernel
Use
CONFIG_KGDB
for live kernel debugging after boot.
bash
qemu-system-aarch64 ... -s -S
gdb vmlinux
(gdb) target remote :1234
(gdb) break start_kernel
使用
CONFIG_KGDB
,可在内核启动后进行实时内核调试。

5. Device tree overrides

5. 设备树覆盖

bash
qemu-system-aarch64 -machine virt -dtb myboard.dtb ...
virt machine provides virtio devices; platform drivers can target QEMU
-device
models.
bash
qemu-system-aarch64 -machine virt -dtb myboard.dtb ...
virt machine 提供 virtio 设备;平台驱动可以针对 QEMU 的
-device
模型进行开发。

6. 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 CI

Common Problems

常见问题

SymptomCauseFix
Kernel panic no rootWrong
root=
root=/dev/vda
for virtio blk
No console outputMissing
console=
on cmdline
console=ttyAMA0
/
ttyS0
Module vermagic errorBuilt against wrong treeSame
KERNEL_SRC
as booted image
Slow without KVMTCG emulation
-enable-kvm
on x86 host
DTB mismatchWrong machineUse
virt
DTB from kernel
make dtbs
症状原因修复
内核 panic,找不到 root
root=
设置错误
对 virtio blk 使用
root=/dev/vda
无控制台输出命令行缺少
console=
console=ttyAMA0
/
ttyS0
模块 vermagic 错误针对错误的内核树构建与已启动的镜像使用相同的
KERNEL_SRC
未使用 KVM 时速度缓慢TCG 模拟在 x86 host 上使用
-enable-kvm
DTB 不匹配machine 不匹配使用通过内核
make dtbs
生成的
virt
DTB

Related Skills

相关 Skill

  • skills/virtualization/qemu-kvm
    — KVM, VFIO, libvirt
  • skills/qemu/qemu-embedded-simulation
    — bare-metal MCU QEMU
  • skills/kernel/kernel-testing
    — KUnit, kselftest
  • skills/kernel-dev/device-tree
    — DT for virt/platform
  • skills/debuggers/gdb
    — remote GDB basics
  • skills/virtualization/qemu-kvm
    — KVM、VFIO、libvirt
  • skills/qemu/qemu-embedded-simulation
    — 裸机 MCU QEMU
  • skills/kernel/kernel-testing
    — KUnit、kselftest
  • skills/kernel-dev/device-tree
    — 用于 virt/platform 的 DT
  • skills/debuggers/gdb
    — 远程 GDB 基础