# 使用qemu建立一個linux核心和busybox根檔案系統 1. [取得工具鏈(toolchain)->arm-2014.05-28-arm-none-eabi-i686-pc-linux-gnu](https://elinux.org/ARMCompilers) ```bash= 1. wget https://sourcery.mentor.com/GNUToolchain/package12813/public/arm-none-linux-gnueabi/arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 2. tar xvf arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 3. export PATH=$PATH:<path to "arm-2014.05/bin"> ``` 2. 編譯linux核心 ```bash= 1. git clone https://github.com/torvalds/linux.git 2. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- distclean 3. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- vexpress_defconfig 4. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- 5. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- modules 6. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- uImage LOADADDR=0x60000000(需要有mkimage(可從u-boot編譯後的tools資料夾裡面取得,否則會失敗) 7. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- dtbs 8. qemu-system-arm -M vexpress-a9 -m 512 -kernel arch/arm/boot/zImage -nographic -append "console=ttyAMA0" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb 如果只是單獨只要要測試檔案系統和核心可以將步驟4~7用一道指令取代-> make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- all ``` 由於沒有根檔案系統,最後執行結果會出現kernel panic ![](https://i.imgur.com/isNWwmt.png) 3. 編譯busybox ```bash 1. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- defconfig 2. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- menuconfig->需更改編譯為static 3. make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- install 4. mkdir rootfs 5. cd rootfs 6. mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}} 7. cp -r ../_install/* . 8. 建立一個initial script(很重要!!) vim init ----------------- #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n" exec /bin/sh ----------------- 9. chmod +x init 10. find . -print0 | cpio --null -ov --format=newc | gzip -9 > ./initramfs-busybox-arm.cpio.gz(find . | cpio -o –format=newc) 11. qemu-system-arm -M vexpress-a9 -kernel <path to the linux source folder>/arch/arm/boot/zImage -initrd initramfs-busybox-arm.cpio.gz -append "console=ttyAMA0" -nographic -dtb <path to the linux source folder>/arch/arm/boot/dts/vexpress-v2p-ca9.dtb ``` 成功: ![](https://i.imgur.com/VG8it1h.png) 3. 之後編譯u-boot使它當作開機之後引入系統核心的媒介 參考資料: 1. [吃盡苦頭編譯busybox](https://www.gomcu.com/busybox/) 2. [基於QEMU搭建完整的虛擬ARM開發環境(uboot+linux+rootfs)](https://www.blogger.com/blogger.g?blogID=4879769000401443188#editor/target=post;postID=3812254851018070546;onPublishedMenu=allposts;onClosedMenu=allposts;postNum=0;src=postname) 3. [VFS: Cannot open root device “mtdblock2” or unknown-block(2,0)](https://www.crifan.com/resolved_vfs_cannot_open_root_device_quotmtdblock2quot_or_unknown-block_20/) 4. [吃盡苦頭編譯busybox](https://www.gomcu.com/busybox/) 5. [基於QEMU搭建完整的虛擬ARM開發環境(uboot+linux+rootfs](https://www.itread01.com/content/1547681225.html) 6. [Using QEMU for Embedded Systems Development, Part 1](https://opensourceforu.com/2011/06/qemu-for-embedded-systems-development-part-1/) 7. [交叉编译器的命名规则及详细解释](https://blog.csdn.net/LEON1741/article/details/81537529) 8. [arm交叉編譯器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi區別](https://www.itread01.com/content/1548021792.html) 9. [How to configure the Linux kernel](https://how-to.fandom.com/wiki/How_to_configure_the_Linux_kernel) ###### tags: `linux`