0

I want to setup a custom init script to run at startup in an image with yocto, i have followed the correct answer here - it is the same as i want - and since i use pyro version i set these variables in the local.conf file as stated in the documentation

DISTRO_FEATURES_append = " systemd"

VIRTUAL-RUNTIME_init_manager = "systemd"

DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"

VIRTUAL-RUNTIME_initscripts = ""

The problem is that the initscript.service is not called when the system boots up and i don't find thing in the /var/log/messages despite the script and the service file are transferred and systemd is exist.

i also tried use this option as a replacement of init in yocto

pkg_postinst_keyfile() { #where keyfile is a recipe is added to IMAGE_INSTALL_append
    if [ x"$D" = "x" ]; then
      logger "key file is added"
    else
      exit 1
    fi
}

And again, i cannot find the statement of "key file is added" in the logs despite the recipe is executed and the a key file is transfer to the image as expected from the recipe.

so, is something wrong about the correct answer ? i see it good but i don't know where the problem. And if anyone can give me a solid example of doing the equivalent using Vinit ?

The target machine is "qemux86-64"

Recipes

I have a layer which is called meta-mylayer, its tree is:

conf/layer.conf

recipes-core/mylayer-initscript/ (as described in the above link)

recipes-core/images/apt-image.bb

the apt-image is the image i built above core-minimal and its content

    SUMMARY = "An image which add the support of using deb package management"

inherit core-image
include recipes­core/images/core­-image-­minimal.bb

IMAGE_FEATURES += "package-management"
IMAGE_INSTALL_append = "gnupg coreutils keyfile initscript"

IMAGE_LINGUA = " "

LICENSE = "MIT"

IMAGE_ROOTFS_SIZE ?= "16192"


PACKAGE_FEED_URIS = "http://<public ip here>/my-repo/yahia-repo/expiremental \"
PACKAGE_FEED_BASE_PATHS = "deb"
PACKAGE_FEED_ARCHS = "all"

pkg_postinst_keyfile() {
    if [ x"$D" = "x" ]; then
      logger "key file is added"
    else
      exit 1
    fi
}

initscript recipe and all files is the same as stated in the link with no change.

if you interested about keyfile recipe, here is it

#
# The goal of this recipe is to move keyFile to target machine so it can be used as public key for 
# authoriztion of deb repo.
#
# Author: Yahia Farghaly

SUMMARY = "Move keyFile example to /deb_key"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://keyFile \
           file://sources.list"

#to inform yocto where to package the file
FILES_${PN} += " /deb_key"

S = "${WORKDIR}"


do_install() {
         install -d ${D}/deb_key
         install -m 0755 keyFile ${D}/deb_key
}

And for sure, i do bitbake apt-image

service script

[Unit]
Description=start initscript upon first boot

[Service]
Type=simple
ExecStart=/bin/sh -c 'sleep 5 ; /usr/sbin/initscript.sh'

initscript.h

#!/bin/sh

logger "starting initscript"

# do some work here. Mount rootfs as rw if needed.

logger "initscript work done"
echo "hi yahia" > /deb_key/hello

i am not sure that rootfs result of yocto is rw by default or r-only ?

initscript recipe

SUMMARY = "Initial boot script"
DESCRIPTION = "Script to do any first boot init, started as a systemd service which removes itself once finished"
LICENSE = "CLOSED"
PR = "r3"

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

inherit allarch systemd

NATIVE_SYSTEMD_SUPPORT = "1"
SYSTEMD_PACKAGES = "${PN}"
SYSTEMD_SERVICE_${PN} = "initscript.service"

do_compile () {
}

do_install () {
    install -d ${D}/${sbindir}
    install -m 0755 ${WORKDIR}/initscript.sh ${D}/${sbindir}

    install -d ${D}${systemd_unitdir}/system/
    install -m 0644 ${WORKDIR}/initscript.service ${D}${systemd_unitdir}/system
}
Yahia Farghaly
  • 857
  • 2
  • 10
  • 20
  • Can you share your recipe? – Jussi Kukkonen Sep 02 '17 at 17:07
  • You haven't shown the recipe that actually installs the service file (which I understood was where the problem is), or the service file itself. As for the alternative method, I'm not sure if the postinst is going to work in an image recipe, normally it's defined in the recipe that produces the package which the postinst function is for. – Jussi Kukkonen Sep 03 '17 at 13:36
  • @jku ok, i put them – Yahia Farghaly Sep 03 '17 at 17:19
  • Have you checked the systemd service status after boot? I'm pretty sure ExecStart can't have multiple commands in it when Type=simple. – Jussi Kukkonen Sep 04 '17 at 08:11
  • Also, is WantedBy (in Install section of the service file) required to get the service autostarted? – Jussi Kukkonen Sep 04 '17 at 08:37
  • @jku, i did what i want with pkg_postinst_${PN} and as you said it worked for package recipe not image. not sure about wantedBy but i will search for systemd unit format and try again – Yahia Farghaly Sep 04 '17 at 16:06

1 Answers1

1

Works for me with the following change to the service script.

[Unit]
Description=start initscript upon first boot

[Service]
Type=simple
ExecStart=/bin/sh -c 'sleep 5 ; /usr/sbin/initscript.sh'

[Install]
WantedBy=multi-user.target