Skip to content
systemcoding.dev
Embedded Linux

Create a Custom Yocto Image Recipe Based on core-image

A custom image is a recipe that inherits core-image, sets IMAGE_FEATURES and IMAGE_INSTALL, and names the output formats. Here is a complete my-image recipe.

6 min read
Summarize in AI
Flat illustration of a single-board computer with its SoC, memory, header and ports, with the Linux, Raspberry Pi and NXP logos.

Adding a package to IMAGE_INSTALL in local.conf is the quick way to test something, and it is also the way to lose track of what your product contains: the image is defined by a personal file in one developer's build directory. The product's image belongs in a recipe in your own layer, where it is reviewed, versioned and built the same way by everyone. An image recipe is short, because the core-image class supplies the machinery, and the work is choosing what goes in. This post writes my-image on Scarthgap 5.0 LTS: the recipe, the two main variables (IMAGE_FEATURES and IMAGE_INSTALL), the output format and size settings, and the checks that show what the image contains. It is the first post in the images and SDK part of the Yocto guide, and it uses the layer from the create-layer post.

What an image recipe is

An image is a recipe whose output is a root filesystem plus files that package it: a disk image, a tarball, a compressed filesystem. Its task chain is different from a software recipe's: instead of do_compile and do_install, it has do_rootfs, which installs the chosen packages into a directory, and do_image, which turns that directory into files. The class image.bbclass provides both, and core-image.bbclass builds on it with the standard feature set used by Poky's images. Inheriting core-image is the usual start.

The recipe

In your layer, create recipes-core/images/my-image.bb:

Text
SUMMARY = "My product image"
DESCRIPTION = "A console image with SSH, the product daemon and basic tools."
LICENSE = "MIT"

inherit core-image

IMAGE_FEATURES += "ssh-server-dropbear"

IMAGE_INSTALL += " \
    sensord \
    hello \
    htop \
"

IMAGE_LINGUAS = ""
IMAGE_FSTYPES = "ext4 tar.bz2"
IMAGE_ROOTFS_EXTRA_SPACE = "65536"

Taking it a variable at a time.

**LICENSE = "MIT".** The recipe's own licence, covering the metadata. It does not describe the licences of the packages in the image, which the manifest reports.

**inherit core-image.** Brings in image.bbclass, sets IMAGE_INSTALL to a default that includes packagegroup-core-boot and the CORE_IMAGE_EXTRA_INSTALL hook, and defines how IMAGE_FEATURES expands into package groups. In Scarthgap the base value of IMAGE_INSTALL that the class sets is packagegroup-core-boot ${CORE_IMAGE_EXTRA_INSTALL}, which is a minimal bootable system: kernel, init, BusyBox and the base files. That is why you append with += and do not assign with =: a plain assignment would remove the boot packages and produce an image that does not start.

**IMAGE_FEATURES.** A list of named features that each pull in a set of packages or change how the rootfs is built. They are the portable way to ask for a capability. Common ones:

FeatureEffect
ssh-server-dropbearInstalls the Dropbear SSH server
ssh-server-opensshInstalls OpenSSH instead
package-managementKeeps the package manager and its database on the target
debug-tweaksAllows an empty root password and similar conveniences, for development only
tools-debugAdds gdb, strace and similar
dbg-pkgs, dev-pkgs, doc-pkgsInstall the -dbg, -dev and -doc package of everything in the image
read-only-rootfsBuilds the root filesystem for read-only mounting
splashAdds a boot splash screen

Features come from meta/classes-recipe/image.bbclass and the FEATURE_PACKAGES_* variables, documented in the reference manual at https://docs.yoctoproject.org/5.0/ref-manual/features.html. Prefer a feature to naming packages when one exists, since the feature adapts to the distribution and machine.

**IMAGE_INSTALL.** The explicit list of packages. Names are package names, not recipe names, though for simple recipes they coincide. Packages named here bring their RDEPENDS with them. The htop package is an example from a layer that provides it (meta-oe), so the layer must be in the build.

**IMAGE_LINGUAS = "".** Skips installing locale packages, which reduces size when the product uses only the default language.

