# Podman auto-start on Windows boot # Runs as {{ windows_containers_service_user }} (set via NSSM ObjectName) $ErrorActionPreference = "Stop" $LogDir = "C:\Logs\Podman" $LogTimestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $MachineName = "{{ windows_containers_machine_name }}" function Write-Log { param([string]$LogName, [System.Collections.ArrayList]$Messages) $Log = Join-Path $LogDir "$LogName-$LogTimestamp.log" $lines = @() foreach ($m in $Messages) { $lines += "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $m" } [System.IO.File]::WriteAllText($Log, ($lines -join "`r`n") + "`r`n", [System.Text.Encoding]::UTF8) } try { $bootMessages = [System.Collections.ArrayList]::new() $bootMessages.Add("Starting Podman auto-start process") > $null # Check if machine is already running $listOutput = & "{{ windows_containers_install_dir }}\podman.exe" @('machine', 'list', '--format', 'table {{ '{{' }}.Name{{ '}}' }}\t{{ '{{' }}.Running{{ '}}' }}') 2>&1 | Out-String $listExit = $LASTEXITCODE $bootMessages.Add("Machine list exit code: $listExit") > $null $bootMessages.Add("Machine list output: $listOutput") > $null $running = ($listOutput -match "$MachineName\s+true") if ($running) { $bootMessages.Add("Machine is already running") > $null } else { $startOutput = & "{{ windows_containers_install_dir }}\podman.exe" @('machine', 'start', $MachineName) 2>&1 | Out-String $bootMessages.Add("Machine start output: $startOutput") > $null if ($LASTEXITCODE -ne 0) { Write-Log "auto-start" $bootMessages exit 1 } } # Wait for the machine to be ready (poll podman info) $bootMessages.Add("Waiting for machine to be ready...") > $null $ready = $false for ($i = 0; $i -lt 90; $i++) { $infoOutput = & "{{ windows_containers_install_dir }}\podman.exe" @('info', '--format', 'json') 2>&1 | Out-String if ($LASTEXITCODE -eq 0 -and $infoOutput -and $infoOutput.Trim()) { $ready = true $bootMessages.Add("Machine is ready after $(($i + 1) * 2) seconds") > $null break } Start-Sleep -Seconds 2 } if (-not $ready) { Write-Log "auto-start" $bootMessages exit 1 } # Start any stopped containers $startContainersOutput = & "{{ windows_containers_install_dir }}\podman.exe" @('start', '-a') 2>&1 | Out-String $bootMessages.Add("Container start output: $startContainersOutput") > $null $bootMessages.Add("Podman auto-start complete") > $null Write-Log "auto-start" $bootMessages } catch { $errMessages = [System.Collections.ArrayList]::new() $errMessages.Add("Podman auto-start failed: $_") > $null $errMessages.Add("Stack trace: $($_.ScriptStackTrace)") > $null Write-Log "auto-start-ERROR" $errMessages exit 1 }