-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsl.ps1
273 lines (223 loc) · 7.82 KB
/
wsl.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# avoid promt blocking after debug messages
$DebugPreference = 'Continue'
#initial ps output encoding
$console = ([console]::OutputEncoding)
#switch to wsl / linux encoding
[console]::OutputEncoding = New-Object System.Text.UnicodeEncoding
#helper
function Finish($code){
#reset to initial console encoding
[console]::OutputEncoding = $console
Write-Debug "finished $($code)"
Exit $code
}
function CheckWsl(){
$wslversion = wsl --status | Select-String -Pattern ".*Standardversion: 1.*" -Quiet
Write-Debug "wsl version 1? $($wslversion -eq $True)"
if($wslversion -eq $True){
Write-Debug "set wsl to version 2"
wsl '--set-default-version' 2
}
return wsl --status | Select-String -Pattern ".*Standardversion: 2.*" -Quiet
}
############
## config ##
############
$wslDistroShort = "Ubuntu"
$wslDistro = "Ubuntu-22.04"
$wslDistroFullName = "Ubuntu 22.04 LTS"
$distroImageName = 'Ubuntu.appx'
$distroUrl = 'https://aka.ms/wslubuntu2204' # https://learn.microsoft.com/en-us/windows/wsl/install-manual#downloading-distributions
#vpn kit
$vpnkitPath = 'https://github.com/sakai135/wsl-vpnkit/releases/download/v0.3.4/'
$vpnKitFilename = 'wsl-vpnkit.tar.gz'
$vpnkitUrl = $vpnkitPath + $vpnKitFilename
#configure the menu with
function Menu{
#Clear-Host
Write-Host "----------------[ install ]----------------"
Write-Host "[1] full install with vpnkit"
Write-Host "[2] full install without vpnkit"
Write-Host "-----------[ selective install ]-----------"
Write-Host "[3] install vpn-kit"
Write-Host "[4] setup distro"
Write-Host "[5] provision distro"
Write-Host "----------------[ remove ]----------------"
Write-Host "[6] reprovision distro (fix podman after restart)"
Write-Host "[7] remove all (vpnkit and distro)"
Write-Host "[8] podman status"
return Read-Host "Select Setup (default 1)"
}
# setup
# vpn kit relatet
function FindDistro($name){
$found = wsl -l | Select-String -Pattern $name -SimpleMatch -Quiet
Write-Debug "finding $name :$found"
return $found
}
function DownloadImage($url, $filename){
Write-Debug "Check download file existence: $filename"
$dest = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').self.Path + '\' + $filename
if (Test-Path $dest){
Write-Debug "$filename file exists"
return $dest
}
$size = (Invoke-WebRequest $url -Method Head).Headers.'Content-Length'
Write-Debug "Downloading $filename from $url to $dest `nfilesize $size"
Invoke-WebRequest -Uri $url -OutFile $dest -Resume #-UseBasicParsing
return $dest
}
function InstallVpnkit{
if(FindDistro('wsl-vpnkit')){
Write-Debug "vpnkit already installed"
RemoveVpnKit('wsl-vpnkit')
}
$vpnkitlocation = DownloadImage $vpnkitUrl $vpnKitFilename
Write-Debug "vpnkit located at: $vpnkitlocation"
#setup
Write-Debug "install vpnkit"
$success = wsl --import wsl-vpnkit $env::USERPROFILE\wsl-vpnkit $vpnkitlocation --version 2 | Select-String -Pattern ".*VM-Plattform.*" -Quiet
if ($success -eq $True){
Write-Debug "vpn import failed, are VM-Platform components installed and virtualization is active in bios?"
Finish(1)
}
Write-Debug "import success"
wsl -d wsl-vpnkit
#start
Write-Debug "start vpnkit"
wsl -d wsl-vpnkit service wsl-vpnkit start
Write-Debug "done"
}
function RemoveVpnKit($distro){
Write-Debug "removing $distro"
wsl -d wsl-vpnkit service wsl-vpnkit stop
wsl -t $distro
wsl --unregister $distro
}
# linux related
function FindUbuntu{
$ubuntu = FindDistro('Ubuntu')
if ($ubuntu){
$rename = Read-Host "found generic ubuntu, is it $wslDistro ([y]es/[n]o)"
if ($rename -eq "yes" -or $rename -eq "y"){
Set-Variable -Name "wslDistro" -Value "Ubuntu" -Scope global
Write-Debug "working with name $wslDistro"
}
}
return wsl -l | Select-String -Pattern '.* $wslDistro .*' -Quiet
}
function ConfigureDistro{
Write-Debug "configure distro"
if (FindUbuntu -ne $True){
Write-Debug "$wslDistro not installed"
Write-Host "[1] install ubuntu via windows store (fast, requires account)"
Write-Host "[2] install via commandline (really slow, no account required)"
Write-Host "[3] provide image manually (download from $distroUrl into $env:USERPROFILE\Downloads and name the file $distroImageName)"
$setup = Read-Host "select setup (default 3)"
switch ($setup) {
1 {
#v1 download via store
Start-Process "ms-windows-store://pdp/?ProductId=9PN20MSR04DW"
Write-Debug "restart setup after install of distro is done"
Finish(1)
}
2 {
#v2 command download
Write-Debug "`n
Downloading distro: $distroImageName, this may take 20 minutes!`n
You can download it possibly faster via browser INTO YOUR downloads folder`n
from here: $distroUrl`n
IMPORTANT name the file $distroImageName"
$ProgressPreference = 'SilentlyContinue'
Write-Host "downloading distro, please wait (20mins)"
$distroDest = DownloadImage $distroUrl $distroImageName
Write-Host "download finished"
Write-Debug "distro located at: $distroDest"
Add-AppxPackage $distroDest
}
default {
$distroDest = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').self.Path + '\' + $distroImageName
if (Test-Path $distroDest){
Write-Debug "$distroImageName file exists`n path: $distroDest"
Add-AppxPackage $distroDest
Start-Process $distroDest
}
else {
Start-Process "$distroUrl"
Finish(1)
}
}
}
}
#after install
#set distro as default
Write-Debug "set $wslDistro as default"
wsl --set-default $wslDistro
Write-Debug "start distro provisioning"
wsl -e sh ./distro/ubuntu.sh
}
function Provision{
Write-Debug "Provisioning distro"
wsl ansible-galaxy collection install containers.podman
wsl wslpath $PWD.Path.Replace('\','/') '|' xargs -i ansible-playbook '{}/ansible/playbooks/demo.yml' -i '{}/ansible/hosts' --connection=local --extra-vars="user=$env:username" --tags "provision, git"
}
### util
function FixPodman{
wsl podman stop -a
wsl podman system prune -f
wsl rm -rf /tmp/podman-run-'$('id -u')'/libpod/tmp
wsl wslpath $PWD.Path.Replace('\','/') '|' xargs -i ansible-playbook '{}/ansible/playbooks/demo.yml' -i '{}/ansible/hosts' --connection=local --tags "remove"
Provision
Write-Debug "if this run showed errors, please repeat"
Finish(0)
}
function RemoveAll{
RemoveVpnKit('wsl-vpnkit')
FindUbuntu
wsl -t $wslDistro
wsl --unregister $wslDistro
# winget uninstall $wslDistroFullName
Finish(0)
}
##program start from here
if ($(CheckWsl) -eq $False){
[console]::OutputEncoding = $console
Write-Debug "kein deutsches wsl version 2 gefunden"
Finish(1)
}
else {
switch (Menu) {
2 {
ConfigureDistro
Provision
Finish(0)
}
3 {
InstallVpnkit
Finish(0)
}
4 {
ConfigureDistro
Finish(0)
}
5 {
Provision
Finish(0)
}
6 {
FixPodman
}
7 {
RemoveAll
}
8 {
wsl podman ps -a
}
Default {
InstallVpnkit
ConfigureDistro
Provision
Finish(0)
}
}
}