【问题标题】:Why does autostart with systemd not work?为什么使用 systemd 自动启动不起作用?
【发布时间】:2020-04-30 15:20:22
【问题描述】:

Yocto 版本是战士。 我使用 GO/golang 用户应用程序(https-server)做了一个 yocto 项目,该应用程序在 raspi3 上运行良好。 现在我试图在 yocto 图像上自动启动它并且没有让它工作。 我知道有很多关于这方面的问题,但我没有找到有帮助的东西。 例如我试图按照这篇文章中的所有步骤 Enable systemd services using yocto 但它不会在 raspi 处自动启动。

我找到的服务的 raspi 中的这些文件:

/lib/systemd/system/https-server.service 
/etc/systemd/system/multi-user.target.wants/https-server.service

如果我手动启动应用程序本身运行良好, 它位于 /usr/bin/https-server 的 raspi

我的构建/conf/local.conf:

IMAGE_INSTALL_append = " kernel-image kernel-devicetree sudo apt dnsmasq nano dhcpcd git glibc-utils localedef curl go https-server"
meta-https-server/
├── conf
│   └── layer.conf
└── recipes-https-server
    └── https-server
        ├── files
        │   ├── https-server.go
        │   ├── https-server.service
        │   ├── mytest
        │   ├── server.crt
        │   ├── server.key
        │   └── testvideo.mp4
        ├── go-sw.inc
        └── https-server.bb

https-server.bb

require go-sw.inc

inherit go systemd
#inherit go update-rc.d systemd


SRC_URI += "file://https-server.service"
SRC_URI += "file://https-server.go"

SYSTEMD_PACKAGES = "${PN}"
INITSCRIPT_PACKAGES = "${PN}"
SYSTEMD_SERVICE_${PN} = "https-server.service"

# Path
MY_KEY = "/data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/server.key"
MY_CERT = "/data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/server.crt"
TESTVIDEO = "/data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/testvideo.mp4"
MY_TEST = "/data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/mytest"

# COMPILER
do_compile() {
go build /data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/https-server.go
}


# INSTALL
do_install() {
#   install -d to create directories, "${D}/${bindir}" is /usr/bin

#   systemd
    install -d ${D}${systemd_unitdir}/system
    install -m 0644 ${WORKDIR}/https-server.service ${D}${systemd_unitdir}/system

#   HTTPS certificate and key
    install -d "${D}/${bindir}"
    install -m 0755 "${MY_KEY}"    "${D}/${bindir}"
    install -m 0755 "${MY_CERT}"   "${D}/${bindir}"
    install -m 0777 "${TESTVIDEO}" "${D}/${bindir}"
    install -m 0777 "${MY_TEST}"   "${D}/${bindir}"

#   HTTPS Server Software
    install -m 0755 "${S}/build/https-server" "${D}/${bindir}"

}

FILES_${PN} += "${bindir}"
FILES_${PN} += "${libexecdir}"
FILES_${PN} += "${systemd_system_unitdir}"


REQUIRED_DISTRO_FEATURES= "systemd"

服务 https-server.service

[Unit]
Description=HTTPS Server sw startup script

[Service]
ExecStart=/usr/bin/https-server

[Install]
WantedBy=multi-user.target

【问题讨论】:

  • 我不知道这与 Go 有什么关系,但让我们猜测一下。是否在您的问题中包含运行 systemctl status -l https-server(这是一个小的拉丁字母 L;ell)的输出?
  • 不要使用/data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/前缀,而是在SRC_URI中添加每个文件。您可以通过oe-pkgdata-util list-pkg-files -p https-server 检查文件是否正确打包。如果变量设置正确,您可以使用bitbake -e https-server | egrep '^[A-Z][A-Z_]*' 检查。并检查@kostix 命令。还有 SYSTEMD_AUTO_ENABLE 变量你可以检查,但它应该默认启用。
  • @kostix [我不知道这与 Go 有什么关系..] 你是对的,我删除了错误的标签,对此感到抱歉。
  • [注意包含运行“systemctl status -l https-server”的输出]命令 systemctl 没有在我的目标上运行,我尝试添加它并尝试。谢谢。

