背景
如题,在postinst文件中直接执行了ldconfig命令, chroot 环境下出错,安装失败
分析
chroot 环境下不能用 ldconfig 和 systemctl
但是:如果环境是 chroot,系统有可能没完整挂载 /proc、/dev、系统路径,跑 ldconfig 就容易段错误(core dump)。
解决
加条件判断,出错后也继续执行接下来的安装任务,防止影响安装。
- chroot 环境下不能用 systemctl,要添加条件判断
is_chroot() {if [ -f /proc/1/sched ]; then # 看 /proc/1/sched 这个文件存不存在。➔ 这个文件是记录系统 第一个进程(PID 1) 的状态。➔ 正常系统里 PID 1 是 systemd,如果没这个文件,大概率是特种环境(比如 chroot)。# 检查 init 是不是 systemd 打开 /proc/1/comm 这个小文件,看里面是不是写着 systemd。 ➔ 这个文件保存了 PID 1 的名字。 ➔ 如果看到是 systemd,说明是正常启动的系统,不是 chroot。if grep -q "systemd" /proc/1/comm 2>/dev/null; thenreturn 1 # 不是 chrootelsereturn 0 # 是 chrootfielsereturn 0 # 没有 /proc,肯定是 chrootfi
}if ! is_chroot; thenif command -v systemctl >/dev/null 2>&1; then #检查有没有 systemctl 这个命令。 ➔ 有些很精简的系统,可能没装 systemctl,所以这里保险一点,先检查systemctl daemon-reload || truesystemctl enable xxx.service || truesystemctl daemon-reload || truefielseecho "Running in chroot, skipping systemctl operations."fi
-
ldconfig = 帮 Linux 记住最新的动态库,避免程序找不到库出错。
-
Debian 包管理系统中常见的脚本有:
preinst:在安装前执行。用于检查环境、备份等操作。postinst:在安装后执行。用于配置服务、创建文件等任务。prerm:在卸载前执行。用于停止服务、清理工作。postrm:在卸载后执行。用于删除配置文件、清理系统。config:用于配置交互,接收用户输入并生成配置文件。