1
0

prep-sd-auto.ps1 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. param([string]$BootDrive) # ex: "U:" ou "U:\", sinon détection auto
  2. $ErrorActionPreference = 'Stop'
  3. function Resolve-BootDrive {
  4. param([string]$d)
  5. # Normalise la lettre si fournie
  6. if ($d) {
  7. if ($d.Length -eq 1) { $d = $d + ':' }
  8. if ($d[-1] -ne '\') { $d = $d + '\' }
  9. if (Test-Path -LiteralPath (Join-Path $d 'cmdline.txt')) { return $d }
  10. }
  11. # Scan des volumes montés à la recherche de cmdline.txt
  12. $cands = Get-PSDrive -PSProvider FileSystem | ForEach-Object {
  13. $p = $_.Root; if ($p[-1] -ne '\') { $p = $p + '\' }
  14. if (Test-Path -LiteralPath (Join-Path $p 'cmdline.txt')) { $p }
  15. }
  16. if ($cands -and $cands.Count -gt 0) { return $cands[0] }
  17. throw "cmdline.txt introuvable. Branche la SD (partition BOOT) ou recrée cmdline.txt."
  18. }
  19. $boot = Resolve-BootDrive -d $BootDrive
  20. # Normaliser $boot en forme 'X:\'
  21. if ($boot) {
  22. if ($boot.Length -eq 1) { $boot = $boot + ':\' }
  23. elseif ($boot.Length -eq 2 -and $boot[-1] -eq ':') { $boot = $boot + '\' }
  24. elseif ($boot[-1] -ne '\') { $boot = $boot + '\' }
  25. }
  26. Write-Host ""
  27. Write-Host "============================================"
  28. Write-Host " WAOUW - PREP SD (firstboot, non-interactif)"
  29. Write-Host "============================================"
  30. Write-Host ""
  31. Write-Host "[OK] BOOT = '$boot'"
  32. # Sanity check
  33. if (-not (Test-Path -LiteralPath (Join-Path $boot 'cmdline.txt'))) {
  34. throw "cmdline.txt introuvable sur $boot (verifie la lettre de lecteur)."
  35. }
  36. # 1) Copier waouw-deploy.zip (cherche dans DEPLOY, racine projet, puis dist)
  37. $zipSrc = $null
  38. $zipCandidates = @(
  39. (Join-Path $PSScriptRoot 'waouw-deploy.zip'),
  40. (Join-Path (Split-Path -Parent $PSScriptRoot) 'waouw-deploy.zip'),
  41. (Join-Path (Join-Path (Split-Path -Parent $PSScriptRoot) 'dist') 'waouw-v2-image-latest.zip')
  42. )
  43. foreach ($cand in $zipCandidates) {
  44. if (Test-Path -LiteralPath $cand) { $zipSrc = $cand; break }
  45. }
  46. if ($zipSrc) {
  47. $zipDst = Join-Path $boot 'waouw-deploy.zip'
  48. Copy-Item -LiteralPath $zipSrc -Destination $zipDst -Force
  49. $a = (Get-Item $zipSrc).Length; $b = (Get-Item $zipDst).Length
  50. if ($a -ne $b) { throw "Copie ZIP incomplete ($a vs $b)" }
  51. Write-Host "[OK] ZIP copied from '$zipSrc' to '$zipDst' ($([int]($b/1KB)) KB)"
  52. } else {
  53. Write-Host "[WARN] Aucun zip trouve (placez 'waouw-deploy.zip' a la racine ou utilisez dist). Je continue sans copier."
  54. }
  55. # 2) Déployer les scripts boot-templates sur la partition BOOT
  56. $tpl = Join-Path $PSScriptRoot 'boot-templates'
  57. foreach ($f in @('firstrun.sh','waouw-activate.sh','waouw-firstboot.service','waouw-install.sh')) {
  58. $src = Join-Path $tpl $f
  59. if (Test-Path -LiteralPath $src) {
  60. Copy-Item -LiteralPath $src -Destination (Join-Path $boot $f) -Force
  61. Write-Host "[OK] $f copied"
  62. } else {
  63. Write-Host "[WARN] $f manquant dans boot-templates"
  64. }
  65. }
  66. # 3) userconf.txt
  67. $hash = '$5$VOr2jdaVXo$nZI1v259hiMq0IM0KpPu8pDI/Zu08Yh5/ZQEOrHTP11'
  68. Set-Content -NoNewline -Encoding ascii -Path (Join-Path $boot 'userconf.txt') -Value "pi:$hash"
  69. Write-Host "[OK] userconf.txt written (pi + hash)"
  70. # 4) Normaliser LF/UTF-8 (sans BOM) pour les scripts copiés
  71. $filesToNormalize = @('firstrun.sh','waouw-activate.sh','waouw-firstboot.service','waouw-install.sh') | ForEach-Object { Join-Path $boot $_ }
  72. foreach ($f in $filesToNormalize) {
  73. if (Test-Path -LiteralPath $f) {
  74. $c = [System.IO.File]::ReadAllText($f)
  75. $c = [regex]::Replace($c,'\r?\n','`n')
  76. [System.IO.File]::WriteAllText($f,$c,(New-Object System.Text.UTF8Encoding($false)))
  77. }
  78. }
  79. Write-Host "[OK] Scripts normalises (UTF-8 / LF)"
  80. # 5) cmdline.txt : patch ou recrée si absent (firstboot + firstrun)
  81. $cmdPath = Join-Path $boot 'cmdline.txt'
  82. if (Test-Path -LiteralPath $cmdPath) {
  83. $c = Get-Content -Raw -LiteralPath $cmdPath
  84. $c = $c -replace 'init=/usr/lib/raspberrypi-sys-mods/firstboot','' -replace 'systemd\.run=[^ ]*','' -replace 'systemd\.run_success_action=[^ ]*','' -replace 'systemd\.unit=[^ ]*',''
  85. $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'
  86. } else {
  87. $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'
  88. }
  89. Set-Content -NoNewline -Encoding ascii -LiteralPath $cmdPath -Value $c
  90. Write-Host "[OK] cmdline.txt updated (ou recree)"
  91. Write-Host "[DONE] BOOT prepare. Ejecte proprement la SD et boote la Pi."