Created
May 5, 2024 23:52
-
-
Save dotysan/63ac8d446cc9cc8e54385d041ffe7cdc to your computer and use it in GitHub Desktop.
WSL Minimal Install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env pwsh | |
<# | |
Notes as code for creating a minimal WSL image. | |
This is not docker! | |
#> | |
#Requires -Version 7.4 | |
Set-StrictMode -Version Latest | |
$ErrorActionPreference = 'Stop' | |
# TODO: parameterize all these hard-coded vars | |
$distro = 'Ubuntu' | |
$ldistro = $distro.ToLower() | |
# TODO: ver/what are synonyms; use one, presume the other | |
$ver = '24' | |
$what = 'noble' | |
# TODO: dynamically lookup arch | |
$arch = 'amd64' | |
# TODO: find the latest image available | |
$when = '20240423' | |
# TODO: un-hard-code this | |
$WSLroot = 'D:\Users\Curtis\VMs\WSL' | |
$OutDir = "$WSLroot\$distro\$what\minimal-$when" | |
# TODO: replace these Ubuntu-specific vars with any distribution such as Fedora | |
$imgfile = "$ldistro-$ver.04-minimal-cloudimg-$arch-root.tar.xz" | |
$Uri = "https://cloud-images.ubuntu.com/minimal/releases/$what/release-$when/$imgfile" | |
$wsldistro = "$distro$ver-minimal-$when" | |
function main { | |
# fetch the root filesystem image | |
wgetN -Uri $Uri -OutDir $OutDir | |
# create WSL distro if it doesn't already exist | |
$exists = wsl --list --quiet |Select-String -Pattern $wsldistro -SimpleMatch | |
if ($exists) { | |
Write-Host "Distribution '$wsldistro' exists. Not overwriting." | |
} else { | |
wsl --import "$wsldistro" "$OutDir" "$OutDir\$imgfile" --version 2 | |
wsl --distribution "$wsldistro" -- bash -c 'apt update && apt upgrade --yes' | |
} | |
wsl --list --verbose | |
} | |
function wgetN { | |
param ( | |
[Parameter(Mandatory = $true)][string]$Uri, | |
[string]$OutDir = $PWD | |
) | |
# $imgfile = Split-Path -Path $Uri -Leaf | |
$OutFile = Join-Path -Path $OutDir -ChildPath $imgfile | |
if (!(Test-Path -LiteralPath $OutFile)) { | |
$resp = Invoke-WebRequest -Uri $Uri -OutFile $OutFile -PassThru | |
# without -PassThru above would be a file object instead of response object with needed headers | |
$lm = $resp.Headers.'Last-Modified' | |
if ($lm) { | |
$rtime = [DateTime]::Parse($lm).ToUniversalTime() | |
(Get-Item $OutFile).LastWriteTime = $rtime | |
} | |
return | |
} | |
$head = Invoke-WebRequest -Uri $Uri -Method HEAD | |
$lm = $head.Headers.'Last-Modified' | |
if ($lm) { | |
$rtime = [DateTime]::Parse($lm).ToUniversalTime() | |
$ltime = (Get-Item $OutFile).LastWriteTime.ToUniversalTime() | |
if ($rtime -gt $ltime) { | |
Invoke-WebRequest -Uri $Uri -OutFile $OutFile | |
(Get-Item $OutFile).LastWriteTime = $rtime | |
} | |
} else { | |
Invoke-WebRequest -Uri $Uri -OutFile $OutFile | |
} | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment