|
| 1 | +#Requires -Version 5.1 |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + MiOS local build entry point for Windows (Docker Desktop + WSL2). |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + Builds the MiOS OCI image locally using Docker Desktop (WSL2 backend), |
| 8 | + then uses bootc-image-builder to produce a VHDX for Hyper-V import. |
| 9 | +
|
| 10 | +.PARAMETER OutputFormat |
| 11 | + Disk image format to produce: vhdx (default), raw, qcow2, wsl2 |
| 12 | +
|
| 13 | +.PARAMETER Tag |
| 14 | + Local image tag (default: mios:local) |
| 15 | +
|
| 16 | +.PARAMETER SkipBib |
| 17 | + Build the container image only; skip bootc-image-builder disk conversion. |
| 18 | +
|
| 19 | +.EXAMPLE |
| 20 | + .\Build-MiOS.ps1 |
| 21 | + .\Build-MiOS.ps1 -OutputFormat wsl2 |
| 22 | + .\Build-MiOS.ps1 -SkipBib |
| 23 | +#> |
| 24 | +param( |
| 25 | + [ValidateSet('vhdx','raw','qcow2','wsl2')] |
| 26 | + [string]$OutputFormat = 'vhdx', |
| 27 | + [string]$Tag = 'mios:local', |
| 28 | + [switch]$SkipBib |
| 29 | +) |
| 30 | + |
| 31 | +Set-StrictMode -Version Latest |
| 32 | +$ErrorActionPreference = 'Stop' |
| 33 | + |
| 34 | +# ── Colour helpers ───────────────────────────────────────────────────────── |
| 35 | +function Write-Step { param([string]$Msg) Write-Host "==> $Msg" -ForegroundColor Cyan } |
| 36 | +function Write-Ok { param([string]$Msg) Write-Host " ok $Msg" -ForegroundColor Green } |
| 37 | +function Write-Warn { param([string]$Msg) Write-Host "WARN $Msg" -ForegroundColor Yellow } |
| 38 | +function Write-Fail { param([string]$Msg) Write-Host "FAIL $Msg" -ForegroundColor Red; exit 1 } |
| 39 | + |
| 40 | +# ── Preflight ────────────────────────────────────────────────────────────── |
| 41 | +Write-Step "Preflight checks" |
| 42 | + |
| 43 | +if (-not (Get-Command docker -ErrorAction SilentlyContinue)) { |
| 44 | + Write-Fail "docker not found. Install Docker Desktop: https://www.docker.com/products/docker-desktop/" |
| 45 | +} |
| 46 | +$dockerInfo = docker info 2>&1 |
| 47 | +if ($LASTEXITCODE -ne 0) { |
| 48 | + Write-Fail "Docker daemon not running. Start Docker Desktop and try again." |
| 49 | +} |
| 50 | +if ($dockerInfo -notmatch 'WSL') { |
| 51 | + Write-Warn "Docker Desktop does not appear to be using the WSL2 backend. Build may be slower." |
| 52 | +} |
| 53 | + |
| 54 | +if (-not (Test-Path 'Containerfile')) { |
| 55 | + Write-Fail "Containerfile not found. Run this script from the MiOS repo root." |
| 56 | +} |
| 57 | +Write-Ok "Docker Desktop + Containerfile found" |
| 58 | + |
| 59 | +# ── Environment variables ────────────────────────────────────────────────── |
| 60 | +Write-Step "Loading build environment" |
| 61 | + |
| 62 | +# Load from ~/.config/mios/env.toml if present |
| 63 | +$EnvToml = "$HOME\.config\mios\env.toml" |
| 64 | +if (Test-Path $EnvToml) { |
| 65 | + Write-Ok "Reading $EnvToml" |
| 66 | + Get-Content $EnvToml | ForEach-Object { |
| 67 | + if ($_ -match '^\s*(\w+)\s*=\s*"?([^"#]+)"?') { |
| 68 | + $k = $Matches[1]; $v = $Matches[2].Trim() |
| 69 | + if (-not [System.Environment]::GetEnvironmentVariable($k)) { |
| 70 | + [System.Environment]::SetEnvironmentVariable($k, $v) |
| 71 | + Write-Host " $k = $v" |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +# Mandatory secrets — prompt if not set |
| 78 | +if (-not $env:MIOS_USER_PASSWORD_HASH) { |
| 79 | + $pw = Read-Host -Prompt "MIOS_USER_PASSWORD_HASH (openssl passwd -6 <password>)" |
| 80 | + $env:MIOS_USER_PASSWORD_HASH = $pw |
| 81 | +} |
| 82 | +if (-not $env:MIOS_SSH_PUBKEY) { |
| 83 | + $key = Read-Host -Prompt "MIOS_SSH_PUBKEY (your SSH public key, or Enter to skip)" |
| 84 | + if ($key) { $env:MIOS_SSH_PUBKEY = $key } |
| 85 | +} |
| 86 | + |
| 87 | +# ── Build OCI image ──────────────────────────────────────────────────────── |
| 88 | +Write-Step "Building MiOS OCI image ($Tag)" |
| 89 | + |
| 90 | +$BuildArgs = @( |
| 91 | + 'build', |
| 92 | + '--tag', $Tag, |
| 93 | + '--file', 'Containerfile', |
| 94 | + '--build-arg', "MIOS_USER_PASSWORD_HASH=$env:MIOS_USER_PASSWORD_HASH" |
| 95 | +) |
| 96 | +if ($env:MIOS_SSH_PUBKEY) { |
| 97 | + $BuildArgs += '--build-arg', "MIOS_SSH_PUBKEY=$env:MIOS_SSH_PUBKEY" |
| 98 | +} |
| 99 | +$BuildArgs += '.' |
| 100 | + |
| 101 | +docker @BuildArgs |
| 102 | +if ($LASTEXITCODE -ne 0) { Write-Fail "docker build failed (exit $LASTEXITCODE)" } |
| 103 | +Write-Ok "OCI image built: $Tag" |
| 104 | + |
| 105 | +if ($SkipBib) { |
| 106 | + Write-Ok "Done (SkipBib set — skipping disk image conversion)" |
| 107 | + exit 0 |
| 108 | +} |
| 109 | + |
| 110 | +# ── bootc-image-builder ──────────────────────────────────────────────────── |
| 111 | +Write-Step "Converting OCI → $OutputFormat via bootc-image-builder" |
| 112 | + |
| 113 | +$OutputDir = Join-Path (Get-Location) 'output' |
| 114 | +New-Item -ItemType Directory -Force -Path $OutputDir | Out-Null |
| 115 | + |
| 116 | +# Map format to BIB --type value |
| 117 | +$BibType = switch ($OutputFormat) { |
| 118 | + 'vhdx' { 'vhd' } |
| 119 | + 'raw' { 'raw' } |
| 120 | + 'qcow2' { 'qcow2' } |
| 121 | + 'wsl2' { 'wsl2' } |
| 122 | +} |
| 123 | + |
| 124 | +# BIB config — substitute env vars |
| 125 | +$BibConfig = @" |
| 126 | +[[customizations.user]] |
| 127 | +name = "mios" |
| 128 | +password = "$env:MIOS_USER_PASSWORD_HASH" |
| 129 | +$(if ($env:MIOS_SSH_PUBKEY) { 'key = "' + $env:MIOS_SSH_PUBKEY + '"' } else { '' }) |
| 130 | +groups = ["wheel"] |
| 131 | +"@ |
| 132 | +$BibConfigPath = Join-Path $env:TEMP 'mios-bib.toml' |
| 133 | +Set-Content -Path $BibConfigPath -Value $BibConfig -Encoding UTF8 |
| 134 | + |
| 135 | +docker run --rm --privileged ` |
| 136 | + --security-opt label=type:unconfined_t ` |
| 137 | + -v "${OutputDir}:/output" ` |
| 138 | + -v "/var/run/docker.sock:/var/run/docker.sock" ` |
| 139 | + -v "${BibConfigPath}:/config.toml" ` |
| 140 | + "ghcr.io/osbuild/bootc-image-builder:latest" ` |
| 141 | + --type $BibType ` |
| 142 | + --config /config.toml ` |
| 143 | + --local ` |
| 144 | + $Tag |
| 145 | + |
| 146 | +if ($LASTEXITCODE -ne 0) { Write-Fail "bootc-image-builder failed (exit $LASTEXITCODE)" } |
| 147 | + |
| 148 | +# Rename vhd → vhdx |
| 149 | +if ($OutputFormat -eq 'vhdx') { |
| 150 | + $VhdPath = Join-Path $OutputDir 'disk.vhd' |
| 151 | + $VhdxPath = Join-Path $OutputDir 'disk.vhdx' |
| 152 | + if (Test-Path $VhdPath) { |
| 153 | + Move-Item -Force $VhdPath $VhdxPath |
| 154 | + Write-Ok "Disk image: $VhdxPath" |
| 155 | + } |
| 156 | +} else { |
| 157 | + Write-Ok "Disk image: $(Join-Path $OutputDir "disk.$OutputFormat")" |
| 158 | +} |
| 159 | + |
| 160 | +Write-Step "Build complete" |
| 161 | +Write-Host "" |
| 162 | +Write-Host " Image tag : $Tag" |
| 163 | +Write-Host " Output : $OutputDir" |
| 164 | +if ($OutputFormat -eq 'vhdx') { |
| 165 | + Write-Host "" |
| 166 | + Write-Host " Import into Hyper-V:" |
| 167 | + Write-Host " New-VM -Name MiOS -BootDevice VHD -VHDPath '$OutputDir\disk.vhdx' -Generation 2" |
| 168 | +} |
0 commit comments