🧰 OpenWRT 使用 rclone 掛載 OneDrive:完整流程筆記

📦 第一步:安裝 rclone

opkg update
opkg install rclone

✅ 安裝成功後可用 rclone version 檢查版本


🔐 第二步:設定 OneDrive remote

rclone config

設定流程:

  1. n → 新增 remote
  2. Remote 名稱:輸入 onedrive
  3. 選擇雲端類型:輸入 38(OneDrive)
  4. Client ID / Secret:直接按 Enter 跳過
  5. 選擇 OneDrive 類型:輸入 1(Global)
  6. tenant:個人帳號直接按 Enter 跳過
  7. 授權方式:
Use web browser to automatically authenticate rclone with remote?
 * Say Y if the machine running rclone has a web browser you can use
 * Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.

y) Yes (default)
n) No
y/n> n

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
	rclone authorize "onedrive"
Then paste the result.
Enter a value.

 在電腦上執行:
 rclone authorize "onedrive"
    
        
   登入 OneDrive 並複製 token(包含 `{}`)
   回到 OpenWRT 貼上 token
  1. y → 儲存設定
  2. q → 離開設定

✅ 完成後可用 rclone ls onedrive:/ 測試連線


📂 第三步:建立掛載點

mkdir -p /mnt/onedrive

🧲 第四步:手動掛載 OneDrive

rclone mount onedrive:/ /mnt/onedrive \
  --copy-links \
  --allow-other \
  --allow-non-empty \
  --umask 000 \
  --daemon

✅ 用 df -hls /mnt/onedrive 檢查是否掛載成功


🔁 第五步:rclone 開機自動掛載完整步驟

本筆記將詳細記錄如何讓 rclone 服務在 OpenWrt 開機完成後 自動掛載雲端硬碟 (以 OneDrive 為例)。

最終診斷:為什麼之前失敗?

  • 問題: OpenWrt 的網路服務需要時間啟動。傳統的 /etc/init.d/ 服務腳本在網路就緒前就開始運行 rclone mount,導致連線失敗。

  • 解決方案: 使用 /etc/rc.local (開機結束後才執行的腳本),並在命令中加入 延遲 (sleep)背景執行 (&),確保 rclone 在網路穩定後才開始嘗試連線。


步驟 1:清理舊的啟動服務 (避免衝突)

如果您曾嘗試使用 /etc/init.d/rcloned 腳本,請先將其移除,以防衝突:

# 1. 禁用服務
/etc/init.d/rcloned disable

# 2. 刪除服務腳本和啟動連結
rm -f /etc/init.d/rcloned
rm -f /etc/rc.d/S99rcloned
rm -f /etc/rc.d/K10rcloned

步驟 3:修改 /etc/rc.local 腳本 (核心步驟)

使用 nanovi 編輯 /etc/rc.local 檔案。您需要將以下程式碼塊放在 exit 0 這一行之前。

root@OpenWrt:~# nano /etc/rc.local

✍️ 貼上以下內容(放在 exit 0 前):

#!/bin/sh
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

# --- rclone mount 邏輯 (開機完成後執行) ---

(
    # 確保 rclone 相關的檔案是新的
    rm -f /tmp/rclone_rc_local.log
    rm -f /tmp/rclone_mount.log
    echo "Starting rclone mount via rc.local at $(date)" > /tmp/rclone_rc_local.log
    
    # 關鍵:延遲 15 秒,確保網路完全穩定
    sleep 15
    
    MOUNTPOINT="/mnt/onedrive"
    CONFIG_PATH="/root/.config/rclone/rclone.conf" 
    REMOTE="onedrive:/"
    RCLONE_LOG="/tmp/rclone_mount.log" 
    
    # 建立掛載點 (若不存在)
    [ -d "$MOUNTPOINT" ] || mkdir -p "$MOUNTPOINT"
    
    # 執行掛載命令
    /usr/bin/rclone mount "$REMOTE" "$MOUNTPOINT" \
      --config "$CONFIG_PATH" \
      --copy-links \
      --allow-other \
      --allow-non-empty \
      --umask 000 \
      --log-file "$RCLONE_LOG" \
      --log-level INFO \
      --retries 5 \
      --low-level-retries 10 \
      --stats 1m \
      --daemon

    if [ $? -eq 0 ]; then
        echo "rclone mount command initiated successfully." >> /tmp/rclone_rc_local.log
    else
        echo "rclone mount command failed to initiate." >> /tmp/rclone_rc_local.log
    fi
) &  # <-- 關鍵:使用 ( ... ) & 確保整個區塊在背景執行

# --- 結束 rclone 邏輯 ---

exit 0

步驟 4:最終測試

儲存並退出 /etc/rc.local 檔案後,執行重啟命令:

Bash

root@OpenWrt:~# reboot

登入 OpenWrt 後,請等待約 30 秒 (給予腳本執行和 rclone 啟動緩衝時間),然後執行以下命令確認:

root@OpenWrt:~# df -h

✅ 預期結果:

您應該會在輸出列表中看到 onedrive: 掛載點,證明自動啟動已成功!

onedrive:                       5.0G      8.0K      5.0G    0% /mnt/onedrive