Skip to content

Commit 1698bc6

Browse files
committed
added the post install procedure
1 parent 43749e5 commit 1698bc6

File tree

6 files changed

+90
-18
lines changed

6 files changed

+90
-18
lines changed

.gitignore

-3
This file was deleted.

.vs/easyWSL/v16/.suo

73 KB
Binary file not shown.

easyWSL/DistroInstaller.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ public class autorizationResponse
2626
}
2727

2828

29-
public static void InstallDistro(string distroImage, string distroName, string distroPath, string easyWSLDataDirectory, string easyWSLDirectory)
29+
public static void InstallDistro(string distroImage, string distroName, string distroPath, string easyWSLDataDirectory, string easyWSLDirectory, bool isCustomImageSpecified)
3030
{
3131

3232
void StartProcessSilently(string processName, string processArguments)
3333
{
3434
ProcessStartInfo psi = new ProcessStartInfo();
3535
psi.FileName = processName;
3636
psi.UseShellExecute = false;
37-
psi.RedirectStandardError = true;
38-
psi.RedirectStandardOutput = true;
3937
psi.Arguments = processArguments;
4038

4139
Process proc = Process.Start(psi);
@@ -158,19 +156,22 @@ void GetRequestWithHeaderToFile(string url, string token, string type, string fi
158156
Console.WriteLine("Cleaning up ...");
159157
Directory.Delete(layersDirectory, true);
160158

161-
Console.Write($"Do you want to start {distroName} distribution? [Y/n]:");
162-
ConsoleKeyInfo chooseToStart = Console.ReadKey();
163-
do
159+
if (isCustomImageSpecified == false)
164160
{
165-
if ((chooseToStart.Key == ConsoleKey.Y) ^ (chooseToStart.Key == ConsoleKey.Enter))
166-
Process.Start("wsl.exe", $"-d {distroName}");
161+
string postInstallPathWindows = $"{easyWSLDirectory}\\post-install.sh";
162+
string postInstallPathLinux = postInstallPathWindows.Replace(@"\", "/");
163+
char windowsDriveLetter = Char.ToLower(postInstallPathLinux[0]);
164+
postInstallPathLinux = postInstallPathLinux.Remove(0, 2);
165+
postInstallPathLinux = $"/mnt/{windowsDriveLetter}{postInstallPathLinux}";
167166

168167

169-
else if (chooseToStart.Key == ConsoleKey.N)
170-
Environment.Exit(0);
171-
} while ((chooseToStart.Key != ConsoleKey.Y) & (chooseToStart.Key != ConsoleKey.Enter) & (chooseToStart.Key != ConsoleKey.N));
172-
168+
StartProcessSilently("wsl.exe", $"-d {distroName} \"{postInstallPathLinux}\"");
169+
StartProcessSilently("wsl.exe", $"-t {distroName}");
170+
StartProcessSilently("wsl.exe", $"-d {distroName}");
171+
}
173172

173+
else
174+
StartProcessSilently("wsl.exe", $"-d {distroName}");
174175
}
175176
}
176177
}

easyWSL/Program.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static void Main(string[] args)
3636
int distroNumber = 0;
3737

3838
bool isConversionSuccessful;
39+
bool isCustomImageSpecified = false;
3940

4041
string easyWSLDirectory = (Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).Remove(0, 6);
4142
string easyWSLDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "easyWSL");
@@ -90,6 +91,7 @@ static void Main(string[] args)
9091
Console.Write("Specify a docker container: ");
9192
distroImage = Console.ReadLine();
9293
}
94+
isCustomImageSpecified = true;
9395
}
9496
else
9597
{
@@ -99,15 +101,15 @@ static void Main(string[] args)
99101

100102
if(distroName == "")
101103
{
102-
if ((distroNumber == sources.sources.Count + 1) || (distroNumber == 0))
104+
if ((isCustomImageSpecified == true) || (distroNumber == 0))
103105
Console.Write("A name for your distro: ");
104106
else
105107
Console.Write("A name for your distro (default " + sources.sources[distroNumber - 1].name + "): ");
106108
distroName = Console.ReadLine();
107109

108110
if (distroName == "")
109111
{
110-
if (distroNumber == sources.sources.Count + 1)
112+
if (isCustomImageSpecified == true)
111113
{
112114
while(distroName == "")
113115
{
@@ -141,7 +143,7 @@ static void Main(string[] args)
141143

142144
Directory.CreateDirectory(distroPath);
143145

144-
DistroInstaller.InstallDistro(distroImage, distroName, distroPath, easyWSLDataDirectory, easyWSLDirectory);
146+
DistroInstaller.InstallDistro(distroImage, distroName, distroPath, easyWSLDataDirectory, easyWSLDirectory, isCustomImageSpecified);
145147
}
146148
}
147149
}

easyWSL/easyWSL.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
</ItemGroup>
7676
<ItemGroup>
7777
<None Include="App.config" />
78+
<None Include="post-install.sh">
79+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
80+
</None>
7881
</ItemGroup>
7982
<ItemGroup>
8083
<Content Include="easyWSL.ico" />

easyWSL/post-install.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
echo "Upgrading the system ..."
2+
if command -v apt &> /dev/null; then
3+
apt-get -y update &> /dev/null
4+
apt-get -y install sudo &> /dev/null
5+
fi
6+
7+
if command -v pacman &> /dev/null; then
8+
pacman -Syu --noconfirm &> /dev/null
9+
pacman -S --noconfirm sudo &> /dev/null
10+
fi
11+
12+
if command -v apk &> /dev/null; then
13+
apk update &> /dev/null
14+
apk add sudo &> /dev/null
15+
fi
16+
17+
if command -v rpm &> /dev/null; then
18+
rpm -U &> /dev/null
19+
rpm -i sudo &> /dev/null
20+
fi
21+
22+
if command -v xbps &> /dev/null; then
23+
xbps-install -Su &> /dev/null
24+
xbps-install sudo &> /dev/null
25+
fi
26+
27+
if command -v emerge &> /dev/null; then
28+
emaint -a sync &> /dev/null
29+
emerge-webrsync &> /dev/null
30+
eix-sync &> /dev/null
31+
emerge -a sudo &> /dev/null
32+
fi
33+
34+
add_user()
35+
{
36+
echo "Creating the user account ..."
37+
read -p "Username: " USERNAME
38+
useradd -m -s /bin/bash $USERNAME
39+
while ! passwd $USERNAME; do
40+
echo Try again.
41+
done
42+
printf "$USERNAME ALL=(ALL:ALL) ALL" >> /etc/sudoers
43+
printf "%s\n" "[user]" "default=$USERNAME" >> /etc/wsl.conf
44+
}
45+
46+
set_root_passwd()
47+
{
48+
if ! passwd; then
49+
set_root_passwd
50+
fi
51+
}
52+
53+
echo "Configuring the distribution ..."
54+
pwconv
55+
grpconv
56+
chmod 0744 /etc/shadow
57+
chmod 0744 /etc/gshadow
58+
chown -R root:root /bin/su
59+
chmod 755 /bin/su
60+
chmod u+s /bin/su
61+
62+
while true; do
63+
read -p "Do you want to create a new user with administrator privilages? [y/n]: " yn
64+
case $yn in
65+
[Yy]* ) add_user; break;;
66+
[Nn]* ) set_root_passwd; break;;
67+
esac
68+
done
69+

0 commit comments

Comments
 (0)