【问题标题】:Execute custom script after boot on Yocto-built linux system在 Yocto 构建的 linux 系统上启动后执行自定义脚本
【发布时间】:2021-12-30 13:35:52
【问题描述】:

我想在每次系统启动时执行一个存在于 rootfs 中的脚本,以指示成功更新和重置引导加载程序变量。

例如,/etc 中的“custom-script”脚本为:

/etc
----/init.d
-----------custom-script.sh

我做的第一步是在 linux 镜像的 rootfs 中安装这个脚本。我在我的自定义 Yocto 层中创建了一个食谱。 meta-custom\recipes-support\images层目录如下:

.
├── files
│   ├── custom-script.sh
└── core-image-base.bb

core-image-base.bb 是:

DESCRIPTION = "Install script to Rootfs"
SUMMARY = "Install script to Rootfs and run after boot"
LICENSE = "CLOSED"

SRC_URI = "file://custom-script.sh"

do_install_append() {
    install -d 644 ${D}${sysconfdir}/init.d
    install -m 0755 ${WORKDIR}/custom-script.sh ${D}${sysconfdir}/init.d/custom-script.sh

FILES_${PN} = "${sysconfdir}/init.d"

conf/layer.conf 中我添加了IMAGE_INSTALL_append = " core-image-base"

现在我想在每次 linux 系统启动时执行这个脚本(因为成功加载了 rootfs)。有人可以帮我完成这个吗?据我了解,我可以使用 systemd 服务来执行此操作,并且每次系统启动时都应执行此服务。任何帮助将不胜感激。

提前致谢。

P.S:我使用的是 Ubuntu 20.04,这是我第一次在 Yocto 中使用带有 Dunfell 版本的 systemd 服务。

【问题讨论】:

  • 执行“每次系统启动” 的脚本不是“安装后脚本”“安装” 只执行一次。如果有(之前的)“成功更新”,那么您将需要一种向系统提供该信息的方法。
  • 好的,但是我们可以使用 systemd 服务来执行脚本吗?如果没有,那您​​能告诉我如何实现吗?
  • 目的是确保一旦更新的 Linux 操作系统启动并运行,我需要执行一个禁用“bootcounter”值的脚本。如果每次成功加载 rootfs 时都没有调用此脚本,则 bootcounter 值会增加,并在几次重新启动后初始化回滚。
  • 安装后脚本,顾名思义,用于在将软件包放入根文件系统或安装软件包后执行一些操作。安装后脚本不执行引导脚本执行。我建议你参考askubuntu.com/questions/919054/…
  • 请参考wiki.yoctoproject.org/wiki/Cookbook:Appliance:Startup_Scripts了解如何添加自定义启动脚本

标签: ubuntu embedded-linux yocto systemd


【解决方案1】:

为了使您的custom-script.sh 在启动时(每次系统启动时)使用init.d 执行,您的custom-script.sh 应具有以下格式

#!/bin/bash
# description: Description comes here....

# Source function library.
. /etc/init.d/functions

start() {
    # code to start app comes here
    # insert any kernel modules prior to 
    # executing/spawning any process that depends
    # on the LKM

}

stop() {
    # code to stop app comes here 
    # example: killproc program_name
    # Kill all the process started in start() function
    # remove any LKM inserted using insmod in start()

}

case "$1" in 
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here 
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0 

然后您还需要在所需的运行级别目录中添加一个符号链接到您的custom-script.sh,例如/etc/rc3.d/etc/rc5.d

要添加符号链接,您需要编辑 core-image-base.bb(您也可以使用自定义的 *.bb 文件):

DESCRIPTION = "Install script to Rootfs"
SUMMARY = "Install script to Rootfs and run after boot"
LICENSE = "CLOSED"

SRC_URI = "file://custom-script.sh"

do_install_append() {
    install -d 644 ${D}${sysconfdir}/init.d
    install -m 0755 ${WORKDIR}/custom-script.sh ${D}${sysconfdir}/init.d/custom-script.sh
    ln -sf ../init.d/custom-script.sh ${D}${sysconfdir}/rc4.d/S99custom-script.sh
    ln -sf ../init.d/custom-script.sh ${D}${sysconfdir}/rc4.d/K99custom-script.sh
}

FILES_${PN} = "${sysconfdir}/init.d"

所以,我认为您缺少指向所需运行级目录的符号链接,也许您需要编写自定义脚本以至少具有 start()stop() 函数。

如果您使用systemd,请参考Enable systemd services using yocto

【讨论】:

  • 嗨 Gaurav,我对自定义脚本有疑问。脚本可以是没有start()stop() 函数的简单脚本吗?我也可以使用systemd service 来做同样的事情,例如install -m 644 ${WORKDIR}/custom-script.service ${D}${systemd_unitdir}/system
  • 当然可以安装systemd单元文件,但是我首先建议你阅读一下systemd服务文件digitalocean.com/community/tutorials/…请参考this link了解如何使用yocto添加systemd单元服务.
  • 为了回答你的问题,我认为init daemon启动库需要一个start()和stop()函数,即使它们是空的,否则它可能会抛出错误。我不确定,也许您可​​以尝试一下并将其发布在此处以消除其他人的疑问:-)
  • 你好 Gaurav,我试图执行你的解决方案来建立到自定义脚本的符号链接,但我得到了 DEBUG: Executing shell function do_install ln: failed to create symbolic link '/root/build-swu-v2/tmp/work/cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi/post-install-initscript/1.0-r0/image/etc/rc4.d/S99custom-script.sh': No such file or directory WARNING: exit code 1 from a shell command. ERROR: Execution of '/root/build-swu-v2/tmp/work/cortexa7t2hf-neon-vfpv4-poky-linux-gnueabi/post-install-initscript/1.0-r0/temp/run.do_install.20665' failed with exit code 1 的 bitbake 错误
猜你喜欢
  • 2017-03-18
  • 2017-02-18
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
相关资源
最近更新 更多