Note: Examples here are only for understanding purpose. Likely we will expand each command with actual/real usage.
Yocto provides a set of powerful commands to customize the root filesystem and images during the build process. These commands allow you to execute specific actions at various stages of the build, offering flexibility and control over the final output. Below is a table summarizing these commands, their purposes, and concise examples to help you get started.
| Yocto Command | Description | Example |
|---|---|---|
| ROOTFS_POSTPROCESS_COMMAND | After the root filesystem is created. | ROOTFS_POSTPROCESS_COMMAND += “chmod 755 ${IMAGE_ROOTFS}/custom_script.sh” |
| ROOTFS_PREPROCESS_COMMAND | Before the root filesystem is created. | ROOTFS_PREPROCESS_COMMAND += “echo ‘Starting pre-process'” |
| ROOTFS_POSTINSTALL_COMMAND | After packages are installed into the root filesystem. | ROOTFS_POSTINSTALL_COMMAND += “update-rc.d custom_service defaults” |
| ROOTFS_POSTUNINSTALL_COMMAND | After packages are removed from the root filesystem. | ROOTFS_POSTUNINSTALL_COMMAND += “rm -rf ${IMAGE_ROOTFS}/obsolete_files” |
| IMAGE_POSTPROCESS_COMMAND | After the image creation process. | IMAGE_POSTPROCESS_COMMAND += “gzip ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ext4” |
| IMAGE_PREPROCESS_COMMAND | Before the image creation process. | IMAGE_PREPROCESS_COMMAND += “echo ‘Preprocessing image…'” |
When and How to Use Yocto Commands
Understanding the execution timing and appropriate usage of each Yocto command is crucial for customizing your build process effectively. Below is a practical explanation of when each hook runs and what it’s best used for.
ROOTFS_POSTPROCESS_COMMAND
When it runs: After the root filesystem is created, but before it is packaged.
Use it for: Final adjustments to the root filesystem (permissions, creating directories, cleanup, adding small files).
Example: ROOTFS_POSTPROCESS_COMMAND += "chmod 755 ${IMAGE_ROOTFS}/custom_script.sh"
ROOTFS_PREPROCESS_COMMAND
When it runs: Before root filesystem creation starts.
Use it for: Preparing files/directories or preconditions needed during rootfs creation.
Example: ROOTFS_PREPROCESS_COMMAND += "echo 'Starting pre-process'"
ROOTFS_POSTINSTALL_COMMAND
When it runs: After packages are installed into the root filesystem, but before finalization.
Use it for: Post-install configuration tasks (enable services, adjust configs, run post-install fixups).
Example: ROOTFS_POSTINSTALL_COMMAND += "update-rc.d custom_service defaults"
ROOTFS_POSTUNINSTALL_COMMAND
When it runs: After packages are removed from the root filesystem.
Use it for: Cleanup of leftover files/directories after package removal.
Example: ROOTFS_POSTUNINSTALL_COMMAND += "rm -rf ${IMAGE_ROOTFS}/obsolete_files"
IMAGE_POSTPROCESS_COMMAND
When it runs: After the image is created and all files have been added.
Use it for: Operations on the final image (compression, checksum generation, signing, post-image tweaks).
Example: IMAGE_POSTPROCESS_COMMAND += "gzip ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ext4"
IMAGE_PREPROCESS_COMMAND
When it runs: Before image creation starts.
Use it for: Preparing anything required before the image step (staging inputs, setting up artifacts, sanity checks).
Example: IMAGE_PREPROCESS_COMMAND += "echo 'Preprocessing image...'"
Conclusion: Customize Your Yocto Builds with Ease
These Yocto hooks give you reliable points to customize both the root filesystem and the final image. Whether you need to add scripts, tweak configuration, enable services, or compress/sign artifacts, these commands provide clean “insertion points” in the build pipeline. Start small, validate outputs, and gradually expand with real project usage.
