-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
uninstall.ps1
286 lines (247 loc) · 9.35 KB
/
uninstall.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
284
285
286
Using module Wsl
param(
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$Distro,
[switch]
$LeaveGPG,
[switch]
$LeaveKernel
)
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
$rootFiles = @(
@{ 'dest' = '/etc/profile.d/00-wsl2-systemd.sh' },
@{ 'dest' = '/etc/sudoers.d/wsl2-systemd' },
@{ 'dest' = '/usr/share/applications/wslview.desktop' },
@{ 'dest' = '/etc/systemd/system/[email protected]/override.conf' },
@{ 'dest' = '/etc/systemd/system/wsl2-xwayland.service' },
@{ 'dest' = '/etc/systemd/system/wsl2-xwayland.socket' }
)
$userFiles = @(
@{ 'dest' = '/home/*/.wslprofile.d' },
@{ 'dest' = '/home/*/.wsl-cmds' }
)
function Invoke-WslCommand
{
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Command,
[Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = "DistributionName", Position = 1)]
[SupportsWildCards()]
[string[]]$DistributionName,
[Parameter(Mandatory = $false, ValueFromPipeline = $true, ParameterSetName = "Distribution")]
[WslDistribution[]]$Distribution,
[Parameter(Mandatory = $false, Position = 2)]
[ValidateNotNullOrEmpty()]
[string]$User
)
process {
if ($PSCmdlet.ParameterSetName -eq "DistributionName") {
if ($DistributionName) {
$Distribution = Get-WslDistribution $DistributionName
} else {
$Distribution = Get-WslDistribution -Default
}
} elseif ($PSCmdLet.ParameterSetName -ne "Distribution") {
$Distribution = Get-WslDistribution -Default
}
$Distribution | ForEach-Object {
$DistroName = $_.Name
$wslargs = @("--distribution", $DistroName)
if ($User) {
$wslargs += @("--user", $User)
}
$Command = $Command + "`n" # Add a trailing new line
$Command = $Command.Replace("`r`n", "`n") # Replace Windows newlines with Unix ones
$Command += '#' # Add a comment on the last line to hide PowerShell cruft added to the end of the string
if ($PSCmdlet.ShouldProcess($DistroName, "Invoke Command")) {
$Command | &$wslPath @wslargs /bin/sh
if ($LASTEXITCODE -ne 0) {
# Note: this could be the exit code of wsl.exe, or of the launched command.
throw "Wsl.exe returned exit code $LASTEXITCODE from distro: ${DistroName}"
}
}
}
}
}
function Add-WslFileContent {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$Content,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[WslDistribution[]]$Distribution,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$User,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$File
)
$commandArgs = @{}
if ($User) {
$commandArgs = @{User = $User}
}
$bytes = [System.Text.Encoding]::UTF8.GetBytes($Content)
$base64 = [Convert]::ToBase64String($bytes)
$Directory = ($File | Split-Path).Replace('\', '/')
$Command = "mkdir -p `"$Directory`" && echo '$base64' | base64 -d > `"$File`""
Invoke-WslCommand -Distribution $Distribution @commandArgs -Command $Command
}
function Remove-WslFiles {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
$Files,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[WslDistribution[]]$Distribution,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$User
)
process {
foreach ($file in $Files) {
$remove = $file.dest
$commandArgs = @{}
if ($file['user']) {
$commandArgs = @{User = $file.user}
} elseif ($User) {
$commandArgs = @{User = $User}
}
Invoke-WslCommand -Distribution $Distribution -Command "rm -rf $remove" @commandArgs
}
}
}
function Get-IniContent($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
"^\[(.+)\]" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=(.*)" # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
function Write-IniOutput($InputObject)
{
foreach ($i in $InputObject.keys)
{
if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))
{
#No Sections
Write-Output "$i=$($InputObject[$i])"
} else {
#Sections
Write-Output "[$i]"
Foreach ($j in ($InputObject[$i].keys | Sort-Object))
{
if ($j -match "^Comment[\d]+") {
Write-Output "$($InputObject[$i][$j])"
} else {
Write-Output "$j=$($InputObject[$i][$j])"
}
}
Write-Output ""
}
}
}
function Out-IniFile($InputObject, $FilePath)
{
$outFile = New-Item -ItemType file -Path $Filepath -Force
foreach ($i in $InputObject.keys)
{
if (!($($InputObject[$i].GetType().Name) -eq "Hashtable"))
{
#No Sections
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])"
} else {
#Sections
Add-Content -Path $outFile -Value "[$i]"
Foreach ($j in ($InputObject[$i].keys | Sort-Object))
{
if ($j -match "^Comment[\d]+") {
Add-Content -Path $outFile -Value "$($InputObject[$i][$j])"
} else {
Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])"
}
}
Add-Content -Path $outFile -Value ""
}
}
}
$powershellProcess = (Get-Process -Id $PID).ProcessName + '.exe'
if (-not [System.Environment]::Is64BitProcess) {
# Allow launching WSL from 32 bit powershell
$wslPath = "$env:windir\sysnative\wsl.exe"
} else {
$wslPath = "$env:windir\system32\wsl.exe"
}
Write-Output "`r`n`n"
Write-Output "#########################################################"
Write-Output "# #"
Write-Output "# One Script WSL2 Systemd uninstall script #"
Write-Output "# #"
Write-Output "#########################################################`n`n"
if ($PSVersionTable.PSEdition -eq "Core" -and -not $IsWindows) {
Write-Output "This script must be run in Windows."
exit
}
if ($Distro -and -not ($Distribution = Get-WslDistribution -Name $Distro)) {
Write-Error "!!! $Distro is not currently installed. Refusing to continue."
exit
}
if (-not $Distribution) {
# Get all distributions except docker-desktop-related
$Distribution = Get-WslDistribution | Where-Object -Property Name -NotLike -Value "docker-desktop*"
}
Write-Output "---------------------------------------------------------`n`n"
$Distribution | ForEach-Object {
$DistroName = $_.Name
Write-Output "Uninstalling systemd enablement from $DistroName"
Remove-WslFiles -Files $rootFiles -Distribution $_ -User 'root'
Remove-WslFiles -Files $userFiles -Distribution $_ -User 'root'
if (Test-Path -Path "$($Distribution.FileSystemPath)\etc\wsl.conf") {
$wslconfig = Get-IniContent "$($Distribution.FileSystemPath)\etc\wsl.conf"
if ($wslconfig["boot"] -and $wslconfig.boot.command -eq "/usr/bin/env -i /usr/bin/unshare --fork --mount-proc --pid -- sh -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; [ -x /usr/lib/systemd/systemd ] && exec /usr/lib/systemd/systemd --unit=multi-user.target || exec /lib/systemd/systemd --unit=multi-user.target'") {
$wslconfig.boot.Remove('command')
(Write-IniOutput $wslconfig) -Join "`n" | Add-WslFileContent -Distribution $Distribution -User "root" -File "/etc/wsl.conf"
}
}
}
if (-not $LeaveGPG) {
Write-Output "Uninstalling GnuPG"
winget.exe uninstall gnupg.Gpg4win
Start-Process -Verb RunAs -Wait -FilePath $powershellProcess -Args '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'ByPass', '-Command', "Unregister-ScheduledJob -Name GPGAgent -Force"
}
if (-not $LeaveKernel) {
Write-Output "Reverting WSL to the Microsoft-provided kernel"
Start-Process -Verb RunAs -Wait -FilePath $powershellProcess -Args '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'ByPass', '-Command', "Unregister-ScheduledJob -Name UpdateWSL2CustomKernel -Force"
if (Test-Path -Path "$env:USERPROFILE/.wslconfig") {
$wslconfig = Get-IniContent "$env:USERPROFILE/.wslconfig"
$wslconfig["wsl2"].Remove("kernel")
Out-IniFile $wslconfig "$env:USERPROFILE/.wslconfig"
}
}
Write-Output "Done."