Administrator
发布于 2025-06-02 / 16 阅读
0

飞牛 fnOS 设置启动挂载到photos目录

飞牛 fnOS 设置启动挂载到photos目录

由于已经有了trueNas 来作为集中大存储nas.

而我的 all in one 飞牛的系统存储只给了32G存储,用来存储照片有点捉襟见肘.

所以便想着在飞牛中挂载 trueNas 共享出来的盘中.实现飞牛直接将文件写入到trueNas 中.

具体步骤

飞牛挂载

在飞牛中挂载nfs 盘.并且复制挂载盘的挂载路径 /vol02/1000-4-b149fca7

替换系统默认photos

创建启动执行脚本

vim /root/wang_bootlanch.sh

#!/bin/sh

# 配置参数
LOG_FILE="/var/log/truenas_mount/mount_bind.log"     # 自定义日志文件路径
SYSLOG_TAG="mount_bind"                # 系统日志标识
MOUNT_PAIRS="
/vol02/1000-4-b149fca7 /vol1/1000/Photos
/vol02/1000-4-d0860ba1 /vol1/1001/Photos
"
MAX_RETRIES=10
RETRY_DELAY=10

# 日志记录函数
log() {
    local level="$1"
    local message="$2"
    local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
    
    # 记录到自定义日志文件
    echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
    
    # 同时显示在终端
    # echo "[$level] $message"
    
    # 记录到系统日志(根据系统选择合适的方式)
    if command -v logger >/dev/null 2>&1; then
        logger -t "$SYSLOG_TAG" "[$level] $message"
    fi
}

# 创建日志目录(如果不存在)
mkdir -p "$(dirname "$LOG_FILE")" || {
    echo "ERROR: Failed to create log directory" >&2
    exit 1
}

# 初始化日志
log "INFO" "====== Starting mount bind script ======"
log "INFO" "Script PID: $$"

# 挂载函数
mount_with_retry() {
    local src="$1"
    local tgt="$2"
    local retry_count=0

    log "INFO" "Processing mount pair: $src → $tgt"

    # 检查目标目录
    if [ ! -d "$tgt" ]; then
        log "WARN" "Target directory $tgt doesn't exist, creating..."
        if ! mkdir -p "$tgt" 2>/dev/null; then
            log "ERROR" "Failed to create target directory $tgt"
            return 1
        fi
        log "INFO" "Created target directory: $tgt"
    fi

    # 检查是否已挂载
    if mountpoint -q "$tgt"; then
        log "INFO" "$tgt is already mounted (skipping)"
        return 0
    fi

    # 尝试挂载
    while [ $retry_count -lt $MAX_RETRIES ]; do
        if [ ! -d "$src" ]; then
            log "WARN" "Source $src not found (attempt $((retry_count+1))/$MAX_RETRIES)"
        else
            log "INFO" "Attempting mount: $src → $tgt (attempt $((retry_count+1))/$MAX_RETRIES)"
            if mount --bind "$src" "$tgt" 2>/dev/null; then
                log "SUCCESS" "Mounted $src to $tgt"
                
                # 验证挂载是否成功
                if mountpoint -q "$tgt"; then
                    log "INFO" "Mount validation passed: $tgt"
                    return 0
                else
                    log "ERROR" "Mount command succeeded but validation failed for $tgt"
                    umount "$tgt" 2>/dev/null
                fi
            else
                log "ERROR" "Mount command failed: $src → $tgt"
            fi
        fi

        retry_count=$((retry_count+1))
        if [ $retry_count -lt $MAX_RETRIES ]; then
            log "INFO" "Retrying in $RETRY_DELAY seconds..."
            sleep $RETRY_DELAY
        fi
    done

    log "CRITICAL" "Failed to mount after $MAX_RETRIES attempts: $src → $tgt"
    return 1
}

# 主程序
main() {
    echo "$MOUNT_PAIRS" | while read -r src tgt; do
        [ -n "$src" ] || continue
        mount_with_retry "$src" "$tgt"
        local ret=$?
        log "INFO" "Mount operation for $src returned $ret"
    done
}

# 执行主程序并捕获退出状态
main
exit_status=$?
log "INFO" "Script completed with exit status $exit_status"

exit $exit_status

给脚本添加执行权限.

chmod +x /root/wang_bootlanch.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_truenas_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

其他命令

删除挂载

umount /vol1/1000/Photos/remote_photos

目前已知的存在问题

  1. 飞牛在系统重启后可能会改变原始挂载路径,比如 /vol02/1000-4-b149fca7,需要解决