QEMU使我们在没有外设设备下常用的调试Linux软件,可以用来模拟系统启动;
并支持GDB,在没有外设的情况,用来学习和跟踪代码的最好工具之一。
我们在学习一个架构前,往往需要跟踪代码流程,那么qemu就是最适合的工具。
依赖环境
sudo apt-get install -y qemu gcc make gdb git figlet
sudo apt-get install -y libncurses5-dev iasl wget
sudo apt-get install -y device-tree-compiler
sudo apt-get install -y flex bison libssl-dev libglib2.0-dev
sudo apt-get install -y libfdt-dev libpixman-1-dev
sudo apt-get install -y python pkg-config u-boot-tools intltool xsltproc
sudo apt-get install -y gperf libglib2.0-dev libgirepository1.0-dev
sudo apt-get install -y gobject-introspection
sudo apt-get install -y python2.7-dev python-dev bridge-utils
sudo apt-get install -y uml-utilities net-tools
sudo apt-get install -y libattr1-dev libcap-dev
sudo apt-get install -y kpartx libsdl2-dev libsdl1.2-dev
sudo apt-get install -y debootstrap bsdtar
sudo apt-get install -y libelf-dev gcc-multilib g++-multilib
sudo apt-get install -y libcap-ng-dev libaio-dev
sudo apt-get install -y libcap-dev libattr1-dev figlet libssl-dev
相关代码下载
快速部署
可以通过以下链接快速部署:
https://github.com/vici-by/Linux-kernel-test/blob/main/kernel-test/vidi-env-init.sh
相关下载
Linux内核下载 | https://mirror.bjtu.edu.cn/kernel/linux/kernel/ |
---|---|
UBOOT下载 | ftp://ftp.denx.de/pub/u-boot |
busybox下载 | https://busybox.net/downloads/ |
QEMU模拟X86_64启动
QEMU模拟X86_64 linux
package.rar :网络配置文件
RunBiscuitOS.sh :qemu测试demo脚本
参考文档:用Qemu搭建x86学习环境
下载相关文件
mkdir x86_64 -p && cd x86_64
x86_64_dir=$(pwd)
export LINUX="linux-5.8.14"
export QEMU="qemu-5.1.0"
export BUSYBOX="busybox-1.32.0"
export EDK2="edk2-UDK2018"
wget https://mirror.bjtu.edu.cn/kernel/linux/kernel/v5.x/${LINUX}.tar.gz && \
tar -zxf ${LINUX}.tar.gz && rm ${LINUX}.tar.gz
wget https://download.qemu.org/${QEMU}.tar.xz && \
tar xJf ${QEMU}.tar.xz && rm ${QEMU}.tar.xz
wget https://busybox.net/downloads/${BUSYBOX}.tar.bz2 && \
tar jxf ${BUSYBOX}.tar.bz2 && rm ${BUSYBOX}.tar.bz2
wget https://github.com/tianocore/edk2/archive/UDK2018.zip && \
unzip edk2-${EDK2}.zip && rm ${EDK2}.zip
ln -s ${x86_64_dir}/${LINUX} ${x86_64_dir}/linux
ln -s ${x86_64_dir}/${QEMU} ${x86_64_dir}/qemu
ln -s ${x86_64_dir}/${BUSYBOX} ${x86_64_dir}/busybox
ln -s ${x86_64_dir}/${EDK2} ${x86_64_dir}/edk2
# 将package.rar放到当前目录,解压
sudo ./package/networking/bridge.sh
sudo cp ./package/networking/qemu-* /etc/
编译并支持qemu-system-x86_64
cd qemu
./configure --prefix=/opt/x86_64/qemu-x86-bin --target-list=x86_64-softmmu --gdb=/usr/bin/gdb \
--enable-linux-aio --enable-debug --enable-debug-info
sudo make && sudo make install
sudo echo "PATH=\$PATH:/usr/local/qemu_x86/bin" >> ~/.bashrc
source ~/.bashrc
which qemu-system-x86_64
编译内核
cd linux
make x86_64_defconfig
make menuconfig
// 这部分默认内核都有配置,这里强调下
General setup --->
[*]Initial RAM filesystem and RAM disk (initramfs/initrd) support
Device Driver --->
[*] Block devices --->
<*> RAM block device support
(153600) Default RAM disk size
make -j$(cat /proc/cpuinfo | grep processor | wc -l)
编译打包文件系统
可能需要下载相应版本编译器:http://ftp.gnu.org/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.gz 否则C程序运行不了
cd busybox
make menuconfig
Settings --->
--- Build Options
[*] Build static binary (no shared libs)
rm _install ; make -j$(cat /proc/cpuinfo | grep processor | wc -l) && make install
cd ../
# need root
BUSYBOX=$(realpath busybox)
OUTPUT=$(realpath ./)
ROOTFS_NAME=x86_64
ROOTFS_SIZE=200
FS_TYPE=ext4
sudo rm ${OUTPUT}/rootfs/ -rf
sudo rm x86_64.img
mkdir -p ${OUTPUT}/rootfs
sudo cp ${BUSYBOX}/_install/* ${OUTPUT}/rootfs -raf
sudo chown root.root ${OUTPUT}/rootfs/* -R
sudo cp ${BUSYBOX}/examples/bootfloppy/etc ${OUTPUT}/rootfs -arf
sudo sed -r "/askfirst/ s/.*/::respawn:-\/bin\/sh/" ${OUTPUT}/rootfs/etc/inittab -i
sudo mkdir -p ${OUTPUT}/rootfs/lib
sudo mkdir -p ${OUTPUT}/rootfs/proc
sudo mkdir -p ${OUTPUT}/rootfs/sys
sudo mkdir -p ${OUTPUT}/rootfs/tmp
sudo mkdir -p ${OUTPUT}/rootfs/root
sudo mkdir -p ${OUTPUT}/rootfs/var
sudo mkdir -p ${OUTPUT}/rootfs/mnt
sudo mkdir -p ${OUTPUT}/rootfs/dev
sudo mknod ${OUTPUT}/rootfs/dev/tty1 c 4 1
sudo mknod ${OUTPUT}/rootfs/dev/tty2 c 4 2
sudo mknod ${OUTPUT}/rootfs/dev/tty3 c 4 3
sudo mknod ${OUTPUT}/rootfs/dev/tty4 c 4 4
sudo mknod ${OUTPUT}/rootfs/dev/console c 5 1
sudo mknod ${OUTPUT}/rootfs/dev/null c 1 3
sudo echo "mount -t sysfs sysfs /sys" >> ${OUTPUT}/rootfs/etc/init.d/rcS
sudo echo "mount -t tracefs nodev /sys/kernel/tracing" >> ${OUTPUT}/rootfs/etc/init.d/rcS
sudo echo "mount -t debugfs nodev /sys/kernel/debug" >> ${OUTPUT}/rootfs/etc/init.d/rcS
dd if=/dev/zero of=${OUTPUT}/ramdisk bs=1M count=${ROOTFS_SIZE}
mkfs.ext4 -E lazy_itable_init=1,lazy_journal_init=1 -F ${OUTPUT}/ramdisk
mkdir -p ${OUTPUT}/tmpfs
sudo mount -t ${FS_TYPE} ${OUTPUT}/ramdisk ${OUTPUT}/tmpfs/ -o loop
sudo cp -raf ${OUTPUT}/rootfs/* ${OUTPUT}/tmpfs/
sudo umount ${OUTPUT}/tmpfs
mv ${OUTPUT}/ramdisk ${OUTPUT}/${ROOTFS_NAME}.img
sudo rm ${OUTPUT}/rootfs/ ${OUTPUT}/tmpfs -rf
# 挂载外接盘
dd if=/dev/zero of=Freeze.img bs=1M count=128
sudo mkfs.ext4 -F ./Freeze.img
mkdir -p freezeDir && FREEDIR=$(realpath freezeDir)
sudo mount -t ext4 -o loop ./Freeze.img ${FREEDIR}
# .... 拷贝测试文件
sync && sudo umount ${FREEDIR}
qemu 模拟启动
注:这部分需要主机开启VM虚拟化
kvm-ok命令检测
baiy@ubuntu:x86_64$ qemu-system-x86_64 -cpu help
x86 base base CPU model type with no features enabled
x86 host KVM processor with all supported host features
x86 max Enables all features supported by the accelerator in the current host
# common usual
/usr/local/qemu_x86/bin/qemu-system-x86_64 \
-smp 2 \
-cpu host \
-enable-kvm \
-m 512M \
-kernel linux/arch/x86/boot/bzImage \
-hda ./x86_64.img \
-hdb ./Freeze.img \
-nographic \
-append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8"
# support network
/usr/local/qemu_x86/bin/qemu-system-x86_64 \
-smp 2 \
-cpu host \
-enable-kvm \
-m 512M \
-kernel linux/arch/x86/boot/bzImage \
-hda ./x86_64.img \
-hdb ./Freeze.img \
-nographic \
-append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8" \
-net tap \
-device virtio-net-device,netdev=bsnet0,mac=E0:FE:D0:3C:2E:EE \
-netdev tap,id=bsnet0,ifname=bsTap0
# debug
/usr/local/qemu_x86/bin/qemu-system-x86_64 \
-smp 2 \
-cpu host \
-enable-kvm \
-m 512M \
-kernel linux/arch/x86/boot/bzImage \
-hda ./x86_64.img \
-hdb ./Freeze.img \
-nographic \
-append "root=/dev/sda rw rootfstype=ext4 console=ttyS0 init=linuxrc loglevel=8" \
-S -s
gdb vmlinux
target remote localhost:1234
b start_kernel
continue
QEMU模拟ARM64启动
环境搭建
# 相关软件包下载
[official git]
git clone git://git.denx.de/u-boot.git
git clone https://github.com/torvalds/linux
git clone git://busybox.net/busybox.git
[local download]
ftp://ftp.denx.de/pub/u-boot/
https://mirror.bjtu.edu.cn/kernel/linux/kernel/v4.x/
https://busybox.net/downloads/
交叉编译器下载
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
https://www.linaro.org/downloads/