Skip to content

Commit 83578b4

Browse files
committed
Add wsl-dist-update
1 parent 61fec67 commit 83578b4

File tree

9 files changed

+96
-80
lines changed

9 files changed

+96
-80
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.exe
1+
*.exe
2+
/bin/

README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,36 @@ Provides a list of installed distributions, the official Linux distro name, the
88

99
## wslctl
1010

11-
Allows WSL distros to be started on launch. The syntax follows that of systemctl:
11+
Allows WSL distros to be started on Windows startup. The syntax follows that of systemctl:
1212

13-
```
14-
wslctl enable pengwin
15-
wslctl disable pengwin
16-
wslctl restart pengwin
17-
```
13+
`wslctl enable pengwin`
14+
15+
`wslctl disable pengwin`
16+
17+
`wslctl restart pengwin`
18+
19+
## wsl-dist-update
20+
21+
Run package updates on all installed WSL distros. Tested on: Pengwin, Fedora Remix for WSL, Ubuntu, Debian, openSUSE Tumbleweed, ArchWSL, Oracle Linux, and the WSL.
22+
23+
wsl-dist-update can optionally additionally update the WSL system distro by passing the `--system` option.
24+
25+
To run wsl-dist-update as a service, copy wsl-dist-update.exe to a permanent location and run wsl-dist-update-sched.ps1, modifying the path to the .exe as needed.
1826

1927
## wsl-reset
2028

21-
A troubleshooting utility that resets WSL to various degrees.
29+
A troubleshooting utility that resets the WSL 2 stack to various degrees.
2230

23-
`wsl-reset --reset` - shuts down WSL, resets the WSL service, and installs any WSL updates, if available
31+
`wsl-reset --reset` - Shuts down WSL, resets the WSL service, and installs any WSL updates, if available.
2432

25-
`wsl-reset --hard-reset` - shuts down WSL, stops the WSL service, uninstalls WSL, and re-installs WSL
33+
`wsl-reset --hard-reset` - Shuts down WSL, stops the WSL service, uninstalls WSL, and re-installs WSL.
2634

27-
`wsl-reset --destrutive-reset` - shuts down WSL, restarts the WSL service, **unregisters all WSL distros**, stops the WSL service, uninstalls WSL, and re-installs WSL
35+
`wsl-reset --destrutive-reset` - Shuts down WSL, restarts the WSL service, **unregisters all WSL distros**, stops the WSL service, uninstalls WSL, and re-installs WSL.
2836

2937
## sysdistrowt
3038

3139
Adds the WSL System Distro (CBL-Mariner) to the Windows Terminal and/or Windows Terminal Preview profiles, for easier debugging.
3240

33-
### Misc
41+
## build-wslinternals
3442

35-
/prompts folder contains example AI prompts used to generate the first draft of these scripts, the final scripts got hand polished, but they are there to see.
43+
Builds wslinternals PowerShell scripts to .exe files using ps2exe. Requires PowerShell 5.

build-wslinternals.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Install ps2exe module if not already installed
2+
if (-not (Get-Module -Name ps2exe -ListAvailable)) {
3+
Install-Module -Name ps2exe -Scope CurrentUser -Force
4+
}
5+
6+
# Create /bin/ subdirectory if it does not exist
7+
if (-not (Test-Path -Path ".\bin\" -PathType Container)) {
8+
New-Item -ItemType Directory -Path ".\bin\"
9+
}
10+
11+
# Get list of .ps1 files in current directory
12+
$ps1Files = Get-ChildItem -Path . -Filter *.ps1
13+
14+
# Loop through each .ps1 file and convert to .exe using ps2exe
15+
foreach ($ps1File in $ps1Files) {
16+
$exeFile = ".\bin\" + $ps1File.Name.Replace(".ps1", ".exe")
17+
ps2exe -inputFile $ps1File.FullName -outputFile $exeFile
18+
}

prompts/list-wsl-prompt.txt

Lines changed: 0 additions & 21 deletions
This file was deleted.

prompts/sysdistrowt-prompt.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

prompts/wsl-reset-prompt.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

prompts/wslctl-prompt.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

wsl-dist-update-sched.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Define variables
2+
$taskName = "WSL Distro Automatic Update"
3+
$taskDescription = "Runs wsl-dist-update.exe 10 minutes after Windows boots"
4+
$executablePath = "wsl-dist-update.exe"
5+
$arguments = "--system"
6+
7+
# Create scheduled task
8+
$action = New-ScheduledTaskAction -Execute $executablePath -Argument $arguments
9+
$trigger = New-ScheduledTaskTrigger -AtStartup -Delay 10m
10+
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
11+
Register-ScheduledTask -TaskName $taskName -Description $taskDescription -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -Force
12+
13+
# Run scheduled task in the background
14+
Start-ScheduledTask -TaskName $taskName

wsl-dist-update.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Get list of installed WSL distros from registry
2+
$distros = Get-ChildItem "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss" | ForEach-Object { $_.GetValue("DistributionName") }
3+
4+
# Loop through each distro and get ID_LIKE variable from /etc/os-release using wsl.exe
5+
$results = foreach ($distro in $distros) {
6+
$osReleasePath = "\\wsl$\$distro\etc\os-release"
7+
#$idLike = (wsl.exe -d $distro cat /etc/os-release | Select-String "^ID_LIKE=").ToString().Split("=")[1]
8+
$id = (wsl.exe -d $distro cat /etc/os-release | Select-String "^ID=").ToString().Split("=")[1]
9+
Write-Host "Updating $distro"
10+
switch -Wildcard ($id) {
11+
"*debian*" {
12+
wsl.exe -d $distro -u root apt-get update > $null
13+
wsl.exe -d $distro -u root apt-get upgrade -y > $null
14+
}
15+
"*ubuntu*" {
16+
wsl.exe -d $distro -u root apt-get update > $null
17+
wsl.exe -d $distro -u root apt-get upgrade -y > $null
18+
}
19+
"*fedora*" {
20+
wsl.exe -d $distro -u root dnf update -y > $null
21+
}
22+
"*rhel*" {
23+
wsl.exe -d $distro -u root dnf update -y > $null
24+
}
25+
"*suse*" {
26+
wsl.exe -d $distro -u root zypper dup -y > $null
27+
}
28+
"*arch*" {
29+
wsl.exe -d $distro -u root pacman -Syu --noconfirm > $null
30+
}
31+
}
32+
[PSCustomObject]@{
33+
DistroName = $distro
34+
ID_LIKE = $idLike
35+
}
36+
}
37+
38+
# Update WSL System Distro if --system flag is passed
39+
if ($args.Contains("--system")) {
40+
Write-Host "Updating WSL System Distro"
41+
wsl.exe -u root --system tdnf update -y > $null
42+
}

0 commit comments

Comments
 (0)