forked from Layer8Err/WSL2setup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WSL2_Install.ps1
283 lines (271 loc) · 11.2 KB
/
WSL2_Install.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
274
275
276
277
278
279
280
281
282
283
# Install WSL
# This script needs to be run as a priviledged user
Write-Host("Checking for Windows Subsystem for Linux...")
$rebootRequired = $false
if ((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux).State -ne 'Enabled'){
Write-Host(" ...Installing Windows Subsystem for Linux.")
$wslinst = Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName Microsoft-Windows-Subsystem-Linux
if ($wslinst.Restartneeded -eq $true){
$rebootRequired = $true
}
} else {
Write-Host(" ...Windows Subsystem for Linux already installed.")
}
Write-Host("Checking for Virtual Machine Platform...")
if ((Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform).State -ne 'Enabled'){
Write-Host(" ...Installing Virtual Machine Platform.")
$vmpinst = Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName VirtualMachinePlatform
if ($vmpinst.RestartNeeded -eq $true){
$rebootRequired = $true
}
} else {
Write-Host(" ...Virtual Machine Platform already installed.")
}
function Update-Kernel () {
Write-Host(" ...Downloading WSL2 Kernel Update.")
$kernelURI = 'https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi'
$kernelUpdate = ((Get-Location).Path) + '\wsl_update_x64.msi'
(New-Object System.Net.WebClient).DownloadFile($kernelURI, $kernelUpdate)
Write-Host(" ...Installing WSL2 Kernel Update.")
msiexec /i $kernelUpdate /qn
Start-Sleep -Seconds 5
Write-Host(" ...Cleaning up Kernel Update installer.")
Remove-Item -Path $kernelUpdate
}
function Get-Kernel-Updated () {
# Check for Kernel Update Package
Write-Host("Checking for Windows Subsystem for Linux Update...")
$uninstall64 = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | ForEach-Object { Get-ItemProperty $_.PSPath } | Select-Object DisplayName, Publisher, DisplayVersion, InstallDate
if ($uninstall64.DisplayName -contains 'Windows Subsystem for Linux Update') {
return $true
} else {
return $false
}
}
$pkgs = (Get-AppxPackage).Name
function Get-WSLlist {
$wslinstalls = New-Object Collections.Generic.List[String]
$(wsl -l) | ForEach-Object { if ($_.Length -gt 1){ $wslinstalls.Add($_) } }
$wslinstalls = $wslinstalls | Where-Object { $_ -ne 'Windows Subsystem for Linux Distributions:' }
return $wslinstalls
}
function Get-WSLExistance ($distro) {
# Check for the existence of a distro
# return Installed as Bool
$wslImport = $false
if (($distro.AppxName).Length -eq 0){ $wslImport = $true }
$installed = $false
if ( $wslImport -eq $false ){
if ($pkgs -match $distro.AppxName) {
$installed = $true
}
} else {
if (Get-WSLlist -contains ($distro.Name).Replace("-", " ")){
$installed = $true
}
}
return $installed
}
function Get-StoreDownloadLink ($distro) {
# Uses $distro.StoreLink to get $distro.URI
# Required when URI is not hard-coded
#### Thanks to MattiasC85 for this excelent method of getting Microsoft Store download URIs ####
# Source: https://github.com/MattiasC85/Scripts/blob/a1163b97875ed075927438505808622614a9961f/OSD/Download-AppxFromStore.ps1
$wchttp=[System.Net.WebClient]::new()
$URI = "https://store.rg-adguard.net/api/GetFiles"
$myParameters = "type=url&url=$($distro.StoreLink)"
$wchttp.Headers[[System.Net.HttpRequestHeader]::ContentType]="application/x-www-form-urlencoded"
$HtmlResult = $wchttp.UploadString($URI, $myParameters)
$Start=$HtmlResult.IndexOf("<p>The links were successfully received from the Microsoft Store server.</p>")
if ($Start -eq -1) {
write-host "Could not get Microsoft Store download URI, please check the StoreURL."
exit
}
$TableEnd=($HtmlResult.LastIndexOf("</table>")+8)
$SemiCleaned=$HtmlResult.Substring($start,$TableEnd-$start)
$newHtml=New-Object -ComObject "HTMLFile"
$src = [System.Text.Encoding]::Unicode.GetBytes($SemiCleaned)
$newHtml.write($src)
$ToDownload=$newHtml.getElementsByTagName("a") | Select-Object textContent, href
$apxLinks = @()
$ToDownload | Foreach-Object {
if ($_.textContent -match '.appxbundle') {
$apxLinks = $_
}
}
$distro.URI = $apxLinks.href
return $distro
}
function Select-Distro () {
# See: https://docs.microsoft.com/en-us/windows/wsl/install-manual
# You can also use https://store.rg-adguard.net to get Appx links from Windows Store links
$distrolist = (
[PSCustomObject]@{
'Name' = 'Ubuntu 20.04'
'URI' = 'https://aka.ms/wslubuntu2004'
'AppxName' = 'CanonicalGroupLimited.Ubuntu20.04onWindows'
'winpe' = 'ubuntu2004.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 18.04'
'URI' = 'https://aka.ms/wsl-ubuntu-1804'
'AppxName' = 'CanonicalGroupLimited.Ubuntu18.04onWindows'
'winpe' = 'ubuntu1804.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Ubuntu 16.04'
'URI' = 'https://aka.ms/wsl-ubuntu-1604'
'AppxName' = 'CanonicalGroupLimited.Ubuntu16.04onWindows'
'winpe' = 'ubuntu1604.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Debian'
'URI' = 'https://aka.ms/wsl-debian-gnulinux'
'AppxName' = 'TheDebianProject.DebianGNULinux'
'winpe' = 'debian.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Kali'
'URI' = 'https://aka.ms/wsl-kali-linux-new'
'AppxName' = 'KaliLinux'
'winpe' = 'kali.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'OpenSUSE Leap 42'
'URI' = 'https://aka.ms/wsl-opensuse-42'
'AppxName' = 'openSUSELeap42'
'winpe' = 'openSUSE-42.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'SUSE Linux Enterprise Server 12'
'URI' = 'https://aka.ms/wsl-sles-12'
'AppxName' = 'SUSELinuxEnterpriseServer12'
'winpe' = 'SLES-12.exe'
'installed' = $false
},
[PSCustomObject]@{
'Name' = 'Alpine'
'StoreLink' = 'https://www.microsoft.com/en-us/p/alpine-wsl/9p804crf0395'
'URI' = ''
'AppxName' = 'AlpineWSL'
'winpe' = 'Alpine.exe'
'installed' = $false
}
# [PSCustomObject]@{
# 'Name' = 'Fedora Remix for WSL'
# 'URI' = 'https://github.com/WhitewaterFoundry/Fedora-Remix-for-WSL/releases/download/31.5.0/Fedora-Remix-for-WSL_31.5.0.0_x64_arm64.appxbundle'
# 'AppxName' = 'FedoraRemix'
# 'winpe' = 'fedora.exe'
# 'installed' = $false
# 'sideloadreqd' = $true # Sideloading not supported by this script... yet
# },
# [PSCustomObject]@{
# 'Name' = 'Ubuntu 20.04'
# 'URI' = 'https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz'
# 'AppxName' = '' # Leave blank for wsl --import install
# 'winpe' = ''
# 'installed' = $false
# }
)
$distrolist | ForEach-Object { $_.installed = Get-WSLExistance($_) }
Write-Host("+------------------------------------------------+")
Write-Host("| Choose your Distro |")
Write-Host("| Ubuntu 20.04 is recommended for Docker on WSL2 |")
Write-Host("+------------------------------------------------+")
For ($i = 0; $i -le ($distrolist.Length - 1); $i++) {
$installedTxt = ""
if (($distrolist.installed)[$i]) {
$installedTxt = "(Installed)"
}
Write-Host(($i + 1).ToString() + " " + ($distrolist.Name)[$i] + " " + $installedTxt)
}
$distroChoice = Read-Host '>'
$choiceNum = 0
if (($distroChoice.Length -ne 0) -and ($distroChoice -match '^\d+$')) {
if (($distroChoice -gt 0) -and ($distroChoice -le $distrolist.Length)) {
$choiceNum = ($distroChoice - 1)
}
}
$choice = $distrolist[$choiceNum]
return $choice
}
function Install-Distro ($distro) {
function Import-WSL ($distro) {
$distroinstall = "$env:LOCALAPPDATA\lxss"
$wslname = $($distro.Name).Replace(" ", "-")
$Filename = $wslname + ".rootfs.tar.gz"
Write-Host(" ...Downloading " + $distro.Name + ".")
Invoke-WebRequest -Uri $distro.URI -OutFile $Filename -UseBasicParsing
wsl.exe --import $wslname $distroinstall $Filename
}
function Add-WSLAppx ($distro) {
# ToDo: Check if sideloading is required
$Filename = "$($distro.AppxName).appx"
$ProgressPreference = 'SilentlyContinue'
Write-Host(" ...Downloading " + $distro.Name + ".")
if ($distro.URI.Length -lt 2) {
$distro = Get-StoreDownloadLink($distro) # Handle dynamic URIs
}
Invoke-WebRequest -Uri $distro.URI -OutFile $Filename -UseBasicParsing
Write-Host(" ...Beginning " + $distro.Name + " install.")
Add-AppxPackage -Path $Filename
Start-Sleep -Seconds 5
}
if (Get-WSLExistance($distro)) {
Write-Host(" ...Found an existing " + $distro.Name + " install")
} else {
if ($($distro.AppxName).Length -gt 1){
Add-WSLAppx($distro)
} else {
Import-WSL($distro)
}
}
}
if ($rebootRequired) {
shutdown /t 120 /r /c "Reboot required to finish installing WSL2"
$cancelReboot = Read-Host 'Cancel reboot for now (you still need to reboot and rerun to finish installing WSL2) [y/N]'
if ($cancelReboot.Length -ne 0){
if ($cancelReboot.Substring(0,1).ToLower() -eq 'y'){
shutdown /a
}
}
} else {
if (!(Get-Kernel-Updated)) {
Write-Host(" ...WSL kernel update not installed.")
Update-Kernel
} else {
Write-Host(" ...WSL update already installed.")
}
Write-Host("Setting WSL2 as the default...")
wsl --set-default-version 2
$distro = Select-Distro
Install-Distro($distro)
if ($distro.AppxName.Length -gt 1) {
Start-Process $distro.winpe
} else {
$wslselect = ""
Get-WSLlist | ForEach-Object {
if ($_ -match $distro.Name){
$wslselect = $_
}
}
if ($wslselect -ne "") {
wsl -d $wslselect
} else {
Write-Host("Run 'wsl -l' to list WSL Distributions")
Write-Host("Run 'wsl -d <distroname>' to start WSL Distro")
}
}
}
Write-Host("Creating desktop shortcut for your Linux home folder...")
$objShell = New-Object -ComObject ("WScript.Shell")
$objShortCut = $objShell.CreateShortcut($env:USERPROFILE + "\Desktop" + "\Ubuntu.lnk")
$username = $env:USERNAME.ToLower()
$objShortCut.TargetPath="\\wsl$\Ubuntu-20.04\home\" + $username
$objShortCut.Save()