[maixpy3 axpi] 编辑发布 debian 镜像与在 PC 上交叉编译程序

承接上文:[maixpy3 axpi] 关于爱芯 ax620a 移植 debian11 系统这件事

当你拥有了一个 rootfs 后,怎么编辑和发布它呢?又如何借助它进行交叉编译呢?

image

挂载可编辑 rootfs 作为回环设备

以下操作必须在一个 linux 环境中完成(如 Ubuntu ),需要用到的命令有 losetup 、mount 、mount 、 chroot ,如果在 docker 上启动 sudo losetup -P /dev/loop401 会失败,可能是 1、docker 内部没有 loop 设备 2、权限不足,创建容器时需要带有 root 权限:--privileged=true,若设备 /dev/loop401 不在则需要修改一下 loop 设备号即可。

在 img 镜像的目录下操作。

  • 挂载 img 到 rootfs 目录

sudo losetup -P /dev/loop401 ./sipeed_ax620a_debian11_*.img && sudo mkdir -p ./rootfs && sudo mount /dev/loop401p2 ./rootfs && ls ./rootfs && sudo mkdir -p ./boot && sudo mount /dev/loop401p1 ./boot && ls ./boot

在 macos 10.15.7 下用 docker 挂进 ubuntu 操作 ext4 吧,ext4fuse 已经弃用无人打理,只能读不能写。

  • 改变 rootfs 目录为 / 根目录

sudo chroot rootfs/

操作记录如下:

juwan@juwan-n85-dls:~/GIT_AX620_SDK_V0.31.0_P23/sipeed$ sudo losetup -P /dev/loop401 ./sipeed_ax620a_debian11_*.img && sudo mkdir -p ./rootfs && sudo mount /dev/loop401p2 ./rootfs && ls ./rootfs && sudo mkdir -p ./boot && sudo mount /dev/loop401p1 ./boot && ls ./bootboot && sudo mount /dev/loop401p1 ./boot && ls .bin   debuerreotype-epoch  etc        home  lost+found  mnt  param  root  sbin  sys  usr
boot  dev                  forcefsck  lib   media       opt  proc   run   srv   tmp  var
boot.bin  dtb.img  kernel.img  u-boot.bin
juwan@juwan-n85-dls:~/GIT_AX620_SDK_V0.31.0_P23/sipeed$ sudo chroot rootfs/
root@juwan-n85-dls:/# uname -a
Linux juwan-n85-dls 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 armv7l GNU/Linux
root@juwan-n85-dls:/# ls -l /home/
total 32
drwxrwxr-x 10 root root 4096 Sep 27 15:07 ax-samples
drwxr-xr-x  2 root root 4096 Sep 28 15:05 examples
drwxrwxr-x  2 root root 4096 Sep 21 19:28 fbv-1.0b
drwxrwxr-x  2 root root 4096 Sep 30 17:44 images
drwxr-xr-x  9 root root 4096 Sep 27 15:07 libmaix
drwxr-xr-x  2 root root 4096 Sep 30 17:49 models
drwxr-xr-x  2 root root 4096 Sep 30 17:46 res
drwxr-xr-x  2 root root 4096 Sep 21 19:28 systemd-usb-gadget

然后就可以当在板子里编译开发了,只是此时运行在你的电脑上,而非板子里,但编译运行借助的是板子里的 linux 环境,所以你可以在这里运行 apt update 之类的修改和配置软件系统。

出现错误chroot: failed to run command ‘/bin/bash’: Exec format error

这代表着需要配置 docker qemu 虚拟机 linux 在 docker 编译 arm 应用

  • 以 Windows WSL ubuntu20.04 为例安装 docker 环境( WSL docker 不可用 apt 安装,Linux 可以跳过)
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
$ sudo service docker start
  • 配置 docker 的 qemu 虚拟机 sudo docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes

操作记录如下:

dl@TABLET-DLS:~$ sudo losetup -P /dev/loop401 ./sipeed_ax620a_debian11_*.img && sudo mkdir -p ./rootfs && sudo mount /dev/loop401p2 ./rootfs && ls ./rootfs && sudo mkdir -p ./boot && sudo mount /dev/loop401p1 ./boot && ls ./boot
bin   debuerreotype-epoch  etc        home  lost+found  mnt  param  root  sbin  sys  usr
boot  dev                  forcefsck  lib   media       opt  proc   run   srv   tmp  var
boot.bin  dtb.img  kernel.img  u-boot.bin
dl@TABLET-DLS:~$ sudo chroot rootfs
chroot: failed to run command ‘/bin/bash’: Exec format error
dl@TABLET-DLS:~$ docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
dl@TABLET-DLS:~$ sudo docker run --rm --privileged multiarch/qemu-user-static --reset --persistent yes
Unable to find image 'multiarch/qemu-user-static:latest' locally
latest: Pulling from multiarch/qemu-user-static
f5b7ce95afea: Pull complete
22ee763f0af7: Pull complete
30abb83a18eb: Pull complete
0657daef200b: Pull complete
998118dcc3de: Pull complete
Digest: sha256:16abbe859db6a7d142d90125f3260008f698a224ccd0e776d554cfc46fd025b6
Status: Downloaded newer image for multiarch/qemu-user-static:latest
Setting /usr/bin/qemu-alpha-static as binfmt interpreter for alpha
Setting /usr/bin/qemu-arm-static as binfmt interpreter for arm
Setting /usr/bin/qemu-armeb-static as binfmt interpreter for armeb
Setting /usr/bin/qemu-sparc-static as binfmt interpreter for sparc
Setting /usr/bin/qemu-sparc32plus-static as binfmt interpreter for sparc32plus
Setting /usr/bin/qemu-sparc64-static as binfmt interpreter for sparc64
Setting /usr/bin/qemu-ppc-static as binfmt interpreter for ppc
Setting /usr/bin/qemu-ppc64-static as binfmt interpreter for ppc64
Setting /usr/bin/qemu-ppc64le-static as binfmt interpreter for ppc64le
Setting /usr/bin/qemu-m68k-static as binfmt interpreter for m68k
Setting /usr/bin/qemu-mips-static as binfmt interpreter for mips
Setting /usr/bin/qemu-mipsel-static as binfmt interpreter for mipsel
Setting /usr/bin/qemu-mipsn32-static as binfmt interpreter for mipsn32
Setting /usr/bin/qemu-mipsn32el-static as binfmt interpreter for mipsn32el
Setting /usr/bin/qemu-mips64-static as binfmt interpreter for mips64
Setting /usr/bin/qemu-mips64el-static as binfmt interpreter for mips64el
Setting /usr/bin/qemu-sh4-static as binfmt interpreter for sh4
Setting /usr/bin/qemu-sh4eb-static as binfmt interpreter for sh4eb
Setting /usr/bin/qemu-s390x-static as binfmt interpreter for s390x
Setting /usr/bin/qemu-aarch64-static as binfmt interpreter for aarch64
Setting /usr/bin/qemu-aarch64_be-static as binfmt interpreter for aarch64_be
Setting /usr/bin/qemu-hppa-static as binfmt interpreter for hppa
Setting /usr/bin/qemu-riscv32-static as binfmt interpreter for riscv32
Setting /usr/bin/qemu-riscv64-static as binfmt interpreter for riscv64
Setting /usr/bin/qemu-xtensa-static as binfmt interpreter for xtensa
Setting /usr/bin/qemu-xtensaeb-static as binfmt interpreter for xtensaeb
Setting /usr/bin/qemu-microblaze-static as binfmt interpreter for microblaze
Setting /usr/bin/qemu-microblazeel-static as binfmt interpreter for microblazeel
Setting /usr/bin/qemu-or1k-static as binfmt interpreter for or1k
Setting /usr/bin/qemu-hexagon-static as binfmt interpreter for hexagon
dl@TABLET-DLS:~$ sudo chroot rootfs/
root@TABLET-DLS:/# uname -a
Linux TABLET-DLS 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 armv7l GNU/Linux
root@TABLET-DLS:/# ls
bin   debuerreotype-epoch  etc        home  lost+found  mnt  param  root  sbin  sys  usr
boot  dev                  forcefsck  lib   media       opt  proc   run   srv   tmp  var
root@TABLET-DLS:/#

