【问题标题】:run initscript with systemd at startup with yocto使用 yocto 在启动时使用 systemd 运行 initscript
【发布时间】:2017-10-11 08:30:18
【问题描述】:

我想设置一个自定义初始化脚本,以便在启动时使用 yocto 在映像中运行,我遵循了correct answer here - 它与我想要的相同 - 因为我使用 pyro 版本,所以我在本地设置了这些变量.conf 文件,如文档中所述

DISTRO_FEATURES_append = "systemd"

VIRTUAL-RUNTIME_init_manager = "systemd"

DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"

VIRTUAL-RUNTIME_initscripts = ""

问题是系统启动时未调用 initscript.service,尽管脚本和服务文件已传输且 systemd 存在,但我在 /var/log/messages 中找不到任何内容。

我也试过用这个选项代替 yocto 中的 init

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
}

再一次,我在日志中找不到“添加密钥文件”的语句,尽管执行了配方并且密钥文件按照配方的预期传输到图像。

那么,正确答案有问题吗?我认为它很好,但我不知道问题出在哪里。如果有人可以给我一个使用 Vinit 进行等效操作的可靠示例?

目标机器是“qemux86-64”

食谱

我有一个叫做 meta-mylayer 的层,它的树是:

conf/layer.conf

recipes-core/mylayer-initscript/(如上述链接所述)

recipes-core/images/apt-image.bb

apt-image 是我在 core-minimal 及其内容之上构建的图像

    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 配方和所有文件与链接中所述相同,没有变化。

如果您对 keyfile 配方感兴趣,这里是它

#
# 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
}

当然,我会bitbake apt-image

服务脚本

[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

我不确定 yocto 的 rootfs 结果默认是 rw 还是 r-only ?

初始化脚本配方

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
}

【问题讨论】:

  • 你能分享你的食谱吗?
  • 您还没有显示实际安装服务文件的配方(我知道问题出在哪里)或服务文件本身。至于替代方法,我不确定 postinst 是否会在图像配方中工作,通常它是在生成 postinst 函数所针对的包的配方中定义的。
  • @jku 好的,我把它们放了
  • 开机后是否检查过systemd服务状态?我很确定当 Type=simple 时,ExecStart 中不能有多个命令。
  • 另外,是否需要 WantedBy(在服务文件的安装部分)才能自动启动服务?

标签: init systemd yocto


【解决方案1】:

对服务脚本进行以下更改后对我有用。

[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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多