1.编译与安装qemu时需要添加debug选项,即--enable-debug,--enable-debug-info
2.编译内核,根据需要,选择编译选项(bzImage)
3.编译安装BusyBox
Busybox Settings --->
--- Build Options
[*] Build BusyBox as a position independent executable
4.制作initramfs
#!/usr/bin/bash
ROOTFS=rootfs
BUSYBOX=$(find busybox* -maxdepth 0 -type d)
SYSROOT=$(x86_64-linux-gnu-gcc --print-sysroot)
GLIBC_VERSION=$(${SYSROOT}/usr/bin/ldd --version | head -1 | cut -d' ' -f4)
DYNAMIC_LIB_PATH_32=${SYSROOT}/lib
DYNAMIC_LIB_PATH_64=${SYSROOT}/lib64
rm -rf $ROOTFS
mkdir -p ${ROOTFS}/{proc,sys,dev,etc,etc/init.d,lib,lib64,mnt,tmp,go}
cat > $ROOTFS/etc/init.d/rcS <<EOF
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
ifconfig lo up
EOF
chmod +x $ROOTFS/etc/init.d/rcS
cat > $ROOTFS/etc/inittab <<EOF
# /etc/inittab
::sysinit:/etc/init.d/rcS
::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
EOF
chmod +x $ROOTFS/etc/inittab
cp -rP ${BUSYBOX}/_install/* ${ROOTFS}
cp -rP ${DYNAMIC_LIB_PATH_64}/libc-${GLIBC_VERSION}.so ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/libc.so.6 ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/libm-${GLIBC_VERSION}.so ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/libm.so.6 ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/librt-${GLIBC_VERSION}.so ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/librt.so.1 ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/libpthread-${GLIBC_VERSION}.so ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/libpthread.so.0 ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/ld-${GLIBC_VERSION}.so ${ROOTFS}/lib64/
cp -rP ${DYNAMIC_LIB_PATH_64}/ld-linux-x86-64.so.2 ${ROOTFS}/lib64/
cd ${ROOTFS}
ln -sf bin/busybox init
find . | cpio -o --format=newc > ../initramfs
4.调试qemu时,命令如下:
gdb -tui --args qemu-system-x86_64 -nographic \
-kernel ./bzImage -initrd ./initramfs -append "console=ttyS0 quiet"
5.利用GDB调试Qemu程序。
6.引用
https://siweixiang.github.io/notes/2016_06_19_build_minimal_linux_hack_env_with_busybox_x86_64.html