hyperreal revised this gist . Go to revision
1 file changed, 126 insertions
installFedoraWSL.ps1(file created)
@@ -0,0 +1,126 @@ | |||
1 | + | Param ( | |
2 | + | [Parameter(Mandatory = $True)][ValidateNotNull()][string]$wslName, | |
3 | + | [Parameter(Mandatory = $True)][ValidateNotNull()][string]$fedoraRelVer, | |
4 | + | [Parameter(Mandatory = $True)][ValidateNotNull()][string]$installationPath, | |
5 | + | [Parameter(Mandatory = $True)][ValidateNotNull()][string]$username | |
6 | + | ) | |
7 | + | ||
8 | + | # Check if required commands/programs are installed | |
9 | + | if (-Not (Get-Command 7z.exe 2>$null)) { | |
10 | + | Write-Output "💥 Cannot find 7z.exe. Please install 7zip first." | |
11 | + | Write-Output "Try 'winget install 7zip.7zip'" | |
12 | + | Exit 55 | |
13 | + | } | |
14 | + | ||
15 | + | if (-Not (Get-Command wsl.exe 2>$null)) { | |
16 | + | Write-Output "🚧 WSL not found. No worries, we'll enable it..." | |
17 | + | dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart | |
18 | + | dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart | |
19 | + | } | |
20 | + | ||
21 | + | # Create staging directory if it does not exist | |
22 | + | if (-Not (Test-Path -Path "$HOME\Downloads\staging")) { $dir = mkdir "$HOME\Downloads\staging" } | |
23 | + | ||
24 | + | # Download Fedora $fedoraRelVer image from GitHub repo to staging dir | |
25 | + | try { | |
26 | + | Write-Output "⚡️ Downloading Fedora $fedoraRelVer image from GitHub..." | |
27 | + | $baseUri = "https://github.com/fedora-cloud/docker-brew-fedora/raw/" | |
28 | + | $tailUri = $fedoraRelVer + "/x86_64/fedora-" + $fedoraRelVer + "-x86_64.tar.xz" | |
29 | + | $fullUri = $baseUri + $tailUri | |
30 | + | Invoke-WebRequest -Uri $fullUri -UseBasicParsing -OutFile "$dir\fedora-$fedoraRelVer.tar.xz" | |
31 | + | } | |
32 | + | catch { | |
33 | + | Write-Output "💥 Error downloading Fedora $fedoraRelVer image:" | |
34 | + | Write-Output $_.ErrorDetails | |
35 | + | exit 1 | |
36 | + | } | |
37 | + | ||
38 | + | # Extract tar archive to staging dir | |
39 | + | try { | |
40 | + | Write-Output "⚡️ Decompressing tar archive..." | |
41 | + | 7z e "$dir\fedora-$fedoraRelVer.tar.xz" -o"$dir" | Out-Null | |
42 | + | } | |
43 | + | catch { | |
44 | + | Write-Output "💥 Error decompressing $dir\fedora-$fedoraRelVer.tar.xz" | |
45 | + | Write-Output $_.ErrorDetails | |
46 | + | exit 1 | |
47 | + | } | |
48 | + | ||
49 | + | # Import tar archive as WSL distro | |
50 | + | try { | |
51 | + | # Ensure installation path exists | |
52 | + | if (-Not (Test-Path -Path $installationPath)) { | |
53 | + | mkdir $installationPath | |
54 | + | } | |
55 | + | ||
56 | + | Write-Output "✨ Importing Fedora $fedoraRelVer as WSL distro..." | |
57 | + | wsl.exe --import $wslName $installationPath "$dir\fedora-$fedoraRelVer.tar" | |
58 | + | } | |
59 | + | catch { | |
60 | + | Write-Output "💥 Error importing Fedora $fedoraRelVer as WSL distro" | |
61 | + | Write-Output $_.ErrorDetails | |
62 | + | exit 1 | |
63 | + | } | |
64 | + | ||
65 | + | # Clean up staging directory | |
66 | + | Write-Output "🗑️ Cleaning up staging directory..." | |
67 | + | Remove-Item -Recurse -Force "$dir" | |
68 | + | ||
69 | + | # Update the Fedora system and install basic utils | |
70 | + | try { | |
71 | + | Write-Output "✨ Updating Fedora $fedoraRelVer..." | |
72 | + | wsl -d $wslName -u root bash -ic "printf 'fastestmirror=True\ndeltarpm=True\nmax_parallel_downloads=10\n' | tee -a /etc/dnf/dnf.conf" | |
73 | + | wsl -d $wslName -u root bash -ic "dnf update -y" | |
74 | + | } | |
75 | + | catch { | |
76 | + | Write-Output "💥 Error updating system" | |
77 | + | Write-Output $_.ErrorDetails | |
78 | + | exit 1 | |
79 | + | } | |
80 | + | ||
81 | + | # Install basic utilities | |
82 | + | try { | |
83 | + | Write-Output "✨ Installing basic utilities..." | |
84 | + | wsl -d $wslName -u root bash -ic "dnf install -y wget curl sudo ncurses dnf-plugins-core dnf-utils passwd" | |
85 | + | wsl -d $wslName -u root bash -ic "dnf copr enable trustywolf/wslu" | |
86 | + | } | |
87 | + | catch { | |
88 | + | Write-Output "💥 Error installing basic utilities" | |
89 | + | Write-Output $_.ErrorDetails | |
90 | + | exit 1 | |
91 | + | } | |
92 | + | ||
93 | + | # Add user | |
94 | + | try { | |
95 | + | Write-Output "✨ Creating user $username..." | |
96 | + | wsl -d $wslName -u root bash -ic "useradd -G wheel -m -s /bin/bash -p temporary -U $username" | |
97 | + | } | |
98 | + | catch { | |
99 | + | Write-Output "💥 Error creating user" | |
100 | + | Write-Output $_.ErrorDetails | |
101 | + | exit 1 | |
102 | + | } | |
103 | + | ||
104 | + | # Ensure WSL distro is restarted when first used | |
105 | + | try { | |
106 | + | Write-Output "✨ Restarting Fedora $fedoraRelVer WSL distro..." | |
107 | + | wsl -t $wslName | |
108 | + | } | |
109 | + | catch { | |
110 | + | Write-Output "💥 Error restarting WSL distro" | |
111 | + | Write-Output $_.ErrorDetails | |
112 | + | exit 1 | |
113 | + | } | |
114 | + | ||
115 | + | # Set Windows registry property to set the default user for this WSL instance | |
116 | + | try { | |
117 | + | Write-Output "✨ Setting Windows registry property..." | |
118 | + | Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | | |
119 | + | Where-Object -Property DistributionName -eq $wslName | | |
120 | + | Set-ItemProperty -Name DefaultUid -Value 1000 | |
121 | + | } | |
122 | + | catch { | |
123 | + | Write-Output "💥 Error setting Windows registry property" | |
124 | + | Write-Output $_.ErrorDetails | |
125 | + | exit 1 | |
126 | + | } |
Newer
Older