Oppretting av Ubuntu-bilde for ARM «from scratch»

Når utviklingen først begynner, kan det ofte være uklart hvilke pakker som skal gå inn i den målrettede rootfs.

Med andre ord, det er kanskje for tidlig å gripe tak i LFS, buildroot eller yocto (eller noe annet), men det er på tide å begynne. For de med solid ressurser (jeg har 4GB eMMC på mine pilotprototyper) kan løsningen være å dele ut en distribusjon til utviklerne som gjør det mulig å raskt levere noe som mangler for øyeblikket, og så kan vi alltid samle pakkelister og lage en liste for den målrettede rootfs.

Denne artikkelen har ikke noe nytt å tilby og er i hovedsak en enkel copy-paste instruksjon.

Målet med artikkelen er å samle Ubuntu rootfs for ARM-brett (i mitt tilfelle basert på Colibri imx7d).

Bygging av bildet

Vi lager den målrettede rootfs for distribusjon.

Pakk ut Ubuntu Base

Vi velger utgivelse basert på behov og egne preferanser. Her har jeg listet opp 20.

$ mkdir ubuntu20
$ cd ubuntu20
$ mkdir rootfs
$ wget http://cdimage.ubuntu.com/ubuntu-base/releases/20.04/release/ubuntu-base-20.04-base-armhf.tar.gz
$ tar xf ubuntu-base-20.04-base-armhf.tar.gz -C rootfs

Sjekk støtten for BINFMT i kjernen

Hvis du bruker en vanlig distribusjon, er støtten for BINFMT_MISC til stede og alt er konfigurert, hvis ikke — så er jeg sikker på at du vet hvordan du aktiverer støtten for BINFMT i kjernen.

Forsikre deg om at BINFMT_MISC er aktivert i kjernen:

$ zcat /proc/config.gz | grep BINFMT
CONFIG_BINFMT_ELF=y
CONFIG_COMPAT_BINFMT_ELF=y
CONFIG_BINFMT_SCRIPT=y
CONFIG_BINFMT_MISC=y

Nå må vi sjekke innstillingene:

$ ls /proc/sys/fs/binfmt_misc
qemu-arm  register  status
$ cat /proc/sys/fs/binfmt_misc/qemu-arm
enabled
interpreter /usr/bin/qemu-arm
flags: OC
offset 0
magic 7f454c4601010100000000000000000002002800
mask ffffffffffffff00fffffffffffffffffeffffff

Manuelt kan den registreres ved hjelp av for eksempel denne instruksen.

Innstilling av qemu statisk arm

Vi trenger nå et statisk bygget eksempel av qemu.

!!! OPPMERKSOMHET!!!
Hvis du planlegger å bruke en container for å bygge noe, se her:
https://sourceware.org/bugzilla/show_bug.cgi?id=23960
https://bugs.launchpad.net/qemu/+bug/1805913
Da må vi for x86_64 vert og arm gjest bruke i386 versjonen av qemu:
http://ftp.ru.debian.org/debian/pool/main/q/qemu/qemu-user-static_5.0-13_i386.deb

$ wget http://ftp.debian.org/debian/pool/main/q/qemu/qemu-user-static_5.0-13_amd64.deb
$ alient -t qemu-user-static_5.0-13_amd64.deb
# banen i rootfs og navnet på den kjørbare filen må samsvare med /proc/sys/fs/binfmt_misc/qemu-arm
$ mkdir qemu
$ tar xf qemu-user-static-5.0.tgz -C qemu
$ file qemu/usr/bin/qemu-arm-static
qemu/usr/bin/qemu-arm-static: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=be45f9a321cccc5c139cc1991a4042907f9673b6, for GNU/Linux 3.2.0, stripped
$ cp qemu/usr/bin/qemu-arm-static rootfs/usr/bin/qemu-arm
$ file rootfs/usr/bin/qemu-arm
rootfs/usr/bin/qemu-arm: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=be45f9a321cccc5c139cc1991a4042907f9673b6, for GNU/Linux 3.2.0, stripped

Chroot

Enkle skript:

ch-mount.sh

#!/bin/bash

function mnt() {
    echo "MOUNTING"
    sudo mount -t proc /proc ${2}proc
    sudo mount --rbind /sys ${2}sys
    sudo mount --make-rslave ${2}sys
    sudo mount --rbind /dev ${2}dev
    sudo mount --make-rslave ${2}dev
    sudo mount -o bind /dev/pts ${2}dev/pts
    sudo chroot ${2}
}

function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}proc
    sudo umount ${2}sys
    sudo umount ${2}dev/pts
    sudo umount ${2}dev

}

if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
    mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
    umnt $1 $2
else
    echo ""
    echo "Either 1'st, 2'nd or both parameters were missing"
    echo ""
    echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
    echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
    echo ""
    echo "For example: ch-mount -m /media/sdcard/"
    echo ""
    echo 1st parameter : ${1}
    echo 2nd parameter : ${2}
fi

Vi beundrer det oppnådde resultatet:

$ .\/ch-mount.sh -m rootfs\/\n# cat \/etc\/os-release\nNAME="Ubuntu"\nVERSION="20.04 LTS (Focal Fossa)"\nID=ubuntu\nID_LIKE=debian\nPRETTY_NAME="Ubuntu 20.04 LTS"\nVERSION_ID="20.04"\nHOME_URL="https:\/\/www.ubuntu.com\/"\nSUPPORT_URL="https:\/\/help.ubuntu.com\/"\nBUG_REPORT_URL="https:\/\/bugs.launchpad.net\/ubuntu\/"\nPRIVACY_POLICY_URL="https:\/\/www.ubuntu.com\/legal\/terms-and-policies\/privacy-policy"\nVERSION_CODENAME=focal\nUBUNTU_CODENAME=focal\n# uname -a\nLinux NShubin 5.5.9-gentoo-x86_64 #1 SMP PREEMPT Mon Mar 16 14:34:52 MSK 2020 armv7l armv7l armv7l GNU\/Linux

For curiosity, let's measure the size before and after installing a minimal set of packages (for me):

# du -d 0 -h / 2>/dev/null
63M     /

Let's update:

# apt update
# apt upgrade --yes

Let's install the packages we are interested in:

# SYSTEMD_IGNORE_CHROOT=yes apt install --yes autoconf kmod socat ifupdown ethtool iputils-ping net-tools ssh g++ iproute2 dhcpcd5 incron ser2net udev systemd gcc minicom vim cmake make mtd-utils util-linux git strace gdb libiio-dev iiod

Kernel header files and modules are a separate discussion. We certainly won't install the bootloader, kernel, modules, and device tree through Ubuntu. They will come to us from external sources, whether we compile them ourselves or receive them from the board manufacturer; in any case, this is beyond the scope of this guide.

To some extent, version mismatches are permissible, but it’s better to use them from the kernel build.

# apt install --yes linux-headers-generic

Let's see what we have got, and it's a lot:

# apt clean
# du -d 0 -h / 2>/dev/null
770M    /

Don't forget to set a password.

Packaging the image

$ sudo tar -C rootfs --transform "s|^.\/||" --numeric-owner --owner=0 --group=0 -c .\/ | tar --delete .\/ | gzip > rootfs.tar.gz

Additionally, we can install etckeeper with autopush configuration

Let's say we've distributed our build, work is in progress, how can we then collect various versions of our system?

Etckeeper can come to our aid.

Security is a personal matter for everyone:

  • you can protect certain branches
  • generate a unique key for each device
  • forbid force push
  • etc. ...
# ssh-keygen
# apt install etckeeper
# etckeeper init
# cd /etc
# git remote add origin ...

Let's configure autopush

Of course, we can also create branches on the device in advance (for example, create a script or service that will work on the first launch).

# cat /etc/etckeeper/etckeeper.conf
PUSH_REMOTE="origin"

Or we can do it more cleverly...

The lazy way

Let us have some unique identifier, such as the processor's serial number (or MAC — serious companies buy a range):

cat \/proc\/cpuinfo

# cat /proc/cpuinfo
processor       : 0
model name      : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 60.36
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

processor       : 1
model name      : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 60.36
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

Hardware        : Freescale i.MX7 Dual (Device Tree)
Revision        : 0000
Serial          : 06372509

Then we can use it as the name of the branch to which we will push:

# cat /proc/cpuinfo | grep Serial | cut -d':' -f 2 | tr -d [:blank:]
06372509

Let's create a simple script:

# cat /etc/etckeeper/commit.d/40myown-push
#!/bin/sh
set -e

if [ "$VCS" = git ] && [ -d .git ]; then
  branch=$(cat /proc/cpuinfo | grep Serial | cut -d':' -f 2 | tr -d [:blank:])
  cd /etc/
  git push origin master:${branch}
fi

And that's it — after a while we can check the changes and generate a list of packages for the target firmware.

Recommended materials

BINFMT_MISC
Kernel Support for miscellaneous Binary Formats (binfmt_misc)
Compiling with qemu user chroot
Building Ubuntu rootfs for ARM
How to create a custom Ubuntu live from scratch
Crossdev qemu-static-user-chroot
etckeeper

getdents64 issue

readdir() returns NULL (errno=EOVERFLOW) for 32-bit user-static qemu on 64-bit host
Ext4 64 bit hash breaks 32 bit glibc 2.28+
compiler_id_detection fails for armhf when using QEMU user-mode emulation
CMake doesn’t work properly under qemu-arm

Kilde: habr.com

Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere 🔥 Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere | ProHoster