Tailoring what is included in an image
The standard image that is part of the OpenSTLinux distribution includes a lot of things that would not be needed in a production environment - most notably the demo applications. Assuming you
built the image, it is possible to list the packages installed with the image by opening
tmp-glibc/deploy/images/stm32mp15-disco/st-image-weston-openstlinux-weston-stm32mp15-disco.rootfs.manifest
As it can be seen a lot of packages are installed. Most of them are pulled in by package groups. Those are the ones that start with packagegroup-. In the following we will build two images - one for deployment and one for development. The development image will be identical to the st-image-weston except we will remove demos and gstreamer. The deployment image will not have debug and ssh access.
Having a split like this allows including some unsafe practices in the development image (such as debug access, hardcoded credentials or SSH keys) that makes development easier.
To get started we do:
cd $BUILDDIR
bitbake-layers create-layer ../layers/meta-bdp
bitbake-layers add-layer ../layers/meta-bdp
mkdir -p ../layers/meta-bdp/recipes-core/images/
to add a new layer to the tree.
Now create ../layers/meta-bdp/recipes-core/images/bdp-image-deploy.bb and add the following contents:
SUMMARY = "Custom image based on st-image-weston"
require recipes-st/images/st-image-weston.bb
# Remove unwanted package groups
IMAGE_INSTALL:remove = " \
packagegroup-st-demo \
packagegroup-gstreamer1-0 \
packagegroup-core-eclipse-debug \ packagegroup-core-ssh-dropbear \
"
Now we can build this image:
bitbake bdp-image-deploy
Since this image is based on the image we already built, it will not take more than a few minutes to build.
Now we can program the new image to the SD card:
cd tmp-glibc/deploy/images/stm32mp15-disco
In there we now have a directory flashlayout_bdp-image-deploy that contains the files necessary to write the SD card, so we insert the SD card in the host machine, unmount all partitions and do:
scripts/create_sdcard_from_flashlayout.sh \
flashlayout_bdp-image-deploy/opteemin/FlashLayout_sdcard_stm32mp157f-dk2-opteemin.tsv
sudo dd if=FlashLayout_sdcard_stm32mp157f-dk2-opteemin.raw of=/dev/mmcblk0 bs=8M conv=fdatasync status=progress
Now we can boot our new image. As it can be seen the demos no longer are available.
Now it is time to create the development image. We will
reate ../layers/meta-bdp/recipes-core/images/bdp-image-devel.bb and add the following contents:
SUMMARY = "Custom image based on bdp-image-deploy"
require bdp-image-deploy.bb
# Install wanted packages
IMAGE_INSTALL:append = " \
packagegroup-core-eclipse-debug \
packagegroup-core-ssh-dropbear \
nano \
wifi-profiles \
"
This is enabling debug and SSH as well as nano as editor (as I am not a big vi fan). Furthermore we pull in wifi-profiles that is a custom recipe that will setup wifi and install a public key to use for login over SSH.
Creating the recipe
First we need to make sure to have an identity to use for SSH. On your host:
ls ~/.ssh
If you have a file called id_ed25519.pub you are good to go. If not you should:
ssh-keygen -t ed25519 -C "your_email@example.com"
to generate a keypair for use with the board.
Now we add a new directory for the files that we need to install in the image. On your host:
cd $BUILDDIR
cd ../layers/meta-bdp
mkdir -p recipes-connectivity/wifi-profiles/files
cd recipes-connectivity/wifi-profiles/filesWe then generate the ssh authorized keys:
cp ~/.ssh/id_ed25519.pub authorized_keys
and we generate the service file for wifi:
cat << EOF > 51-wireless.network
[Match]
Name=wlan0
[Network]
DHCP=ipv4
EOF
... and the wlan0 setup for wpa-supplicant:
cat << EOF > wpa_supplicant-wlan0.conf
ctrl_interface=/var/run/wpa_supplicant
eapol_version=1
ap_scan=1
fast_reauth=1
EOF
wpa_passphrase SSID PASSPHRASE >> wpa_supplicant-wlan0.conf
In the last line you need to replace SSID and PASSPHRASE with the SSID and password of the network you connect to.
Now it is time to build the recipe. It is put in ../wifi-profiles.bb and looks like this:
SUMMARY = "WiFi configuration"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "\
file://wpa_supplicant-wlan0.conf \
file://51-wireless.network \
file://authorized_keys \
"
S = "${WORKDIR}"
do_install() {
# ---- wireless network definition ----
install -d ${D}/usr/lib/systemd/network
install -m 0644 ${WORKDIR}/51-wireless.network ${D}/usr/lib/systemd/network/51-wireless.network
# ---- wlan0 definition ----
install -d ${D}/etc/wpa_supplicant
install -m 0644 ${WORKDIR}/wpa_supplicant-wlan0.conf ${D}/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
# ---- SSH AUTHORIZED KEY ----
install -d ${D}/home/root/.ssh
install -m 0600 ${WORKDIR}/authorized_keys ${D}/home/root/.ssh/authorized_keys
}
# Enable the wifi link in systemd. This makes it autostart
# but not first time
pkg_postinst_ontarget:${PN}() {
systemctl enable wpa_supplicant@wlan0.service
}
# List the files that this recipe adds
FILES:${PN} = "\
/usr/lib/systemd/network/51-wireless.network \
/etc/wpa_supplicant/wpa_supplicant-wlan0.conf \
/home/root/.ssh/authorized_keys \
"
Finally we create meta-bdp/recipes-core/dropbear/dropbear_%.bbappend to change the ssh behavior from disallowing root login to disallowing password login by replacing the -w argument with -s -g. That way we can still log in as root with the public key. A .bbappend file attaches to the build of the original file. The _% means that we will attach to any version of this file. In other words we are adding steps to do after that file is parsed, and it looks like this:
do_install:append() {
sed -i 's/-w/-s -g/g' ${D}/etc/default/dropbear
}
Now it is a good time to check the layer. It should have this structure:
├── conf
│ └── layer.conf
├── COPYING.MIT
├── README
├── recipes-connectivity
│ └── wifi-profiles
│ ├── files
│ │ ├── 51-wireless.network
│ │ ├── authorized_keys
│ │ └── wpa_supplicant-wlan0.conf
│ └── wifi-profiles.bb
└── recipes-core
├── dropbear
│ └── dropbear_%.bbappend
└── images
├── bdp-image-deploy.bb
└── bdp-image-devel.bb
Then we do the following to build and write this to the SD card (save this in a script - when adding new recipes later we will rerun this):
cd $BUILDDIR
bitbake bdp-image-devel
cd tmp-glibc/deploy/images/stm32mp15-disco/
rm FlashLayout_sdcard_stm32mp157f-dk2-opteemin.raw
scripts/create_sdcard_from_flashlayout.sh flashlayout_bdp-image-devel/opteemin/FlashLayout_sdcard_stm32mp157f-dk2-opteemin.tsv
sudo umount `lsblk --list | grep mmcblk0 | grep part | gawk '{ print $7 }' | tr '\n' ' '`
sudo dd if=FlashLayout_sdcard_stm32mp157f-dk2-opteemin.raw of=/dev/mmcblk0 bs=8M conv=fdatasync status=progress
Now it is time to check the connectivity by using the terminal that is offered via UART (so running picocom -b 115200 /dev/ttyACM0) - so not the terminal on the host machine. After first boot the wifi service is enabled, but not started (due to a race condition in the setup process). So do a single reboot:
shutdown -r now
Then after coming up again:
ifconfig
You should now see the wlan0 interface having an IP address. And you can
ping google.com
To check the connection to the internet. Now the card has internet access via wifi, and it also (thanks to ZeroConf) has a name on your network. Looking at the prompt in picocom, it says something like
root@stm32mp15-disco-xx-yy-zz:~#
The part in bold is your cards hostname (which can also be seen in /etc/hostname) and it will respond to a DNS lookup with that name. So if you:
ping stm32mp15-disco-xx-yy-zz
from your host PC with the right substitution for xx-yy-zz you will get an answer from the board. Now - you can also go
ssh root@stm32mp15-disco-xx-yy-zz
Note that if you have rebuilt the image, ssh may complain that the fingerprint of the host have changed. To fix this you do as the error message tells you :)
Now we are ready for software development