嵌入式Linux开发-交叉编译开发环境
qemu-user-static
multiarch/qemu-user-static是通过 QEMU 和 binfmt_misc 启用不同架构的容器。
https://hub.docker.com/r/multiarch/qemu-user-static
# 方法1
# 在x86架构平台上启用以下容器
docker pull multiarch/qemu-user-static
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
# 之后可以运行其他arm架构的容器
docker run -itd --name pythonslim arm32v7/python:3.7-slim bash # 精简版python
docker run -itd --https://github.com/tonistiigi/binfmtname pandas -v $PWD:/work registry.cn-hangzhou.aliyuncs.com/latelee/python-pandas:arm bash # 某博主制作的pandas环境容器
https://github.com/tonistiigi/binfmt
# 方法2
# Installing emulators
docker run --privileged --rm tonistiigi/binfmt --install all
docker run --privileged --rm tonistiigi/binfmt --install arm64,riscv64,arm
# 测试
docker run --rm arm64v8/alpine uname -a
docker run --rm arm32v7/alpine uname -a
docker run --rm ppc64le/alpine uname -a
docker run --rm s390x/alpine uname -a
docker run --rm tonistiigi/debian:riscv uname -a
制作arm架构平台镜像
详情可见该博客
缘由:在使用pyinstaller等打包工具发现这些工具只能跨平台,不能跨架构进行交叉编译。所以选择在x86的ubuntu系统上构建 arm (linux_armv7l) 的 python 镜像,带 numpy、 pandas、sklearn等库,自行添加其他所需库。
主要过程是通过qemu使x86平台可以运行arm架构的容器,再在arm容器中做修改形成镜像,或者在arm容器中编译开发。
步骤
-
启用qemu
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
-
选择基础镜像
docker run -itd --name pythonslim arm32v7/python:3.7-slim bash
-
添加源
cat > /etc/apt/sources.list <<-EOF deb http://mirrors.aliyun.com/debian/ buster main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib deb http://mirrors.aliyun.com/debian-security buster/updates main deb-src http://mirrors.aliyun.com/debian-security buster/updates main deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib EOF
pip源
mkdir ~/.pip/ cat > ~/.pip/pip.conf <<-EOF [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = https://pypi.tuna.tsinghua.edu.cn EOF
-
安装编译环境&库
apt-get install gcc g++ gfortran python-dev libopenblas-dev libblas-dev liblapack-dev cython -y apt-get install libfreetype6-dev libpng-dev -y apt-get install pkg-config -y # 注:需要此工具找freetype apt-get install libfontconfig1-dev -y pip install numpy==1.18.1 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install pandas==0.23.4 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install scipy==1.4.1 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install Cython -i https://pypi.tuna.tsinghua.edu.cn/simple pip install sklearn -i https://pypi.tuna.tsinghua.edu.cn/simple 注:依赖scipy Cython pip install six -i https://pypi.tuna.tsinghua.edu.cn/simple pip install xlrd -i https://pypi.tuna.tsinghua.edu.cn/simple pip install pyparsing -i https://pypi.tuna.tsinghua.edu.cn/simple pip install python-dateutil -i https://pypi.tuna.tsinghua.edu.cn/simple pip install matplotlib==3.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple # 注:要freetype,先不安装 pip install pyhht -i https://pypi.tuna.tsinghua.edu.cn/simple # 注:需要 scipy、matplotlib
-
缩减体积
# 查看文件体积 # du -h --max-depth=1 136M ./tmp 26M ./var 2.5M ./sbin 3.2M ./bin 220M ./root 1.5M ./etc 835M ./usr 7.1M ./lib 1.2G
# 清除不必要的文件: apt-get autoremove python2 # bzip2 file会被删除 apt-get autoremove gcc g++ gfortran # libgomp1 binutils binutils-arm-linux-gnueabihf 会被删除 apt-get autoremove cython apt-get autoremove perl apt-get autoremove openssl rm /usr/bin/perl /usr/bin/perl5.28.1 # 补回被删除的包 apt-get install libgomp1 # 注:sklearn依赖此包 apt-get install bzip2 # 清除缓存 apt-get clean && rm -rf /var/lib/apt/lists/* rm -rf /root/.cache
-
打包容器为镜像
docker commit pythonslim python-pandas:arm
x86环境运行arm架构程序
sudo apt-get install qemu-user
qemu-arm -L /home/dev/toolchain/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc helloDemo