Wsl.ps1
function Connect-Wsl() { param( [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Enter a virtual machine name")] [string] $Name, [switch] $New, [switch] $Docker ) if ($New) { if (-Not($Docker)) { New-Wsl $Name } else { New-Wsl $Name -Docker } } if (-Not(Test-WslRunning $Name)) { Start-Wsl $Name } wsl --distribution $Name --cd '~' --user 'box' } function Get-Wsl() { wsl -l -v } function New-Wsl() { param( [Parameter(Mandatory = $true, HelpMessage = "Enter a virtual machine name")] [string] $Name, [switch] $Docker, [switch] $Update ) $instancePath = Get-WslPath $Name # Check vm not registered if (Test-Path (Join-Path $instancePath "ext4.vhdx") -PathType Leaf ) { Write-Host -ForegroundColor Red "A virtual machine with the name '$Name' already exists." exit } # Get image $imagePath = Get-Ubuntu -Path "wsl/noble/current" -File "ubuntu-noble-wsl-amd64-wsl.rootfs.tar.gz" -Update $Update # Create config file $file = Get-WslConfigFile $Name $userData = Get-WslConfig $Name $Docker $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False [System.IO.File]::WriteAllLines($file, $userData, $Utf8NoBomEncoding) # Create virtual machine if (-Not(Test-Path $instancePath)) { mkdir -Path $instancePath | Out-Null } Write-WslLog $Name Write-Host("Importing ...") -NoNewline if ($Docker) { Write-Host -ForegroundColor Yellow " with docker ..." -NoNewline } Write-Host("") wsl --import $name $instancePath $imagePath } function Remove-Wsl() { param( [Parameter(Mandatory = $true)] [string] $Name ) wsl --unregister $Name $instancePath = Get-WslPath $Name if (Test-Path $instancePath) { Remove-Item -Path $instancePath | Out-Null } $configFile = Get-WslConfigFile $Name if (Test-Path $configFile) { Remove-Item -Path (Get-WslConfigFile $Name) | Out-Null } } function Start-Wsl() { param( [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Enter a virtual machine name")] [string] $Name ) if (Test-WslRunning $Name) { Write-Host -ForegroundColor Yellow "${Name}: Is running!" } else { Write-WslLog $Name Write-Host "Starting ..." -NoNewline wsl --distribution $Name --exec dbus-launch true Write-Host -ForegroundColor Green " Done." } } function Stop-Wsl() { param( [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Enter a virtual machine name")] [string] $Name ) wsl --terminate $Name } ##### Private function Get-WslConfig { param( [string] $Name, [bool] $Docker = $False ) $dockerConfig = @" apt: sources: docker: keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88 source: 'deb [signed-by=`$KEY_FILE] https://download.docker.com/linux/ubuntu `$RELEASE stable' packages: - docker-ce - docker-ce-cli - docker-buildx-plugin - docker-compose-plugin "@ if (-Not($Docker)) { $dockerConfig = "" } $config = @" #cloud-config users: - name: box groups: sudo,docker,users,netdev,audio sudo: ALL=(ALL) NOPASSWD:ALL plain_text_passwd: password lock_passwd: false shell: /bin/bash write_files: - path: /etc/wsl.conf append: true content: | [user] default=box [network] hostname=${Name} generateHosts=false - path: /home/box/.hushlogin owner: box:box defer: true - path: /etc/hostname defer: true content: | ${Name} - path: /etc/hosts content: | 127.0.0.1 localhost 127.0.1.1 ${Name}. ${Name} runcmd: - hostnamectl --transient set-hostname ${Name} ${dockerConfig} "@ return $config } function Get-WslConfigFile { param( [string] $Name ) $configDir = Join-Path $env:USERPROFILE ".cloud-init" if (-Not(Test-Path $configDir)) { mkdir $configDir } $file = Join-Path $configDir "${Name}.user-data" return $file } function Get-WslPath { param( [string] $Name ) $path = Get-BoxPath -Path "wsl" if ($Name) { $path = Join-Path $path $Name } return $path } function Test-WslRunning { param( [string] $Name ) $running = wsl --list --running return $running.Contains($Name) } function Write-WslLog { param( [string] $Name ) Write-Host -ForegroundColor Blue -NoNewline $Name Write-Host ": " -NoNewline } |