What is rpi-image-gen?
rpi-image-gen is a lightweight, scriptable framework for generating Raspberry Pi OS–based images. It takes a base root filesystem (Raspberry Pi OS Lite or Full) and applies layered customizations to produce a ready-to-flash .img file.
In essence, it’s a reproducible dd workflow — automated, version-controlled, and highly configurable.
Why use it?
Unlike Yocto or Buildroot, rpi-image-gen doesn’t rebuild packages from source. Instead, it leverages Debian’s binary packages and the existing Raspberry Pi infrastructure. This allows you to:
- Add configuration files, services, and users
- Enable or disable systemd units
- Preinstall applications and dependencies
- Embed SSH keys, Wi-Fi credentials, and device IDs
It’s the fastest way to go from prototype to production, while keeping full control over your system setup.
Core concepts
A typical rpi-image-gen project consists of the following components:
- Base image — the official Raspberry Pi OS Lite
- Overlay directory — your additional files, services, and scripts
- Customization hooks — pre- and post-filesystem injection scripts
- Build configuration — defining rootfs, boot partitions, layout, and version tags
Together, these elements produce a consistent, bootable image that’s ready for provisioning.
Installation
Follow the installation instructions at https://github.com/raspberrypi/rpi-image-gen.
Example
A typical project might look like this:
deb12-cm5-min-desktop
├── README.md
├── config
│ └── deb12-cm5-min-desktop.yaml
├── device
│ ├── cm5-device.yaml
│ └── rootfs-overlay
│ ├── etc
│ │ ├── lightdm
│ │ └── skel
│ └── usr
│ └── local
└── layer
├── image
│ └── simple-dual-nosparse
│ ├── bdebstrap
│ ├── device
│ ├── genimage.cfg.in.btrfs
│ ├── genimage.cfg.in.ext4
│ ├── image.yaml
│ ├── mke2fs.conf
│ ├── pre-image.sh
│ └── setup.sh
└── packages.yamlThe most important files are the YAML configuration files — these define which packages, files, and settings are included in the generated image.
Configuration Example
config/deb12-cm5-min-desktop.yaml — defines global parameters such as hostname, user credentials, partition sizes, and SSH keys:
device:
layer: cm5-device
hostname: cm5-dev
user1: pi
user1pass: StrongPass!1
image:
layer: image-rpios-nosparse
rootfs_type: ext4
boot_part_size: 512M
root_part_size: 4G
compression: zstd
name: deb12-cm5-min-desktop
layer:
custom: base-packages
ssh:
pubkey_user1: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB" # your custom public key
pubkey_only: ylayer/packages.yaml — lists the software packages, configuration files, and custom additions, such as a Qt application:
# METABEGIN
# X-Env-Layer-Name: base-packages
# X-Env-Layer-Desc: Installs base packages
# for sw development.
# X-Env-Layer-Version: 1.0.0
# X-Env-Layer-Requires: debian-bookworm-arm64-multi,
# rpi-debian-bookworm,
# rpi-misc-utils,
# rpi-essential-base,
# rpi-misc-skel,
# rpi-user-credentials,
# systemd-net-min,
# fake-hwclock,
# openssh-server
# METAEND
---
mmdebstrap:
setup-hooks:
- echo "hello - no setup-hooks in base-packages"
packages:
# Firmware to enable SHIFT boot manager and EEPROM maintenance
- raspberrypi-bootloader
- rpi-eeprom
- raspberrypi-ui-mods
- lightdm
- lightdm-gtk-greeter
- labwc
- wf-panel-pi
- lxsession
- lxpanel
- pcmanfm
- lxterminal
- gvfs
- xserver-xorg
- xwayland
- wayland-utils
- wlr-randr
- x11-xserver-utils
- xdg-user-dirs
- xdg-utils
- policykit-1
# helper and utilities
- bash
- sudo
- vim-tiny
- less
- nano
- network-manager
- bash-completion
# library manager
- libinput10
- libinput-bin
- libinput-dev
# sqlite
- sqlite3
- libsqlite3-0
- libsqlite3-dev
customize-hooks:
- echo "hello - customize-hooks in base-packages"
- $BDEBSTRAP_HOOKS/enable-units "$1" lightdm
- sed "s|alias ls='ls --color=auto'|alias ll='ls -al --color=auto'|" /home/$IGconf_device_user1/.bashrc > $1/home/$IGconf_device_user1/.bashrc
- uchroot $1 'mkdir -m 0700 -p ${HOME}/.config/labwc'
- cp "${IGconf_device_assetdir}/rootfs-overlay/etc/skel/.config/labwc/autostart" "$1/home/$IGconf_device_user1/.config/labwc/autostart"
- cp "${IGconf_device_assetdir}/rootfs-overlay/etc/skel/.config/labwc/environment" "$1/home/$IGconf_device_user1/.config/labwc/environment"
- cp "${IGconf_device_assetdir}/rootfs-overlay/etc/skel/.config/labwc/rc.xml" "$1/home/$IGconf_device_user1/.config/labwc/rc.xml"
- chown "${IGconf_device_user1}:${IGconf_device_user1}" "$1/home/$IGconf_device_user1/.config/labwc/autostart"
- chown "${IGconf_device_user1}:${IGconf_device_user1}" "$1/home/$IGconf_device_user1/.config/labwc/environment"
- chown "${IGconf_device_user1}:${IGconf_device_user1}" "$1/home/$IGconf_device_user1/.config/labwc/rc.xml"For a complete working example, see: https://github.com/interelectronix/rpi-image-gen-projects/tree/main/deb12-cm5-min-desktop
Build command
To build the image, run:
rpi-image-gen build -S path-to-project-folder -c deb12-cm5-min-desktop.yamlCI/CD and reproducibility
Once your build scripts are deterministic, you can integrate rpi-image-gen into a CI/CD pipeline (e.g., GitHub Actions, GitLab CI, Jenkins).
Each build produces a traceable, versioned artifact, enabling reliable updates and easier debugging — a crucial step toward production-grade deployment.
Articles in this series
- Building a Production-Ready Linux for Raspberry Pi Compute Module 5
- From Stock OS to Production Platform
- Customizing Raspberry Pi OS with rpi-image-gen
- System Robustness — Designing an A/B Root Filesystem Layout
- Provisioning — Automating First Boot with rpi-sb-provisioner
- OTA and Lifecycle — Software Updates with SWUpdate
Sources
- rpi-image-gen: https://github.com/raspberrypi/rpi-image-gen
- rpi-sb-provisioner: https://github.com/raspberrypi/rpi-sb-provisioner
- SWUpdate: https://github.com/sbabic/swupdate
- swugenerator: https://github.com/sbabic/swugenerator