Qemu模擬arm cpu運行Linux

Qemu模擬arm cpu運行Linux系統
本文介紹使用qemu虛擬arm cpu來運行和調試Linux系統的一般方法
一.首先需要構建tool-chain
可以使用crosstool-ng構建,從官網下載1.9.3版本,解壓後在源代碼根目錄運行,並假設$CTNG爲安裝目錄
./configure --prefix=$CTNG
make;make install
在源代碼目錄的samples目錄下有一個arm-unknown-linux-gnueabi的目錄,該目錄下的crosstool.config文件,把文件複製到$CTNG/bin目錄下並更名爲.config,這是crosstool編譯arm工具鏈的基本配置,我們可以在這個基礎上進行修改
執行如下的命令進入配置界面:
cd $CTNG/bin
./ct-ng menuconfig
進行如下的修改:

1. Paths and misc options->Local tarballs directory,這裏填寫編譯toolchain所需要的依賴tarball的下載目錄,由於編譯工具鏈可能會反覆多次,所以我們最好還是把下載的tarball保存下來,節省時間。

另外,ct-ng會在$CTNG/bin目錄下建立.build目錄,該目錄下的tarball目錄中建立了很多的軟連接就是指向這個local tarball目錄的,另外有一個src目錄就是解壓縮tarball的目錄

2.Prefix directory會指定最後工具鏈的存放位置,默認是~/x-tools/arm-unknown-linux-gnueabi
3.Target options,確認architecture是arm,確認Use EABI
4.Operating System,這一步很重要,其實不用修改這裏的選項,只需要記住這裏編譯的Linux kernel version,因爲後面我們編譯內核的時候,需要下載同樣版本的內核代碼,而且最好選擇2.6.32.25,因爲這是一個long-term stable ersion,如果使用custom tarballs的話,雖然工具鏈可以編譯通過,但是有可能運行內核的時候會panic

修改完配置之後,執行
./ct-ng build
運行過程中可能出現的問題
主要就是tarball下載失敗或者解壓縮失敗的情況,可以手動下載,然後存放到local tarball目錄中,然後到$CTNG/bin/.build/src目錄中把前一次解壓縮失敗的.extracting文件刪除,然後繼續即可

編譯完成後,將在~/x-tools/arm-unknown-linux-gnueabi/bin目錄下看到衆多的交叉編譯器命令,把該目錄加入到PATH中,後續會用到這些命令
二.製作root文件系統
1.編譯busybox
下載busybox1.21.1,解壓縮後,在源代碼根目錄執行
make defconfig
make menuconfig
需要修改的地方如下:
Busybox settings->Build options->Cross compiler prefix,這裏設置交叉編譯的前綴,設置爲arm-unknown-linux-gnueabi-,注意最後那個‘-’
Busybox settings->Installation options->busybox installation prefix,這裏設置最後busybox生成的根文件系統的目錄,這裏假設該目錄爲$RFS
其他就沒什麼修改的了,保存退出,執行
make ; make install
2.root fs
1)創建lib目錄,並從~/x-tools/arm-unknown-linux-gnueabi/arm-unknown-linux-gnueabi/sys-root/lib目錄中複製ld-linux.so.3,libc.so.6,libm.so.6這三個文件到該目錄
2)創建etc目錄,並在該目錄下創建如下的文件
fstab
none /proc proc   defaults   0 0
none        /dev/pts   devpts mode=0622  0 0
tmpfs       /dev/shm   tmpfs  defaults   0 0

groups
root:x:0

inittab
::respawn:/sbin/getty -L ttyAMA0 115200 xterm
# Stuff to do when restarting the init process                                 
::sysinit:/etc/init.d/rcS                                                      
::restart:/sbin/init                                                                                                      
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a   

passwd
root::0:0:root:/root:/bin/sh
3)創建etc/init.d目錄,並在該目錄中創建rcS文件
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
/sbin/mdev -s
並設置rcS爲可執行
chmod +x rcS
4)創建dev目錄,並在該目錄下執行
mknod console c 5 1
mknod null c 1 3
mknod ttyAMA0 c 204 64
5)創建mnt,proc,root,tmp目錄
6)從sourceforge上下載一個名爲genext2fs的程序,用來製作rootfs
genext2fs -b 32768 -d $RFS rootfs.img
該命令將創建32M的名爲rootfs.img的根文件系統
gzip -9v rootfs.img
該命令將把rootfs.img壓縮爲rootfs.img.gz,至此根文件系統就製作完畢了

三.編譯內核
1.下載2.6.32.25版本的linux內核,並解壓縮,在源代碼根目錄執行
make ARCH=arm versatile_defconfig
make ARCH=arm menuconfig
2.配置內核參數
1)Kernel Features->Use the ARM EABI to compile the kernel
                    Allow old ABI binaries to run with this kernel
2)ensure General Setup->Inital Ram filesystem and RAM Disk support checked
3)Boot Options->default kernel command string  : console=ttyAMA0
4)Device Drivers->Block devices->Ram block device support->Default ram disk size : 32768
5)如果需要調試內核的話,還要加上kernel hacking->compile the kernel with debug info
3.執行
make zImage ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnueabi-

四.在qemu中加載zImage
apt-get install qemu
執行如下的命令加載
qemu-system-arm -M versatilepb -s -m 256M -serial stdio -kernel {your-compile-dir}/arch/arm/boot/zImage -initrd {your-rfs-dir}/rootfs.img.gz -append "root=/dev/ram rw init=/linuxrc mem=256M console=ttyAMA0"

五.調試內核
在上述命令中加入-S參數,qemu會停下來在1234端口監聽
重新開一個terminal,執行
arm-unknown-linux-gnueabi-gdb -x "gdb.cmds" {your-kernel-dir}/vmlinux}
其中gdb.cmds的內容如下:
target remote localhost:1234
如果你喜歡有界面的ddd,可以使用如下命令
ddd --debugger arm-unknown-linux-gnueabi-gdb -x "ddd.cmds" $KERNEL_SRC/vmlinux
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章