【问题标题】:How to correctly add a systemd service to a cpack generated debian package?如何正确地将 systemd 服务添加到 cpack 生成的 debian 包中?
【发布时间】:2022-02-14 16:57:37
【问题描述】:

我正在尝试通过 cpack 生成一个尊重系统配置的 debian 包(例如,如果管理员不希望启动服务,则不要启动该服务),并且在安装在 systemd free 中时不会导致错误环境(如在一些 docker 图像中)。

我当前的设置包括一个 postinst 和一个 prerm 文件,它们通过 CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA 简单地提供给 cpack。 这些在两个脚本中都调用systemctl enable/start/stop/disable

根据我收集到的信息,应该调用dh_installsystemd --name=foo foo.service 来启动服务。

用我的 postinst 文件中的 systemctl enable foo.service 替换会导致错误:

dh_installsystemd: error: "debian/control" not found. Are you sure you are in the correct directory?
dpkg: error processing package foo (--configure):
 installed foo package post-installation script subprocess returned error exit status 255
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
Errors were encountered while processing:
 foo

我必须承认,我对如何处理这件事有些迷茫。 如何通过 CPack 正确地将 systemd 服务添加到 debian 包中?

【问题讨论】:

  • dh_installsystemd 旨在生成 preinstpostinstprerm 片段,而不是实际执行安装。
  • 您可以将包拆分为 2 个组件:一个用于二进制文件,一个用于安装服务,并将服务组件的依赖项添加到二进制文件组件。这将为您提供 2 个要安装的文件,并在无 systemd 的系统上选择正确的文件不会导致问题...

标签: cmake package debian systemd cpack


【解决方案1】:

想到的最佳解决方案是剖析另一个软件包并使用 dh_installsystemd 生成的相同命令。

这会生成一个 postinst 文件,如下所示:

# End automatically added section
# Based on output by dh_installsystemd/13.5.2

if [ \"$1\" = \"configure\" ] || [ \"$1\" = \"abort-upgrade\" ] || [ \"$1\" = \"abort-deconfigure\" ] || [ \"$1\" = \"abort-remove\" ] ; then

    # was-enabled defaults to true, so new installations run enable.
    if deb-systemd-helper --quiet was-enabled 'foo.service'; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable 'foo.service' >/dev/null || true
    else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state 'foo.service' >/dev/null || true
    fi
fi
if [ \"$1\" = \"configure\" ] || [ \"$1\" = \"abort-upgrade\" ] || [ \"$1\" = \"abort-deconfigure\" ] || [ \"$1\" = \"abort-remove\" ] ; then
    if [ -z \"${DPKG_ROOT\:-}\" ] && [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
        deb-systemd-invoke restart 'foo.service' >/dev/null || true
    fi
fi

由于这些指令是宏中的静态更新,因此必须手动应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多