**IMAGE_FSTYPES.** The output formats. Each type is produced by a conversion class (image_types.bbclass and friends): ext4, tar.bz2, squashfs, cpio.gz, wic and others. The default for a machine comes from the machine configuration, and setting it in your image overrides it. Several types can be built at once, with one file for each.

**IMAGE_ROOTFS_EXTRA_SPACE.** Extra free space to add to the filesystem image, in kilobytes. The value 65536 adds 64 MiB. Images are otherwise sized to their contents plus IMAGE_OVERHEAD_FACTOR, so a device that writes data at run time needs headroom.

Build and check

Shell
bitbake my-image
Text
NOTE: Tasks Summary: Attempted 3254 tasks of which 3198 didn't need to be rerun and all succeeded.

Then look at what was produced and what it contains:

Shell
ls tmp/deploy/images/qemux86-64/ | grep my-image
Text
my-image-qemux86-64.rootfs-20250101000000.ext4
my-image-qemux86-64.rootfs-20250101000000.manifest
my-image-qemux86-64.rootfs-20250101000000.tar.bz2
my-image-qemux86-64.rootfs.ext4
my-image-qemux86-64.rootfs.manifest
my-image-qemux86-64.rootfs.tar.bz2

The timestamped files are the artefacts, and the names without a timestamp are symbolic links to the latest. The manifest lists each installed package with its architecture and version, one per line. To check that your packages are in the image, search it:

Shell
grep -E '^(sensord|hello|dropbear) ' tmp/deploy/images/qemux86-64/my-image-qemux86-64.rootfs.manifest
Text
dropbear core2_64 2024.85
hello core2_64 1.0
sensord core2_64 1.0

Versions and architecture strings depend on your build. To see the final value of the package list for the recipe:

Shell
bitbake -e my-image | grep '^IMAGE_INSTALL='

and to see which features ended up active, including those added from local.conf's EXTRA_IMAGE_FEATURES:

Shell
bitbake -e my-image | grep '^IMAGE_FEATURES='

EXTRA_IMAGE_FEATURES from local.conf is merged into the image's IMAGE_FEATURES, which is how a developer adds debug-tweaks locally without changing the product recipe.

Reuse another image

If your image is a variation of an existing one, you can start from it with require:

Text
require recipes-core/images/core-image-minimal.bb

IMAGE_INSTALL += "sensord"

require includes the other recipe's content, so the image inherits its settings. It is neat, and it ties your recipe to the internals of the other. A fresh recipe that inherits core-image states exactly what you want, and does not change when OE-Core changes its example images. For products, prefer the fresh recipe.

Package groups

When the package list grows, group related packages into a package group recipe, and add the group to the image. A group is a recipe that inherits packagegroup and sets RDEPENDS for its packages:

Text
SUMMARY = "Product networking packages"
inherit packagegroup

RDEPENDS:${PN} = " \
    dropbear \
    iproute2 \
    ethtool \
"

Name it packagegroup-mycompany-net and install it with IMAGE_INSTALL += "packagegroup-mycompany-net". Groups keep image recipes readable, and let several images share a set of packages.

Other useful image settings

VariableUse
PACKAGE_EXCLUDEPackages that must not be installed; the build fails if something requires one
BAD_RECOMMENDATIONSRecommended packages to skip
NO_RECOMMENDATIONS = "1"Install no recommended packages
ROOTFS_POSTPROCESS_COMMANDShell or Python functions run on the root filesystem after assembly
IMAGE_NAME_SUFFIX, IMAGE_BASENAMENaming of the output files
WKS_FILEA wic kickstart that describes the partition layout of a disk image

Use ROOTFS_POSTPROCESS_COMMAND for last-step changes that no package should own, such as removing a file or writing a version stamp. Keep it small, and prefer packages for anything that belongs in the system.

Testing the result

Boot the image in QEMU:

Shell
runqemu qemux86-64 my-image nographic slirp
Text
Poky (Yocto Project Reference Distro) 5.0.5 qemux86-64 ttyS0

qemux86-64 login:

The debug-tweaks feature is added by the sample local.conf through EXTRA_IMAGE_FEATURES, so root logs in without a password in a development build. Your product build must not use it.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.