Skip to content

Commit

Permalink
added the post install procedure
Browse files Browse the repository at this point in the history
wr0belj committed Apr 7, 2021
1 parent 43749e5 commit 1698bc6
Showing 6 changed files with 90 additions and 18 deletions.
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

Binary file added .vs/easyWSL/v16/.suo
Binary file not shown.
25 changes: 13 additions & 12 deletions easyWSL/DistroInstaller.cs
Original file line number Diff line number Diff line change
@@ -26,16 +26,14 @@ public class autorizationResponse
}


public static void InstallDistro(string distroImage, string distroName, string distroPath, string easyWSLDataDirectory, string easyWSLDirectory)
public static void InstallDistro(string distroImage, string distroName, string distroPath, string easyWSLDataDirectory, string easyWSLDirectory, bool isCustomImageSpecified)
{

void StartProcessSilently(string processName, string processArguments)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = processName;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.Arguments = processArguments;

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

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


else if (chooseToStart.Key == ConsoleKey.N)
Environment.Exit(0);
} while ((chooseToStart.Key != ConsoleKey.Y) & (chooseToStart.Key != ConsoleKey.Enter) & (chooseToStart.Key != ConsoleKey.N));

StartProcessSilently("wsl.exe", $"-d {distroName} \"{postInstallPathLinux}\"");
StartProcessSilently("wsl.exe", $"-t {distroName}");
StartProcessSilently("wsl.exe", $"-d {distroName}");
}

else
StartProcessSilently("wsl.exe", $"-d {distroName}");
}
}
}
8 changes: 5 additions & 3 deletions easyWSL/Program.cs
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ static void Main(string[] args)
int distroNumber = 0;

bool isConversionSuccessful;
bool isCustomImageSpecified = false;

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

if(distroName == "")
{
if ((distroNumber == sources.sources.Count + 1) || (distroNumber == 0))
if ((isCustomImageSpecified == true) || (distroNumber == 0))
Console.Write("A name for your distro: ");
else
Console.Write("A name for your distro (default " + sources.sources[distroNumber - 1].name + "): ");
distroName = Console.ReadLine();

if (distroName == "")
{
if (distroNumber == sources.sources.Count + 1)
if (isCustomImageSpecified == true)
{
while(distroName == "")
{
@@ -141,7 +143,7 @@ static void Main(string[] args)

Directory.CreateDirectory(distroPath);

DistroInstaller.InstallDistro(distroImage, distroName, distroPath, easyWSLDataDirectory, easyWSLDirectory);
DistroInstaller.InstallDistro(distroImage, distroName, distroPath, easyWSLDataDirectory, easyWSLDirectory, isCustomImageSpecified);
}
}
}
3 changes: 3 additions & 0 deletions easyWSL/easyWSL.csproj
Original file line number Diff line number Diff line change
@@ -75,6 +75,9 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="post-install.sh">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="easyWSL.ico" />
69 changes: 69 additions & 0 deletions easyWSL/post-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
echo "Upgrading the system ..."
if command -v apt &> /dev/null; then
apt-get -y update &> /dev/null
apt-get -y install sudo &> /dev/null
fi

if command -v pacman &> /dev/null; then
pacman -Syu --noconfirm &> /dev/null
pacman -S --noconfirm sudo &> /dev/null
fi

if command -v apk &> /dev/null; then
apk update &> /dev/null
apk add sudo &> /dev/null
fi

if command -v rpm &> /dev/null; then
rpm -U &> /dev/null
rpm -i sudo &> /dev/null
fi

if command -v xbps &> /dev/null; then
xbps-install -Su &> /dev/null
xbps-install sudo &> /dev/null
fi

if command -v emerge &> /dev/null; then
emaint -a sync &> /dev/null
emerge-webrsync &> /dev/null
eix-sync &> /dev/null
emerge -a sudo &> /dev/null
fi

add_user()
{
echo "Creating the user account ..."
read -p "Username: " USERNAME
useradd -m -s /bin/bash $USERNAME
while ! passwd $USERNAME; do
echo Try again.
done
printf "$USERNAME ALL=(ALL:ALL) ALL" >> /etc/sudoers
printf "%s\n" "[user]" "default=$USERNAME" >> /etc/wsl.conf
}

set_root_passwd()
{
if ! passwd; then
set_root_passwd
fi
}

echo "Configuring the distribution ..."
pwconv
grpconv
chmod 0744 /etc/shadow
chmod 0744 /etc/gshadow
chown -R root:root /bin/su
chmod 755 /bin/su
chmod u+s /bin/su

while true; do
read -p "Do you want to create a new user with administrator privilages? [y/n]: " yn
case $yn in
[Yy]* ) add_user; break;;
[Nn]* ) set_root_passwd; break;;
esac
done

0 comments on commit 1698bc6

Please sign in to comment.