借助 rootfs 交叉编译程序

比如举例如何编译 libmaix 的 axpi 项目。

root@juwan-n85-dls:/# cd /home/libmaix/examples/axpi/
root@juwan-n85-dls:/home/libmaix/examples/axpi# python3 project.py build
-- SDK_PATH:/home/libmaix
-- project name: axpi
build now
[  3%] Built target gen_exe_src
-- Update build time and version info to header  config  at: ('/home/libmaix/examples/axpi/build/config/global_build_info_time.h', '/home/libmaix/examples/axpi/build/config/global_build_info_version.h')
[  3%] Built target update_build_info
[ 11%] Built target maix_cv_image
[ 80%] Built target libmaix
[ 88%] Built target main
[100%] Built target axpi
==================================
build end, time last:4.00s
==================================
root@juwan-n85-dls:/home/libmaix/examples/axpi# ./dist/start_app.sh 
[libmaix_camera_module_init]-90: ISP Sample. Build at Oct  8 2022 15:13:57
[libmaix_camera_module_init]-139: AX_NPU_SDK_EX_Init_with_attr failed, ret=0x80060087.
root@juwan-n85-dls:/home/libmaix/examples/axpi# ls /dev/
null    pts/    shm/    stderr  stdin   stdout  
root@juwan-n85-dls:/home/libmaix/examples/axpi# ls /dev/

需要注意的是在这里运行是不会成功的,比如上述的报错,因为电脑里没有相关的 dev 设备,所以需要你推送程序到板子里运行即可。

ax-sample 也同理:

root@juwan-n85-dls:/home/libmaix/examples/axpi# cd /home/ax-samples/build/
root@juwan-n85-dls:/home/ax-samples/build# make
[  4%] Built target ax_nanodet
[  8%] Built target ax_yolov3_tiny
[ 12%] Built target ax_models_load_inspect
[ 16%] Built target ax_yolov3
[ 20%] Built target ax_paddle_yolov3
[ 24%] Built target ax_yolo_fastest_body
[ 28%] Built target ax_paddle_mobileseg
[ 32%] Built target ax_yolov4_tiny
[ 36%] Built target ax_yolo_fastest
[ 40%] Built target ax_yolov4_tiny_3l
[ 44%] Built target ax_classification_nv12
[ 48%] Built target ax_yoloxs
[ 52%] Built target ax_classification
[ 56%] Built target ax_classification_accuracy
[ 60%] Built target ax_yolov5s
[ 64%] Built target ax_scrfd
[ 68%] Built target ax_cv_test
[ 72%] Built target ax_yolov3_accuracy
[ 76%] Built target ax_yolov5s_620u
[ 80%] Built target ax_hrnet
[ 84%] Built target ax_robot_obstacle
[ 88%] Built target ax_yolov7
[ 92%] Built target ax_yolov4
[ 96%] Built target ax_paddle_mobilehumseg
[100%] Built target ax_monodlex
root@juwan-n85-dls:/home/ax-samples/build# 

这样就不用忍受板子慢吞吞的编译速度啦,此时退出磁盘只需要卸载它们即可。

卸载 rootfs 保存修改内容到发布镜像

  • 卸载分区 sudo umount /dev/loop401p2 & sudo umount /dev/loop401p1

  • 卸载设备 losetup -d /dev/loop401

操作记录如下:

juwan@juwan-n85-dls:~/GIT_AX620_SDK_V0.31.0_P23/sipeed$ sudo umount /dev/loop401p2
juwan@juwan-n85-dls:~/GIT_AX620_SDK_V0.31.0_P23/sipeed$ sudo umount /dev/loop401p1
juwan@juwan-n85-dls:~/GIT_AX620_SDK_V0.31.0_P23/sipeed$ losetup -d /dev/loop401

此时你的所有改动都会保存到这个 img 镜像里,这意味着完成本次的 dd img 镜像的编辑与打包,拿着这个去给其他同学烧录即可。

是不是很简单呢?

还可以把板子里的根文件系统通过网络(或读卡器)挂载到 x86 机器上直接拉取文件来编译!

posted @ 2022-10-08 16:01  Juwan  阅读(1031)  评论(1编辑  收藏  举报