简体   繁体   English

我将如何在Fakeroot Deb软件包构建器中使用postinst脚本

[英]How would I use postinst script with fakeroot deb package builder

Good afternoon, 下午好,

I was able to build my project into a deb package using: 我能够使用以下命令将项目构建到一个deb包中:

fakeroot dpkg-deb --build mypackage

Next, I can install the package using 接下来,我可以使用

dpkg -i mypackage.deb

Everything is installed and copied correctly when I do this, however I would like to run a few bash commands after the package is installed. 这样做时,所有内容均已正确安装并复制,但是在安装软件包后,我想运行一些bash命令。

I understand this needs to be done using the postinst file in the mypackage/DEBIAN directory 我知道需要使用mypackage / DEBIAN目录中的postinst文件来完成此操作

I have seen a few examples of this script online, but no clear explanation of how to write one and how to include it in the build. 我在线上已经看到了该脚本的一些示例,但是没有清楚说明如何编写脚本以及如何将其包括在构建中。

  1. How do I make sure fakeroot dpkg-deb includes this script, is placing it into DEBIAN directory enough? 如何确定fakeroot dpkg-deb包含此脚本,并将其放置在DEBIAN目录中足够?

  2. There is a case structure in the postinst script, what is this for, and where do I place the bash commands to execute in that script postinst脚本中有一个case结构,这是做什么用的,我将bash命令放在该脚本中执行的位置

  3. If I install the package with dpkg - i mypackage.deb is this enough to run that script? 如果我使用dpkg安装软件包-我的mypackage.deb是否足以运行该脚本?

An example script I would like to make is shown below. 我想制作的示例脚本如下所示。

  1. What do " configure, abort-upgrade, abort-remove, and abort-deconfigure " stand for. 配置,中止升级,中止删除和中止取消配置 ”代表什么。

  2. What does the " update-alternatives " line do. update-alternatives ”行是做什么的。

Thank you for your help, 谢谢您的帮助,

postinst file below. 下面的postinst文件。

#!/bin/sh

set -e

case "$1" in
    configure)
        # EXECUTE MY BASH COMMAND
        echo /usr/local/lib > /etc/ld.so.conf && ldconfig
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
        exit 0
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

update-alternatives --install /usr/bin/fakeroot fakeroot /usr/bin/fakeroot-ng 5 \
        --slave /usr/share/man/man1/fakeroot.1.gz \
            fakeroot.1.gz /usr/share/man/man1/fakeroot-ng.1.gz

exit 0

First, here is possibly the most relevant documentation: Debian Policy Manual: Package Maintainer Scripts and Installation Procedure . 首先,这里可能是最相关的文档: Debian策略手册:软件包维护者脚本和安装过程

Second, the very most important thing to remember when writing or dealing with maintainer scripts is that they must be idempotent. 其次,在编写或处理维护者脚本时要记住的最重要的事情是它们必须是幂等的。 Assume the script will be run many times in succession, and make sure things still won't break if so. 假设脚本将连续运行多次,并确保脚本不会中断。

