====== Python3 の追加 ====== \\ 機械学習などで使われることも多く、現在もっとも書籍が多く出ていて取り組みやすい Python を追加したファームウェアを作成してみます。 ===== 手順 ===== ==== root filesystem の準備 ==== [[mas1xx_devel:prepare_rootfs:start|root filesystem を準備]] して、わかりやすいように名前を変更しておきます(Optional)。 root@jammy64-dev:/home/user1/work/MAS1xx# ls -l mas1xx_jammy_python3_rootfs total 72 drwxr-xr-x 2 root root 4096 Apr 15 10:48 bin drwxr-xr-x 2 root root 4096 Apr 19 15:08 boot drwxr-xr-x 4 root root 4096 Sep 7 2017 dev drwxr-xr-x 82 root root 4096 Apr 25 09:54 etc drwxr-xr-x 3 root root 4096 Aug 13 2021 home drwxr-xr-x 15 root root 4096 Nov 11 09:29 lib drwxr-xr-x 2 root root 4096 Sep 7 2017 media drwxr-xr-x 7 root root 4096 May 29 2020 mnt drwxr-xr-x 3 root root 4096 Jun 8 2020 opt drwxr-xr-x 2 root root 4096 Apr 13 2016 proc drwx------ 4 root root 4096 Apr 7 12:33 root drwxr-xr-x 15 root root 4096 Nov 11 09:41 run drwxr-xr-x 2 root root 4096 Apr 15 10:48 sbin drwxr-xr-x 2 root root 4096 Sep 7 2017 srv drwxr-xr-x 2 root root 4096 Feb 5 2016 sys drwxrwxrwt 2 root root 4096 Apr 25 09:54 tmp drwxr-xr-x 11 root root 4096 Aug 31 2021 usr drwxr-xr-x 12 root root 4096 Aug 27 2018 var root@jammy64-dev:/home/user1/work/MAS1xx# \\ ==== root filesystem へ chroot ==== [[mas1xx_devel:customize_rootfs:start|chroot のスクリプト]]を使用し、MA-S1xx の root filesytem へ入ります。 root@jammy64-dev:/home/user1/work/MAS1xx# ./chroot_arm.sh mas1xx_jammy_python3_rootfs/ root@jammy64-dev:/# uname -a Linux jammy64-dev 5.15.0-70-lowlatency #77~20.04.1-Ubuntu SMP PREEMPT Wed Apr 5 18:52:27 UTC 2023 armv7l armv7l armv7l GNU/Linux root@jammy64-dev:/# \\ ==== パッケージデータベースの更新 ==== **apt update** でパッケージデータベースを更新し、必要であれば((重大なセキュリティアップデートなど))パッケージを更新しておきます。 root@jammy64-dev:/# apt update Hit:1 http://ppa.launchpad.net/mosquitto-dev/mosquitto-ppa/ubuntu jammy InRelease Hit:2 http://ports.ubuntu.com/ubuntu-ports jammy InRelease Hit:3 http://ports.ubuntu.com/ubuntu-ports jammy-updates InRelease Hit:4 http://ports.ubuntu.com/ubuntu-ports jammy-security InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done 5 packages can be upgraded. Run 'apt list --upgradable' to see them. root@jammy64-dev:/# apt upgrade Reading package lists... Done Building dependency tree... Done Reading state information... Done Calculating upgrade... Done The following packages have been kept back: initramfs-tools initramfs-tools-bin initramfs-tools-core kmod libkmod2 0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded. root@jammy64-dev:/# \\ ==== Python3 パッケージの追加 ==== Python3 本体と、使用したいライブラリがあればそれらを **apt install** で追加します。\\ [例] Python3 本体と、シリアルポート通信用ライブラリ pyseial を追加する例です。 root@jammy64-dev:/# apt install python3 python3-serial Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libmpdec3 libpython3-stdlib libpython3.10-minimal libpython3.10-stdlib media-types python3-minimal python3.10 python3.10-minimal Suggested packages: python3-doc python3-tk python3-venv python3-wxgtk3.0 | python3-wxgtk python3.10-venv python3.10-doc binutils binfmt-support The following NEW packages will be installed: libmpdec3 libpython3-stdlib libpython3.10-minimal libpython3.10-stdlib media-types python3 python3-minimal python3-serial python3.10 python3.10-minimal 0 upgraded, 10 newly installed, 0 to remove and 5 not upgraded. Need to get 5216 kB of archives. After this operation, 18.3 MB of additional disk space will be used. Do you want to continue? [Y/n] ※ ''Need to get 5216 kB of archives.'' というメッセージから、ファームウェア((圧縮ファイルシステム)) になった場合に増加するサイズをだいたい見積もることができます。 Need to get 5216 kB of archives. After this operation, 18.3 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main armhf libpython3.10-minimal armhf 3.10.6-1~22.04.2ubuntu1 [794 kB] Get:2 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main armhf python3.10-minimal armhf 3.10.6-1~22.04.2ubuntu1 [1941 kB] Get:3 http://ports.ubuntu.com/ubuntu-ports jammy-updates/main armhf python3-minimal armhf 3.10.6-1~22.04 [24.3 kB] ... 中略 ... Preparing to unpack .../3-libpython3.10-stdlib_3.10.6-1~22.04.2ubuntu1_armhf.deb ... Unpacking libpython3.10-stdlib:armhf (3.10.6-1~22.04.2ubuntu1) ... Selecting previously unselected package python3.10. Preparing to unpack .../4-python3.10_3.10.6-1~22.04.2ubuntu1_armhf.deb ... Unpacking python3.10 (3.10.6-1~22.04.2ubuntu1) ... Selecting previously unselected package libpython3-stdlib:armhf. Preparing to unpack .../5-libpython3-stdlib_3.10.6-1~22.04_armhf.deb ... Unpacking libpython3-stdlib:armhf (3.10.6-1~22.04) ... Setting up python3-minimal (3.10.6-1~22.04) ... Selecting previously unselected package python3. (Reading database ... 16354 files and directories currently installed.) Preparing to unpack .../python3_3.10.6-1~22.04_armhf.deb ... Unpacking python3 (3.10.6-1~22.04) ... Selecting previously unselected package python3-serial. Preparing to unpack .../python3-serial_3.5-1_all.deb ... Unpacking python3-serial (3.5-1) ... Setting up media-types (7.0.0) ... Setting up libmpdec3:armhf (2.5.1-2build2) ... Setting up libpython3.10-stdlib:armhf (3.10.6-1~22.04.2ubuntu1) ... Setting up libpython3-stdlib:armhf (3.10.6-1~22.04) ... Setting up python3.10 (3.10.6-1~22.04.2ubuntu1) ... Setting up python3 (3.10.6-1~22.04) ... running python rtupdate hooks for python3.10... running python post-rtupdate hooks for python3.10... Setting up python3-serial (3.5-1) ... Processing triggers for libc-bin (2.35-0ubuntu3.1) ... root@jammy64-dev:/# \\ ==== chroot 環境から出る ==== **exit** で chroot 環境から出ます。 root@jammy64-dev:/# exit exit root@jammy64-dev:/home/user1/work/MAS1xx# \\ ==== ファームウェアのビルド ==== [[mas1xx_devel:build_firmware:start|]] のとおり、ファームウェアをビルドします。 root@jammy64-dev:/home/user1/work/MAS1xx# ./build_firm.sh -k /home/user1/src/MAS1xx/linux-kernel-mchp -r mas1xx_jammy_python3_rootfs -o firmware/mas1xx_jammy_python3_v6_0_0.img MA-S1xx series Firmware build script * kernelrelease: 6.1.22-linux4microchip-2023.04-rc3 SYNC include/config/auto.conf.cmd ... 中略 ... unknown (119) staff (50) adm (4) * create initramfs ... update-initramfs: Generating /boot/initrd.img-6.1.22-linux4microchip-2023.04-rc3 /bin/sed: can't read /var/tmp/mkinitramfs_qHsu3k/scripts/init-top/udev: No such file or directory adding: kernel.img (stored 0%) adding: kmod.img (stored 0%) adding: initramfs.img (stored 0%) adding: rootfs.img (stored 0%) adding: .config (deflated 77%) adding: at91-centurysys-gemini-00.dtb (deflated 77%) adding: at91-centurysys-gemini-01.dtb (deflated 77%) adding: at91-centurysys-gemini-01.dtbo (deflated 64%) adding: at91-centurysys-gemini-02.dtb (deflated 77%) adding: at91-centurysys-gemini-02.dtbo (deflated 63%) adding: at91-centurysys-gemini-03.dtb (deflated 77%) adding: at91-centurysys-gemini-03.dtbo (deflated 63%) adding: at91-centurysys-gemini-07.dtbo (deflated 71%) * Firmware filename: /home/user1/work/MAS1xx/firmware/mas1xx_jammy_python3_v6_0_0.img done. please remove working directory '/home/user1/work/MAS1xx/build_1682386233' manually. root@jammy64-dev:/home/user1/work/MAS1xx# \\ ファームウェアのビルドが完了しました。 root@jammy64-dev:/home/user1/work/MAS1xx# ls -l firmware/mas1xx_jammy_python3_v6_0_0.img -rw-r--r-- 1 root root 93494104 Apr 25 10:31 firmware/mas1xx_jammy_python3_v6_0_0.img root@jammy64-dev:/home/user1/work/MAS1xx# \\ ビルドに出ているメッセージのとおり、作業ディレクトリを削除しておきます。 root@jammy64-dev:/home/user1/work/MAS1xx# rm -rf build_1682386233/ root@jammy64-dev:/home/user1/work/MAS1xx# \\ ===== 【参考】Python3 追加によるサイズ増分の確認 ===== さきほどの見積もりでは **5216 kB** ほど増加すると見積もりましたが、実際にどの程度サイズが増えたか確認してみます。 * ベースにした標準ファームウェアの root filesystem # unzip -lv firmware/mas1xx_jammy_v6_0_0.img|grep rootfs 78942208 Stored 78942208 0% 2023-04-19 15:08 8c166045 rootfs.img * Python3 を追加したファームウェアの root filesystem # unzip -lv firmware/mas1xx_jammy_python3_v6_0_0.img|grep rootfs 84148224 Stored 84148224 0% 2023-04-25 10:31 7634ca56 rootfs.img ''84148224 - 78942208 = 5206016'' と 5084KiB(5206KB) となり、だいたい見積もりしたくらいサイズが増えていることが確認できました。 \\