kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

bootstrap.ps1.in (3602B)


      1 # kit guest bootstrap, run once by FirstLogonCommands from the seed media.
      2 # Brings up networking and an SSH server with no Windows Update dependency:
      3 #   1. Install the virtio NetKVM network driver from the attached virtio-win CD.
      4 #   2. Install OpenSSH server from the OpenSSH-ARM64.zip bundled on the seed CD.
      5 #   3. Authorize the host's SSH key and open the firewall.
      6 # The AT-sign placeholders below are substituted at seed-build time.
      7 $ErrorActionPreference = 'Continue'
      8 Start-Transcript -Path C:\kit-bootstrap.log -Append | Out-Null
      9 
     10 function Find-OnAnyDrive([string]$rel) {
     11   foreach ($v in (Get-PSDrive -PSProvider FileSystem -ErrorAction SilentlyContinue)) {
     12     $p = Join-Path $v.Root $rel
     13     if (Test-Path -LiteralPath $p) { return (Get-Item -LiteralPath $p).FullName }
     14   }
     15   return $null
     16 }
     17 
     18 try {
     19   # --- 1. virtio network driver (NetKVM) ---------------------------------
     20   $netinf = Find-OnAnyDrive 'NetKVM\w11\ARM64\netkvm.inf'
     21   if ($netinf) {
     22     Write-Output "kit: installing NetKVM driver from $netinf"
     23     & pnputil.exe /add-driver $netinf /install
     24   } else {
     25     Write-Output 'kit: WARNING NetKVM driver not found on any drive'
     26   }
     27 
     28   # --- 2. OpenSSH server (offline, from the seed CD) ---------------------
     29   $zip = Join-Path $PSScriptRoot 'OpenSSH-ARM64.zip'
     30   $dest = 'C:\Program Files\OpenSSH'
     31   if (Test-Path -LiteralPath $zip) {
     32     Write-Output "kit: installing OpenSSH from $zip"
     33     if (Test-Path $dest) { Remove-Item $dest -Recurse -Force }
     34     Expand-Archive -LiteralPath $zip -DestinationPath 'C:\Program Files' -Force
     35     if (Test-Path 'C:\Program Files\OpenSSH-ARM64') {
     36       Rename-Item 'C:\Program Files\OpenSSH-ARM64' $dest
     37     }
     38     & powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $dest 'install-sshd.ps1')
     39   } else {
     40     Write-Output 'kit: OpenSSH zip not found; trying Windows capability'
     41     Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 -ErrorAction SilentlyContinue
     42   }
     43 
     44   Set-Service -Name sshd -StartupType Automatic
     45   New-NetFirewallRule -Name 'kit-sshd' -DisplayName 'kit OpenSSH Server' `
     46     -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 `
     47     -ErrorAction SilentlyContinue | Out-Null
     48 
     49   # --- 3. Authorize the host key ----------------------------------------
     50   $pub = @'
     51 @SSH_PUBKEY@
     52 '@
     53   $pub = $pub.Trim()
     54   $sshProgramData = 'C:\ProgramData\ssh'
     55   New-Item -ItemType Directory -Force -Path $sshProgramData | Out-Null
     56   # sshd treats members of the Administrators group specially: it reads
     57   # administrators_authorized_keys (not the per-user file) and requires the
     58   # file be writable only by Administrators/SYSTEM.
     59   $ak = Join-Path $sshProgramData 'administrators_authorized_keys'
     60   # Write with a trailing newline (no -NoNewline) so a later append can't
     61   # concatenate onto this key's line.
     62   Set-Content -LiteralPath $ak -Value $pub -Encoding ascii
     63   icacls $ak /inheritance:r /grant 'Administrators:F' /grant 'SYSTEM:F' | Out-Null
     64 
     65   $userSsh = 'C:\Users\@KIT_USER@\.ssh'
     66   New-Item -ItemType Directory -Force -Path $userSsh | Out-Null
     67   Set-Content -LiteralPath (Join-Path $userSsh 'authorized_keys') -Value $pub -Encoding ascii
     68 
     69   Start-Service sshd -ErrorAction SilentlyContinue
     70   Restart-Service sshd -ErrorAction SilentlyContinue
     71 
     72   Set-Content -LiteralPath C:\kit-ready.txt `
     73     -Value ('kit-ready ' + (Get-Date -Format o)) -Encoding ascii
     74   Write-Output 'kit: bootstrap complete'
     75 } catch {
     76   Write-Output ('kit: bootstrap ERROR ' + ($_ | Out-String))
     77   $_ | Out-String | Set-Content -LiteralPath C:\kit-bootstrap-error.log
     78 } finally {
     79   Stop-Transcript | Out-Null
     80 }