又花了半天的时间终于搞完了,实现了半自动化搭建环境.废话少说,开始了!!
主要思路:在Docker容器中交叉编译linux-3.16,busybox-1.20.2.在主机中制作根目录并搭建QEMU模拟硬件运行环境.
Docker镜像制作脚本Dockerfile如下:
FROM ubuntu:16.04
WORKDIR /root/
RUN apt-get update && apt-get install bc xz-utils bzip2 wget make gcc gcc-4.9-arm-linux-gnueabi vim -y
ADD busybox-1.20.2.tar.bz2 ./
ADD linux-3.16.tar.xz ./
COPY compile.sh mkvm.sh ./
由脚本可知需要在buid content(Dockerfile文件所在的目录)中准备四个文件(下载连接 busybox,linux):
## 源文件
busybox-1.20.2.tar.bz2
linux-3.16.tar.xz
## shell 脚本
compile.sh // 编译源文件
mkvm.sh // 从编译完成的文件中收集QEMU运行所需要的文件,并打包成vm.tar.bz2
#!/bin/bash
# fileName: compile.sh
# Date: Sun 17 Jun 2018 05:12:05 PM CST
# Author: light
# link arm-gcc
[ ! -f /usr/bin/arm-linux-gnueabi-gcc ] && ln -s /usr/bin/arm-linux-gnueabi-gcc-4.9 /usr/bin/arm-linux-gnueabi-gcc
# compile busybox
cd /root/busybox-1.20.2/
[ ! -f include/.libbb.h ] && cp include/libbb.h include/.libbb.h
sed '43i #include' include/.libbb.h > include/libbb.h # why? 请参考我的文章 交叉编译busybox
make defconfig
make CROSS_COMPILE=arm-linux-gnueabi-
make install CROSS_COMPILE=arm-linux-gnueabi-
# compile kernel
cd /root/linux-3.16/
make CROSS_COMPILE=arm-linux-gnueabi- ARCH=arm vexpress_defconfig
make CROSS_COMPILE=arm-linux-gnueabi- ARCH=arm
#!/bin/bash
# fileName: mkvm.sh
# Date: Sun 17 Jun 2018 05:13:40 PM CST
# Author: light
# collect files to rootfs/
cd /root/
mkdir -p vm/rootfs/{proc,etc/init.d,sys,tmp,root,var,mnt,lib,dev}
cp busybox-1.20.2/_install/* vm/rootfs/ -r
cp busybox-1.20.2/examples/bootfloppy/etc/* vm/rootfs/etc/ -r
cp /usr/arm-linux-gnueabi/lib/* vm/rootfs/lib/ -Pr
dd if=/dev/zero of=a9rootfs.ext3 bs=1M count=32
mkfs.ext3 a9rootfs.ext3
mv a9rootfs.ext3 vm/a9rootfs.ext3
cp linux-3.16/arch/arm/boot/dts/vexpress-v2p-ca9.dtb vm/
cp linux-3.16/arch/arm/boot/zImage vm/
# generate boot.sh mkrootfs.sh
cat > vm/boot.sh << EOF
qemu-system-arm -M vexpress-a9 -m 512M -kernel ./zImage -dtb ./vexpress-v2p-ca9.dtb -nographic -append "root=/dev/mmcblk0 console=ttyAMA0" -sd a9rootfs.ext3
EOF
cat > vm/mkrootfs.sh << EOF
sudo mknod ./rootfs/dev/tty1 c 4 1
sudo mknod ./rootfs/dev/tty2 c 4 2
sudo mknod ./rootfs/dev/tty3 c 4 3
sudo mknod ./rootfs/dev/tty4 c 4 4
mkdir tmpfs
sudo mount -t ext3 a9rootfs.ext3 tmpfs/ -o loop
sudo cp rootfs/* tmpfs/ -r
sudo umount tmpfs/
EOF
# tar
tar cjf vm.tar.bz2 vm/
rm -fr vm
此时build content中有五个文件:
light@light-Inspiron-3559 ~/Docker/arm $ ls
busybox-1.20.2.tar.bz2 compile.sh Dockerfile linux-3.16.tar.xz mkvm.sh
build一下,"镜子"就做好了:
light@light-Inspiron-3559 ~/Docker/arm $ docker build -t arm-compil ./
light@light-Inspiron-3559 ~/Docker/arm $ docker images | grep arm
arm-compile latest 1bb5ddf21eca About an hour ago 929MB
足足有1G....^.^```
构建容器:
light@light-Inspiron-3559 ~/Docker/arm $ docker run --name arm -ti arm-compil /bin/bash
编译源文件:
root@5845a78f3cc9:~# bash compile.sh
从编译完成的文件中收集QEMU运行所需要的文件,并打包成vm.tar.bz2:
root@5845a78f3cc9:~# bash mkvm.sh
32+0 records in
32+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.020181 s, 1.7 GB/s
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 32768 1k blocks and 8192 inodes
Filesystem UUID: 821b2aaf-7ce0-498b-b5f4-8d47824d68ec
Superblock backups stored on blocks:
8193, 24577
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
root@5845a78f3cc9:~# ls vm.tar.bz2
vm.tar.bz2
从容器中导出文件至host:
light@light-Inspiron-3559 ~/Docker/arm $ docker cp arm:/root/vm.tar.bz2 ./
light@light-Inspiron-3559 ~/Docker/arm $ ls vm.tar.bz2
vm.tar.bz2
解包,切换文件,构建根文件:
light@light-Inspiron-3559 ~/Docker/arm $ tar xjf vm.tar.bz2 ; cd vm
light@light-Inspiron-3559 ~/Docker/arm/vm $ bash mkrootfs.sh
[sudo] password for light:
启动:
light@light-Inspiron-3559 ~/Docker/arm/vm $ bash boot.sh
/ # cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 0 (v7l)
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc09
CPU revision : 0
Hardware : ARM-Versatile Express
Revision : 0000
Serial : 0000000000000000
/ #
相关文件已上传至github.
参考文章:
从零使用qemu模拟器搭建arm运行环境
================================================
修改排版问题 Sun Jun 17 19:22:32 CST 2018
================================================
添加相关文件github连接 Sun Jun 17 19:52:55 CST 2018