乘风破浪,遇见最佳跨平台跨终端框架.Net Core/.Net生态 - 基于Mono在Ubuntu打包开发桌面应用,打包成DEB指南
打包成Deb
四大维护脚本
定义
preinst
,软件安装前执行的脚本。在从deb文件中解压缩它所属的包之前执行此脚本。许多preinst脚本停止正在升级的包的服务,直到它们的安装或升级完成。postinst
,软件安装后执行的脚本。一旦app从它的deb文件中解包,这个脚本通常会完成包foo安装完成后的必需配置工作。通常,postinst脚本会要求用户输入,或警告用户,如果他们接受默认值,他们应该记得返回并根据需要重新配置该包。一旦安装或升级了新包,许多postinst脚本就会执行脚本内的命令来启动或重启服务。prerm
,软件卸载前执行的脚本。此脚本通常会停止与包关联的任何守护进程,它在卸载软件包的相关文件前执行。postrm
,软件卸载后执行的脚本。这个脚本通常修改与app相关的链接或其他文件,或删除由包创建的文件。
*.preinst
,*.postinst
,*.prerm
,*.postrm
这四类文件被称为维护者脚本,这些脚本被放置在Debian目录下的控制区内,并且被dpkg
用来控制安装,升级和删除。
位置
这些维护者脚本都可以在/var/lib/dpkg/info/
目录下被找到。
执行顺序
- 首次安装应用,
dpkg -i xxxxxxx.deb
,执行顺序是preinst --> postinst
。 - 卸载并保留配置,
dpkg -r xxxxxxx
,执行顺序是prerm --> postrm
。 - 卸载不保留配置,
dpkg -P xxxxxxx
,执行顺序是prerm --> postrm --> postrm
,第一次执行postrm
传入的$1
为remove
; 第二次执行postrm
传入的$1
为purge
。 - 升级安装,
dpkg -i xxxxxxx_v2.deb
,,执行顺序是prerm --> preinst --> postrm --> postinst
。
测试顺序
- 如果可能,安装前一个版本的软件包。
- 从前一个版本升级软件包。
- 降级软件包到前一个版本(可选)。
- 彻底删除该软件包。
- 全新安装该软件包。
- 卸载该软件包。
- 再次安装该软件包。
- 彻底删除该软件包。
举个例子
cd /var/lib/dpkg/info && ls | grep 'postinst'
随便看几个
软件安装后执行的脚本(libpam-runtime.postinst)
#!/bin/sh -e
. /usr/share/debconf/confmodule
calculate_md5sum()
{
configfile="$1"
sed -n -e'1,/# here are the per-package modules (the "Primary" block)/p;
/# here.s the fallback if no module succeeds/,/# and here are more per-package modules (the "Additional" block)/p;
/# end of pam-auth-update config/,$p' \
/etc/pam.d/"$configfile" | md5sum | awk '{ print $1 }'
}
# If the user has removed the config file, respect this sign of dementia
# -- only create on package install.
force=
if [ -z "$2" ] || dpkg --compare-versions "$2" lt 1.0.1-11
then
force=--force
for configfile in common-auth common-account common-session \
common-password
do
if [ -f /etc/pam.d/$configfile ] && \
! fgrep -q $(calculate_md5sum $configfile) \
/usr/share/pam/$configfile.md5sums 2>/dev/null
then
force=
fi
done
fi
pam-auth-update --root "$DPKG_ROOT" --package $force
if [ -n "$force" ]; then
rm -f /etc/pam.d/common-auth.pam-old \
/etc/pam.d/common-account.pam-old \
/etc/pam.d/common-password.pam-old \
/etc/pam.d/common-session.pam-old
elif dpkg --compare-versions "$2" lt-nl 1.1.0-1 \
&& [ ! -e /etc/pam.d/common-session-noninteractive ]
then
cp -a /etc/pam.d/common-session /etc/pam.d/common-session-noninteractive
fi
软件卸载前执行的脚本(libpam-runtime.prerm)
#!/bin/sh
set -e
# Packages including pam configurations need to include a block like
# the following to arrange for removal of the PAM profile prior to
# package removale. Libpam-runtime is special because it includes the
# machinery for managing the profiles and because there is a check to
# make sure at least one profile is enabled. Libpam-runtime can only
# be removed in a situation where dpkg checks are being overriden; in
# that case the resulting PAM config is likely to be broken anyway.
# But we prefer to permit the removal if enough dpkg force options are
# given than to create an infinite loop. So, this block is commented
# out, left here as an example for other packages.
# if [ "$1" = remove ] ; then
# pam-auth-update --package --remove unix
# fi
软件卸载后执行的脚本(libpam-runtime.postrm)
#!/bin/sh -e
if [ "$1" = "purge" ]; then
rm -f /etc/pam.d/common-auth /etc/pam.d/common-account \
/etc/pam.d/common-session /etc/pam.d/common-password \
/etc/pam.d/common-session-noninteractive
rm -f /var/lib/pam/auth /var/lib/pam/account /var/lib/pam/session \
/var/lib/pam/password /var/lib/pam/seen \
/var/lib/pam/session-noninteractive
rmdir --ignore-fail-on-non-empty /var/lib/pam ||true
fi
# Automatically added by dh_installdebconf/13.6ubuntu1
if [ "$1" = purge ] && [ -e /usr/share/debconf/confmodule ]; then
. /usr/share/debconf/confmodule
db_purge
fi
# End automatically added section
配置文件列表
定义
Conffiles是一个配置文件列表(通常放在/etc
中),当包升级时,包管理系统不会覆盖这些配置文件。这确保了这些文件内容的本地值将被保留,这是一个关键特性,可以在运行的系统上对包进行就地升级。
Conffiles的作用就是在软件包升级时,不同于其他文件只需要简单的暴力覆盖即可,放置于/etc
下的(配置)文件需要需要特殊考虑,是保留旧配置还是使用新配置,所以有了这个特殊的行为。
打包注意
对于Deb包升级时文件的更新机制,dpkg有一套默认的方案:所有放到/etc
目录下的文件都被归类为Conffile,此类文件的升级方式大概如下:
-
如果用户未修改Conffile,且软件包升级会变更此文件,会自动更新Conffile;
-
如果用户修改了Conffile,且软件包升级不会变更此文件,deb包升级时不会更新该文件,保存用户修改的状态;
-
如果用户修改了Conffile,且软件包升级会变更此文件,则默认是confask的行为,安装时会询问用户是使用最新的文件还是用户修改过的文件;
所以Deb包的开发者要注意这个规范,将需要保留用户配置的文件放/etc
目录。
举个例子
配置文件列表(libpam-runtime.conffiles)
/etc/pam.conf
/etc/pam.d/other
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-09-06 温故知新,遇见BeginInvoke/Invoke,适用于MFC/WinFroms的线程和进程间消息通信,从源码视角一探究竟
2021-09-06 乘风破浪,遇见最美Windows 11之现代Windows桌面应用开发 - 十一项让应用更出色的新机遇