Skip to content

Commit a1beeb9

Browse files
committed
use Powershell Workflow
1 parent 4f347f7 commit a1beeb9

File tree

6 files changed

+225
-22
lines changed

6 files changed

+225
-22
lines changed

README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
# easyWSL - have your own custom WSL distro easily
1+
<h1 align="center"> easyWSL</h1> <br>
2+
<p align="center">
3+
<a>
4+
<img src="easyWSL.png" width="450">
5+
</a>
6+
</p>
27

3-
![wsl -l -v](wsl%20-l%20-v.png)
8+
<p align="center">
9+
Create custom WSL distros and manage them easily
10+
</p>
411

5-
This is a tutorial how to easily have custom WSL distro, for example Arch Linux.
6-
Change distro and directories if you want.
12+
## Instructions
713

8-
> I recommend using Windows Terminal, remember to use preview version of App Installer in order to use `winget` functionality.
9-
10-
1. Change your ExecutionPolicy.
11-
1. Open Powershell as an administrator.
12-
2. `Set-ExecutionPolicy Unrestricted`
13-
2. Clone and go to the dir. of this repository.
14+
1. Clone and go to the dir. of this repository.
1415
1. `git clone https://github.com/Unrooted/easyWSL`
1516
2. `cd easyWSL`
16-
3. Run Powershell script. Click `Yes` and install Docker just by doing what an installator says.
17-
1. `&"C:\PATH\TO\CLONED\REPO\easyWSL\easyWSLPowershellPart.ps1"`
18-
4. If your Docker container boots up, run:
19-
1. `cd /mnt/c/PATH/TO/CLONED/REPO/easyWSL && ./easyWSLLinuxPart.sh`
20-
5. Docker container will exit itself. Let the Powershell finish the job.
21-
22-
> You can also run first three steps commands in one line, but just to have a clarity in this README I decided to put them separatly.
23-
24-
If you want to run your freshly created WSL, just go to your Powershell and type `wsl -d wslarchlinux` and enjoy it!
25-
If you got bored with your new WSL, you can `wsl.exe --unregister wslarchlinux`.
17+
2. Change the post-installation script already. Use commands dependent on the container of your choice.
18+
3. Run Powershell script as an administrator.
19+
4. Click `Yes` and install Docker, continuing with the installation instructions from the .exe
20+
5. Your system needs to be rebooted. After a reboot, the script will continue to run.
21+
6. After the script is done, everything is done! Feel free and enjoy your Docker-container-based WSL distro!

easyWSL.png

7.51 KB
Loading

easyWSL.ps1

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
Import-Module PSWorkflow
2+
function Show-Menu {
3+
Clear-Host
4+
Write-Host " _ _______ __ "
5+
Write-Host " ___ ____ ________ _| | / / ___// / "
6+
Write-Host " / _ \/ __ `/ ___/ / / / | /| / /\__ \/ / "
7+
Write-Host " / __/ /_/ (__ ) /_/ /| |/ |/ /___/ / /___"
8+
Write-Host " \___/\__,_/____/\__, / |__/|__//____/_____/"
9+
Write-Host " /____/ "
10+
11+
Write-Host "1: Install Distro"
12+
Write-Host "2: Show currently installed distros"
13+
Write-Host "3: Unregister Distro"
14+
Write-Host "H: Help"
15+
Write-Host "Q: Quit"
16+
}
17+
18+
Show-Menu
19+
$selection = Read-Host "Choose what you want to do"
20+
switch ($selection) {
21+
22+
"1" {
23+
$DistroName = Read-Host "Name of your distro"
24+
if ($DistroName) {
25+
Write-Host "You've choosen [$DistroName] as the name for your distro"
26+
} else {
27+
Write-Warning -Message "No name input."
28+
}
29+
$DockerContainer = Read-Host "Docker Hub repo name (for example: archlinux:latest)"
30+
if ($DockerContainer) {
31+
Write-Host "You've choosen [$DockerContainer] as your Docker container"
32+
} else {
33+
Write-Warning -Message "No Docker container name input."
34+
}
35+
}
36+
37+
"2" {
38+
wsl.exe -l -v
39+
}
40+
41+
"3" {
42+
wsl.exe -l
43+
Read-Host "Name of your distro that you want to unregister"
44+
if ($UnregisterDistro) {
45+
Write-Host "You've choosen [$UnregisterDistro] as the name for your distro"
46+
} else {
47+
Write-Warning -Message "No name input."
48+
}
49+
}
50+
51+
"H" {
52+
Start-Process "https://github.com/redcode-labs/easyWSL/blob/master/README.md"
53+
exit
54+
}
55+
56+
"q" {
57+
exit
58+
}
59+
"Q" {
60+
exit
61+
}
62+
default {
63+
Write-Host "Not a correct command"
64+
Show-Menu
65+
$selection = Read-Host "Choose what you want to do"
66+
switch ($selection) {
67+
68+
"1" {
69+
$DistroName = Read-Host "Name of your distro"
70+
if ($DistroName) {
71+
Write-Host "You've choosen [$DistroName] as the name for your distro"
72+
} else {
73+
Write-Warning -Message "No name input."
74+
}
75+
$DockerContainer = Read-Host "Docker Hub repo name (for example: archlinux:latest)"
76+
if ($DockerContainer) {
77+
Write-Host "You've choosen [$DockerContainer] as your Docker container"
78+
} else {
79+
Write-Warning -Message "No Docker container name input."
80+
}
81+
}
82+
83+
"2" {
84+
wsl.exe -l -v
85+
}
86+
87+
"3" {
88+
wsl.exe -l
89+
Read-Host "Name of your distro that you want to unregister"
90+
if ($UnregisterDistro) {
91+
Write-Host "You've choosen [$UnregisterDistro] as the name for your distro"
92+
} else {
93+
Write-Warning -Message "No name input."
94+
}
95+
}
96+
97+
"H" {
98+
Start-Process "https://github.com/redcode-labs/easyWSL/blob/master/README.md"
99+
exit
100+
}
101+
102+
"q" {
103+
exit
104+
}
105+
"Q" {
106+
exit
107+
}
108+
default {
109+
exit
110+
}
111+
}
112+
}
113+
}
114+
function ChooseInstallationDirectory {
115+
do
116+
{
117+
$script:DistroInstallationDirectory = Read-Host -Prompt "Type the path to directory where you want to store all your custom made distros "
118+
if (($script:DistroInstallationDirectory.Substring($script:DistroInstallationDirectory.Length-1) -eq "\")) {
119+
$script:DistroInstallationDirectory = $script:DistroInstallationDirectory.Substring(0,$script:DistroInstallationDirectory.Length-1)
120+
}
121+
122+
$script:DistroDir = $script:DistroInstallationDirectory+"\"+$script:DistroName
123+
if (-Not (Test-Path -Path $script:DistroInstallationDirectory)) {write-Host "This is not a correct path!"}
124+
} while (-Not (Test-Path -Path $script:DistroInstallationDirectory))
125+
126+
}
127+
128+
129+
ChooseInstallationDirectory
130+
131+
132+
$confirmation = Read-Host "Are you sure you want to install" $DistroName "to path" $DistroDir "[y/n]"
133+
while($confirmation -eq "n")
134+
{
135+
if ($confirmation -eq 'y') {break}
136+
ChooseInstallationDirectory
137+
$confirmation = Read-Host "Are you sure you want to install" $DistroName "to path" $DistroDir"? [y/n]"
138+
}
139+
140+
$confirmation = Read-Host "Do you want to set" $DistroName "as a default wsl distro? [y/n]"
141+
if ($confirmation -eq 'y') {
142+
$DefaultDistro = "true"
143+
}
144+
else {
145+
$DefaultDistro = "false"
146+
}
147+
148+
149+
$DockerWingetPackageName = "Docker.DockerDesktop"
150+
$DockerContainerLocal = "wsl"+$DistroName
151+
$DockerExportTarName = "install.tar"
152+
153+
154+
workflow Install-easyWSL {
155+
param (
156+
$DockerWingetPackageName,
157+
$DistroInstallationDirectory,
158+
$DistroName,
159+
$DockerExportTarName,
160+
$DistroDir,
161+
$DockerContainerLocal,
162+
$DockerContainer
163+
)
164+
165+
Set-ExecutionPolicy Unrestricted
166+
167+
# check if Docker is installed on a machine, if not install it
168+
if (-Not (Get-Command docker -errorAction SilentlyContinue))
169+
{
170+
if (Get-Command winget -errorAction SilentlyContinue)
171+
{
172+
winget install -e --id Docker.DockerDesktop
173+
} else {
174+
Invoke-WebRequest -Uri "https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe" -OutFile $env:USERPROFILE"\Downloads"
175+
Start-Process -Wait "c:\Users\$env:USERNAME\Downloads\Docker Desktop Installer.exe"
176+
}
177+
}
178+
179+
# ask user to confirm rebooting machine
180+
$confirmation = Read-Host "Your computer will have to be rebooted. Are you ready to do that now? [y/n]"
181+
do
182+
{
183+
if (-Not ($confirmation -eq 'y')) {
184+
$confirmation = Read-Host "Maybe now? [y/n]"
185+
}
186+
} while($confirmation -eq "n")
187+
188+
Restart-Computer -Wait
189+
190+
# create a directory for the distribution inside a user specified directory
191+
New-Item -Path $DistroInstallationDirectory -Name $DistroName -ItemType "directory"
192+
193+
# run the docker container with a chosen distro and extract the rootfs to the .tar file located in a distribution directory
194+
docker run -it --name $DockerContainerLocal $DockerContainer
195+
docker export --output=$DistroDir+"\"+$DockerExportTarName $DockerContainerLocal
196+
197+
# register the distribution in wsl
198+
wsl.exe --import $DistroName $DistroDir $DistroDir+"\"+$DockerExportTarName
199+
200+
#set the distro default in wsl if the user specified that he want do it
201+
if($DefaultDistro -eq "true") {
202+
wsl.exe --set-default $DistroName
203+
}
204+
205+
# post setup, linux commands in here are specified to be for Arch-based systems
206+
wsl.exe --distro $DistroName "pacman -Syu -y && pacman -S sudo -y && pacman -S vim -y && echo export EDITOR=/usr/bin/vim >> ~/.bashrc && pwconv && grpconv && chmod 0744 /etc/shadow && chmod 0744 /etc/gshadow && exit"
207+
}
208+
209+
Install-easyWSL $DockerWingetPackageName $DistroInstallationDirectory $DistroName $DockerExportTarName $DistroDir $DockerContainerLocal $DockerContainer

easyWSLLinuxPart.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

easyWSLPowershellPart.ps1

Lines changed: 0 additions & 1 deletion
This file was deleted.

wsl -l -v.png

-11.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)