To answer your questions directly, 要直接回答您的问题,

  1. Putting it in the DEBIAN directory is correct, when building with dpkg-deb . 使用dpkg-deb构建时,将其放入DEBIAN目录是正确的。 If you were instead using Debhelper for a safer or more convenient build setup, you might put the postinst in debian/$packagename.postinst . 如果改为使用Debhelper进行更安全或更方便的构建设置,则可以将postinst放在debian/$packagename.postinst

  2. The postinst script can be called in a number of different situations. 可以在许多不同情况下调用postinst脚本。 The "case" statement you can find in many (most?) postinsts is meant to check which situation it is. 您可以在许多(大多数?)后插入语句中找到“ case”语句,用于检查它的情况。 Generally speaking, it makes sense to take most postinst actions in all of the possible situations, and that's why they are grouped together in one script. 一般来说,在所有可能的情况下采取大多数后期操作都是有道理的,这就是为什么将它们组合在一个脚本中的原因。 But sometimes it is better to differentiate. 但有时最好区分。 I'll explain the different scenarios under #4. 我将在#4下解释不同的情况。

  3. Yes. 是。 A successful installation of a deb package (whether by dpkg -i , apt-get install , or whatever) must involve a successful run of its preinst and postinst scripts, if present. 成功安装deb软件包(无论是dpkg -iapt-get install还是其他方法), 必须成功运行其preinst和postinst脚本(如果存在)。 It is possible to "unpack" a deb without running any maintainer scripts, but that is not considered "installing". 可以在不运行任何维护程序脚本的情况下对Deb进行“解压缩”,但这不被视为“正在安装”。

  4. These "action" names correspond to the different situations in which a postinst can be run. 这些“动作”名称对应于可以运行postinst的不同情况。

    • configure : A package is being installed or upgraded. configure :正在安装或升级软件包。 If the package wasn't installed before, $2 will be empty. 如果该软件包之前未安装,则$2将为空。 Otherwise $2 will contain the old version number of the package; 否则, $2将包含该软件包的旧版本号; the version from which you are upgrading. 您要升级的版本。

    • abort-upgrade : An upgrade operation was aborted. abort-upgrade :升级操作已中止。 As an example, I have version V1 of mypkg installed, and I try to upgrade it to V2. 例如,我安装了版本1的mypkg,然后尝试将其升级到版本2。 But the preinst or postinst of V1 fail to run successfully, or there is a file conflict. 但是V1的preinst或postinst无法成功运行,或者存在文件冲突。 dpkg stops trying to install V2, and re-runs the postinst from V1 (with the " abort-upgrade " action) in case any state needs to be restored. dpkg停止尝试安装V2,并从V1重新运行postinst(带有“ abort-upgrade ”操作),以防万一需要恢复任何状态。

    • abort-remove : A remove operation was aborted. abort-remove :删除操作已中止。 For example, if I ran "dpkg -P mypkg", but mypkg's prerm script failed to run, or something else happened that made dpkg think it could not safly uninstall mypkg. 例如,如果我运行了“ dpkg -P mypkg”,但是mypkg的prerm脚本无法运行,或者发生了其他事情,使dpkg认为无法删除mypkg。 So it runs mypkg's postinst again (with the " abort-remove " action) in case any state needs to be restored. 因此,如果需要恢复任何状态,它将再次运行mypkg的postinst(带有“ abort-remove ”操作)。

    • abort-deconfigure : As you might guess, a deconfigure operation was aborted. abort-deconfigure :您可能会猜到,取消配置操作已中止。 "deconfiguring" is sort of a half-removal action used when a package being installed conflicts with others already installed. 当正在安装的软件包与已经安装的其他软件包冲突时,“取消配置”是一种半删除操作。 To make the explanation short, if the abort-deconfigure action is being run, the postinst is expected to restore any state that might have been undone by the prerm script with the deconfigure action. 为了使说明简短,如果正在运行abort-deconfigure操作,则预期postinst可以还原由prerm脚本使用deconfigure操作撤消的所有状态。

    For lots of additional nitty-gritty details, see the great charts and explanations at https://people.debian.org/~srivasta/MaintainerScripts.html . 有关许多其他细节的信息,请参见https://people.debian.org/~srivasta/MaintainerScripts.html上的图表和说明。

  5. The "update-alternatives" command updates entries in the Debian "alternatives" system. “ update-alternatives”命令更新Debian“ alternatives”系统中的条目。 See the man page. 请参见手册页。 In this specific case, the command is telling Debian that " /usr/bin/fakeroot-ng " is an alternative for the fakeroot command. 在这种特定情况下,该命令告诉Debian,“ /usr/bin/fakeroot-ng ”是fakeroot命令的替代方法。 Depending on the priority of this alternative and the priority of other registered alternatives, and the preference of the user, fakeroot-ng might now be invoked when someone runs " fakeroot ". 根据此替代方案的优先级和其他已注册替代方案的优先级以及用户的偏好,当有人运行“ fakeroot ”时,现在可能会调用fakeroot-ng

Just one think about this line : 只需考虑一下这一行:

echo /usr/local/lib > /etc/ld.so.conf && ldconfig

According the Debian Policy, you shouldn't modify ld.so.conf 根据Debian政策, 您不应修改ld.so.conf

A simple alternative is to do something like that : 一个简单的替代方法是做这样的事情:

In your postinst script : 在您的postinst脚本中:

/usr/local/lib > /etc/ld.so.conf.d/EXAMPLE.conf && ldconfig

and in your postrm script : 并在您的postrm脚本中:

rm /etc/ld.so.conf.d/EXAMPLE.conf && ldconfig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM