Search code examples
linuxyoctosystemdbitbakerecipe

run yocto package via systemd service in image


I have an yocto image with my own package appended. My recipe:

FILESEXTRAPATHS_append := ":${THISDIR}/../systemd/files"
inherit systemd
SRC_URI = "file://myserver.tar.gz"

SYSTEMD_AUTO_ENABLE = "enable"
SYSTEMD_SERVICE_${PN} = "serverAutoStart.service"

SRC_URI_append = " file://serverAutoStart.service"

FILES_${PN} += "${systemd_unitdir}/system/serverAutoStart.service"


do_compile() {
        make
}

#for myserver package
do_install() {
        install -m 0755 -d ${D}${bindir} ${D}${docdir}/myserver
        install -m 0644 ${S}//myserver ${D}${bindir}
}

#for systemd package
do_install_append() {
  install -d ${D}/${systemd_unitdir}/system
  install -m 0644 ${WORKDIR}/serverAutoStart.service ${D}/${systemd_unitdir}/system
}

In this recipe I have a systemd class inherited. I also have a systemd service which I need to auto execute package myserver when some machine is ON. Where is located this package and how can I execute it from systemd unit?(What path in ExecStart I need to use).

Thanks for your answers! May be you give me some remarks, because I`m relatively new in this area


Solution

  • First of all, note that to use systemd service you need to make sure that systemd in DISTRO_FEATURES.

    check with:

    bitbake -e (YOUR_IMAGE) | grep ^DISTRO_FEATURES=
    

    Because sysvinit is the default init manager, make sure that:

    INIT_MANAGER = "systemd"
    

    Here is a simple systemd recipe example (meta-custom/recipes-example/systemd-example/):

    • files/example.service
    [Unit]
    Description=Systemd Service Example
    Before=basic.target
    
    [Service]
    Type=simple
    
    # Put the path to the binary installed by do_install()
    ExecStart=/usr/bin/example
    ExecStop=/bin/killall example
    ExecReload=/bin/killall example; /usr/bin/example
    
    # These are optional, you can remove them, or
    # read about systemd properties for more control
    RemainAfterExit=yes
    Restart=on-failure
    RestartSec=30s
    
    [Install]
    WantedBy=basic.target
    
    • files/example.c

    • systemd-example_0.1.bb

    SUMMARY = "Systemd example recipe"
    DESCRIPTION = "Systemd service example for xxx"
    LICENSE = "CLOSED"
    
    SRC_URI = "file://example.c"
    SRC_URI += "file://example.service"
    
    S = "${WORKDIR}"
    
    do_compile(){
        ${CC} ${CFLAGS} ${LDFLAGS} ${WORKDIR}/example.c -o ${WORKDIR}/example
    }
    
    do_install(){
        install -d ${D}/usr/bin
    
        # Install the binary
        install -m 0744 ${WORKDIR}/example ${D}/usr/bin
    
        # Install systemd stuff
        install -d ${D}${systemd_unitdir}/systemd
        install -m 0644 ${WORKDIR}/example.service ${D}${systemd_unitdir}/systemd
    }
    
    # Systemd class stuff
    inherit systemd
    NATIVE_SYSTEMD_SUPPORT = "1"
    SYSTEMD_SERVICE_${PN} = "example.service"
    

    No need for SYSTEMD_AUTO_ENABLE = "enable" because it is enable by default.

    UPDATE

    Now, add the recipe to your image via:

    IMAGE_INSTALL_append = " systemd-example"
    

    put that line in your custom image recipe or in local.conf for testing.

    When you build the image you will find /usr/sbin/example and it will be automatically executed by the systemd service example.service, to check for the status :

    systemctl status example