param([string]$BootDrive) # ex: "U:" ou "U:\", sinon détection auto $ErrorActionPreference = 'Stop' function Resolve-BootDrive { param([string]$d) # Normalise la lettre si fournie if ($d) { if ($d.Length -eq 1) { $d = $d + ':' } if ($d[-1] -ne '\') { $d = $d + '\' } if (Test-Path -LiteralPath (Join-Path $d 'cmdline.txt')) { return $d } } # Scan des volumes montés à la recherche de cmdline.txt $cands = Get-PSDrive -PSProvider FileSystem | ForEach-Object { $p = $_.Root; if ($p[-1] -ne '\') { $p = $p + '\' } if (Test-Path -LiteralPath (Join-Path $p 'cmdline.txt')) { $p } } if ($cands -and $cands.Count -gt 0) { return $cands[0] } throw "cmdline.txt introuvable. Branche la SD (partition BOOT) ou recrée cmdline.txt." } $boot = Resolve-BootDrive -d $BootDrive # Normaliser $boot en forme 'X:\' if ($boot) { if ($boot.Length -eq 1) { $boot = $boot + ':\' } elseif ($boot.Length -eq 2 -and $boot[-1] -eq ':') { $boot = $boot + '\' } elseif ($boot[-1] -ne '\') { $boot = $boot + '\' } } Write-Host "" Write-Host "============================================" Write-Host " WAOUW - PREP SD (firstboot, non-interactif)" Write-Host "============================================" Write-Host "" Write-Host "[OK] BOOT = '$boot'" # Sanity check if (-not (Test-Path -LiteralPath (Join-Path $boot 'cmdline.txt'))) { throw "cmdline.txt introuvable sur $boot (verifie la lettre de lecteur)." } # 1) Copier waouw-deploy.zip (cherche dans DEPLOY, racine projet, puis dist) $zipSrc = $null $zipCandidates = @( (Join-Path $PSScriptRoot 'waouw-deploy.zip'), (Join-Path (Split-Path -Parent $PSScriptRoot) 'waouw-deploy.zip'), (Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) 'dist') 'waouw-v2-image-latest.zip') ) foreach ($cand in $zipCandidates) { if (Test-Path -LiteralPath $cand) { $zipSrc = $cand; break } } if ($zipSrc) { $zipDst = Join-Path $boot 'waouw-deploy.zip' Copy-Item -LiteralPath $zipSrc -Destination $zipDst -Force $a = (Get-Item $zipSrc).Length; $b = (Get-Item $zipDst).Length if ($a -ne $b) { throw "Copie ZIP incomplete ($a vs $b)" } Write-Host "[OK] ZIP copied from '$zipSrc' to '$zipDst' ($([int]($b/1KB)) KB)" } else { Write-Host "[WARN] Aucun zip trouve (placez 'waouw-deploy.zip' a la racine ou utilisez dist). Je continue sans copier." } # 2) Déployer les scripts boot-templates sur la partition BOOT $tpl = Join-Path $PSScriptRoot 'boot-templates' foreach ($f in @('firstrun.sh','waouw-activate.sh','waouw-firstboot.service','waouw-install.sh')) { $src = Join-Path $tpl $f if (Test-Path -LiteralPath $src) { Copy-Item -LiteralPath $src -Destination (Join-Path $boot $f) -Force Write-Host "[OK] $f copied" } else { Write-Host "[WARN] $f manquant dans boot-templates" } } # 3) userconf.txt $hash = '$5$VOr2jdaVXo$nZI1v259hiMq0IM0KpPu8pDI/Zu08Yh5/ZQEOrHTP11' Set-Content -NoNewline -Encoding ascii -Path (Join-Path $boot 'userconf.txt') -Value "pi:$hash" Write-Host "[OK] userconf.txt written (pi + hash)" # 4) Normaliser LF/UTF-8 (sans BOM) pour les scripts copiés $filesToNormalize = @('firstrun.sh','waouw-activate.sh','waouw-firstboot.service','waouw-install.sh') | ForEach-Object { Join-Path $boot $_ } foreach ($f in $filesToNormalize) { if (Test-Path -LiteralPath $f) { $c = [System.IO.File]::ReadAllText($f) $c = [regex]::Replace($c,'\r?\n','`n') [System.IO.File]::WriteAllText($f,$c,(New-Object System.Text.UTF8Encoding($false))) } } Write-Host "[OK] Scripts normalises (UTF-8 / LF)" # 5) cmdline.txt : patch ou recrée si absent (firstboot + firstrun) $cmdPath = Join-Path $boot 'cmdline.txt' if (Test-Path -LiteralPath $cmdPath) { $c = Get-Content -Raw -LiteralPath $cmdPath $c = $c -replace 'init=/usr/lib/raspberrypi-sys-mods/firstboot','' -replace 'systemd\.run=[^ ]*','' -replace 'systemd\.run_success_action=[^ ]*','' -replace 'systemd\.unit=[^ ]*','' $c = ([regex]::Replace($c,'\s+',' ')).Trim() + ' init=/usr/lib/raspberrypi-sys-mods/firstboot systemd.run=/bin/sh,/boot/firmware/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target' } else { $c = 'console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 fsck.repair=yes rootwait quiet init=/usr/lib/raspberrypi-sys-mods/firstboot systemd.run=/bin/sh,/boot/firmware/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target' } Set-Content -NoNewline -Encoding ascii -LiteralPath $cmdPath -Value $c Write-Host "[OK] cmdline.txt updated (ou recree)" Write-Host "[DONE] BOOT prepare. Ejecte proprement la SD et boote la Pi."