224 lines
9.3 KiB
Markdown
224 lines
9.3 KiB
Markdown
# 固资履历共享盘挂载(Linux 运维命令)
|
||
|
||
> 适用:Linux 宿主机 + Docker Compose 部署的 hxops-backend。
|
||
> test / prod **各自**按本文在宿主机执行一次(凭据、挂载不会随代码自动同步到另一台机)。
|
||
> 已验证方式:**CIFS 挂共享根 + `mount --bind` 履历子目录**。
|
||
> **不要使用** `subdir=履历`(易报 `mount error(22): Invalid argument`)。
|
||
> 首次挂载建议**不要加 `ro`**(正式环境曾出现误导性报错 `cannot mount ... read-only`)。
|
||
|
||
## 1. 背景
|
||
|
||
| 项 | 值 |
|
||
| --- | --- |
|
||
| Windows 源路径 | `\\192.168.100.130\设备履历记录\履历` |
|
||
| 业务变量 | `FIXED_ASSET_HISTORY_LOCAL_ROOT`(优先于 UNC;Linux 必须配) |
|
||
| test 本地根 | `/mnt/hxops-fixed-asset-history-test` |
|
||
| prod 本地根 | `/mnt/hxops-fixed-asset-history-prod` |
|
||
| 共享根挂载点 | test: `/mnt/hxops-fixed-asset-history-test-share`<br>prod: `/mnt/hxops-fixed-asset-history-prod-share` |
|
||
| 凭据文件(每台宿主机各一份) | `/etc/smbcredentials/hxops-fixed-asset-history` |
|
||
| 部署目录示例 | test: `~/ai/platform/hxops`<br>prod: `/data/ai-platform/hxops` |
|
||
|
||
挂载顺序:
|
||
|
||
1. 在本机创建 SMB 凭据文件(`chmod 600`,属主 root)
|
||
2. `sudo smbclient` 探测共享可达
|
||
3. CIFS 挂载共享根 `设备履历记录` → `*-share`(先不加 `ro`)
|
||
4. `mount --bind` 把其中的 `履历` 挂到 `FIXED_ASSET_HISTORY_LOCAL_ROOT`
|
||
5. 重建 `hxops-backend`,并确认容器内已注入 `FIXED_ASSET_HISTORY_LOCAL_ROOT`
|
||
|
||
---
|
||
|
||
## 2. 公共准备(test / prod 每台宿主机各做一次)
|
||
|
||
```bash
|
||
# 安装工具
|
||
sudo apt update
|
||
sudo apt install -y cifs-utils smbclient
|
||
|
||
# 凭据文件:密码取自本机仓库 .env.shared / .env.<env>
|
||
# grep FIXED_ASSET_HISTORY_SHARE_ .env.shared .env.prod
|
||
sudo mkdir -p /etc/smbcredentials
|
||
sudo tee /etc/smbcredentials/hxops-fixed-asset-history >/dev/null <<'EOF'
|
||
username=SBLL00001
|
||
password=这里填真实密码
|
||
domain=WORKGROUP
|
||
EOF
|
||
sudo chmod 600 /etc/smbcredentials/hxops-fixed-asset-history
|
||
|
||
# 连通性探测(凭据为 root:600,必须加 sudo,否则会 Unable to open credentials file)
|
||
sudo smbclient //192.168.100.130/设备履历记录 \
|
||
-A /etc/smbcredentials/hxops-fixed-asset-history -c "ls"
|
||
```
|
||
|
||
说明:
|
||
|
||
- `uid/gid` 默认 `1001`;不一致时先执行 `id`,替换下文命令中的数值。
|
||
- 若 `vers=3.0` 报 `Invalid argument`,改为 `vers=2.1` 再试。
|
||
- 正式机若从未建过凭据文件,`dmesg` 常见 `STATUS_LOGON_FAILURE` / `cifs_mount failed ... -13`。
|
||
|
||
---
|
||
|
||
## 3. Test 环境命令(已验证)
|
||
|
||
```bash
|
||
# 1) 挂共享根(首次不加 ro)
|
||
sudo mkdir -p /mnt/hxops-fixed-asset-history-test-share
|
||
sudo mount -t cifs //192.168.100.130/设备履历记录 /mnt/hxops-fixed-asset-history-test-share \
|
||
-o credentials=/etc/smbcredentials/hxops-fixed-asset-history,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0755,dir_mode=0755
|
||
|
||
# 2) 确认有「履历」
|
||
ls /mnt/hxops-fixed-asset-history-test-share
|
||
|
||
# 3) bind 履历 -> LOCAL_ROOT
|
||
sudo mkdir -p /mnt/hxops-fixed-asset-history-test
|
||
sudo mount --bind /mnt/hxops-fixed-asset-history-test-share/履历 /mnt/hxops-fixed-asset-history-test
|
||
|
||
# 4) 确认履历文件可见
|
||
ls /mnt/hxops-fixed-asset-history-test | head
|
||
|
||
# 5) 重建后端(必须带 COMPOSE_ENV_BASENAME,否则读不到 LOCAL_ROOT,会回退 UNC 导致 503)
|
||
cd ~/ai/platform/hxops # 按实际部署目录调整
|
||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||
export COMPOSE_ENV_BASENAME=.env.test
|
||
docker compose --env-file .env.test up -d hxops-backend
|
||
docker exec hxops-backend printenv | grep FIXED_ASSET_HISTORY
|
||
docker exec hxops-backend ls -la /mnt/hxops-fixed-asset-history-test | head
|
||
```
|
||
|
||
期望 `printenv` 含:
|
||
|
||
```text
|
||
FIXED_ASSET_HISTORY_LOCAL_ROOT=/mnt/hxops-fixed-asset-history-test
|
||
```
|
||
|
||
### Test 开机自动挂载(可选)
|
||
|
||
```bash
|
||
echo '//192.168.100.130/设备履历记录 /mnt/hxops-fixed-asset-history-test-share cifs credentials=/etc/smbcredentials/hxops-fixed-asset-history,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0755,dir_mode=0755,nofail,_netdev 0 0' | sudo tee -a /etc/fstab
|
||
|
||
echo '/mnt/hxops-fixed-asset-history-test-share/履历 /mnt/hxops-fixed-asset-history-test none bind,nofail 0 0' | sudo tee -a /etc/fstab
|
||
|
||
sudo mount -a
|
||
findmnt /mnt/hxops-fixed-asset-history-test
|
||
```
|
||
|
||
---
|
||
|
||
## 4. Prod 环境命令
|
||
|
||
与 test 相同,仅路径与 compose env 不同。**正式机必须单独再建一份凭据**(不会从测试机自动带过来)。
|
||
|
||
```bash
|
||
# 0) 若尚未建凭据,先完成「§2 公共准备」,并用 sudo smbclient 测通
|
||
|
||
# 1) 挂共享根(首次不加 ro;正式机加 ro 曾报 cannot mount ... read-only)
|
||
sudo mkdir -p /mnt/hxops-fixed-asset-history-prod-share
|
||
sudo mount -t cifs //192.168.100.130/设备履历记录 /mnt/hxops-fixed-asset-history-prod-share \
|
||
-o credentials=/etc/smbcredentials/hxops-fixed-asset-history,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0755,dir_mode=0755
|
||
|
||
# 若仍失败:
|
||
# sudo dmesg | tail -n 40
|
||
# 再试 vers=2.1
|
||
|
||
# 2) 确认有「履历」
|
||
ls /mnt/hxops-fixed-asset-history-prod-share
|
||
|
||
# 3) bind 履历 -> LOCAL_ROOT
|
||
sudo mkdir -p /mnt/hxops-fixed-asset-history-prod
|
||
sudo mount --bind /mnt/hxops-fixed-asset-history-prod-share/履历 /mnt/hxops-fixed-asset-history-prod
|
||
|
||
# 4) 确认履历文件可见
|
||
ls /mnt/hxops-fixed-asset-history-prod | head
|
||
|
||
# 5) 重建后端
|
||
cd /data/ai-platform/hxops # 按实际部署目录调整
|
||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||
export COMPOSE_ENV_BASENAME=.env.prod
|
||
docker compose --env-file .env.prod up -d hxops-backend
|
||
docker exec hxops-backend printenv | grep FIXED_ASSET_HISTORY
|
||
docker exec hxops-backend ls -la /mnt/hxops-fixed-asset-history-prod | head
|
||
```
|
||
|
||
期望 `printenv` 含:
|
||
|
||
```text
|
||
FIXED_ASSET_HISTORY_LOCAL_ROOT=/mnt/hxops-fixed-asset-history-prod
|
||
```
|
||
|
||
### Prod 开机自动挂载(可选)
|
||
|
||
```bash
|
||
echo '//192.168.100.130/设备履历记录 /mnt/hxops-fixed-asset-history-prod-share cifs credentials=/etc/smbcredentials/hxops-fixed-asset-history,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0755,dir_mode=0755,nofail,_netdev 0 0' | sudo tee -a /etc/fstab
|
||
|
||
echo '/mnt/hxops-fixed-asset-history-prod-share/履历 /mnt/hxops-fixed-asset-history-prod none bind,nofail 0 0' | sudo tee -a /etc/fstab
|
||
|
||
sudo mount -a
|
||
findmnt /mnt/hxops-fixed-asset-history-prod
|
||
```
|
||
|
||
---
|
||
|
||
## 5. 卸载(需要时)
|
||
|
||
先卸 bind,再卸共享根:
|
||
|
||
```bash
|
||
# test
|
||
sudo umount /mnt/hxops-fixed-asset-history-test
|
||
sudo umount /mnt/hxops-fixed-asset-history-test-share
|
||
|
||
# prod
|
||
sudo umount /mnt/hxops-fixed-asset-history-prod
|
||
sudo umount /mnt/hxops-fixed-asset-history-prod-share
|
||
```
|
||
|
||
---
|
||
|
||
## 6. 排障
|
||
|
||
| 现象 | 原因 / 处理 |
|
||
| --- | --- |
|
||
| `Unable to open credentials file` / `Failed to set parse authentication file` | 凭据不存在,或用普通用户读 `root:600` 文件。先建文件,探测时用 **`sudo smbclient`** |
|
||
| `STATUS_LOGON_FAILURE` / `cifs_mount failed ... -13`(见 `dmesg`) | 账号密码错误,或凭据文件缺失/读失败。核对 `/etc/smbcredentials/hxops-fixed-asset-history` 与 `.env.*` |
|
||
| `cannot mount ... read-only` | 多为误导文案。去掉 `ro` 再挂,并看 `dmesg \| tail -n 40` |
|
||
| `mount error(22): Invalid argument` | 勿用 `subdir=`;或改 `vers=2.1` / `vers=2.0` |
|
||
| `smbclient` 超时 / Host is down | 正式机到 `192.168.100.130` 网络未放通(需 SMB 445) |
|
||
| `asset history share is unreachable`(API 503) | 宿主机未挂载,或容器未注入 `FIXED_ASSET_HISTORY_LOCAL_ROOT`(回退到 UNC)。按 §3/§4 重建并检查 `printenv` |
|
||
| 容器 `ls` 正常,API 200,但提示「未找到履历文件」 | 共享盘已通,但文件名/路径未命中该资产编号或名称;属检索/归档命名问题,不是挂载问题 |
|
||
| 宿主机有文件、容器内没有 | 挂载后 recreate backend;确认 `docker-compose.yml` 已 bind `LOCAL_ROOT` |
|
||
| 目录存在但为空 | 可能 Docker 先创建了空目录;先正确 CIFS/bind,再 recreate 容器 |
|
||
|
||
### 快速自检清单
|
||
|
||
```bash
|
||
# 凭据
|
||
sudo ls -l /etc/smbcredentials/hxops-fixed-asset-history
|
||
sudo smbclient //192.168.100.130/设备履历记录 -A /etc/smbcredentials/hxops-fixed-asset-history -c "ls"
|
||
|
||
# 挂载
|
||
findmnt /mnt/hxops-fixed-asset-history-prod-share # 或 test-share
|
||
findmnt /mnt/hxops-fixed-asset-history-prod # 或 test
|
||
ls /mnt/hxops-fixed-asset-history-prod | head
|
||
|
||
# 容器
|
||
docker exec hxops-backend printenv | grep FIXED_ASSET_HISTORY
|
||
docker exec hxops-backend ls /mnt/hxops-fixed-asset-history-prod | head
|
||
```
|
||
|
||
### 相关配置
|
||
|
||
- `.env.test` / `.env.prod`:`FIXED_ASSET_HISTORY_LOCAL_ROOT`
|
||
- `.env.shared` / `.env.<env>`:`FIXED_ASSET_HISTORY_SHARE_USERNAME` / `PASSWORD`
|
||
- `docker-compose.yml`:已把 test/prod 两个 `LOCAL_ROOT` 只读挂进 `hxops-backend`,并注入 `FIXED_ASSET_HISTORY_LOCAL_ROOT`
|
||
- 可选脚本:`scripts/mount_fixed_asset_history.sh`(封装同上逻辑;执行用 `sudo bash scripts/...`)
|
||
|
||
---
|
||
|
||
## 7. 验收
|
||
|
||
1. 宿主机:`ls $LOCAL_ROOT` 能看到履历文件
|
||
2. 容器:`docker exec hxops-backend ls $LOCAL_ROOT` 同样可见
|
||
3. 容器:`FIXED_ASSET_HISTORY_LOCAL_ROOT` 已注入且指向上述本地路径(不是 UNC)
|
||
4. 前端:点击资产编号不再出现 503 / `asset history share is unreachable`
|
||
5. 若仍提示「未找到履历文件」:挂载已正常,需核对共享盘是否存在该资产对应文件(编号或设备名)
|