Created
December 7, 2025 07:04
-
-
Save bliotti/a0ac11c478e3fc116bc3df4e840824d7 to your computer and use it in GitHub Desktop.
make-temp-debian-wsl.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ErrorActionPreference = "Stop" | |
| # -------- CONFIG -------- | |
| $distroName = "temp-debian" | |
| $templateTarPath = "C:\wsl-temp\temp-debian-initialized.tar" | |
| $importDir = "C:\wsl-temp\imported-test" | |
| $unixUser = "test" | |
| $unixPassword = "test" # change if you want | |
| # ------------------------ | |
| function Step($title) { | |
| Write-Host "" | |
| Write-Host "==================================================" -ForegroundColor Cyan | |
| Write-Host ">>> $title" -ForegroundColor Cyan | |
| Write-Host "==================================================" -ForegroundColor Cyan | |
| Write-Host "" | |
| } | |
| # Ensure base directories exist | |
| Step "PREPARE DIRECTORIES" | |
| New-Item -ItemType Directory -Path (Split-Path $templateTarPath) -Force | Out-Null | |
| New-Item -ItemType Directory -Path $importDir -Force | Out-Null | |
| Write-Host "Directories ready." | |
| # ================================================== | |
| # 1. CREATE TEMPLATE TARBALL IF MISSING | |
| # ================================================== | |
| if (-not (Test-Path -LiteralPath $templateTarPath)) { | |
| Step "TEMPLATE TARBALL NOT FOUND – CREATING base distro '$distroName'" | |
| # Check if distro already exists | |
| $existing = wsl -l -q 2>$null | Where-Object { $_ -eq $distroName } | |
| if (-not $existing) { | |
| Step "INSTALLING Debian distro as '$distroName' (wsl --install ... --no-launch)" | |
| wsl --install Debian --name $distroName --no-launch | |
| Write-Host "Debian install command issued. (If already installed, WSL may report success or no-op.)" | |
| } | |
| else { | |
| Write-Host "Distro '$distroName' already exists; reusing it to build template." -ForegroundColor Yellow | |
| } | |
| Step "CREATING UNIX USER '$unixUser' INSIDE BASE DISTRO (non-interactive)" | |
| # Single-line bash command to avoid here-string / CRLF issues | |
| $createUserCmd = "id -u ${unixUser} >/dev/null 2>&1 || (echo 'Creating user ${unixUser}...' && useradd -m ${unixUser} && echo '${unixUser}:${unixPassword}' | chpasswd && (usermod -aG sudo ${unixUser} || true))" | |
| wsl -d $distroName -u root -- bash -lc $createUserCmd | |
| Write-Host "User creation step completed." | |
| Step "EXPORTING BASE DISTRO TO TEMPLATE TARBALL" | |
| wsl --export $distroName $templateTarPath | |
| Write-Host "Template exported to: $templateTarPath" | |
| Step "UNREGISTERING BASE DISTRO '$distroName' (we keep only the template tarball)" | |
| wsl --unregister $distroName | |
| Write-Host "Base distro unregistered; template tarball is now your golden image." | |
| } | |
| else { | |
| Step "TEMPLATE TARBALL FOUND – SKIPPING base distro creation" | |
| Write-Host "Using existing template: $templateTarPath" | |
| } | |
| # ================================================== | |
| # 2. CREATE FRESH TEST DISTRO FROM TEMPLATE | |
| # ================================================== | |
| Step "UNREGISTER EXISTING TEST DISTRO '$distroName' (if present)" | |
| wsl --unregister $distroName 2>$null | |
| Write-Host "Any existing '$distroName' removed (if it was present)." | |
| Step "IMPORTING TEST DISTRO FROM TEMPLATE TARBALL" | |
| wsl --import $distroName $importDir $templateTarPath | |
| Write-Host "Test distro '$distroName' imported to $importDir." | |
| Step "SETTING DEFAULT USER TO '$unixUser'" | |
| wsl --manage $distroName --set-default-user $unixUser | |
| Write-Host "Default user set." | |
| # ================================================== | |
| # 3. PROVISION TEST DISTRO (APT + CURL) | |
| # ================================================== | |
| Step "RUNNING BASE PROVISIONING AS ROOT (apt update && apt install -y curl)" | |
| wsl -d $distroName -u root -- bash -lc "apt update && apt install -y curl" | |
| Write-Host "Provisioning complete." | |
| # ================================================== | |
| # 4. FINAL VERIFICATION | |
| # ================================================== | |
| Step "FINAL VERIFICATION (whoami, which curl)" | |
| wsl -d $distroName -- bash -lc "whoami && which curl" | |
| Step "DONE" | |
| Write-Host "Temp Debian distro '$distroName' is ready for use." -ForegroundColor Green | |
| Write-Host "Template tarball: $templateTarPath" | |
| Write-Host "Install directory: $importDir" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment