创建脚本和配置文件
创建挂载文件
/root/mount_disk.sh
#!/bin/bash
# 简单版:按 comment -> targetPath 映射 bind mount
# 配置方式 l_fn|/vol1/1001/Photos
MOUNT_INFO="/etc/mountmgr/mount_info.json"
MAP_FILE="/root/mount_map.txt"
if [ ! -f "$MOUNT_INFO" ]; then
echo "找不到 $MOUNT_INFO"
exit 1
fi
if [ ! -f "$MAP_FILE" ]; then
echo "找不到映射文件 $MAP_FILE"
exit 1
fi
# 遍历映射文件
while IFS='|' read comment target; do
# 去掉前后空格
comment=$(echo "$comment" | tr -d '[:space:]')
target=$(echo "$target" | tr -d '[:space:]')
# 跳过空行和注释
[ -z "$comment" ] && continue
case "$comment" in
\#*) continue ;;
esac
# 从 JSON 找到 mountPoint
mount_point=$(jq -r \
--arg c "$comment" \
'to_entries[] | .value | to_entries[] | select(.value.comment == $c) | .value.mountPoint' \
"$MOUNT_INFO")
if [ -z "$mount_point" ] || [ "$mount_point" = "null" ]; then
echo "⚠️ 未找到 comment=$comment 的 mountPoint"
continue
fi
# 如果已挂载则跳过
if mountpoint -q "$target" 2>/dev/null; then
echo "⏩ 已挂载,跳过: $target"
continue
fi
# 创建目标目录
mkdir -p "$target"
# 执行 bind 挂载
echo "🔗 挂载: $mount_point -> $target"
if ! mount --bind "$mount_point" "$target"; then
echo "❌ 挂载失败: $target"
fi
done < "$MAP_FILE"
创建挂载配置文件
/root/mount_map.txt
remote_dev_data|/vol1/1000/mnt_dev_data
配置用 | 分割, 前面配置的是在飞牛挂载远程目录的 挂载名 ,后面配置的是想要替换挂载的本地路径.
多个挂载就配置多行.
添加权限
chmod +x /root/mount_disk.sh
创建开机启动服务
vim /etc/systemd/system/mnt_truenas.service
[Unit]
Description=mnt_truenas mount service
After=network-online.target
Requires=network-online.target
[Service]
Type=simple
User=root
ExecStartPre=/bin/sleep 10
ExecStart=/root/mount_disk.sh
Restart=on-failure # 关键修改
RestartSec=30 # 失败后等待30秒重启
TimeoutStopSec=10 # 停止超时设置
[Install]
WantedBy=multi-user.target
更新systemd目录
systemctl daemon-reload
创建开机启动快捷方式
systemctl enable mnt_truenas.service
启动服务
systemctl start mnt_truenas.service
查看服务状态
systemctl status mnt_truenas.service