forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 6
Installation and Automatic updater
Daniel Llewellyn edited this page Sep 10, 2020
·
4 revisions
To install the kernel, run the following in a PowerShell window, which will also set-up a task to check for updated kernels and download them automatically when you login. The script only needs to be run once, so you don't need to save it to a file. Any new users added to the system after this is configured will also get their WSL2 instances set-up to use the custom kernel without requiring them to set up their own scheduled tasks.
You can also save the script below into a file named something like install-kernel.ps1
and then run it with powershell.exe -executionpolicy bypass install-kernel.ps1
Start-Process -Verb RunAs -FilePath powershell.exe -ArgumentList {
Register-ScheduledJob -Name UpdateWSL2CustomKernel -Trigger (New-JobTrigger -AtLogOn) -ScheduledJobOption (New-ScheduledJobOption -RequireNetwork) -RunNow -ScriptBlock {
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 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 ""
}
}
}
$latest = (Invoke-RestMethod -Uri https://api.github.com/repos/diddlesnaps/WSL2-Linux-Kernel/releases -UseBasicParsing)[0]
$latest_version = [version]$latest.tag_name.Remove($latest.tag_name.IndexOf('-microsoft-snapd'))
$current_version = [version](Get-Content $env:APPDATA/wsl2-custom-kernel-version.txt)
if ($latest_version -gt $current_version) {
$assets = $latest.assets | Where-Object {$_.name -Like '*-x86_64'}
Invoke-WebRequest -Uri $assets.browser_download_url -OutFile $env:APPDATA/wsl2-custom-kernel
if ($?) {
Move-Item $env:APPDATA/wsl2-custom-kernel.tmp $env:APPDATA/wsl2-custom-kernel -Force
$latest_version | Set-Content $env:APPDATA/wsl2-custom-kernel-version.txt
$wslconfig = @{'wsl2'=@{'kernel'=''}}
if (TestPath($env:USERPROFILE/.wslconfig)) {
$wslconfig = Get-IniContent $env:USERPROFILE/.wslconfig
}
$wslconfig.wsl2.kernel = "$env:APPDATA\wsl2-custom-kernel".Replace('\', '\\')
Out-IniFile $wslconfig $env:USERPROFILE/.wslconfig
}
}
}
}