QEMU
There are two modes in QEMU:
- user mode: execute simple application code in different architecutre.
qemu-xxx
- system mode: need a kernel img file including the whole os system.
qemu-system-xxx
User mode
Compile a 32-bits i386 program:gcc -m32 hello.c -o hello
run in qemu by user mode: qemu-i386 ./hello
System mode
In system mode we need to provide kernel, rootfs and other arguments to qemu.
qemu-system-x86_64 -kernel xxx -initrd xxx -append xxx
Compile Kernel
After download linux source code, execute make
under root directory. When compile finished, vmlinux
is generated which is the raw kernel including debug info, and arch/x86/boot/bzImage
which is the compressed kernel。
Make RootFS
There are two ways, for convenience, only a simple program is written to initrd and is used as the init program. Alternatively, you can use busybox as the init program in initrd.
prepare a simple c program init.c
#include <stdio.h>
int main()
{
printf("hello world\n");
while(1);
return 0;
}
Then compile the code with -static
linking.gcc -staic -o init init.c
Then use the cpio program to package.echo init | cpio -o --format=newc > initrd_rootfs.img
Thus, an initrd-based rootfs is created.
Busybox
Debug the kernel in QEMU
When the kernel and rootfs are ready, we can start the QEMU with
qemu-system-x86_64 \
-kernel ./linux/arch/x86/boot/bzImage \
-initrd initrd_rootfs.img \
-append "root=/dev/ram rdinit=/init"