Skip to content

Commit

Permalink
Add wsl-dist-update
Browse files Browse the repository at this point in the history
  • Loading branch information
sirredbeard committed Oct 11, 2023
1 parent 61fec67 commit 83578b4
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.exe
*.exe
/bin/
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,36 @@ Provides a list of installed distributions, the official Linux distro name, the

## wslctl

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

```
wslctl enable pengwin
wslctl disable pengwin
wslctl restart pengwin
```
`wslctl enable pengwin`

`wslctl disable pengwin`

`wslctl restart pengwin`

## wsl-dist-update

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.

wsl-dist-update can optionally additionally update the WSL system distro by passing the `--system` option.

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.

## wsl-reset

A troubleshooting utility that resets WSL to various degrees.
A troubleshooting utility that resets the WSL 2 stack to various degrees.

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

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

`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
`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.

## sysdistrowt

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

### Misc
## build-wslinternals

/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.
Builds wslinternals PowerShell scripts to .exe files using ps2exe. Requires PowerShell 5.
18 changes: 18 additions & 0 deletions build-wslinternals.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Install ps2exe module if not already installed
if (-not (Get-Module -Name ps2exe -ListAvailable)) {
Install-Module -Name ps2exe -Scope CurrentUser -Force
}

# Create /bin/ subdirectory if it does not exist
if (-not (Test-Path -Path ".\bin\" -PathType Container)) {
New-Item -ItemType Directory -Path ".\bin\"
}

# Get list of .ps1 files in current directory
$ps1Files = Get-ChildItem -Path . -Filter *.ps1

# Loop through each .ps1 file and convert to .exe using ps2exe
foreach ($ps1File in $ps1Files) {
$exeFile = ".\bin\" + $ps1File.Name.Replace(".ps1", ".exe")
ps2exe -inputFile $ps1File.FullName -outputFile $exeFile
}
21 changes: 0 additions & 21 deletions prompts/list-wsl-prompt.txt

This file was deleted.

23 changes: 0 additions & 23 deletions prompts/sysdistrowt-prompt.txt

This file was deleted.

11 changes: 0 additions & 11 deletions prompts/wsl-reset-prompt.txt

This file was deleted.

12 changes: 0 additions & 12 deletions prompts/wslctl-prompt.txt

This file was deleted.

14 changes: 14 additions & 0 deletions wsl-dist-update-sched.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Define variables
$taskName = "WSL Distro Automatic Update"
$taskDescription = "Runs wsl-dist-update.exe 10 minutes after Windows boots"
$executablePath = "wsl-dist-update.exe"
$arguments = "--system"

# Create scheduled task
$action = New-ScheduledTaskAction -Execute $executablePath -Argument $arguments
$trigger = New-ScheduledTaskTrigger -AtStartup -Delay 10m
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName $taskName -Description $taskDescription -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -Force

# Run scheduled task in the background
Start-ScheduledTask -TaskName $taskName
42 changes: 42 additions & 0 deletions wsl-dist-update.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Get list of installed WSL distros from registry
$distros = Get-ChildItem "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss" | ForEach-Object { $_.GetValue("DistributionName") }

# Loop through each distro and get ID_LIKE variable from /etc/os-release using wsl.exe
$results = foreach ($distro in $distros) {
$osReleasePath = "\\wsl$\$distro\etc\os-release"
#$idLike = (wsl.exe -d $distro cat /etc/os-release | Select-String "^ID_LIKE=").ToString().Split("=")[1]
$id = (wsl.exe -d $distro cat /etc/os-release | Select-String "^ID=").ToString().Split("=")[1]
Write-Host "Updating $distro"
switch -Wildcard ($id) {
"*debian*" {
wsl.exe -d $distro -u root apt-get update > $null
wsl.exe -d $distro -u root apt-get upgrade -y > $null
}
"*ubuntu*" {
wsl.exe -d $distro -u root apt-get update > $null
wsl.exe -d $distro -u root apt-get upgrade -y > $null
}
"*fedora*" {
wsl.exe -d $distro -u root dnf update -y > $null
}
"*rhel*" {
wsl.exe -d $distro -u root dnf update -y > $null
}
"*suse*" {
wsl.exe -d $distro -u root zypper dup -y > $null
}
"*arch*" {
wsl.exe -d $distro -u root pacman -Syu --noconfirm > $null
}
}
[PSCustomObject]@{
DistroName = $distro
ID_LIKE = $idLike
}
}

# Update WSL System Distro if --system flag is passed
if ($args.Contains("--system")) {
Write-Host "Updating WSL System Distro"
wsl.exe -u root --system tdnf update -y > $null
}

0 comments on commit 83578b4

Please sign in to comment.