| 1 | $logfile = "C:\Users\{{.User}}\lima-setup.log" |
| 2 | $globalStart = Get-Date |
| 3 | |
| 4 | # Record logs |
| 5 | Start-Transcript -Path $logfile -Append |
| 6 | |
| 7 | Write-Output "Lima Setup Started at $globalStart" |
| 8 | |
| 9 | $sectionStart = Get-Date |
| 10 | # We need to change password because the current password is specified in autounattend.xml, so all users/processes can see it. |
| 11 | # Generate a random 16 character password. |
| 12 | # Avoid special characters to minimize potential keyboard layout issue. |
| 13 | $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' |
| 14 | $newPassword = -join ((1..16) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] }) |
| 15 | |
| 16 | # Store the password under the user directory so that user can know/change it. |
| 17 | # Use ASCII encoding to avoid the UTF-8 BOM that Windows PowerShell's `Out-File -Encoding utf8` |
| 18 | # prepends; the generated password only contains ASCII characters. |
| 19 | $newPassword | Out-File -FilePath "C:\Users\{{.User}}\password.txt" -Encoding ascii -NoNewline |
| 20 | |
| 21 | # Change the password |
| 22 | $username = $env:USERNAME |
| 23 | $newSecurePassword = ConvertTo-SecureString $newPassword -AsPlainText -Force |
| 24 | Set-LocalUser -Name $username -Password $newSecurePassword |
| 25 | |
| 26 | $elapsed = (Get-Date) - $sectionStart |
| 27 | Write-Output "Password change completed in $($elapsed.TotalSeconds) seconds" |
| 28 | |
| 29 | |
| 30 | $sectionStart = Get-Date |
| 31 | # Install OpenSSH via MSI installer |
| 32 | # Avoid using the OpenSSH.Server included in the OS because installing OpenSSH takes much longer than Win32-OpenSSH. |
| 33 | $installer = "C:\Users\{{.User}}\openssh.msi" |
| 34 | {{ $openSSHInstaller := "https://github.com/PowerShell/Win32-OpenSSH/releases/download/10.0.0.0p2-Preview/OpenSSH-Win64-v10.0.0.0.msi" -}} |
| 35 | {{ $openSSHInstallerSHA256 := "ddec9c53864280759cf9f74791cefd387100e3946aa849a1c138a4ed1b96b7d9" -}} |
| 36 | {{ if eq .Arch "aarch64" -}} |
| 37 | {{ $openSSHInstaller = "https://github.com/PowerShell/Win32-OpenSSH/releases/download/10.0.0.0p2-Preview/OpenSSH-ARM64-v10.0.0.0.msi" -}} |
| 38 | {{ $openSSHInstallerSHA256 = "7a17d0e22d004fb47ca4bfd8fef926fa305de4ebf70a6f3c7a29c39aabef0023" -}} |
| 39 | {{ end -}} |
| 40 | Invoke-WebRequest -Uri "{{ $openSSHInstaller }}" -OutFile $installer |
| 41 | |
| 42 | # Expected hash comes from https://github.com/powershell/win32-openssh/releases |
| 43 | $expectedHash = "{{ $openSSHInstallerSHA256 }}".ToLower() |
| 44 | $actualHash = (Get-FileHash -Path $installer -Algorithm SHA256).Hash.ToLower() |
| 45 | |
| 46 | # Verify the integrity of the downloaded installer before executing it. |
| 47 | if ($actualHash -eq $expectedHash) { |
| 48 | Write-Output "OpenSSH SHA256 verification succeeded" |
| 49 | }else{ |
| 50 | Write-Output "OpenSSH SHA256 verification failed. Expected: ${expectedHash}, Actual: ${actualHash}" |
| 51 | Stop-Transcript |
| 52 | exit 1 |
| 53 | } |
| 54 | |
| 55 | msiexec /i $installer ADDLOCAL=Server |
| 56 | [Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path",[System.EnvironmentVariableTarget]::Machine) + ';' + ${Env:ProgramFiles} + '\OpenSSH', [System.EnvironmentVariableTarget]::Machine) |
| 57 | Get-Service -Name ssh* |
| 58 | |
| 59 | $elapsed = (Get-Date) - $sectionStart |
| 60 | Write-Output "OpenSSH server installation completed in $($elapsed.TotalSeconds) seconds" |
| 61 | |
| 62 | $sectionStart = Get-Date |
| 63 | # Modify firewall rule |
| 64 | # Note that Windows server may have a firewall rule for SSH by default, but it doesn't work on my env. |
| 65 | # So I remove and recreate the rule. |
| 66 | Remove-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction Ignore |
| 67 | New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 |
| 68 | |
| 69 | # Set a public key. Since a user `lima` is in Administrators group, |
| 70 | # The public key should be located under C:\ProgramData\ssh instead of under C:\Users\lima\.ssh. |
| 71 | $pubkey = Get-Content -Path F:\ssh_authorized_keys |
| 72 | $pubkeyLocation = 'C:\ProgramData\ssh\administrators_authorized_keys' |
| 73 | Add-Content -Force -Path $pubkeyLocation -Value $pubkey |
| 74 | icacls $pubkeyLocation /inheritance:r |
| 75 | icacls $pubkeyLocation /grant "SYSTEM:F" |
| 76 | icacls $pubkeyLocation /grant "Administrators:F" |
| 77 | |
| 78 | $elapsed = (Get-Date) - $sectionStart |
| 79 | Write-Output "SSH setting completed in $($elapsed.TotalSeconds) seconds" |
| 80 | |
| 81 | # Install provision runner and set task scheduler. |
| 82 | $root = Split-Path -Path $PSScriptRoot -Qualifier |
| 83 | powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$root\install_provision.ps1" -Root "$root\" |
| 84 | |
| 85 | # Execute provision scripts here for the current boot. |
| 86 | $runnerPath = "C:\ProgramData\Lima\provision\startup_runner.ps1" |
| 87 | |
| 88 | # Dependency scripts are only run here, not scheduled. |
| 89 | powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "dependency" |
| 90 | # Startup System scripts are run here, and run in startup of a next boot. |
| 91 | powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "system" |
| 92 | # Startup User scripts are run here, and run in startup of a next boot. |
| 93 | powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$runnerPath" -Mode "user" |
| 94 | |
| 95 | # Finally, it creates a marker file. Host agent will check the file. |
| 96 | $bootDoneDir = 'C:\ProgramData\Lima' |
| 97 | New-Item -ItemType Directory -Path $bootDoneDir -Force | Out-Null |
| 98 | $bootDoneFile = Join-Path $bootDoneDir 'lima-boot-done.txt' |
| 99 | 'done' | Out-File -FilePath $bootDoneFile -Encoding ascii -Force -NoNewline |
| 100 | |
| 101 | |
| 102 | $elapsed = (Get-Date) - $globalStart |
| 103 | Write-Output "All works completed in $($elapsed.TotalSeconds) seconds" |
| 104 | |
| 105 | # Finish recording logs |
| 106 | Stop-Transcript |