refactor: auto-mount スクリプトの改善(マウント健全性チェック方式を刷新)
This commit is contained in:
1 parent
8f6ab47363
commit
54881f5d68
2 files changed
+42
-52
No files matched your search
+31
-42
@@ -1,6 +1,7 @@
|
||||
# ==============================================================================
|
||||
# Google Drive (G:) WSL2 Auto-Mount & MusicDownloader Recovery Script
|
||||
# Runs automatically on Windows logon to ensure G: drive is mounted into WSL2.
|
||||
# ==============================================================================
|
||||
# Google Drive (G:) WSL2 Auto-Mount & MusicDownloader Health Check Script
|
||||
# Periodically monitors Google Drive mount status and automatically repairs
|
||||
# broken mounts or stale containers.
|
||||
# ==============================================================================
|
||||
|
||||
$logDir = "C:\feeds\Log"
|
||||
@@ -16,55 +17,43 @@ function Write-Log($msg) {
|
||||
Add-Content -Path $logFile -Value $line -Encoding UTF8
|
||||
}
|
||||
|
||||
Write-Log "=========================================================="
|
||||
Write-Log "Start Google Drive Auto-Mount and MusicDownloader Health Check"
|
||||
Write-Log "=========================================================="
|
||||
|
||||
# 1. Wait for Google Drive (max 180s)
|
||||
# C:\MyMusic is a junction pointing to G:\マイドライブ\MyMusic
|
||||
$maxWaitSeconds = 180
|
||||
$waited = 0
|
||||
|
||||
Write-Log "Waiting for Google Drive (C:\MyMusic\playlists.txt)..."
|
||||
while (!(Test-Path "C:\MyMusic\playlists.txt") -and ($waited -lt $maxWaitSeconds)) {
|
||||
Start-Sleep -Seconds 3
|
||||
$waited += 3
|
||||
# 1. ホスト側 Google Drive (G:) のアクセス健全性確認
|
||||
if (!(Test-Path "G:\マイドライブ\MyMusic") -or !(Test-Path "C:\MyMusic")) {
|
||||
# ホスト側で Google Drive が起動していない場合は何もしない
|
||||
exit 0
|
||||
}
|
||||
|
||||
if (!(Test-Path "C:\MyMusic\playlists.txt")) {
|
||||
Write-Log "[Warning] Google Drive target (C:\MyMusic\playlists.txt) not accessible within $maxWaitSeconds seconds."
|
||||
exit 1
|
||||
# 2. WSL2 (docker-desktop) 上の Google Drive マウント健全性確認
|
||||
$wslCheckCmd = "ls -d /mnt/host/c/MyMusic/Youtube >/dev/null 2>&1"
|
||||
$null = wsl -d docker-desktop sh -c $wslCheckCmd 2>&1
|
||||
$isWslMountBroken = ($LASTEXITCODE -ne 0)
|
||||
|
||||
if ($isWslMountBroken) {
|
||||
Write-Log "[異常検知] WSL2 の Google Drive マウント切断 (Invalid argument 等) を検知しました。マウントを修復します..."
|
||||
|
||||
$remountCmd = "while umount -l /tmp/docker-desktop-root/run/desktop/mnt/host/g 2>/dev/null; do :; done; while umount -l /tmp/docker-desktop-root/mnt/host/g 2>/dev/null; do :; done; while umount -l /mnt/host/g 2>/dev/null; do :; done; mkdir -p /mnt/host/g && mount -t drvfs G: /mnt/host/g && mkdir -p /tmp/docker-desktop-root/run/desktop/mnt/host/g /tmp/docker-desktop-root/mnt/host/g && mount --bind /mnt/host/g /tmp/docker-desktop-root/run/desktop/mnt/host/g && mount --bind /mnt/host/g /tmp/docker-desktop-root/mnt/host/g && (ls -d /mnt/host/c/MyMusic/Youtube >/dev/null 2>&1 && echo 'MOUNT_OK' || echo 'MOUNT_NG')"
|
||||
$remountResult = wsl -d docker-desktop sh -c $remountCmd 2>&1
|
||||
Write-Log "WSL2 再マウント結果: $remountResult"
|
||||
}
|
||||
Write-Log "[OK] Google Drive detected and accessible via junction (waited: ${waited}s)"
|
||||
|
||||
# 2. Update WSL2 mounts
|
||||
Write-Log "Updating WSL2 mounts for G: drive..."
|
||||
$shCmd = "while umount -l /tmp/docker-desktop-root/run/desktop/mnt/host/g 2>/dev/null; do :; done; while umount -l /tmp/docker-desktop-root/mnt/host/g 2>/dev/null; do :; done; while umount -l /mnt/host/g 2>/dev/null; do :; done; mkdir -p /mnt/host/g && mount -t drvfs G: /mnt/host/g && mkdir -p /tmp/docker-desktop-root/run/desktop/mnt/host/g /tmp/docker-desktop-root/mnt/host/g && mount --bind /mnt/host/g /tmp/docker-desktop-root/run/desktop/mnt/host/g && mount --bind /mnt/host/g /tmp/docker-desktop-root/mnt/host/g && (ls -d /mnt/host/c/MyMusic/Youtube >/dev/null 2>&1 && echo 'MOUNT_OK' || echo 'MOUNT_NG')"
|
||||
|
||||
$mountResult = wsl -d docker-desktop sh -c $shCmd
|
||||
Write-Log "WSL2 Mount Result: $mountResult"
|
||||
|
||||
# 3. Check and recover MusicDownloader container
|
||||
# 3. MusicDownloader コンテナの健全性確認および復旧
|
||||
try {
|
||||
$containerStatus = docker inspect MusicDownloader --format "{{.State.Status}}" 2>&1
|
||||
if ($containerStatus -eq "running") {
|
||||
$mountCheck = docker exec MusicDownloader sh -c "mount | grep 'on /music'" 2>&1
|
||||
Write-Log "Container mount status: $mountCheck"
|
||||
# コンテナ内部からのマウントアクセス確認
|
||||
$null = docker exec MusicDownloader ls -d /music/Youtube 2>&1
|
||||
$isContainerMountBroken = ($LASTEXITCODE -ne 0)
|
||||
|
||||
$isReadOnly = ($mountCheck -match '\b(ro)\b') -or ($mountCheck -like "*overlay*") -or ($mountCheck -notlike "*G:*")
|
||||
if ($isReadOnly) {
|
||||
Write-Log "[Notice] Container mount is Read-only or stale. Restarting MusicDownloader..."
|
||||
if ($isWslMountBroken -or $isContainerMountBroken) {
|
||||
Write-Log "[コンテナ修復] コンテナ内のマウント無効化を検知しました。MusicDownloader を再起動します..."
|
||||
docker restart MusicDownloader | Out-Null
|
||||
Write-Log "[Success] MusicDownloader container restarted."
|
||||
} else {
|
||||
Write-Log "[OK] MusicDownloader container is running with healthy mount (rw)."
|
||||
Write-Log "[成功] MusicDownloader コンテナを再起動しました。"
|
||||
}
|
||||
} else {
|
||||
Write-Log "Container status: $containerStatus. Starting container..."
|
||||
docker start MusicDownloader 2>&1 | Out-Null
|
||||
} elseif ($containerStatus -eq "exited" -or $containerStatus -eq "created") {
|
||||
Write-Log "コンテナが停止しています (Status: $containerStatus)。MusicDownloader を起動します..."
|
||||
docker start MusicDownloader | Out-Null
|
||||
Write-Log "[成功] MusicDownloader コンテナを起動しました。"
|
||||
}
|
||||
} catch {
|
||||
Write-Log "[Error] Exception occurred while inspecting container: $_"
|
||||
Write-Log "[エラー] コンテナ確認中に例外が発生しました: $_"
|
||||
}
|
||||
|
||||
Write-Log "Auto-mount process completed successfully."
|
||||
Reference in new issue
Block a user