本文记录下搭建qemu的arm linux 环境,虚拟机是 ubuntu 14.04.
kernel: 4.4.1
busybox:1.25.1
qemu:sudo apt-get install qemu,实际的版本如下:
jeyawn@ubuntu:~/Work$ qemu-system-arm --version
QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.46), Copyright (c) 2003-2008 Fabrice Bellard
kernel,busybox编译的方法可以按照网上其它的介绍
kernel的编译:
export CROSS_COMPILE=arm-linux-gnueabi-
export ARCH=arm
make vexpress_defconfig
make -j2
制作rootfs的脚本如下:
jeyawn@ubuntu:~/Work$ cat mkrootfs.sh
#!/bin/bash
sudo rm -rf rootfs
sudo rm -rf tmpfs
sudo rm -f a9rootfs.ext3
#创建根目录rootfs
sudo mkdir rootfs
#复制busybox命令到根目录下,注意busybox路径!划重点!
sudo cp -r busybox-1.27.2/_install/* rootfs/
cd rootfs
#从工具链中复制运行库到lib目录下
sudo mkdir lib proc sys dev etc etc/init.d
sudo cp -P /usr/arm-linux-gnueabi/lib/* lib/
#创建4个tty终端设备(c代表字符设备,4是主设备号,1 2 3 4分别是次设备号
sudo mknod dev/tty1 c 4 1
sudo mknod dev/tty2 c 4 2
sudo mknod dev/tty3 c 4 3
sudo mknod dev/tty4 c 4 4
sudo mknod -m 666 dev/console c 5 1
sudo touch etc/init.d/rcS
sudo chmod 0777 etc/init.d/rcS
sudo echo "#!/bin/sh" >> etc/init.d/rcS
sudo echo "mount -t proc none /proc" >> etc/init.d/rcS
sudo echo "mount -t sysfs none /sys" >> etc/init.d/rcS
sudo echo "mount -t tmpfs none /dev" >> etc/init.d/rcS
sudo echo "/sbin/mdev -s" >> etc/init.d/rcS
sudo echo "ifconfig eth0 192.168.206.110" >> etc/init.d/rcS
sudo chmod 0755 etc/init.d/rcS
#生成镜像
dd if=/dev/zero of=a9rootfs.ext3 bs=1M count=32
#格式化生成ext3文件系统
mkfs.ext3 a9rootfs.ext3
#将文件复制到镜像中
sudo mkdir tmpfs
sudo mount -t ext3 a9rootfs.ext3 tmpfs/ -o loop
sudo cp -r rootfs/* tmpfs/
sudo umount tmpfs
我们一般还要配置网络访问,那么这么弄,
1. 修改interfaces文件
jeyawn@ubuntu:/etc/network$ cat interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto eth0
auto br0
iface br0 inet dhcp
bridge_ports eth0
2. 修改qemu-ifup 以及 qemu-ifdown
jeyawn@ubuntu:/etc$ cat qemu-ifup
#! /bin/sh
echo sudo tunctl -u $(id -un) -t $1
sudo tunctl -u $(id -un) -t $1
echo sudo ifconfig $1 0.0.0.0 promisc up
sudo ifconfig $1 0.0.0.0 promisc up
echo sudo brctl addif br0 $1
sudo brctl addif br0 $1
echo brctl show
brctl show
sudo ifconfig br0 192.168.206.130 《----注意这里的ip与虚拟机分配的ip一致
----------------------------
jeyawn@ubuntu:/etc$ cat qemu-ifdown
#! /bin/sh
echo sudo brctl delif br0 $1
sudo brctl delif br0 $1
echo sudo tunctl -d $1
sudo tunctl -d $1
echo brctl show
brctl show
ifdown br0
ifup br0
编译配置完毕,就可以启动这个虚拟的板子,注意启动的时候需要用sudo 来提权
jeyawn@ubuntu:~/Work$ cat start.sh
#!/bin/bash
qemu-system-arm \
-M vexpress-a9 \
-m 512M \
-kernel linux-4.4.1/arch/arm/boot/zImage \
-dtb linux-4.4.1/arch/arm/boot/dts/vexpress-v2p-ca9.dtb \
-nographic \
-append "root=/dev/mmcblk0 rw rootfstype=ext4 console=ttyAMA0 init=/linuxrc ignore_loglevel" \
-sd a9rootfs.ext3 \
-net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
-redir tcp:2333::2333
这里我们就完成了linux的启动
如果是要gdb,那么建议安装gdb-multiarch,之后执行
jeyawn@ubuntu:~/Work$ gdb-multiarch ./linux-4.4.1/vmlinux
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./linux-4.4.1/vmlinux...done.
(gdb) b tcp_init_sock
Breakpoint 1 at 0x80419658: file net/ipv4/tcp.c, line 384.
(gdb) target remote localhost:1234
然后执行c就可以进入调试,相应的启动脚本更改为(如果不是gdb,则去掉 -S -s \)
jeyawn@ubuntu:~/Work$ cat gdb.sh
#!/bin/bash
qemu-system-arm \
-S -s \
-M vexpress-a9 \
-m 512M \
-kernel linux-4.4.1/arch/arm/boot/zImage \
-dtb linux-4.4.1/arch/arm/boot/dts/vexpress-v2p-ca9.dtb \
-nographic \
-append "root=/dev/mmcblk0 rw rootfstype=ext4 console=ttyAMA0 init=/linuxrc ignore_loglevel" \
-sd a9rootfs.ext3 \
-net nic,vlan=0 -net tap,vlan=0,ifname=tap0 \
-redir tcp:2333::2333