Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable systemd service in Yocto

I am trying to enable a systemd service automatically after successful boot of my STM32MP1 based Avnger96 board. I am using Yocto Project as build system with Ubuntu 20.04. My image recipe to enable systemd service is example-systemd.bb:

.
.
inherit systemd

SRC_URI = "file://example.sh \
           file://example.service \
          "

S = "${WORKDIR}"

SYSTEMD_AUTO_ENABLE_${PN} = "enable"
SYSTEMD_PACKAGES = "${PN}"

SYSTEMD_SERVICE_${PN} = "example.service"

do_install_append() {

    install -d 644 ${D}${sysconfdir}/init.d
    install -m 0755 ${WORKDIR}/example.sh ${D}${sysconfdir}/init.d

    install -d ${D}${systemd_system_unitdir}
    install -m 0644 ${WORKDIR}/example.service ${D}${systemd_system_unitdir}
}

FILES_${PN} += "${sysconfdir}/init.d"
FILES_${PN} += "${systemd_system_unitdir}/example.service"

REQUIRED_DISTRO_FEATURES= " systemd"

And included in my main image in local.conf with IMAGE_INSTALL_append = " example-systemd"

After building the image, bitbake -e (YOUR_IMAGE) | grep ^DISTRO_FEATURES= shows systemd.

But in my rootfs I can't find systemd/system directory with above service file. And also when I run systemctl status example I get the error -sh: systemctl: command not found.

Update

After including DISTRO_FEATURES_append= " systemd" in the example-service.bb, in the rootfs etc/systemd/system/multi-user.target.wants/ is created and it contains example.service file. But doing cat example.service results in cat: example.service: No such file or directory. Is this expected?

Can anyone please let me know how to run this service with systemd and how to check if service is installed and executed from linux user space?

And also I have one more doubt: Can both Systemd and Sysvinit exist together and some services are run by systemd and others by sysvinit?

Your help will be much appreciated.

Thanks in advance.

P.S: Please let me know if any info is missing here

like image 997
Preeti Avatar asked Oct 25 '25 01:10

Preeti


2 Answers

Your project might still use systemv or something else as init manager instead of systemd.

You can enable systemd by adding these lines to your local.conf (for versions Kirkstone and newer):

DISTRO_FEATURES:append = " systemd usrmerge"
DISTRO_FEATURES_BACKFILL_CONSIDERED += "sysvinit"
VIRTUAL-RUNTIME_init_manager = "systemd"
VIRTUAL-RUNTIME_initscripts = "systemd-compat-units"

You can enable systemd by adding these lines to your local.conf (for versions Honister and older):

DISTRO_FEATURES_append = " systemd"
DISTRO_FEATURES_BACKFILL_CONSIDERED += "sysvinit"
VIRTUAL-RUNTIME_init_manager = "systemd"
VIRTUAL-RUNTIME_initscripts = "systemd-compat-units"
like image 181
Odysseus Avatar answered Oct 27 '25 00:10

Odysseus


If you are using Yocto Kirkstone or later, be sure to change the line

SYSTEMD_SERVICE_${PN} = "example.service"

To

SYSTEMD_SERVICE:${PN} = "example.service"

I ran into this issue when upgrading from Hardknott to Kirkstone. Yocto built and installed just fine, but none of my custom services were starting on boot. I was missing that : and it didn't throw an error on build.

like image 37
Dan Eisenhut Avatar answered Oct 26 '25 23:10

Dan Eisenhut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!