前言

近期在折腾Chroot环境下的设备时,发现软件包无法更新,得到以下错误:

(Reading database ... 77272 files and directories currently installed.)
Preparing to unpack .../mariadb-server-10.3_1%3a10.3.34-0ubuntu0.20.04.1_arm64.deb ...
Running in chroot, ignoring request.
/usr/sbin/invoke-rc.d: 551: rc-service: not found
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: warning: old mariadb-server-10.3 package pre-removal script subprocess returned error exit status 127
dpkg: trying script from the new package instead ...
Running in chroot, ignoring request.
/usr/sbin/invoke-rc.d: 551: rc-service: not found
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: error processing archive /var/cache/apt/archives/mariadb-server-10.3_1%3a10.3.34-0ubuntu0.20.04.1_arm64.deb (--unpack):
 new mariadb-server-10.3 package pre-removal script subprocess returned error exit status 127
Running in chroot, ignoring request.
Errors were encountered while processing:
 /var/cache/apt/archives/mariadb-server-10.3_1%3a10.3.34-0ubuntu0.20.04.1_arm64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

问题分析

分析文件/usr/sbin/invoke-rc.d:551,可知卸载软件包时,dpkg将系统环境识别为openrc

550             elif [ -n "$is_openrc" ]; then
551                 rc-service "${INITSCRIPTID}" "${saction}" && exit 0
552             else

由于Ubuntu采用systemd作为系统组件,那么openrc相关的二进制文件自然是不存在的,最后导致了这个问题的发生。

由于文件/usr/sbin/invoke-rc.d有判断是否使用openrc的变量,那么对该变量赋值的源头进行分析,可以发现也有判断systemd环境的变量。

277 if test -d /run/systemd/system ; then
278     is_systemd=1
279     UNIT="${INITSCRIPTID%.sh}.service"
280 elif test -f /run/openrc/softlevel ; then
281     is_openrc=1

由第277-278行可知,要使dpkg正确识别systemd环境,需要存在目录/run/systemd/system

由于是Chroot环境,外部系统不一定使用systemd进行管理,所以导致了问题的发生。

解决方案

mkdir /run/systemd/system