qemu-arm-static

嵌入式开发有时会在ARM设备上使用ubuntu文件系统。开发者常常会面临这样一个问题,想预先交叉编译并安装一些应用程序,但是交叉编译的环境配置以及依赖包的安装十分繁琐,并且容易出错。想直接在目标板上进行编译和安装,但是ARM的资源和处理能力有限,会非常耗费时间。在这里给大家推荐一个ubuntu下好用的工具qemu-arm-static,这是QEMU用户模式下的ARM仿真器。

通过qemu-arm-static,我们在x86的ubuntu PC机上,可以模拟ARM处理器,就像运行在ARM上一样进行各种操作。这样既实现了ARM环境,又利用了x86 PC的处理能力。

安装使用

首先要确保在x86 ubuntu上使用root权限。

安装

apt install qemu-user-static

下载ubuntu arm文件系统并解压

wget http://cdimage.ubuntu.com/ubuntu-base/releases/xenial/release/ubuntu-base-16.04.6-base-armhf.tar.gz
mkdir rootfs
tar zxvf ubuntu-base-16.04.6-base-armhf.tar.gz -C rootfs/

配置

cp /usr/bin/qemu-arm-static rootfs/usr/bin/
cp /etc/resolv.conf rootfs/etc/resolv.conf (确保网络可用)
mount -t proc /proc rootfs/proc
mount -t sysfs /sys rootfs/sys
mount -o bind /dev rootfs/dev

chroot
切换root路径,进入ARM环境。此时你就可以当成OS和ubuntu真的运行在ARM处理器上了,做你想做的事情。

chroot rootfs
uname -a
Linux OptiPlex 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 armv7l armv7l armv7l GNU/Linux

qarm

上面的配置和使用方法是不是多少有些繁琐。我个人写了一个tool可以更加方便的使用qemu-arm-static,叫qarm。

git clone https://github.com/yangbolu1991/qarm.git
cd qarm
cp qarm /usr/sbin

使用起来非常便捷,help信息如下。

qarm
Usage:

This is a tool to use qemu-arm-static on ubuntu easily.

options
  - download  download and unpack ubuntu-base-16.04.6-base-armhf.tar.gz
              to ubuntu-rootfs/
  - chroot    change root directory for ARM emulation
  - pexit     post-exit. reverse operations of chroot, like,
              umounting /proc /sys /dev.
  - pack      pack ubuntu-rootfs/ to tar.gz

一共四个命令,下面是使用的示例。

# qarm download
downloading ubuntu-base-16.04.6-base-armhf.tar.gz ...
unpacking to ubuntu-rootfs/ ...

# ls
ubuntu-base-16.04.6-base-armhf.tar.gz  ubuntu-rootfs

# qarm chroot
# uname -a
Linux OptiPlex 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 armv7l armv7l armv7l GNU/Linux
# exit
exit

# qarm pexit

# qarm pack
packing into ubuntu-rootfs.tar.gz ...

# ls
ubuntu-base-16.04.6-base-armhf.tar.gz  ubuntu-rootfs  ubuntu-rootfs.tar.gz

本文地址:https://blog.csdn.net/yanceylu/article/details/108570423