标签: yocto systemd


【解决方案1】:

我发现了导致问题的原因:

在我的本地配置中 build/conf/local.conf 我只有这个:

    DISTRO_FEATURES_append = " systemd "

在我添加以下内容后:

    DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"
    VIRTUAL-RUNTIME_init_manager = "systemd"
    VIRTUAL-RUNTIME_initscripts = ""

工作正常。进程 https-server 在启动时启动。 我用ps启动后检查了正在运行的进程,如 systemctl 无法处理我的 core-image-minimal 图像:

    root@raspberrypi3:~# ps
    [...]
    152 root      861m S    /usr/bin/https-server
    [...]
    root@raspberrypi3:~#

所以这有所不同。不知道我的空间是否缺失 DISTRO_FEATURES_append = "systemd" 也错了... ??

    #DISTRO_FEATURES_append = " systemd"

    DISTRO_FEATURES_append = " systemd "
    DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"
    VIRTUAL-RUNTIME_init_manager = "systemd"
    VIRTUAL-RUNTIME_initscripts = ""

@Nayfe 建议的 oe-pkgdata-util 在这里是一个非常有用的工具:

user@machine:[...]/poky-warrior/build$ 
oe-pkgdata-util list-pkg-files -p https-server
https-server:
        /lib/systemd/system/https-server.service
        /usr/bin/https-server
        /usr/bin/mytest
        /usr/bin/server.crt
        /usr/bin/server.key
        /usr/bin/testvideo.mp4
https-server-dbg:
        /usr/bin/.debug/https-server
https-server-dev:
https-server-ptest:

我还从上面研究了配方 https-server.bb 避免@Nayfe 建议的绝对路径。这不是造成 问题,但它的风格很糟糕。

不要使用 /data/yocto/2020-04-21-poky-warrior/poky-warrior/meta-https-server/recipes-https-server/https-server/files/ 前缀,并添加每个文件而是在 SRC_URI 中。

require go-sw.inc

inherit go systemd

#  "${D}/${bindir}" is /usr/bin 
#  ${WORKDIR} is path at local directory, 
#  this can be used instead of absolute paths

SRC_URI += "file://https-server.service"
SRC_URI += "file://https-server.go"
SRC_URI += "file://server.key"
SRC_URI += "file://server.crt"
SRC_URI += "file://testvideo.mp4"
SRC_URI += "file://mytest"

SYSTEMD_PACKAGES = "${PN}"
INITSCRIPT_PACKAGES = "${PN}"
SYSTEMD_SERVICE_${PN} = "https-server.service"

# COMPILER
do_compile() {
go build ${WORKDIR}/https-server.go
}


# INSTALL
do_install() {
#   install -d to create directories, "${D}/${bindir}" is /usr/bin

#   systemd
    install -d ${D}${systemd_unitdir}/system
    install -m 0644 ${WORKDIR}/https-server.service ${D}${systemd_unitdir}/system

#   HTTPS certificate, key and testdata for https-server
    install -d "${D}/${bindir}"
    install -m 0755  ${WORKDIR}/server.key     "${D}/${bindir}"
    install -m 0755  ${WORKDIR}/server.crt     "${D}/${bindir}"
    install -m 0777  ${WORKDIR}/testvideo.mp4  "${D}/${bindir}"
    install -m 0777  ${WORKDIR}/mytest         "${D}/${bindir}"

#   HTTPS Server Software
    install -m 0755 "${WORKDIR}/build/https-server" "${D}/${bindir}"
}

# FILES_${PN} is Yocto’s way of specifying
# which files are expected to be installed along with which package
# (${PN} is a variable holding the main package’s name).
FILES_${PN} += "${bindir}"
FILES_${PN} += "${libexecdir}"
FILES_${PN} += "${systemd_system_unitdir}"

REQUIRED_DISTRO_FEATURES= "systemd"

感谢@kostix 和@nayfe 的建议。

【讨论】:

    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多