Param ( [Parameter(Mandatory = $True)][ValidateNotNull()][string]$wslName, [Parameter(Mandatory = $True)][ValidateNotNull()][string]$fedoraRelVer, [Parameter(Mandatory = $True)][ValidateNotNull()][string]$installationPath, [Parameter(Mandatory = $True)][ValidateNotNull()][string]$username ) # Check if required commands/programs are installed if (-Not (Get-Command 7z.exe 2>$null)) { Write-Output "💥 Cannot find 7z.exe. Please install 7zip first." Write-Output "Try 'winget install 7zip.7zip'" Exit 55 } if (-Not (Get-Command wsl.exe 2>$null)) { Write-Output "🚧 WSL not found. No worries, we'll enable it..." dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart } # Create staging directory if it does not exist if (-Not (Test-Path -Path "$HOME\Downloads\staging")) { $dir = mkdir "$HOME\Downloads\staging" } # Download Fedora $fedoraRelVer image from GitHub repo to staging dir try { Write-Output "⚡️ Downloading Fedora $fedoraRelVer image from GitHub..." $baseUri = "https://github.com/fedora-cloud/docker-brew-fedora/raw/" $tailUri = $fedoraRelVer + "/x86_64/fedora-" + $fedoraRelVer + "-x86_64.tar.xz" $fullUri = $baseUri + $tailUri Invoke-WebRequest -Uri $fullUri -UseBasicParsing -OutFile "$dir\fedora-$fedoraRelVer.tar.xz" } catch { Write-Output "💥 Error downloading Fedora $fedoraRelVer image:" Write-Output $_.ErrorDetails exit 1 } # Extract tar archive to staging dir try { Write-Output "⚡️ Decompressing tar archive..." 7z e "$dir\fedora-$fedoraRelVer.tar.xz" -o"$dir" | Out-Null } catch { Write-Output "💥 Error decompressing $dir\fedora-$fedoraRelVer.tar.xz" Write-Output $_.ErrorDetails exit 1 } # Import tar archive as WSL distro try { # Ensure installation path exists if (-Not (Test-Path -Path $installationPath)) { mkdir $installationPath } Write-Output "✨ Importing Fedora $fedoraRelVer as WSL distro..." wsl.exe --import $wslName $installationPath "$dir\fedora-$fedoraRelVer.tar" } catch { Write-Output "💥 Error importing Fedora $fedoraRelVer as WSL distro" Write-Output $_.ErrorDetails exit 1 } # Clean up staging directory Write-Output "🗑️ Cleaning up staging directory..." Remove-Item -Recurse -Force "$dir" # Update the Fedora system and install basic utils try { Write-Output "✨ Updating Fedora $fedoraRelVer..." wsl -d $wslName -u root bash -ic "printf 'fastestmirror=True\ndeltarpm=True\nmax_parallel_downloads=10\n' | tee -a /etc/dnf/dnf.conf" wsl -d $wslName -u root bash -ic "dnf update -y" } catch { Write-Output "💥 Error updating system" Write-Output $_.ErrorDetails exit 1 } # Install basic utilities try { Write-Output "✨ Installing basic utilities..." wsl -d $wslName -u root bash -ic "dnf install -y wget curl sudo ncurses dnf-plugins-core dnf-utils passwd" wsl -d $wslName -u root bash -ic "dnf copr enable trustywolf/wslu" } catch { Write-Output "💥 Error installing basic utilities" Write-Output $_.ErrorDetails exit 1 } # Add user try { Write-Output "✨ Creating user $username..." wsl -d $wslName -u root bash -ic "useradd -G wheel -m -s /bin/bash -p temporary -U $username" } catch { Write-Output "💥 Error creating user" Write-Output $_.ErrorDetails exit 1 } # Ensure WSL distro is restarted when first used try { Write-Output "✨ Restarting Fedora $fedoraRelVer WSL distro..." wsl -t $wslName } catch { Write-Output "💥 Error restarting WSL distro" Write-Output $_.ErrorDetails exit 1 } # Set Windows registry property to set the default user for this WSL instance try { Write-Output "✨ Setting Windows registry property..." Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq $wslName | Set-ItemProperty -Name DefaultUid -Value 1000 } catch { Write-Output "💥 Error setting Windows registry property" Write-Output $_.ErrorDetails exit 1 }