Compare commits

...

33 Commits

Author SHA1 Message Date
shenmo7192 c71679a23d 修复 安装异常时 卡住dpkg 的问题 2026-03-15 12:13:01 +08:00
shenmo7192 c0494c640b 新增 apm launch 相关自动补全能力 2026-03-15 12:05:06 +08:00
shenmo7192 b7c9797ef1 apm launch 会在当前进程启动,而不是在新的 2026-03-15 12:01:18 +08:00
shenmo7192 844eed8a3d apm run 支持自动启动,新增 apm launch ,1.1.8 2026-03-15 11:58:18 +08:00
shenmo7192 c69ab42228 fix: 首次从星火安装APM包的时候无法正确配置桌面快捷方式
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2026-03-15 02:17:19 +00:00
shenmo7192 0a93793fff fix: 生成的env.sh文件名错误的问题 2026-01-24 11:22:27 +08:00
shenmo7192 6ce70b3021 bump version to 1.1.7 2026-01-24 11:16:51 +08:00
shenmo7192 d67880b156 更新:修复Nvidia驱动透传后容器内部仍不可用的问题 2026-01-24 11:16:13 +08:00
shenmo7192 e47d74b136 update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2026-01-01 11:10:42 +00:00
shenmo7192 9c0a2606ba update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2026-01-01 11:08:07 +00:00
shenmo7192 c9220f3412 Merge remote-tracking branch 'refs/remotes/origin/master' 2025-12-27 18:51:00 +08:00
shenmo7192 01bbf1265d 修复打包出来的应用包含 .dpkg-new 的问题
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-12-27 10:44:49 +00:00
shenmo7192 89d17eb5f1 update src/var/lib/apm/apm/files/bin/ace-init.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-12-19 07:03:45 +00:00
shenmo7192 60ff1c1ec8 fix 报错 2025-12-19 08:34:04 +08:00
shenmo7192 3a8a8e2f14 dummy update 2025-12-19 08:32:41 +08:00
shenmo7192 270fa875f8 修正上游链接 2025-12-18 16:00:02 +08:00
shenmo7192 301079a999 add: apm 上游更新源 2025-12-18 15:56:03 +08:00
shenmo7192 705c16937d update doc 2025-12-18 15:27:12 +08:00
shenmo7192 869d11d351 version bump to 1.1.6 2025-12-18 15:22:18 +08:00
shenmo7192 3b25a62a54 支持info_env功能 2025-12-18 15:21:52 +08:00
shenmo7192 e373ae7bde 修复:在btrfs上convert时无法挂载多层layer 2025-12-11 10:12:04 +08:00
shenmo7192 6abdb10a7c update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-12-09 09:16:14 +00:00
shenmo7192 9b1e435f4f Revert "update src/usr/bin/amber-pm-convert."
This reverts commit 61e747ba088d398c4e6401110803146e203f81aa.
2025-12-09 09:10:29 +00:00
shenmo7192 306416d2e4 update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-12-03 08:27:54 +00:00
shenmo7192 1865d994ce update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-12-03 05:36:35 +00:00
shenmo7192 b6bf96817e bump apm dummy version to 1.1.1
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-28 14:01:32 +00:00
shenmo7192 6cfd4a9931 fix: Can't handle space line
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-28 14:00:05 +00:00
shenmo7192 ddcd6ae421 新增 templates 直通
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-27 08:36:01 +00:00
shenmo7192 b1f72c8984 update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-27 07:12:03 +00:00
shenmo7192 891b9b3f52 update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-27 07:04:09 +00:00
shenmo7192 609f2a051d update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-26 02:46:02 +00:00
shenmo7192 08be97227d update src/usr/bin/amber-pm-convert.
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-26 02:41:56 +00:00
shenmo7192 3a0094f2c3 适配 ACE
Signed-off-by: shenmo <jifengshenmo@outlook.com>
2025-11-25 12:10:23 +00:00
15 changed files with 1434 additions and 269 deletions
+273 -95
View File
@@ -1,16 +1,19 @@
# APM 软件包打包流程 # APM 软件包打包流程
本文档为开发者准备,若您只是想从 deb 软件包打包 APM 软件包,您可以通过 `amber-pm-convert`指令进行全自动一键转换 本文档为开发者准备,若您只是想从 deb 软件包打包 APM 软件包,您可以通过 `amber-pm-convert` 指令进行全自动一键转换
通过 `apm search amber-pm- ` 即可搜索到所有可用的 base 列表 通过 `apm search amber-pm-` 即可搜索到所有可用的 base 列表
--- ---
## APM 软件包结构规范 ## APM 软件包结构规范
在阅读前,请确保您对overlayfs有了基本的了解 在阅读前,请确保您对 OverlayFS 有了基本的了解
overlayfs 原理解析:https://www.cnblogs.com/arnoldlu/p/13055501.html OverlayFS 原理解析:
[https://www.cnblogs.com/arnoldlu/p/13055501.html](https://www.cnblogs.com/arnoldlu/p/13055501.html)
--- ---
@@ -18,16 +21,25 @@ overlayfs 原理解析:https://www.cnblogs.com/arnoldlu/p/13055501.html
APM 使用 OverlayFS 来管理软件包的文件系统层级,从上到下的层叠顺序为: APM 使用 OverlayFS 来管理软件包的文件系统层级,从上到下的层叠顺序为:
1. **Upperdir** - 当前包的可写层 `files/core/` 1. **Upperdir**
2. **Info Layer Override** - 覆盖层指定的目录(位于所有依赖层之上) 当前包的可写层:`files/core/`
3. **依赖层** - 从 `info` 文件递归解析出的所有依赖包
3. **底层** - 最基础的运行时环境
这种层叠结构允许上层文件覆盖下层文件,实现依赖管理和自定义覆盖。 2. **Info Layer Override**
`info_layer_override` 指定的覆盖层,位于所有依赖层之上
3. **依赖层**
`info` 文件递归解析出的所有依赖包
4. **底层 Runtime**
最基础的运行时环境(如 `amber-pm-bookworm`
这种层叠结构允许上层文件覆盖下层文件,实现灵活、高效的依赖管理与环境定制。
--- ---
一个典型的 APM 软件/中层依赖包应当包含以下内容 ## APM 软件包目录结构示例
一个典型的 APM 应用或中层依赖包应当包含以下内容:
``` ```
├── DEBIAN ├── DEBIAN
@@ -41,20 +53,23 @@ APM 使用 OverlayFS 来管理软件包的文件系统层级,从上到下的
│ ├── applications │ ├── applications
│ ├── doc │ ├── doc
│ ├── glib-2.0 │ ├── glib-2.0
│ └── man │ └── man
├── files ├── files
│ ├── core │ ├── core
│ └── work │ └── work
├── info ├── info
── info_layer_override # 可选,用于自定义覆盖层 ── info_layer_override # 可选
└── info_env # 可选(高级功能)
``` ```
### DEBIAN 目录 ---
包含软件包的基本信息和依赖的环境信息 ## DEBIAN 目录说明
包含软件包的基本信息和依赖环境声明。
### control 文件示例
**control 文件示例:**
``` ```
Package: eom Package: eom
Version: 1.26.0-2-apm Version: 1.26.0-2-apm
@@ -63,18 +78,34 @@ Maintainer: APM Converter <apm-convert@spark-app.store>
Depends: amber-pm-bookworm Depends: amber-pm-bookworm
Installed-Size: 45228 Installed-Size: 45228
Description: APM converted package from eom Description: APM converted package from eom
This package was automatically converted from the original deb package. This package was automatically converted from the original deb package.
Based on: amber-pm-bookworm Based on: amber-pm-bookworm
``` ```
- **Package**: 包名,应当唯一。若使用转换器进行转换,默认和原包名一致 字段说明:
- **Version**: 版本号。若使用转换器进行转换,默认在原版本号后加 `-apm`
- **Architecture**: 软件包架构,同 dpkg 规范填写 * **Package**
- **Depends**: 直接依赖的 base 包名 包名,应当唯一。使用转换器时默认与原 deb 包名一致
- **Installed-Size**: 安装后的大小,转换器会自动计算
- **Description**: 包描述,转换器会自动填写 * **Version**
软件包版本号,转换器会自动追加 `-apm`
* **Architecture**
架构信息,遵循 dpkg 规范
* **Depends**
直接依赖的 base 包名
* **Installed-Size**
安装后大小,转换器自动计算
* **Description**
软件包描述信息
---
### postinst 文件
**postinst 文件内容:**
``` ```
#!/bin/bash #!/bin/bash
PACKAGE_NAME="$DPKG_MAINTSCRIPT_PACKAGE" PACKAGE_NAME="$DPKG_MAINTSCRIPT_PACKAGE"
@@ -82,121 +113,268 @@ PACKAGE_NAME="$DPKG_MAINTSCRIPT_PACKAGE"
if [ "$1" = "remove" ] || [ "$1" = "purge" ]; then if [ "$1" = "remove" ] || [ "$1" = "purge" ]; then
echo "清理卸载残留" echo "清理卸载残留"
rm -rf "/var/lib/apm/$PACKAGE_NAME" rm -rf "/var/lib/apm/$PACKAGE_NAME"
for username in $(ls /home)
do for username in $(ls /home); do
echo /home/$username if [ -d "/home/$username/.apm/$PACKAGE_NAME" ]; then
if [ -d "/home/$username/.apm/$PACKAGE_NAME" ] rm -rf "/home/$username/.apm/$PACKAGE_NAME"
then
rm -fr "/home/$username/.apm/$PACKAGE_NAME"
fi fi
done done
else else
echo "非卸载,跳过清理" echo "非卸载,跳过清理"
fi fi
``` ```
若无特殊需求,内容保持一致即可,用于卸载软件包后清理环境。 若无特殊需求,保持该内容即可,用于卸载时清理残留环境。
### /var/lib/apm 目录结构 ---
包含 APM 软件容器的文件和信息: ## /var/lib/apm 目录结构说明
- **entries** (可选):包含需要放置到 `/usr/share/` 的文件,如 desktop、icon 等 该目录包含 APM 软件包的运行环境与元数据。
- **files** (必须):包含软件包的 upperdir 和 workdir
- **info** (必须):包含直接依赖的 base 信息。若应用使用了多层的依赖,会一层一层寻找 info 信息,直到找到底层依赖。如填写 amber-pm-bookworm-spark-wine10 会自动解析出 amber-pm-bookworm
- **info_layer_override** (可选):用于指定自定义覆盖层的目录
> 关于 Info: 使用多层的依赖并不是必须的,即使不使用也可以正常打包,但恰当地使用多层依赖可大大降低包体积。 可用的多层依赖见 `apm search amber-pm-[base名称]- ` 。若有必要,可申请新增 base ### 必须目录
* **files/**
**entries 目录说明:** * `core/`upperdir,可写层
- `entries/applications`:存放 `.desktop` 文件 * `work/`OverlayFS 工作目录
- `entries/doc`:存放文档
- `entries/glib-2.0`:存放 GLib 相关文件
- `entries/man`:存放帮助文档
> **重要提示**`.desktop` 文件应当添加一行 `X-APM-APPID=包名` 来允许软件管理器管理应用 * **info**
### info_layer_override 文件 * 声明直接依赖的 base 包
* 支持多层递归解析
`info_layer_override` 是一个可选文件,用于在当前包的依赖层之上插入额外的覆盖层。这个功能在以下场景特别有用: ### 可选目录 / 文件
1. **自定义库版本**:覆盖依赖包中的特定库文件,如你想要用更新版本的 mesa 覆盖 debian 默认提供的版本作为运行环境 * **entries/**
2. **配置文件自定义**:使用自定义配置覆盖默认配置
- **语法**:与 `info` 文件一致,每行一个包名
- **层叠位置**:位于所有依赖层之上、当前包的 upperdir 之下
- **文件位置**`${PATH_PREFIX}/var/lib/apm/${coredir}/info_layer_override`
**示例:** * `applications/``.desktop` 文件
假设您想用自定义的 `override-layer` 包来覆盖 `base-package` 中的某些文件: * `doc/`:文档
* `glib-2.0/`GLib 相关文件
* `man/`:手册页
> ⚠ `.desktop` 文件中 **必须** 添加:
>
> ```
> X-APM-APPID=包名
> ```
>
> 以允许软件管理器正确识别和管理应用。
---
## info 文件说明(依赖解析)
`info` 文件用于声明当前包直接依赖的 base 包,每行一个包名:
```
amber-pm-bookworm-spark-wine10
```
APM 会递归解析该 base 的 `info` 文件,直到找到最底层 runtime(如 `amber-pm-bookworm`)。
> 使用多层依赖并非强制,但合理拆分 base 能显著减小包体积。
> 可用的 base 列表可通过:
>
> ```
> apm search amber-pm-
> ```
>
> 查看。
---
## info_layer_override 文件(覆盖层)
`info_layer_override` 是一个可选文件,用于在**所有依赖层之上**插入额外覆盖层。
### 使用场景
1. 覆盖依赖中的特定库版本(如 mesa)
2. 覆盖默认配置文件
3. 提供特殊运行环境
### 规则说明
* 语法与 `info` 完全一致
* 每行一个包名
* 层级位置:
```
upperdir
info_layer_override
info 递归依赖
```
### 示例
`info`
`my-package/info` 内容:
``` ```
amber-pm-bookworm amber-pm-bookworm
``` ```
`my-package/info_layer_override` 内容 `info_layer_override`
``` ```
amber-pm-bookworm-mesa amber-pm-bookworm-mesa
``` ```
最终的挂载 lowerdir 为:`amber-pm-bookworm-mesa:amber-pm-bookworm` 最终 lowerdir 顺序:
这样,`override-layer` 中的文件会覆盖 `base-package` 中的同名文件(除非当前包的 upperdir 中也有该文件)。 ```
amber-pm-bookworm-mesa:amber-pm-bookworm
## APM upperdir 制作流程
以下为手动制作 upperdir 的流程:
1. **安装依赖**:首先安装 apm 并使用 `sudo apm install` 安装所需的 base
2. **创建目录结构**
```bash
mkdir -p core work ace-env
``` ```
3. **挂载 overlay** ---
```bash
sudo mount -t overlay overlay \ ## info_env(环境变量层 · 高级功能)
-o lowerdir='/var/lib/apm/apm/files/ace-env/var/lib/apm/base包的包名(如amber-pm-trixie/files/ace-env',upperdir=core/,workdir=work/ ./ace-env
`info_env` 是一个 **可选的高级特性**,用于为 APM 容器运行时提供**分层的环境变量配置能力**。
### 功能概述
* 为软件包及其依赖提供环境变量
* 支持 **多层叠加**
* **上层自动覆盖下层**
* 与 OverlayFS 层级顺序完全一致
* 不执行 shell 代码,仅解析键值对,安全可靠
---
### info_env 文件位置
```
/var/lib/apm/<包名>/info_env
``` ```
4. **chroot 安装**chroot 进入 `./ace-env` 进行安装操作,可以使用 `apt install` 或其他方式 ---
5. **卸载并打包**:完成后解除挂载 `./ace-env` ### info_env 应用顺序(重要)
您将得到 环境变量的加载顺序为
- **core**:保存新增文件
- **work**:保存变更信息
将这两个目录权限设置为 755 后放入对应的目录进行 apm 打包。 1. 底层 runtime 的 `info_env`
2. 中间依赖包的 `info_env`
3. 当前包的 `info_env`
4. `info_layer_override` 中包的 `info_env`(最高优先级)
**后加载的变量会覆盖之前的同名变量。**
---
### info_env 文件格式
每行一条环境变量定义:
```
KEY=VALUE
```
示例:
```
QT_QPA_PLATFORM=dxcb;xcb
LANG=zh_CN.UTF-8
XMODIFIERS="@im=fcitx"
PATH="/custom/bin:$PATH"
```
#### 规则说明
* 支持分号 `;`
* 支持带引号的值
* 支持引用已有环境变量(如 `$PATH`
* 支持注释行(`#`
* 不允许执行任何 shell 语句
❌ 以下内容将被忽略:
```
export A=1
rm -rf /
$(whoami)
```
---
### 使用场景示例
* 指定 Qt / GTK 平台插件
* 设置输入法变量
* 调整运行时 PATH / LD_LIBRARY_PATH
* 为特定应用注入兼容性环境变量
---
## APM upperdir 制作流程(手动)
1. 安装 APM 并安装所需 base
```bash
sudo apm install amber-pm-xxx
```
2. 创建目录结构:
```bash
mkdir -p core work ace-env
```
3. 挂载 OverlayFS
```bash
sudo mount -t overlay overlay \
-o lowerdir='/var/lib/apm/apm/files/ace-env/var/lib/apm/amber-pm-xxx/files/ace-env',upperdir=core/,workdir=work/ \
./ace-env
```
4. chroot 进入 `ace-env` 进行安装
5. 卸载并打包
---
## APM 软件包测试 ## APM 软件包测试
您可以测试刚刚打包的软件:
```bash ```bash
fuse-overlayfs -o lowerdir='/var/lib/apm/apm/files/ace-env/var/lib/apm/base包的包名(如amber-pm-trixie/files/ace-env',upperdir=core/,workdir=work/ ./ace-env fuse-overlayfs -o lowerdir='...',upperdir=core/,workdir=work/ ./ace-env
``` ```
即可只读挂载。`apm run 包名` 会自动完成 或直接使用
- 寻找 `/var/lib/apm/包名/` 是否存在
- 根据 `info` 文件(和可选的 `info_layer_override`)合成 fuse-overlayfs 参数进行挂载
- 使用 ACE 工具 chroot 进入并启动应用
使用 `./ace-run` 即可进入容器环境,测试您安装的应用。 ```bash
apm run 包名
```
## APM 打包 APM 会自动完成:
* 解析 `info` / `info_layer_override`
* 应用 `info_env`
* 构建 OverlayFS
* 进入容器并运行应用
---
## APM 软件包打包
使用以下命令进行打包:
```bash ```bash
dpkg-deb --build 软件包目录 输出目录 dpkg-deb --build 软件包目录 输出目录
``` ```
## APM 底层 Base Runtime 的构建 ---
详见 https://gitee.com/amber-ce/amber-pm-common ## APM 底层 Base Runtime 构建
详见:
[https://gitee.com/amber-ce/amber-pm-common](https://gitee.com/amber-ce/amber-pm-common)
--- ---
**备注**:APM 打包工具和转换器会为您自动处理大部分复杂操作,手动打包主要用于特殊情况或自定义需求。 ### 备注
APM 的打包工具与转换器会自动处理绝大多数复杂操作。
手动打包与 `info_env` 主要用于 **特殊运行环境、深度定制或调试用途**
+1 -1
View File
@@ -1,5 +1,5 @@
Package: apm Package: apm
Version: 1.1 Version: 1.1.2
Maintainer: shenmo <jifengshenmo@outlook.com> Maintainer: shenmo <jifengshenmo@outlook.com>
Priority: optional Priority: optional
Section: utils Section: utils
+2 -2
View File
@@ -30,7 +30,7 @@ linkDir() {
ensureTargetDir "$targetFile" ensureTargetDir "$targetFile"
sourceFile=$(realpath --relative-to="$(dirname $targetFile)" "$sourceFile" ) sourceFile=$(realpath --relative-to="$(dirname $targetFile)" "$sourceFile" )
if [ ! -e ${targetFile} ];then if [ ! -e "${targetFile}" ];then
ln -sv "$sourceFile" "$targetFile" ln -sv "$sourceFile" "$targetFile"
fi fi
done done
@@ -98,7 +98,7 @@ function exec_link_clean(){
find /usr/share/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/help -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/help -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/locale -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/locale -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/lib/$(gcc -dumpmachine)/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & # find /usr/lib/$(gcc -dumpmachine)/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/lib/mozilla/plugins -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/lib/mozilla/plugins -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/polkit-1/actions -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/polkit-1/actions -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/fonts -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/fonts -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
+1 -1
View File
@@ -1 +1 @@
@VERSION@=1.1.5 @VERSION@=1.1.8
+1 -1
View File
@@ -15,7 +15,7 @@ systemctl restart apparmor.service || true
fi fi
# Send statistics data # Send statistics data
/var/lib/apm/apm/files/feedback.sh & /var/lib/apm/apm/files/feedback.sh &
amber-pm-dstore-patch
;; ;;
triggered) triggered)
+2
View File
@@ -0,0 +1,2 @@
deb [signed-by=/etc/apt/trusted.gpg.d/apm.gpg] https://d.spark-app.store/apm-deb-source /
# 上面这行配置可在4.1.2+版本普及后启用,可以做到分不同目录
Binary file not shown.
+84
View File
@@ -0,0 +1,84 @@
#!/bin/bash
# ===== 日志函数(简化版)=====
log.info() { echo "INFO: $*"; }
log.warn() { echo "WARN: $*"; }
log.error() { echo "ERROR: $*"; }
log.debug() { :; } # APM 场景下可禁用 debug 日志
# ===== APM 专用桌面文件扫描(单文件)=====
function scan_apm_desktop_log() {
unset desktop_file_path
local pkg_name="$1"
local desktop_dir="/var/lib/apm/apm/files/ace-env/var/lib/apm/${pkg_name}/entries/applications"
[ -d "$desktop_dir" ] || return 1
while IFS= read -r -d '' path; do
[ -f "$path" ] || continue
if ! grep -q 'NoDisplay=true' "$path" 2>/dev/null; then
log.info "Found valid APM desktop file: $path"
export desktop_file_path="$path"
return 0
fi
done < <(find "$desktop_dir" -name "*.desktop" -type f -print0 2>/dev/null)
return 1
}
# ===== APM 专用桌面文件扫描(多文件列表)=====
function scan_apm_desktop_list() {
local pkg_name="$1"
local desktop_dir="/var/lib/apm/apm/files/ace-env/var/lib/apm/${pkg_name}/entries/applications"
local result=""
[ -d "$desktop_dir" ] || { echo ""; return; }
while IFS= read -r -d '' path; do
[ -f "$path" ] || continue
if ! grep -q 'NoDisplay=true' "$path" 2>/dev/null; then
result+="${path},"
fi
done < <(find "$desktop_dir" -name "*.desktop" -type f -print0 2>/dev/null)
echo "${result%,}"
}
# ===== 启动应用 =====
function launch_app() {
local desktop_path="${1#file://}"
local exec_cmd
shift # 移除第一个参数(desktop_path),剩余的是要传递给应用的参数
# 提取并清理 Exec 行(移除字段代码如 %f %u 等)
exec_cmd=$(grep -m1 '^Exec=' "$desktop_path" | cut -d= -f2- | sed 's/%[fFuUdDnNickvm]*//g; s/^[[:space:]]*//; s/[[:space:]]*$//')
[ -z "$exec_cmd" ] && return 1
# 如果有额外参数,添加到命令中
if [ $# -gt 0 ]; then
log.info "Launching with arguments: $*"
exec_cmd="$exec_cmd $*"
fi
log.info "Launching: $exec_cmd"
${SHELL:-bash} -c "$exec_cmd"
}
# 导出函数供 ACE 环境调用
export -f launch_app scan_apm_desktop_log scan_apm_desktop_list log.info log.error
# ===== 主逻辑 =====
[ $# -lt 1 ] && {
log.error "Usage: $0 <apm-package-name> [additional arguments...]"
exit 1
}
pkg_name="$1"
shift # 移除包名参数,剩余的都是要传递给应用的参数
# 直接执行 launch 逻辑,并将剩余参数传递给 launch_app
if scan_apm_desktop_log "$pkg_name" && launch_app "$desktop_file_path" "$@"; then
exit 0
else
exit 1
fi
+118 -20
View File
@@ -233,7 +233,7 @@ log.debug "最终 lowerdir: $LOWERDIR"
# 3. 进行融合挂载 # 3. 进行融合挂载
log.info "正在进行融合挂载..." log.info "正在进行融合挂载..."
sudo mount -t overlay overlay \ sudo fuse-overlayfs \
-o "lowerdir=$LOWERDIR,upperdir=$CRAFT_DIR/core/,workdir=$CRAFT_DIR/work/" \ -o "lowerdir=$LOWERDIR,upperdir=$CRAFT_DIR/core/,workdir=$CRAFT_DIR/work/" \
"$CRAFT_DIR/mergedir" "$CRAFT_DIR/mergedir"
@@ -322,6 +322,7 @@ resolve_symlink() {
echo "$file" echo "$file"
} }
# 函数:交互式选择文件复制到entries目录(用于手动模式无DEB情况)
# 函数:交互式选择文件复制到entries目录(用于手动模式无DEB情况) # 函数:交互式选择文件复制到entries目录(用于手动模式无DEB情况)
interactive_copy_entries() { interactive_copy_entries() {
local core_dir="$CRAFT_DIR/core" local core_dir="$CRAFT_DIR/core"
@@ -330,13 +331,13 @@ interactive_copy_entries() {
log.info "开始交互式选择文件复制到 entries 目录..." log.info "开始交互式选择文件复制到 entries 目录..."
mkdir -p "$entries_dir/applications" "$entries_dir/icons" mkdir -p "$entries_dir/applications" "$entries_dir/icons"
# 查找桌面文件 # 查找桌面文件(保留完整路径)
local desktop_files=() local desktop_files=()
while IFS= read -r -d '' file; do while IFS= read -r -d '' file; do
[[ -f "$file" ]] && desktop_files+=("$file") [[ -f "$file" ]] && desktop_files+=("$file")
done < <(find "$core_dir/usr/share" -name "*.desktop" -print0 2>/dev/null || true) done < <(find "$core_dir/usr/share" -name "*.desktop" -print0 2>/dev/null || true)
# 查找图标文件 # 查找图标文件(保留完整路径)
local icon_files=() local icon_files=()
while IFS= read -r -d '' file; do while IFS= read -r -d '' file; do
[[ -f "$file" ]] && icon_files+=("$file") [[ -f "$file" ]] && icon_files+=("$file")
@@ -347,8 +348,9 @@ interactive_copy_entries() {
log.info "找到 ${#desktop_files[@]} 个桌面文件:" log.info "找到 ${#desktop_files[@]} 个桌面文件:"
for i in "${!desktop_files[@]}"; do for i in "${!desktop_files[@]}"; do
local file="${desktop_files[$i]}" local file="${desktop_files[$i]}"
local filename=$(basename "$file") # 显示完整路径(相对于 core_dir)
echo " $((i+1)). $filename" local relative_path="${file#$core_dir}"
echo " $((i+1)). $relative_path"
# 检查是否是符号链接 # 检查是否是符号链接
if [ -L "$file" ]; then if [ -L "$file" ]; then
@@ -357,7 +359,8 @@ interactive_copy_entries() {
# 解析符号链接获取实际文件 # 解析符号链接获取实际文件
local resolved_file=$(resolve_symlink "$file" "$core_dir") local resolved_file=$(resolve_symlink "$file" "$core_dir")
if [ "$resolved_file" != "$file" ] && [ -f "$resolved_file" ]; then if [ "$resolved_file" != "$file" ] && [ -f "$resolved_file" ]; then
echo " → 解析为: $(basename "$resolved_file")" local resolved_relative="${resolved_file#$core_dir}"
echo " → 解析为: $resolved_relative"
desktop_files[$i]="$resolved_file" desktop_files[$i]="$resolved_file"
fi fi
fi fi
@@ -367,10 +370,22 @@ interactive_copy_entries() {
read -r -p "请选择要复制的桌面文件编号(多个用逗号分隔,all=全部,none=跳过): " desktop_choice read -r -p "请选择要复制的桌面文件编号(多个用逗号分隔,all=全部,none=跳过): " desktop_choice
if [[ "$desktop_choice" =~ ^[Aa][Ll][Ll]$ ]]; then if [[ "$desktop_choice" =~ ^[Aa][Ll][Ll]$ ]]; then
# 复制所有桌面文件到 entries/applications # 复制所有桌面文件到 entries/applications,但保持目录结构
for file in "${desktop_files[@]}"; do for file in "${desktop_files[@]}"; do
local filename=$(basename "$file") local relative_path="${file#$core_dir}"
local dest_path="$entries_dir/applications/$filename" local dest_filename=$(basename "$file")
# 如果文件在 applications 目录下,直接复制到 entries/applications
if [[ "$relative_path" == /usr/share/applications/* ]]; then
local dest_path="$entries_dir/applications/$dest_filename"
else
# 其他位置的桌面文件,保持相对路径结构
local path_dir=$(dirname "$relative_path")
local dest_dir="$entries_dir/applications$path_dir"
mkdir -p "$dest_dir"
local dest_path="$dest_dir/$dest_filename"
fi
cp -v "$file" "$dest_path" cp -v "$file" "$dest_path"
# 处理桌面文件内容 # 处理桌面文件内容
@@ -384,8 +399,19 @@ interactive_copy_entries() {
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#desktop_files[@]} ]; then if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#desktop_files[@]} ]; then
local idx=$((choice-1)) local idx=$((choice-1))
local file="${desktop_files[$idx]}" local file="${desktop_files[$idx]}"
local filename=$(basename "$file") local relative_path="${file#$core_dir}"
local dest_path="$entries_dir/applications/$filename" local dest_filename=$(basename "$file")
# 根据路径决定目标位置
if [[ "$relative_path" == /usr/share/applications/* ]]; then
local dest_path="$entries_dir/applications/$dest_filename"
else
local path_dir=$(dirname "$relative_path")
local dest_dir="$entries_dir/applications$path_dir"
mkdir -p "$dest_dir"
local dest_path="$dest_dir/$dest_filename"
fi
cp -v "$file" "$dest_path" cp -v "$file" "$dest_path"
# 处理桌面文件内容 # 处理桌面文件内容
@@ -406,8 +432,8 @@ interactive_copy_entries() {
log.info "找到 ${#icon_files[@]} 个图标文件:" log.info "找到 ${#icon_files[@]} 个图标文件:"
for i in "${!icon_files[@]}"; do for i in "${!icon_files[@]}"; do
local file="${icon_files[$i]}" local file="${icon_files[$i]}"
local filename=$(basename "$file") local relative_path="${file#$core_dir}"
echo " $((i+1)). $filename" echo " $((i+1)). $relative_path"
# 检查是否是符号链接 # 检查是否是符号链接
if [ -L "$file" ]; then if [ -L "$file" ]; then
@@ -416,7 +442,8 @@ interactive_copy_entries() {
# 解析符号链接获取实际文件 # 解析符号链接获取实际文件
local resolved_file=$(resolve_symlink "$file" "$core_dir") local resolved_file=$(resolve_symlink "$file" "$core_dir")
if [ "$resolved_file" != "$file" ] && [ -f "$resolved_file" ]; then if [ "$resolved_file" != "$file" ] && [ -f "$resolved_file" ]; then
echo " → 解析为: $(basename "$resolved_file")" local resolved_relative="${resolved_file#$core_dir}"
echo " → 解析为: $resolved_relative"
icon_files[$i]="$resolved_file" icon_files[$i]="$resolved_file"
fi fi
fi fi
@@ -426,10 +453,23 @@ interactive_copy_entries() {
read -r -p "请选择要复制的图标文件编号(多个用逗号分隔,all=全部,none=跳过): " icon_choice read -r -p "请选择要复制的图标文件编号(多个用逗号分隔,all=全部,none=跳过): " icon_choice
if [[ "$icon_choice" =~ ^[Aa][Ll][Ll]$ ]]; then if [[ "$icon_choice" =~ ^[Aa][Ll][Ll]$ ]]; then
# 复制所有图标文件到 entries/icons # 复制所有图标文件到 entries/icons,保持目录结构
for file in "${icon_files[@]}"; do for file in "${icon_files[@]}"; do
local filename=$(basename "$file") local relative_path="${file#$core_dir}"
local dest_path="$entries_dir/icons/$filename" local dest_filename=$(basename "$file")
# 如果文件在 icons 主题目录下,直接复制到 entries/icons
if [[ "$relative_path" == /usr/share/icons/* ]] ||
[[ "$relative_path" == /usr/share/pixmaps/* ]]; then
local dest_path="$entries_dir/icons/$dest_filename"
else
# 其他位置的图标文件,保持相对路径结构
local path_dir=$(dirname "$relative_path")
local dest_dir="$entries_dir/icons$path_dir"
mkdir -p "$dest_dir"
local dest_path="$dest_dir/$dest_filename"
fi
cp -v "$file" "$dest_path" cp -v "$file" "$dest_path"
done done
elif [[ ! "$icon_choice" =~ ^[Nn][Oo][Nn][Ee]$ ]] && [ -n "$icon_choice" ]; then elif [[ ! "$icon_choice" =~ ^[Nn][Oo][Nn][Ee]$ ]] && [ -n "$icon_choice" ]; then
@@ -440,8 +480,20 @@ interactive_copy_entries() {
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#icon_files[@]} ]; then if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#icon_files[@]} ]; then
local idx=$((choice-1)) local idx=$((choice-1))
local file="${icon_files[$idx]}" local file="${icon_files[$idx]}"
local filename=$(basename "$file") local relative_path="${file#$core_dir}"
local dest_path="$entries_dir/icons/$filename" local dest_filename=$(basename "$file")
# 根据路径决定目标位置
if [[ "$relative_path" == /usr/share/icons/* ]] ||
[[ "$relative_path" == /usr/share/pixmaps/* ]]; then
local dest_path="$entries_dir/icons/$dest_filename"
else
local path_dir=$(dirname "$relative_path")
local dest_dir="$entries_dir/icons$path_dir"
mkdir -p "$dest_dir"
local dest_path="$dest_dir/$dest_filename"
fi
cp -v "$file" "$dest_path" cp -v "$file" "$dest_path"
else else
log.warn "无效的选择: $choice" log.warn "无效的选择: $choice"
@@ -456,13 +508,39 @@ interactive_copy_entries() {
} }
# 函数:处理桌面文件内容 # 函数:处理桌面文件内容
# 函数:处理桌面文件内容(安全版本,避免重复处理)
process_desktop_file() { process_desktop_file() {
local desktop_file="$1" local desktop_file="$1"
local pkgname="$2" local pkgname="$2"
# 新增:精确路径检查
local apps_path1="/usr/share/applications"
local apps_path2="/opt/apps/${ORIG_PKGNAME}/entries/applications"
local apps_path3="/usr/local/share/applications"
if [[ ! "$desktop_file" =~ ^.*${apps_path1}/.*\.desktop$ ]] &&
[[ ! "$desktop_file" =~ ^.*${apps_path2}/.*\.desktop$ ]] &&
[[ ! "$desktop_file" =~ ^.*${apps_path3}/.*\.desktop$ ]]; then
log.debug "跳过非应用程序 desktop 文件: $desktop_file"
return 0
fi
log.info "处理桌面文件: $desktop_file" log.info "处理桌面文件: $desktop_file"
# 检查文件是否已经处理过(避免重复添加 apm run)
if grep -q "^Exec=apm run $pkgname " "$desktop_file"; then
log.info "桌面文件已经处理过,跳过: $desktop_file"
return 0
fi
# 检查是否有其他包的 apm run 前缀(清理旧的)
if grep -q "^Exec=apm run [^ ]* " "$desktop_file"; then
log.info "发现旧的 apm run 前缀,清理后重新添加"
# 移除所有 apm run 前缀
sed -i "s|^Exec=apm run [^ ]* ||" "$desktop_file"
fi
# 尝试用 busybox dos2unix(若不存在则跳过转换) # 尝试用 busybox dos2unix(若不存在则跳过转换)
if command -v busybox >/dev/null 2>&1; then if command -v busybox >/dev/null 2>&1; then
busybox dos2unix "$desktop_file" 2>/dev/null || true busybox dos2unix "$desktop_file" 2>/dev/null || true
@@ -486,8 +564,9 @@ process_desktop_file() {
sed -i "s|^Icon=/|Icon=/var/lib/apm/apm/files/ace-env/var/lib/apm/$pkgname/files/core/|" "$desktop_file" sed -i "s|^Icon=/|Icon=/var/lib/apm/apm/files/ace-env/var/lib/apm/$pkgname/files/core/|" "$desktop_file"
fi fi
# 添加 X-APM-APPID # 添加 X-APM-APPID(如果不存在)
if ! grep -q "X-APM-APPID" "$desktop_file"; then if ! grep -q "X-APM-APPID" "$desktop_file"; then
echo "" >> "$desktop_file"
echo "X-APM-APPID=$pkgname" >> "$desktop_file" echo "X-APM-APPID=$pkgname" >> "$desktop_file"
fi fi
@@ -499,6 +578,7 @@ process_desktop_file() {
fi fi
} }
# 4. 如果有 DEB 文件,进行自动化的检查、解包与修改 # 4. 如果有 DEB 文件,进行自动化的检查、解包与修改
if [ -n "$DEB_PATH" ]; then if [ -n "$DEB_PATH" ]; then
@@ -511,6 +591,7 @@ if [ -n "$DEB_PATH" ]; then
exit 1 exit 1
fi fi
log.info "安装前检查通过,准备进行提取与修改..." log.info "安装前检查通过,准备进行提取与修改..."
sudo -E chrootEnvPath="$chrootEnvPath" /var/lib/apm/apm/files/ace-run-pkg mkdir -p /var/cache/apt/archives/partial
# 提取 DEB 包内容并准备修改 # 提取 DEB 包内容并准备修改
log.info "提取并修改原DEB包..." log.info "提取并修改原DEB包..."
@@ -567,6 +648,23 @@ fi
sudo -E chrootEnvPath="$chrootEnvPath" /var/lib/apm/apm/files/ace-run-pkg rm -vfr /var/lib/aptss/lists || true sudo -E chrootEnvPath="$chrootEnvPath" /var/lib/apm/apm/files/ace-run-pkg rm -vfr /var/lib/aptss/lists || true
sudo -E chrootEnvPath="$chrootEnvPath" /var/lib/apm/apm/files/ace-run-pkg rm -vfr /var/cache/apt/* || true sudo -E chrootEnvPath="$chrootEnvPath" /var/lib/apm/apm/files/ace-run-pkg rm -vfr /var/cache/apt/* || true
#清理 .dpkg-new 文件
log.info "搜索并清理 .dpkg-new 文件..."
# 在 core 目录下查找并删除所有以 .dpkg-new 结尾的文件
find "$CRAFT_DIR/core" -name "*.dpkg-new" 2>/dev/null | while read -r file; do
log.info "删除: $file"
sudo rm -f "$file"
done
# 统计清理结果
COUNT=$(find "$CRAFT_DIR/core" -name "*.dpkg-new" -type f 2>/dev/null | wc -l)
if [ "$COUNT" -eq 0 ]; then
log.info "已清理所有 .dpkg-new 文件"
else
log.warn "仍有 $COUNT 个 .dpkg-new 文件存在"
fi
# 5. 创建新的 APM 包结构 # 5. 创建新的 APM 包结构
log.info "创建新的APM包结构..." log.info "创建新的APM包结构..."
PKG_BUILD_DIR="$CRAFT_DIR/new-pkg" PKG_BUILD_DIR="$CRAFT_DIR/new-pkg"
+2 -2
View File
@@ -30,7 +30,7 @@ linkDir() {
ensureTargetDir "$targetFile" ensureTargetDir "$targetFile"
sourceFile=$(realpath --relative-to="$(dirname $targetFile)" "$sourceFile" ) sourceFile=$(realpath --relative-to="$(dirname $targetFile)" "$sourceFile" )
if [ ! -e ${targetFile} ];then if [ ! -e "${targetFile}" ];then
ln -sv "$sourceFile" "$targetFile" ln -sv "$sourceFile" "$targetFile"
fi fi
done done
@@ -98,7 +98,7 @@ function exec_link_clean(){
find /usr/share/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/help -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/help -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/locale -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/locale -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/lib/`dpkg-architecture -qDEB_HOST_MULTIARCH`/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & # find /usr/lib/$(gcc -dumpmachine)/fcitx -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/lib/mozilla/plugins -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/lib/mozilla/plugins -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/polkit-1/actions -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/polkit-1/actions -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
find /usr/share/fonts -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null & find /usr/share/fonts -xtype l -exec echo '{} is invalid now and going to be cleaned' \; -exec unlink {} \; 2>/dev/null &
+155 -49
View File
@@ -21,9 +21,10 @@ Usage:
Commands: Commands:
install 安装软件包 install 安装软件包
remove 卸载软件包 remove 卸载软件包
run <package> 运行指定软件包的可执行文件 launch <package> [args...] 启动软件包(通过应用启动器)
sandbox-run <package> 运行指定软件包的可执行文件(主目录沙箱化 run <package> [EXEC_PATH] [args...] 运行指定软件包的可执行文件(可指定容器内路径
bwrap-run <package> 运行指定软件包的可执行文件(使用特殊的挂载参数以支持bwrap sandbox-run <package> [EXEC_PATH] [args...] 运行指定软件包的可执行文件(主目录沙箱化
bwrap-run <package> [EXEC_PATH] [args...] 运行指定软件包的可执行文件(使用特殊的挂载参数以支持bwrap)
update 更新软件包信息 update 更新软件包信息
hold 锁定软件包版本 hold 锁定软件包版本
@@ -50,32 +51,36 @@ Commands:
EOF EOF
} }
apm_exec(){ apm_exec(){
# 递归读取info文件并构建lowerdir # ===============================
# 基础变量
# ===============================
local lowerdirs=() local lowerdirs=()
local current_dir="${PATH_PREFIX}/var/lib/apm/${coredir}" # 当前目录开始 local env_layers=()
local current_dir="${PATH_PREFIX}/var/lib/apm/${coredir}"
local next_info_file="" local next_info_file=""
local APM_RUN_EXEC=/var/lib/apm/apm/files/ace-run
# 使用统一的 ace-run 脚本 # ===============================
APM_RUN_EXEC=/var/lib/apm/apm/files/ace-run # 递归读取 info / info_env
# ===============================
while : ; do while : ; do
# 构建info文件的路径
next_info_file="${current_dir}/info" next_info_file="${current_dir}/info"
# 检查info文件是否存在
if [[ ! -f "$next_info_file" ]]; then # 记录 info_env(底层优先)
break if [[ -f "${current_dir}/info_env" ]]; then
env_layers+=("${current_dir}/info_env")
fi fi
# 读取info文件的每一行并构建lowerdir # 没有 info 就停止
while IFS= read -r basedir; do [[ ! -f "$next_info_file" ]] && break
[[ -z "$basedir" ]] && continue # 跳过空行
# 读取依赖层
while IFS= read -r basedir; do
[[ -z "$basedir" ]] && continue
# 检查ace-env目录是否存在
if [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env" ]]; then if [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env" ]]; then
lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env") lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env")
# 如果ace-env不存在,检查core目录
elif [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/core" ]]; then elif [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/core" ]]; then
lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/core") lowerdirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/core")
else else
@@ -83,66 +88,142 @@ apm_exec(){
fi fi
done < "$next_info_file" done < "$next_info_file"
# 尝试获取下一个依赖信息的路径 # 递归到下一个
local next_basedir=$(tail -n 1 "$next_info_file") local next_basedir
if [[ -z "$next_basedir" || ! -d "${PATH_PREFIX}/var/lib/apm/${next_basedir}" ]]; then next_basedir="$(tail -n 1 "$next_info_file")"
break [[ -z "$next_basedir" || ! -d "${PATH_PREFIX}/var/lib/apm/${next_basedir}" ]] && break
fi
# 更新当前目录,递归处理下一个依赖
current_dir="${PATH_PREFIX}/var/lib/apm/${next_basedir}" current_dir="${PATH_PREFIX}/var/lib/apm/${next_basedir}"
done done
# 添加info_layer_override功能 # ===============================
# info_layer_override(最高优先级)
# ===============================
local override_file="${PATH_PREFIX}/var/lib/apm/${coredir}/info_layer_override" local override_file="${PATH_PREFIX}/var/lib/apm/${coredir}/info_layer_override"
if [[ -f "$override_file" ]]; then if [[ -f "$override_file" ]]; then
log.debug "Found info_layer_override file: $override_file" log.debug "Found info_layer_override: $override_file"
local override_dirs=()
local override_dirs=()
local override_envs=()
# 读取override文件并构建override目录数组
while IFS= read -r basedir; do while IFS= read -r basedir; do
[[ -z "$basedir" ]] && continue [[ -z "$basedir" ]] && continue
local base="${PATH_PREFIX}/var/lib/apm/${basedir}"
# 检查override目录是否存在 if [[ -d "${base}/files/ace-env" ]]; then
if [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env" ]]; then override_dirs+=("${base}/files/ace-env")
override_dirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/ace-env") elif [[ -d "${base}/files/core" ]]; then
elif [[ -d "${PATH_PREFIX}/var/lib/apm/${basedir}/files/core" ]]; then override_dirs+=("${base}/files/core")
override_dirs+=("${PATH_PREFIX}/var/lib/apm/${basedir}/files/core")
else else
log.warn "Neither ace-env nor core directory found for override base: $basedir" log.warn "Override layer not found: $basedir"
fi
if [[ -f "${base}/info_env" ]]; then
override_envs+=("${base}/info_env")
fi fi
done < "$override_file" done < "$override_file"
# override目录插入到现有lowerdirs数组的最前(最左侧/最顶层 # override 层放最前(最
if [[ ${#override_dirs[@]} -gt 0 ]]; then if [[ ${#override_dirs[@]} -gt 0 ]]; then
log.debug "Adding override directories to the top layer"
lowerdirs=("${override_dirs[@]}" "${lowerdirs[@]}") lowerdirs=("${override_dirs[@]}" "${lowerdirs[@]}")
fi fi
# override env 最后应用(最高)
if [[ ${#override_envs[@]} -gt 0 ]]; then
env_layers+=("${override_envs[@]}")
fi
fi fi
# 检查是否找到了有效的lowerdir # ===============================
# 检查 lowerdir
# ===============================
if [[ ${#lowerdirs[@]} -eq 0 ]]; then if [[ ${#lowerdirs[@]} -eq 0 ]]; then
log.error "No valid lower directories found for package: $coredir" log.error "No valid lower directories found for package: $coredir"
return 1 return 1
fi fi
# 将lowerdirs数组用冒号连接起来 local lowerdir
local lowerdir=$(IFS=:; echo "${lowerdirs[*]}") lowerdir=$(IFS=:; echo "${lowerdirs[*]}")
# 创建挂载点目录
mkdir -p "/tmp/apm/${coredir}" mkdir -p "/tmp/apm/${coredir}"
# 使用fuse-overlayfs挂载 # ===============================
log.debug "Mounting with lowerdir: $lowerdir" # 应用 info_env(从下到上)
fuse-overlayfs -o lowerdir="$lowerdir",upperdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/core/",workdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/work/" "/tmp/apm/${coredir}" # ===============================
for env_file in "${env_layers[@]}"; do
log.debug "Applying env: $env_file"
# 执行命令 while IFS= read -r line || [[ -n "$line" ]]; do
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
local key="${line%%=*}"
local val="${line#*=}"
# 去首尾空白
val="${val#"${val%%[![:space:]]*}"}"
val="${val%"${val##*[![:space:]]}"}"
# 去外层引号
if [[ "$val" =~ ^\".*\"$ || "$val" =~ ^\'.*\'$ ]]; then
val="${val:1:-1}"
fi
export "$key=$val"
else
log.warn "Invalid env line ignored: $line"
fi
done < "$env_file"
done
# ===============================
# 挂载 overlay
# ===============================
log.debug "Mounting overlayfs"
log.debug "lowerdir=$lowerdir"
fuse-overlayfs \
-o lowerdir="$lowerdir",upperdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/core/",workdir="${PATH_PREFIX}/var/lib/apm/${coredir}/files/work/" \
"/tmp/apm/${coredir}"
# ===============================
# 执行
# ===============================
chrootEnvPath="/tmp/apm/${coredir}" "${APM_RUN_EXEC}" "$@" chrootEnvPath="/tmp/apm/${coredir}" "${APM_RUN_EXEC}" "$@"
# ===============================
# 卸载 # 卸载
# ===============================
umount "/tmp/apm/${coredir}" umount "/tmp/apm/${coredir}"
} }
# 启动应用:通过 amber-pm-app-launcher 运行
apm_launch() {
local pkg="$1"
shift
if [ -z "$pkg" ]; then
log.error "Package name required for 'launch' command"
return 1
fi
# 保存原始 PATH_PREFIX,检查包是否存在(逻辑同 run 分支)
local original_path_prefix="$PATH_PREFIX"
if ! [ -d "${PATH_PREFIX}/var/lib/apm/$pkg" ]; then
if [ -d "/var/lib/apm/$pkg" ]; then
PATH_PREFIX=""
else
log.error "Package not installed: $pkg"
return 1
fi
fi
# 调用应用启动器,传递所有参数
amber-pm-app-launcher "$pkg" "$@"
local exit_code=$?
# 恢复 PATH_PREFIX(不影响后续命令)
PATH_PREFIX="$original_path_prefix"
return $exit_code
}
# 调试信息函数 # 调试信息函数
debug_info() { debug_info() {
@@ -180,11 +261,11 @@ bronya_egg() {
cat <<'EOF' cat <<'EOF'
_ __ ____ _ ____ __ _ __ ____ _ ____ __
| | / /__ _/ / /____ ______(_)__ / __/_ _____ / /____ __ _ | | / /__ _/ / /____ ______(_)__ / __/_ _____ / /____ __ _
| |/ / _ `/ / '_/ // / __/ / -_) _\ \/ // (_-</ __/ -_) ' \ | |/ / _ `/ / '_/ // / __/ / _-) _\ \/ // (_-</ __/ -_) ' \
|___/\_,_/_/_/\_\\_, /_/ /_/\__/ /___/\_, /___/\__/\__/_/_/_/ |___/\_,_/_/_/\_\\_, /_/ /_/\__/ /___/\_, /___/\__/\__/_/_/_/
/ / ___ ___ __/___/____/ / /___/ / / ___ ___ __/___/____/ / /___/
/ /__/ _ `/ // / _ \/ __/ _ \ / /__/ _ `/ // / _ \/ __/ _ \
/____/\_,_/\_,_/_//_/\__/_//_/ /____/\_,_/_,_/_//_/\__/_//_/
Valkyrie 系统启动 - 重装小兔,Fire! Valkyrie 系统启动 - 重装小兔,Fire!
💎 感谢 Anysets 为 AmberCE 和 AmberPM 的 Arch 架构支持提供帮助~ 💎 感谢 Anysets 为 AmberCE 和 AmberPM 的 Arch 架构支持提供帮助~
@@ -249,13 +330,24 @@ for dir in "$APM_BASE"/*/; do
fi fi
done done
} }
# 主命令处理 # 主命令处理
case "$1" in case "$1" in
install|full-upgrade|upgrade|reinstall) install|full-upgrade|upgrade|reinstall)
command=$1 command=$1
shift shift
amber-pm-debug aptss "$command" "$@" amber-pm-debug aptss "$command" "$@"
exit_code=$?
# 如果第一次执行失败,尝试修复并重试
if [ $exit_code -ne 0 ]; then
log.warn "Command failed, attempting to fix with dpkg --configure -a..."
amber-pm-debug dpkg --configure -a
log.info "Retrying $command..."
amber-pm-debug aptss "$command" "$@"
exit_code=$? exit_code=$?
fi
if [ $exit_code -eq 0 ]; then if [ $exit_code -eq 0 ]; then
log.info "Operation successful" log.info "Operation successful"
else else
@@ -306,6 +398,17 @@ case "$1" in
amber-pm-debug amber-pm-dstore-patch amber-pm-debug amber-pm-dstore-patch
amber-pm-gxde-desktop-fix amber-pm-gxde-desktop-fix
;; ;;
launch)
shift
apm_launch "$@"
exit_code=$?
if [ $exit_code -eq 0 ]; then
log.info "Operation successful"
else
log.error "Error: Operation failed"
exit $exit_code
fi
;;
run) run)
# 运行包命令:第二个参数必须是包名 # 运行包命令:第二个参数必须是包名
if [ -z "$2" ]; then if [ -z "$2" ]; then
@@ -339,9 +442,11 @@ case "$1" in
log.info "Running user command: $*" log.info "Running user command: $*"
apm_exec "$@" apm_exec "$@"
else else
# 没有额外参数:提示 # 没有额外参数:提示用户改用 launch,并自动调用 launch
log.info "Usage: $SCRIPT_NAME run $pkg [EXEC_PATH]" log.info "未指定可执行文件路径。如果希望在未指定容器路径的情况下启动应用程序,推荐使用 "launch" 命令"
exit 1 log.info "正在启动:$SCRIPT_NAME launch $pkg"
apm_launch "$pkg"
exit $?
fi fi
;; ;;
sandbox-run) sandbox-run)
@@ -361,6 +466,7 @@ case "$1" in
debug_info $@ debug_info $@
;; ;;
ssaudit) ssaudit)
amber-pm-debug dpkg --configure -a
amber-pm-debug ssaudit $@ --native amber-pm-debug ssaudit $@ --native
exit_code=$? exit_code=$?
if [ $exit_code -eq 0 ]; then if [ $exit_code -eq 0 ]; then
+80 -24
View File
@@ -37,6 +37,7 @@ _apm()
# see if the user selected a command already # see if the user selected a command already
local COMMANDS=( local COMMANDS=(
"ssaudit" "ssaudit"
"launch"
"list" "list"
"search" "search"
"show" "showsrc" "show" "showsrc"
@@ -185,7 +186,7 @@ find_directories_without_ace_env() {
# 查找所有直接子目录,排除包含ace-env子目录的目录 # 查找所有直接子目录,排除包含ace-env子目录的目录
while IFS= read -r -d '' dir; do while IFS= read -r -d '' dir; do
if [[ -d "$dir" ]] && [[ ! -d "$dir/files/ace-env" ]]; then if [[ -d "$dir" ]] && [[ ! -d "$dir/files/ace-env" ]]; then
result+=("$(basename $dir)") result+=("$(basename "$dir")")
fi fi
done < <(find "$base_dir" -maxdepth 1 -type d ! -path "$base_dir" -print0 2>/dev/null) done < <(find "$base_dir" -maxdepth 1 -type d ! -path "$base_dir" -print0 2>/dev/null)
@@ -196,52 +197,107 @@ find_directories_without_ace_env() {
fi fi
return 1 return 1
} }
function apm_run_compgen(){
result=$(find_directories_without_ace_env "$primary_dir")
if [[ -n "$result" ]]; then function apm_run_compgen(){
echo "$result" result=$(find_directories_without_ace_env "$primary_dir")
else
result=$(find_directories_without_ace_env "$fallback_dir")
if [[ -n "$result" ]]; then if [[ -n "$result" ]]; then
echo "$result" echo "$result"
else else
echo "" result=$(find_directories_without_ace_env "$fallback_dir")
if [[ -n "$result" ]]; then
echo "$result"
else
echo ""
fi
fi fi
fi
} }
# 获取当前命令的参数位置
get_arg_position() {
local cmd="$1"
local pos=0
local found_cmd=0
for (( i=1; i < ${#words[@]}; i++ )); do
if [[ $found_cmd -eq 0 ]]; then
if [[ "${words[i]}" == "$cmd" ]]; then
found_cmd=1
fi
else
# 跳过选项参数(以-开头)
if [[ "${words[i]}" != -* ]]; then
((pos++))
fi
fi
done
echo $pos
}
# specific command arguments # specific command arguments
if [[ -n $command ]]; then if [[ -n $command ]]; then
# 获取参数位置
local arg_pos=$(get_arg_position "$command")
case $command in case $command in
remove|purge|autoremove) remove|purge|autoremove)
# Debian system # 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(ls /var/lib/apm/apm/files/ace-env/var/lib/apm/ )" "$cur" ) )
COMPREPLY=( $( compgen -W "$(ls /var/lib/apm/apm/files/ace-env/var/lib/apm/ )" $cur ) ) fi
return 0 return 0
;; ;;
show|list|download|changelog|depends|rdepends) show|list|download|changelog|depends|rdepends)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \ # 第一个参数匹配包名
2> /dev/null ) ) if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) )
fi
return 0 return 0
;; ;;
install) install)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \ # 第一个参数匹配包名
2> /dev/null ) ) if [[ $arg_pos -eq 1 ]]; then
if [[ "$cur" == ./* || "$cur" == /* ]]; then COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
_filedir "deb" 2> /dev/null ) )
if [[ "$cur" == ./* || "$cur" == /* ]]; then
_filedir "deb"
fi
fi fi
return 0 return 0
;; ;;
source|build-dep|showsrc|policy) source|build-dep|showsrc|policy)
COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \ # 第一个参数匹配包名
2> /dev/null ) $( apt-cache dumpavail -o Dir::Cache="/var/lib/aptss/" | \ if [[ $arg_pos -eq 1 ]]; then
command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) ) COMPREPLY=( $( amber-pm-debug apt-cache --no-generate pkgnames "$cur" -o Dir::Cache="/var/lib/aptss/" \
2> /dev/null ) $( apt-cache dumpavail -o Dir::Cache="/var/lib/aptss/" | \
command grep "^Source: $cur" | sort -u | cut -f2 -d" " ) )
fi
return 0 return 0
;; ;;
run|sandbox-run|bwrap-run) run|sandbox-run|bwrap-run)
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) ) # 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) )
# 第二个及以后参数匹配文件
elif [[ $arg_pos -ge 2 ]]; then
_filedir
fi
return 0
;;
launch)
# 第一个参数匹配包名
if [[ $arg_pos -eq 1 ]]; then
COMPREPLY=( $( compgen -W "$(apm_run_compgen)" "$cur" ) )
# 第二个及以后参数匹配文件
elif [[ $arg_pos -ge 2 ]]; then
_filedir
fi
return 0
;;
ssaudit)
# ssaudit 命令总是匹配文件
_filedir
return 0 return 0
;; ;;
esac esac
+1 -1
View File
@@ -27,7 +27,7 @@ function ensure_dir() {
return 0 return 0
fi fi
} }
chrootEnvPath="${chrootEnvPath:-$(pwd)/ace-env}" chrootEnvPath="${chrootEnvPath:-$(dirname $0)/ace-env}"
APM_PKG_NAME="${APM_PKG_NAME:-apm-general}" APM_PKG_NAME="${APM_PKG_NAME:-apm-general}"
+4
View File
@@ -112,6 +112,10 @@ chown -R root $chrootEnvPath
mkdir -p $chrootEnvPath/amber-ce-tools/data-dir mkdir -p $chrootEnvPath/amber-ce-tools/data-dir
ln -sv ../../usr/share/applications $chrootEnvPath/amber-ce-tools/data-dir/ ln -sv ../../usr/share/applications $chrootEnvPath/amber-ce-tools/data-dir/
ln -sv ../../usr/share/icons $chrootEnvPath/amber-ce-tools/data-dir/ ln -sv ../../usr/share/icons $chrootEnvPath/amber-ce-tools/data-dir/
mkdir -p $chrootEnvPath/usr/share/templates
ln -sfv ../../usr/share/templates/ $chrootEnvPath/amber-ce-tools/data-dir/
chmod 777 -R $chrootEnvPath/usr/share/icons chmod 777 -R $chrootEnvPath/usr/share/icons
rm -vfr $chrootEnvPath/dev/* rm -vfr $chrootEnvPath/dev/*
true true
@@ -1,81 +1,718 @@
#!/bin/bash #!/bin/bash
# 日志函数
log.warn() { echo -e "[\e[33mWARN\e[0m]: \e[1m$*\e[0m"; } log.warn() { echo -e "[\e[33mWARN\e[0m]: \e[1m$*\e[0m"; }
log.error() { echo -e "[\e[31mERROR\e[0m]: \e[1m$*\e[0m"; } log.error() { echo -e "[\e[31mERROR\e[0m]: \e[1m$*\e[0m"; }
log.info() { echo -e "[\e[96mINFO\e[0m]: \e[1m$*\e[0m"; } log.info() { echo -e "[\e[96mINFO\e[0m]: \e[1m$*\e[0m"; }
log.debug() { echo -e "[\e[32mDEBUG\e[0m]: \e[1m$*\e[0m"; } log.debug() { echo -e "[\e[32mDEBUG\e[0m]: \e[1m$*\e[0m"; }
if [ "$UID" != "0" ];then # 检查权限
log.error "需要以root权限运行 Need to be run as root." if [ "$UID" != "0" ]; then
exit 1 log.error "需要以root权限运行 Need to be run as root."
fi
if [ -z "$1" ];then
log.error "需要把ace-env所在的路径设置为第一个参数"
exit 1
fi
# 1. 获取宿主机 NVIDIA 驱动版本
nvidia_version=$(cat /sys/module/nvidia/version 2>/dev/null)
if [ -z "$nvidia_version" ]; then
# log.warn "无法获取 NVIDIA 驱动版本 Can not determine NVIDIA Driver version"
exit 1 exit 1
fi fi
# 2. 目标目录准备 # 检查参数
ACE_DIR="$1" if [ -z "$1" ]; then
if [[ ! -e "${ACE_DIR}" ]];then log.error "需要把ace-env所在的路径设置为第一个参数"
log.error "未检测到 apm安装,请安装后再试 apm is not detected. Please try again after installation" exit 1
exit 1
fi fi
mkdir -p "$ACE_DIR/usr/lib" "$ACE_DIR/usr/lib32"
# 检查版本是否已存在且匹配 # 辅助函数
if [ -f "$ACE_DIR/current_version" ]; then trim() {
existing_version=$(cat "$ACE_DIR/current_version") local str="$1"
if [ "$existing_version" = "$nvidia_version" ]; then str="${str#"${str%%[![:space:]]*}"}" # 移除前导空格
# log.info "NVIDIA 驱动版本未变化,跳过链接操作 NVIDIA Driver version unchanged, skipping linking." str="${str%"${str##*[![:space:]]}"}" # 移除尾部空格
exit 0 echo "$str"
else }
log.info "检测到 NVIDIA 驱动版本变化: $existing_version -> $nvidia_version"
log.info "NVIDIA Driver version changed: $existing_version -> $nvidia_version" is_regular_or_symlink() {
local path="$1"
if [ -f "$path" ] || [ -L "$path" ]; then
return 0
fi fi
return 1
}
is_directory() {
local path="$1"
if [ -d "$path" ]; then
return 0
fi
return 1
}
is_char_or_block_device() {
local path="$1"
if [ -c "$path" ] || [ -b "$path" ]; then
return 0
fi
return 1
}
read_driver_version() {
# 尝试从/sys目录读取
if [ -f "/sys/module/nvidia/version" ]; then
local version=$(cat "/sys/module/nvidia/version" 2>/dev/null)
version=$(trim "$version")
if [ -n "$version" ]; then
echo "$version"
return 0
fi
fi
# 尝试从/proc目录读取
if [ -f "/proc/driver/nvidia/version" ]; then
local proc_version=$(cat "/proc/driver/nvidia/version" 2>/dev/null)
# 提取版本号 (格式: 535.86.05)
if [[ "$proc_version" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "${BASH_REMATCH[0]}"
return 0
fi
fi
return 1
}
version_from_filename() {
local filename="$1"
local prefix="$2"
if [[ "$filename" != "$prefix"* ]]; then
return 1
fi
local version="${filename#$prefix}"
if [[ -z "$version" ]]; then
return 1
fi
echo "$version"
return 0
}
compare_versions() {
local ver1="$1"
local ver2="$2"
# 分割版本号
IFS='.' read -r -a v1_parts <<< "$ver1"
IFS='.' read -r -a v2_parts <<< "$ver2"
local max_len=$(( ${#v1_parts[@]} > ${#v2_parts[@]} ? ${#v1_parts[@]} : ${#v2_parts[@]} ))
for ((i=0; i<max_len; i++)); do
local v1=${v1_parts[i]:-0}
local v2=${v2_parts[i]:-0}
if (( v1 < v2 )); then
echo "-1"
return
elif (( v1 > v2 )); then
echo "1"
return
fi
done
echo "0"
}
collect_files() {
local search_dirs=("${!1}")
local pattern="$2"
local results=()
for dir in "${search_dirs[@]}"; do
if [ ! -d "$dir" ]; then
continue
fi
# 使用find搜索文件
while IFS= read -r -d '' file; do
if is_regular_or_symlink "$file"; then
results+=("$file")
fi
done < <(find "$dir" -name "$pattern" -type f 2>/dev/null | head -100)
done
# 去重
local unique_results=()
declare -A seen
for file in "${results[@]}"; do
local realpath=$(readlink -f "$file" 2>/dev/null || echo "$file")
if [ -z "${seen[$realpath]}" ]; then
seen["$realpath"]=1
unique_results+=("$realpath")
fi
done
echo "${unique_results[@]}"
}
select_best_versioned_lib() {
local files=("${!1}")
local prefix="$2"
local prefer_version="$3"
local best=""
local best_version=""
for file in "${files[@]}"; do
local filename=$(basename "$file")
# 提取版本号
local version_result=$(version_from_filename "$filename" "$prefix")
if [ -z "$version_result" ]; then
continue
fi
local version="$version_result"
# 如果指定了首选版本,优先匹配
if [ -n "$prefer_version" ] && [ "$version" != "$prefer_version" ]; then
continue
fi
if [ -z "$best" ] || [ "$(compare_versions "$version" "$best_version")" -gt 0 ]; then
best="$file"
best_version="$version"
fi
done
# 如果没有匹配到首选版本,选择最高版本
if [ -z "$best" ] && [ -n "$prefer_version" ]; then
for file in "${files[@]}"; do
local filename=$(basename "$file")
local version_result=$(version_from_filename "$filename" "$prefix")
if [ -z "$version_result" ]; then
continue
fi
local version="$version_result"
if [ -z "$best" ] || [ "$(compare_versions "$version" "$best_version")" -gt 0 ]; then
best="$file"
best_version="$version"
fi
done
fi
echo "$best"
}
detect_driver_info() {
local info=""
# 读取驱动版本
local driver_version=$(read_driver_version)
if [ -z "$driver_version" ]; then
driver_version=""
fi
# 默认库搜索路径
local default_search_paths=(
"/usr/lib64"
"/usr/lib/x86_64-linux-gnu"
"/usr/lib/i386-linux-gnu"
"/usr/lib/aarch64-linux-gnu"
"/usr/lib/x86_64-linux-gnu/nvidia/current"
"/usr/lib/i386-linux-gnu/nvidia/current"
"/usr/lib/aarch64-linux-gnu/nvidia/current"
"/lib64"
"/lib/x86_64-linux-gnu"
"/lib/i386-linux-gnu"
"/lib/aarch64-linux-gnu"
"/lib/x86_64-linux-gnu/nvidia/current"
"/lib/i386-linux-gnu/nvidia/current"
"/lib/aarch64-linux-gnu/nvidia/current"
"/usr/lib"
"/lib"
)
# 搜索libcuda.so
local cuda_files=($(collect_files default_search_paths[@] "libcuda.so.*"))
local nvidia_ml_files=($(collect_files default_search_paths[@] "libnvidia-ml.so.*"))
local selected_lib=""
if [ ${#cuda_files[@]} -gt 0 ]; then
selected_lib=$(select_best_versioned_lib cuda_files[@] "libcuda.so." "$driver_version")
fi
if [ -z "$selected_lib" ] && [ ${#nvidia_ml_files[@]} -gt 0 ]; then
selected_lib=$(select_best_versioned_lib nvidia_ml_files[@] "libnvidia-ml.so." "$driver_version")
fi
local lib_dir=""
if [ -n "$selected_lib" ]; then
lib_dir=$(dirname "$selected_lib")
# 如果还没有驱动版本,从文件名提取
if [ -z "$driver_version" ]; then
local filename=$(basename "$selected_lib")
local cuda_version=$(version_from_filename "$filename" "libcuda.so.")
local ml_version=$(version_from_filename "$filename" "libnvidia-ml.so.")
if [ -n "$cuda_version" ]; then
driver_version="$cuda_version"
elif [ -n "$ml_version" ]; then
driver_version="$ml_version"
fi
fi
fi
# 如果还没找到库目录,使用默认的
if [ -z "$lib_dir" ]; then
for path in "${default_search_paths[@]}"; do
if is_directory "$path"; then
lib_dir="$path"
break
fi
done
fi
echo "$driver_version:$lib_dir"
}
read_elf_soname() {
local file="$1"
# 使用readelf读取SONAME
if command -v readelf >/dev/null 2>&1; then
local soname=$(readelf -d "$file" 2>/dev/null | grep -E "SONAME.*\[.*\]" | sed -E 's/.*\[(.*)\].*/\1/')
if [ -n "$soname" ]; then
echo "$soname"
return 0
fi
fi
# 使用objdump作为备选
if command -v objdump >/dev/null 2>&1; then
local soname=$(objdump -p "$file" 2>/dev/null | grep -E "SONAME" | awk '{print $2}')
if [ -n "$soname" ]; then
echo "$soname"
return 0
fi
fi
return 1
}
is_elf32() {
local file="$1"
if ! [ -f "$file" ]; then
return 1
fi
# 检查文件头部
local header=$(head -c 5 "$file" 2>/dev/null | od -An -t x1 | tr -d ' \n')
# ELF魔法字节: 7f 45 4c 46
if [[ "$header" == 7f454c46* ]]; then
# 检查第5个字节:01表示32位,02表示64位
local class_byte=${header:8:2}
if [ "$class_byte" = "01" ]; then
return 0
fi
fi
return 1
}
ensure_symlink() {
local target="$1"
local link_path="$2"
# 创建父目录
mkdir -p "$(dirname "$link_path")" 2>/dev/null
# 如果链接已存在且正确,跳过
if [ -L "$link_path" ]; then
local current_target=$(readlink -f "$link_path" 2>/dev/null || readlink "$link_path")
if [ "$current_target" = "$target" ]; then
return 0
fi
fi
# 删除现有文件/链接
rm -f "$link_path" 2>/dev/null
# 创建符号链接
ln -sf "$target" "$link_path" 2>/dev/null
return $?
}
# 主函数
main() {
ACE_DIR="$1"
# 检查目标目录
if [[ ! -e "${ACE_DIR}" ]]; then
log.error "ACE_DIR为空,退出。"
exit 1
fi
# 获取驱动信息
local driver_info=$(detect_driver_info)
if [ -z "$driver_info" ]; then
# log.error "无法检测到NVIDIA驱动信息 Cannot detect NVIDIA driver information"
exit
fi
local nvidia_version=$(echo "$driver_info" | cut -d':' -f1)
local lib_dir=$(echo "$driver_info" | cut -d':' -f2)
if [ -z "$nvidia_version" ]; then
# log.error "无法获取NVIDIA驱动版本 Cannot determine NVIDIA driver version"
exit
fi
# 检查版本是否已存在且匹配
if [ -f "$ACE_DIR/amber-ce-tools/nvidia_current_version" ]; then
local existing_version=$(cat "$ACE_DIR/amber-ce-tools/nvidia_current_version")
if [ "$existing_version" = "$nvidia_version" ]; then
# log.info "NVIDIA驱动版本未变化,跳过链接操作 NVIDIA Driver version unchanged, skipping linking."
exit 0
else
log.info "检测到NVIDIA驱动版本变化: $existing_version -> $nvidia_version"
fi
fi
log.info "正在链接NVIDIA驱动库和GLX组件 Linking NVIDIA Driver Libs and GLX components"
# 准备目录结构
mkdir -p "$ACE_DIR/usr/lib" "$ACE_DIR/usr/lib32"
mkdir -p "$ACE_DIR/orig" "$ACE_DIR/orig/32"
mkdir -p "$ACE_DIR/etc"
# 清理旧链接
# find "$ACE_DIR/usr/lib" -type l -name "*.so*" -delete 2>/dev/null
# find "$ACE_DIR/usr/lib32" -type l -name "*.so*" -delete 2>/dev/null
# find "$ACE_DIR/orig" -type l -name "*.so*" -delete 2>/dev/null
# find "$ACE_DIR/orig/32" -type l -name "*.so*" -delete 2>/dev/null
# 默认库搜索路径
local default_search_paths=(
"$lib_dir"
"/usr/lib64"
"/usr/lib/x86_64-linux-gnu"
"/usr/lib/i386-linux-gnu"
"/usr/lib/aarch64-linux-gnu"
"/usr/lib/x86_64-linux-gnu/nvidia/current"
"/usr/lib/i386-linux-gnu/nvidia/current"
"/usr/lib/aarch64-linux-gnu/nvidia/current"
"/lib64"
"/lib/x86_64-linux-gnu"
"/lib/i386-linux-gnu"
"/lib/aarch64-linux-gnu"
"/lib/x86_64-linux-gnu/nvidia/current"
"/lib/i386-linux-gnu/nvidia/current"
"/lib/aarch64-linux-gnu/nvidia/current"
"/usr/lib"
"/lib"
)
# 1. 首先收集核心NVIDIA库
log.debug "收集核心NVIDIA库..."
# 核心库列表
local core_libs=(
"libnvidia-ml.so.*"
"libcuda.so.*"
"libnvidia-ptxjitcompiler.so.*"
"libnvidia-fatbinaryloader.so.*"
"libnvidia-opencl.so.*"
"libnvidia-compiler.so.*"
"libnvidia-encode.so.*"
"libnvidia-opticalflow.so.*"
"libnvcuvid.so.*"
"libnvidia-cfg.so.*"
"libnvidia-allocator.so.*"
"libnvidia-nvvm.so.*"
)
# 2. 收集图形库(包含GLX)
log.debug "收集图形库..."
local graphics_libs=(
"libGLX_nvidia.so.*"
"libEGL_nvidia.so.*"
"libGLESv1_CM_nvidia.so.*"
"libGLESv2_nvidia.so.*"
"libnvidia-glcore.so.*"
"libnvidia-glsi.so.*"
"libnvidia-tls.so.*"
"libnvidia-egl-gbm.so.*"
"libnvidia-egl-wayland.so.*"
"libnvidia-vulkan-producer.so.*"
"libEGL.so*"
"libGL.so*"
"libGLESv1_CM.so*"
"libGLESv2.so*"
"libGLX.so*"
"libGLdispatch.so*"
"libOpenCL.so*"
"libOpenGL.so*"
"libnvidia-api.so*"
"libnvidia-egl-xcb.so*"
"libnvidia-egl-xlib.so*"
)
# 收集所有库文件
local all_libs=()
for pattern in "${core_libs[@]}" "${graphics_libs[@]}"; do
local files=($(collect_files default_search_paths[@] "$pattern"))
all_libs+=("${files[@]}")
done
# 去重
declare -A seen_libs
local unique_libs=()
for lib in "${all_libs[@]}"; do
local realpath=$(readlink -f "$lib" 2>/dev/null || echo "$lib")
if [ -z "${seen_libs[$realpath]}" ]; then
seen_libs["$realpath"]=1
unique_libs+=("$realpath")
fi
done
# 创建链接
local has_32bit=false
local has_64bit=false
local has_glx=false
for lib_path in "${unique_libs[@]}"; do
if [ ! -f "$lib_path" ] && [ ! -L "$lib_path" ]; then
continue
fi
local filename=$(basename "$lib_path")
local is_32bit=false
# 检查是否是32位库
if is_elf32 "$lib_path"; then
is_32bit=true
has_32bit=true
else
has_64bit=true
fi
# 创建原始链接(容器内路径)
local orig_dest_dir="$ACE_DIR/orig"
if [ "$is_32bit" = true ]; then
orig_dest_dir="$ACE_DIR/orig/32"
fi
# 创建主链接
local container_target="/host$lib_path"
local orig_link_path="$orig_dest_dir/$filename"
if ensure_symlink "$container_target" "$orig_link_path"; then
# 检查是否是GLX库
if [[ "$filename" == libGLX_nvidia.so.* ]]; then
has_glx=true
fi
# 创建SONAME链接
local soname=$(read_elf_soname "$lib_path")
if [ -n "$soname" ] && [ "$soname" != "$filename" ]; then
local soname_link_path="$orig_dest_dir/$soname"
ensure_symlink "$container_target" "$soname_link_path"
fi
fi
done
# 3. 收集X.Org模块
log.debug "收集X.Org模块..."
local xorg_paths=(
"$lib_dir/nvidia/xorg"
"$lib_dir/xorg/modules/drivers"
"$lib_dir/xorg/modules/extensions"
"$lib_dir/xorg/modules/updates/drivers"
"$lib_dir/xorg/modules/updates/extensions"
"/usr/lib/xorg/modules/drivers"
"/usr/lib/xorg/modules/extensions"
"/usr/lib/xorg/modules/updates/drivers"
"/usr/lib/xorg/modules/updates/extensions"
"/usr/lib64/xorg/modules/drivers"
"/usr/lib64/xorg/modules/extensions"
"/usr/lib64/xorg/modules/updates/drivers"
"/usr/lib64/xorg/modules/updates/extensions"
)
# 查找X.Org驱动程序
local xorg_driver=""
for xorg_dir in "${xorg_paths[@]}"; do
if [ -f "$xorg_dir/nvidia_drv.so" ]; then
xorg_driver="$xorg_dir/nvidia_drv.so"
break
fi
done
# 查找GLX服务器模块
local glx_server=""
for xorg_dir in "${xorg_paths[@]}"; do
if [ -f "$xorg_dir/libglxserver_nvidia.so.$nvidia_version" ]; then
glx_server="$xorg_dir/libglxserver_nvidia.so.$nvidia_version"
break
fi
done
# 如果没有找到特定版本,尝试通配符
if [ -z "$glx_server" ]; then
for xorg_dir in "${xorg_paths[@]}"; do
local found=$(find "$xorg_dir" -name "libglxserver_nvidia.so.*" -type f 2>/dev/null | head -1)
if [ -n "$found" ]; then
glx_server="$found"
break
fi
done
fi
# 创建X.Org文件链接
if [ -n "$xorg_driver" ]; then
local dest_dir="$ACE_DIR$(dirname "$xorg_driver")"
mkdir -p "$dest_dir"
local container_target="/host$xorg_driver"
ensure_symlink "$container_target" "$dest_dir/$(basename "$xorg_driver")"
fi
if [ -n "$glx_server" ]; then
has_glx=true
local dest_dir="$ACE_DIR$(dirname "$glx_server")"
mkdir -p "$dest_dir"
local container_target="/host$glx_server"
ensure_symlink "$container_target" "$dest_dir/$(basename "$glx_server")"
fi
# 4. 复制配置文件和辅助文件
log.debug "处理配置和辅助文件..."
# Vulkan配置文件
local vulkan_files=(
"/usr/share/vulkan/icd.d/nvidia_icd.json"
"/usr/share/vulkan/icd.d/nvidia_icd.x86_64.json"
"/usr/share/vulkan/icd.d/nvidia_icd.aarch64.json"
"/usr/share/vulkan/implicit_layer.d/nvidia_layers.json"
)
# EGL配置文件
local egl_files=(
"/usr/share/egl/egl_external_platform.d/10_nvidia_wayland.json"
"/usr/share/egl/egl_external_platform.d/15_nvidia_gbm.json"
"/usr/share/egl/egl_external_platform.d/20_nvidia_xcb.json"
)
# GLVND配置文件
local glvnd_files=(
"/usr/share/glvnd/egl_vendor.d/10_nvidia.json"
)
# X11配置文件
local x11_files=(
"/usr/share/X11/xorg.conf.d/10-nvidia.conf"
"/usr/share/X11/xorg.conf.d/nvidia-drm-outputclass.conf"
)
# 处理所有配置文件
for file in "${vulkan_files[@]}" "${egl_files[@]}" "${glvnd_files[@]}" "${x11_files[@]}"; do
if [ -f "$file" ]; then
local dest_dir="$ACE_DIR$(dirname "$file")"
mkdir -p "$dest_dir"
local container_target="/host$file"
ensure_symlink "$container_target" "$dest_dir/$(basename "$file")"
fi
done
# 5. 生成ld.so.conf文件
if [ "$has_64bit" = true ] || [ "$has_32bit" = true ]; then
echo "/opt/extensions/nvidia/orig" > "$ACE_DIR/etc/ld.so.conf"
if [ "$has_32bit" = true ]; then
echo "/opt/extensions/nvidia/orig/32" >> "$ACE_DIR/etc/ld.so.conf"
fi
fi
# 6. 标记版本
echo "$nvidia_version" > "$ACE_DIR/amber-ce-tools/nvidia_current_version"
# 7. 生成环境变量脚本
cat > "$ACE_DIR/nvidia_env.sh" << EOF
#!/bin/bash
# NVIDIA驱动环境变量
export NVIDIA_DRIVER_VERSION="$nvidia_version"
# 库路径
if [ -d "/opt/extensions/nvidia/orig" ]; then
export LD_LIBRARY_PATH="/opt/extensions/nvidia/orig:\${LD_LIBRARY_PATH}"
fi
if [ -d "/opt/extensions/nvidia/orig/32" ]; then
export LD_LIBRARY_PATH="/opt/extensions/nvidia/orig/32:\${LD_LIBRARY_PATH}"
fi fi
log.info "正在链接 NVIDIA 驱动库 Linking NVIDIA Driver Libs" # GLX和EGL配置
if [ "$has_glx" = true ]; then
export __GLX_VENDOR_LIBRARY_NAME="nvidia"
export __NV_PRIME_RENDER_OFFLOAD="1"
fi
# 3. 收集库文件路径 # Vulkan ICD文件
lib_list=$(ldconfig -p | grep -Ei "nvidia|libcuda" | cut -d'>' -f2) if [ -f "/opt/extensions/nvidia/usr/share/vulkan/icd.d/nvidia_icd.json" ]; then
export VK_ICD_FILENAMES="/opt/extensions/nvidia/usr/share/vulkan/icd.d/nvidia_icd.json"
export VK_ADD_DRIVER_FILES="\${VK_ICD_FILENAMES}"
fi
# 4. 复制库文件 # EGL外部平台配置
copied=0 EGL_CONF_DIRS=""
for lib in $lib_list; do for dir in /opt/extensions/nvidia/usr/share/egl/egl_external_platform.d \
resolved=$(readlink -f "$lib") # 解析符号链接 /usr/share/egl/egl_external_platform.d; do
if file "$resolved" | grep -q "32-bit"; then if [ -d "\$dir" ]; then
ln -sf "/host/$resolved" "$ACE_DIR/usr/lib32/$(basename $lib)" EGL_CONF_DIRS="\$dir:\${EGL_CONF_DIRS}"
else
ln -sf "/host/$resolved" "$ACE_DIR/usr/lib/$(basename $lib)"
copied=1
fi fi
done done
if [ -n "\${EGL_CONF_DIRS}" ]; then
export EGL_EXTERNAL_PLATFORM_CONFIG_DIRS="\${EGL_CONF_DIRS%:}"
export __EGL_EXTERNAL_PLATFORM_CONFIG_DIRS="\${EGL_CONF_DIRS%:}"
fi
# 5. 复制辅助文件 # EGL供应商库目录
additional_files=( EGL_VENDOR_DIRS=""
/usr/share/vulkan/icd.d/nvidia_icd.json for dir in /opt/extensions/nvidia/usr/share/glvnd/egl_vendor.d \
/usr/share/egl/egl_external_platform.d/20_nvidia_xcb.json /usr/share/glvnd/egl_vendor.d; do
) if [ -d "\$dir" ]; then
for file in "${additional_files[@]}"; do EGL_VENDOR_DIRS="\$dir:\${EGL_VENDOR_DIRS}"
if [ -f "$file" ]; then
file=$(readlink -f "$file")
dir=$(dirname "$file")
mkdir -p "$ACE_DIR/$dir"
ln -sf "/host/$file" "$ACE_DIR/$dir"
fi fi
done done
if [ -n "\${EGL_VENDOR_DIRS}" ]; then
# 6. 标记版本 export __EGL_VENDOR_LIBRARY_DIRS="\${EGL_VENDOR_DIRS%:}"
if [ $copied -eq 1 ]; then
echo "$nvidia_version" > "$ACE_DIR/current_version"
log.info "NVIDIA 驱动库已成功链接 Nvidia Driver Libs are successfully linked. "
else
log.info "未找到有效 NVIDIA 库文件 No valid NVIDIA Driver Libs found."
fi fi
export NVIDIA_CTK_LIBCUDA_DIR="/opt/extensions/nvidia/orig"
EOF
chmod +x "$ACE_DIR/nvidia_env.sh"
log.info "NVIDIA驱动库和GLX组件已成功链接 Nvidia Driver Libs and GLX components are successfully linked."
log.info "驱动版本: $nvidia_version"
log.info "64位库: $has_64bit, 32位库: $has_32bit, GLX支持: $has_glx"
log.info "环境变量脚本已生成: $ACE_DIR/nvidia_env.sh"
# 创建设备节点信息文件(供容器运行时使用)
cat > "$ACE_DIR/devices.info" << EOF
# NVIDIA设备节点
/dev/nvidiactl
/dev/nvidia-uvm
/dev/nvidia-uvm-tools
/dev/nvidia-modeset
/dev/nvidia[0-9]*
/dev/dri/card*
/dev/dri/renderD*
EOF
exit 0
}
# 执行主函数
main "$1"