release v2.0.27:pkg install 打通——dpkg wrapper 补环境 + --instdir + --force-script-chrootless + 改写 var/lib/dpkg/info
This commit is contained in:
@@ -48,7 +48,7 @@ object TermuxInstaller {
|
||||
* 把戳的值带上版本号,**对不上就只重跑修正式步骤**(不重新解压,
|
||||
* 解压要几十秒)。以后再加这类"装完之后要修一下"的逻辑,把这里 +1。
|
||||
*/
|
||||
private const val STAMP_VALUE = "ok:16"
|
||||
private const val STAMP_VALUE = "ok:20"
|
||||
|
||||
/**
|
||||
* 安装日志。
|
||||
@@ -93,6 +93,7 @@ object TermuxInstaller {
|
||||
// 报 "Unable to determine a suitable packaging system type"。
|
||||
writeAptConf(context)
|
||||
fixBrokenSymlinks(context).let { trace(context, "修了 $it 个断链") }
|
||||
installDpkgWrapper(context)
|
||||
// ★ 顺手把源也重设一次。两个原因:
|
||||
// 1. 老设备上 bootstrap 自带的是俄罗斯源,国内连不上
|
||||
// 2. v2.0.20 之前只写了 sources.list,而 **pkg 不认那个文件** ——
|
||||
@@ -144,6 +145,7 @@ object TermuxInstaller {
|
||||
|
||||
writeAptConf(context)
|
||||
fixBrokenSymlinks(context).let { trace(context, "修了 $it 个断链") }
|
||||
installDpkgWrapper(context)
|
||||
progress("正在改写写死的 com.termux 路径…")
|
||||
val fixed = rewritePrefixes(context)
|
||||
trace(context, "改写了 $fixed 个文件里的硬编码路径")
|
||||
@@ -418,6 +420,14 @@ object TermuxInstaller {
|
||||
File(prefix, "libexec/termux-am"),
|
||||
File(prefix, "etc"),
|
||||
File(prefix, "share"),
|
||||
// ★★ dpkg 装完包会把每个包的**安装脚本**放在这里
|
||||
// (xxx.postinst / xxx.prerm ...),它们的 shebang 也是
|
||||
// 按真 Termux 的包名写的。不改写的话装到配置阶段就报:
|
||||
// dpkg: unable to execute .../var/lib/dpkg/info/openssh.postinst:
|
||||
// No such file or directory
|
||||
// —— 看着像文件不存在,其实是 shebang 里的解释器找不到。
|
||||
File(prefix, "var/lib/dpkg/info"),
|
||||
File(prefix, "var/lib/dpkg/updates"),
|
||||
)
|
||||
for (dir in dirs) {
|
||||
// etc/ 往下还有子目录(dpkg、apt 的配置)
|
||||
@@ -554,6 +564,20 @@ object TermuxInstaller {
|
||||
Dir::Bin::apt-cache "$prefix/bin/apt-cache";
|
||||
Dir::Bin::apt-config "$prefix/bin/apt-config";
|
||||
DPkg::options:: "--force-confnew";
|
||||
// ★ dpkg 的**安装根**也是编译死的(= 真 Termux 的 prefix)。
|
||||
// 不指定的话它会去创建 /data/data/com.termux/... :
|
||||
// dpkg: error: creating directory './data/data/com.termux':
|
||||
// Permission denied
|
||||
DPkg::options:: "--instdir=$prefix";
|
||||
DPkg::options:: "--admindir=$prefix/var/lib/dpkg";
|
||||
// ★ Android 应用**不能 chroot**(没 CAP_SYS_CHROOT),
|
||||
// 而 dpkg 跑 maintainer script 时默认要 chroot 进去。
|
||||
// 不加这个开关的话包能装上,但配置阶段直接失败:
|
||||
// dpkg (subprocess): failed to chroot to ...: Operation not permitted
|
||||
// dpkg: error processing package openssh (--configure)
|
||||
// dpkg 为这种情况提供了 --force-script-chrootless。
|
||||
DPkg::options:: "--force-script-chrootless";
|
||||
|
||||
// ★★ apt 的 HTTPS 要单独告诉它证书在哪。
|
||||
//
|
||||
// `SSL_CERT_FILE` / `CURL_CA_BUNDLE` 管的是 **curl**;
|
||||
@@ -745,4 +769,57 @@ object TermuxInstaller {
|
||||
return fixed
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ★★ 给 `$PREFIX/bin/dpkg` 套一层 wrapper。
|
||||
*
|
||||
* 为什么需要:`apt` 调 `dpkg` 时给的是**清洗过的环境** ——
|
||||
* 实测直接跑 dpkg 一切正常,apt 一调就报:
|
||||
*
|
||||
* dpkg: warning: 'sh' not found in PATH or not executable
|
||||
* dpkg: error: 6 expected programs not found in PATH or not executable
|
||||
*
|
||||
* 那 6 个程序都在、PATH 也对 —— 因为 PATH 根本没传进 dpkg。
|
||||
* apt 怎么派生 dpkg 我们控制不了,但**可以控制 dpkg 这个入口**:
|
||||
* 把原来的 dpkg 改名成 dpkg.real,原地放一个脚本,谁来调都先补齐环境。
|
||||
*
|
||||
* 顺带把 `--instdir` / `--admindir` 也带上(dpkg 的安装根同样是
|
||||
* 编译死的,不指定它会去创建 /data/data/com.termux/... 然后失败)。
|
||||
*/
|
||||
private fun installDpkgWrapper(context: Context) {
|
||||
val prefix = prefix(context).absolutePath
|
||||
val real = File(prefix, "bin/dpkg.real")
|
||||
val wrapper = File(prefix, "bin/dpkg")
|
||||
// 已经包过就别再包一层
|
||||
if (real.exists()) return
|
||||
if (!wrapper.exists()) {
|
||||
trace(context, "没有 bin/dpkg,跳过 wrapper")
|
||||
return
|
||||
}
|
||||
try {
|
||||
if (!wrapper.renameTo(real)) {
|
||||
trace(context, "dpkg 改名失败,跳过 wrapper")
|
||||
return
|
||||
}
|
||||
wrapper.writeText(
|
||||
"#!/data/user/0/" + context.packageName + "/files/usr/bin/sh\n" +
|
||||
"# 由豆包 App 生成:补全环境再调真正的 dpkg。\n" +
|
||||
"# apt 派生子进程时会清洗环境,PATH 传不进来,这里补上。\n" +
|
||||
"P=" + prefix + "\n" +
|
||||
"export PATH=\$P/bin:\$P/bin/applets:/system/bin:/system/xbin\n" +
|
||||
"export LD_LIBRARY_PATH=\$P/lib\n" +
|
||||
"export TMPDIR=\$P/tmp\n" +
|
||||
"export DPKG_ADMINDIR=\$P/var/lib/dpkg\n" +
|
||||
"export TERMUX__PREFIX=\$P\n" +
|
||||
"exec \$P/bin/dpkg.real --instdir=\$P --admindir=\$P/var/lib/dpkg --force-script-chrootless \"\$@\"\n",
|
||||
Charsets.UTF_8,
|
||||
)
|
||||
wrapper.setExecutable(true, false)
|
||||
real.setExecutable(true, false)
|
||||
trace(context, "已给 dpkg 套上 wrapper")
|
||||
} catch (t: Throwable) {
|
||||
trace(context, "套 wrapper 失败:${t.message}")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user