Files
MusicDownloader/auto-mount-gdrive.ps1
T

71 lines
3.3 KiB
PowerShell

# ==============================================================================
# Google Drive (G:) WSL2 Auto-Mount & MusicDownloader Recovery Script
# Runs automatically on Windows logon to ensure G: drive is mounted into WSL2.
# ==============================================================================
$logDir = "C:\feeds\Log"
if (!(Test-Path $logDir)) {
New-Item -ItemType Directory -Force -Path $logDir | Out-Null
}
$logFile = Join-Path $logDir "auto-mount-gdrive.log"
function Write-Log($msg) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$line = "[$timestamp] $msg"
Write-Output $line
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
}
if (!(Test-Path "C:\MyMusic\playlists.txt")) {
Write-Log "[Warning] Google Drive target (C:\MyMusic\playlists.txt) not accessible within $maxWaitSeconds seconds."
exit 1
}
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
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"
$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..."
docker restart MusicDownloader | Out-Null
Write-Log "[Success] MusicDownloader container restarted."
} else {
Write-Log "[OK] MusicDownloader container is running with healthy mount (rw)."
}
} else {
Write-Log "Container status: $containerStatus. Starting container..."
docker start MusicDownloader 2>&1 | Out-Null
}
} catch {
Write-Log "[Error] Exception occurred while inspecting container: $_"
}
Write-Log "Auto-mount process completed successfully."