Add a systemd service file into a Yocto image

From KoanSoftware Wiki
Jump to: navigation, search

Add a systemd service file into a Yocto image

During KOAN training we use a meta-training layer to create an recipe building a helloworld executable.

In this article we want to create a recipe to add a systemd service file that starts such helloworld executable.

Enable systemd in the final image

If systemd is not enabled by default in your Yocto Project final image you are very likely still using SystemV. Add the following lines to the local.conf to enable systemd as default init manager.

DISTRO_FEATURES_append = " systemd"
VIRTUAL-RUNTIME_init_manager = "systemd"
VIRTUAL-RUNTIME_initscripts = "systemd-compat-units"

Create a recipe to add a systemd service file

For the helloworld application, we don’t necessarily need a service file as this application does not provide services to other parts of this system. However, for example purposes, we will create an autostart script to run hello at boot time. The output from this invocation will be available in the systemd logs using the journalctl command. This service file can also be manually invoked at runtime.

First, we will create the service file itself which is read and processed by systemd:

mkdir -p meta-training/recipes-example/systemd/files/

Edit a new configuration file

gedit meta-training/recipes-example/systemd/files/hello.service

containing the following code

Description=GNU Hello World startup script for KOAN training course



Now let’s add the recipe settings to integrate this into the systemd configuration for our build:

Edit a new configuration file

gedit meta-training/recipes-example/systemd/

containing the following code

inherit systemd

SYSTEMD_SERVICE_${PN} = "hello.service"

SRC_URI_append = " file://hello.service "
FILES_${PN} += "${systemd_unitdir}/system/hello.service"

do_install_append() {
  install -d ${D}/${systemd_unitdir}/system
  install -m 0644 ${WORKDIR}/hello.service ${D}/${systemd_unitdir}/system

Add the recipe to the image editing local.conf

IMAGE_INSTALL_append = " hellosystemd"

Now rebuild your image

bitbake core-image-minimal

Boot and verify that the service started and the output is visible in the systemd logs.