`.deb` 包中的条件文件和目录安装

`.deb` 包中的条件文件和目录安装

是否可以创建具有条件文件和目录安装的二进制包(例如,在用户确认后.deb安装脚本)?init/etc/init.d/

答案1

为了在软件包安装过程中以交互方式提问,你应该使用德布康夫. 动态创建和管理配置文件(并且 中的文件/etc/init.d/被视为配置文件)中佛罗里达大学可以使用。

关于如何使用 debconf 的教程可以在这里找到:
http://www.fifi.org/doc/debconf-doc/tutorial.html

最小示例

debconf模板

将其放入文件中debian/templates。它包含安装期间向用户显示的文本。请务必将其替换demo-pkg为您实际的包名称!

Template: demo-pkg/install-init.d
Type: boolean
Default: false
Description: Would you like to install a service for this package?
 Services are really cool! They allow stuff to be started in the
 background without you having to start them manually!!!

包配置脚本

在这里,您可以(以交互方式)询问用户您需要知道的事情。此脚本很特别,因为在安装多个软件包时,所有软件包的所有这些文件都会在dpkg实际安装过程开始之前运行。这意味着,如果多个软件包想要询问用户某事,则所有问题都会在安装开始时针对所有软件包进行询问,而不是稍后针对每个软件包单独询问。

只需将以下内容放入名为的文件中debian/config并将其标记为可执行文件(记得demo-pkg用正确的包名称替换):

#!/bin/sh

# Make sure this script fails if any unexpected errors happen
set -e

# Load debconf library
. /usr/share/debconf/confmodule

# Should an init job be installed?
db_input high demo-pkg/install-init.d || true
db_go

# You can also act upon the user's answers using `db_get`
#db_get demo-pkg/install-init.d
#[ "${RET}" = "false"] && echo "I don't think that was a wise decision..."

维护者脚本(postinstprermpostrm

在这里我们真正施展魔法,动态添加和删除配置文件/初始化脚本。要使此功能正常工作,您必须将初始化脚本作为常规文件与软件包一起安装(我/usr/share/demo-pkg/init-service在此示例中假设)。

debian/postinst文件(在提取所有文件后调用dpkg):

#!/bin/sh

# Make sure this script fails if any unexpected errors happen
set -e

# Load debconf library
. /usr/share/debconf/confmodule

if [ "$1" = "configure" ];
then
    # Check if the user decided the enable the service using `db_get`
    db_get demo-pkg/install-init.d
    if [ "${RET}" != "false" ];
    then
        # Install init script using `ucf`
        ucf /usr/share/demo-pkg/init-service /etc/init.d/demo-service

        # Register init script as service
        update-rc.d demo-service defaults

        # Start service
        invoke-rc.d demo-service start
    fi
fi

debian/prerm文件(在文件被删除dpkg或者软件包升级到较新版本之前调用):

#!/bin/sh

# Make sure this script fails if any unexpected errors happen
set -e

# Load debconf library
. /usr/share/debconf/confmodule

# Check if the user decided the enable the service using `db_get`
db_get demo-pkg/install-init.d
if [ "${RET}" != "false" ];
then
    # Stop the service before upgrade or removal
    invoke-rc.d demo-service stop

    if [ "$1" = "remove" ] || [ "$1" = "deconfigure" ];
    then
        # Unregister service prior to removal
        update-rc.d -f demo-service remove
    fi
fi

debian/postrm文件(所有文件均被删除后调用dpkg):

#!/bin/sh

# Make sure this script fails if any unexpected errors happen
set -e

if [ "$1" = "purge" ];
then
    # Remove service file using `ucf` (checking whether `ucf` is still available first)
    if type ucf >/dev/null 2>&1;
    then
        ucf --purge "/etc/init.d/demo-service"
    fi

    # Remove service file by hand (in case the above failed somehow)
    rm -f "/etc/init.d/demo-service"
fi

最后步骤

  1. 在文件中添加Pre-Depends:依赖项debconfdebian/control
  2. 在文件中添加正常Depends:依赖ucfdebian/control
  3. 确保文件/usr/share/demo-pkg/init-service(或任何你称之为的文件)正确安装

相关内容