我正在对从下面的包含脚本构建的基本 .deb 包进行故障排除,特别是通过 .deb 安装两次时其操作dpkg -i <package-name>
。我发现postrm
脚本总是被调用。
这官方维护者脚本不显示同一个软件包被安装两次的情况。当我安装以下脚本两次时,它将在和之间执行postrm
(或者prerm
如果我用一个配置包)。preisnt
postinst
这个流程对我来说似乎没有意义,因为为什么要再次按照流程安装:
- 预安斯特
- 预雷姆
- 后期
- 后置
如果 rm 中的任何内容被执行,我认为它将是以下内容:
- 预雷姆
- 后期
- 预安斯特
- 后置
即使第二次执行,也会返回preinst
a exit code 0
,因此不会触发prerm
or postrm
。
我的问题是:在这种情况下有没有办法避免打电话prerm
?postrm
有没有办法确定脚本本身如何调用prerm
或被调用? postrm
(因为在正常安装过程中没有调用它)。
这是脚本,以防万一查看有用:
#!/bin/bash
# Command line parameters
USERNAME="$1"
PUBKEY_FILE="$2"
TEMP_DIR="$3"
OUTPUT_PATH=$4
MAINTAINER_EMAIL="$5"
# Verify required args
if [ -z "${USERNAME}" ] || [ -z "${PUBKEY_FILE}" ] || [ -z "${TEMP_DIR}" ] || [ -z "${OUTPUT_PATH}" ]; then
echo "Usage: $0 USERNAME PUBKEY_FILE TEMP_DIR OUTPUT_PATH" >&2
exit 1
fi
# Ensure maintainer email
if [ -z "${MAINTAINER_EMAIL}" ]
then
MAINTAINER_HOSTNAME="$(hostname)"
if [ -z "$HOSTNAME" ]
then
MAINTAINER_HOSTNAME="localhost"
fi
MAINTAINER_EMAIL="admin@${MAINTAINER_HOSTNAME}"
fi
# Constants
# Package data
PACKAGE_NAME="xyz-lsc-target-${USERNAME}"
PACKAGE_VERSION="0.5-1"
PACKAGE_NAME_VERSION="${PACKAGE_NAME}_${PACKAGE_VERSION}"
MAINTAINER="Maintainer Name <${MAINTAINER_EMAIL}>"
PACKAGE_DATE=$(date "+%a, %d %b %Y %H:%M:%S %z")
# Used for identifying username entry in /etc/passwd
USER_COMMENT="XYZ Local Security Checks"
USER_COMMENT_GREP="XYZ\\ Local\\ Security\\ Checks"
# Paths
PACKAGE_BASE_DIR="${TEMP_DIR}/${PACKAGE_NAME_VERSION}"
# Data paths
DATA_DIR="${PACKAGE_BASE_DIR}"
HOME_SUBDIR="home/${USERNAME}"
HOME_DATA_DIR="${DATA_DIR}/${HOME_SUBDIR}"
SSH_DATA_DIR="${HOME_DATA_DIR}/.ssh"
DOC_SUBDIR="usr/share/doc/${PACKAGE_NAME}"
DOC_DATA_DIR="${DATA_DIR}/${DOC_SUBDIR}"
# Control file path
CONTROL_DIR="${PACKAGE_BASE_DIR}/DEBIAN"
#
# Test dependencies
#
if [ -z "$(which dpkg)" ]
then
echo "dpkg not found" >&2
exit 1
fi
if [ -z "$(which fakeroot)" ]
then
echo "fakeroot not found" >&2
exit 1
fi
if [ -z "$(which md5sum)" ]
then
echo "md5sum not found" >&2
exit 1
fi
#
# Create data files
#
# Create .ssh directory
mkdir -p "${SSH_DATA_DIR}"
# Copy public key
AUTH_KEYS_FILE="${SSH_DATA_DIR}/authorized_keys"
cp "${PUBKEY_FILE}" "${AUTH_KEYS_FILE}"
# Create doc directory
mkdir -p "${DOC_DATA_DIR}"
# Create Changelog
cd "${DOC_DATA_DIR}"
CHANGELOG_FILE="${DOC_DATA_DIR}/changelog.Debian"
{
echo "${PACKAGE_NAME} (${PACKAGE_VERSION}) experimental; urgency=low"
echo ""
echo " * Automatically generated local security check credential package"
echo " "
echo ""
echo " -- ${MAINTAINER} ${PACKAGE_DATE}"
} > "${CHANGELOG_FILE}"
# Compress Changelog
gzip -f --best "${CHANGELOG_FILE}"
CHANGELOG_FILE="${CHANGELOG_FILE}.gz"
# Create Copyright info
COPYRIGHT_FILE="${DOC_DATA_DIR}/copyright"
{
echo "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/"
echo ""
echo "Files: *"
echo "Copyright: 2018-2020 Greenbone AG"
echo "License: GPL-2+ (/usr/share/common-licenses/GPL-2)"
} > "${COPYRIGHT_FILE}"
# Create data archive
cd "${DATA_DIR}"
tar -C "${DATA_DIR}" -z -cf "../data.tar.gz" "${HOME_SUBDIR}" "${DOC_SUBDIR}"
#
# Create control files
#
# Create directory
mkdir -p "${CONTROL_DIR}"
chmod "0755" "${CONTROL_DIR}"
# Create "control" file
CONTROL_FILE="${CONTROL_DIR}/control"
{
echo "Package: ${PACKAGE_NAME}"
echo "Version: ${PACKAGE_VERSION}"
echo "Maintainer: ${MAINTAINER}"
echo "Priority: optional"
echo "Architecture: all"
echo "Description: XYZ local security check preparation"
echo " This package prepares a system for XYZ local security checks."
echo " A user is created with a specific SSH authorized key."
echo " The corresponding private key is located at the respective"
echo " XYZ installation."
} > "${CONTROL_FILE}"
# Create "preinst" file run before installation
PREINST_FILE="${CONTROL_DIR}/preinst"
touch "${PREINST_FILE}"
chmod "0755" "${PREINST_FILE}"
{
echo "#!/bin/sh"
echo "# Delete XYZ Local Security Checks by username and comment"
echo "grep \"${USERNAME}.*${USER_COMMENT_GREP}\" /etc/passwd && userdel -fr ${USERNAME}"
echo "# XYZ Local Security Checks user and create home directory"
echo "useradd -c \"${USER_COMMENT}\" -d /home/${USERNAME} -m -s /bin/bash ${USERNAME}"
echo "# Return exit status 0 to prevent instrm from being triggered"
echo "# https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html"
echo "exit 0"
} > "${PREINST_FILE}"
# Create "postinst" file run after installation
POSTINST_FILE="${CONTROL_DIR}/postinst"
touch "${POSTINST_FILE}"
chmod "0755" "${POSTINST_FILE}"
{
echo "#!/bin/sh"
echo "# Change file permissions"
echo "chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}"
echo "chmod 500 /home/${USERNAME}/.ssh"
echo "chmod 400 /home/${USERNAME}/.ssh/authorized_keys"
echo "exit 0"
} > "${POSTINST_FILE}"
# Create "postinst" file run after removal or on error
POSTRM_FILE="${CONTROL_DIR}/prerm"
touch "${POSTRM_FILE}"
chmod "0755" "${POSTRM_FILE}"
{
echo "#!/bin/sh"
echo "echo 'prerm file called!!'"
echo "# Remove user only if it was created by this package."
echo "# The debian package will run the postun script in case of errors"
echo "# (e.g. user already existed)."
echo "# Delete the user only if /etc/passwd lists content that suggests"
echo "# that the user was created by this package."
# echo "set -e # abort on errors"
echo "grep \"${USERNAME}.*${USER_COMMENT_GREP}\" /etc/passwd && userdel -fr ${USERNAME}"
} > "${POSTRM_FILE}"
# Calculate md5 checksums
MD5SUMS_FILE="${CONTROL_DIR}/md5sums"
cd "${DATA_DIR}"
{
md5sum "${HOME_SUBDIR}/.ssh/authorized_keys"
md5sum "${DOC_SUBDIR}/changelog.Debian.gz"
md5sum "${DOC_SUBDIR}/copyright"
} > "${MD5SUMS_FILE}"
#
# Build package
#
# Combine into .deb file
cd "${TEMP_DIR}"
fakeroot -- dpkg --build "${PACKAGE_NAME_VERSION}" "${OUTPUT_PATH}"
答案1
在我看来,您可能不想尝试区分初始安装、升级和重复安装,而可能需要更改您的设置,postrm
以便仅删除 上的用户purge
。这样,用户在升级等过程中会保留下来,并且只有在使用apt purge
或dpkg --purge
(或等效项)删除软件包时才会被删除。
为此,请检查postrm
的第一个参数;如果是purge
,则删除该用户,否则不执行任何操作。您可能还想更改您的用户preinst
,以便它不会删除现有用户,并且仅在用户尚不存在时创建该用户。