Skip to content

Instantly share code, notes, and snippets.

@trzecieu
Last active June 29, 2019 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save trzecieu/f69d30c391e13aa5224c15933f00a532 to your computer and use it in GitHub Desktop.
Save trzecieu/f69d30c391e13aa5224c15933f00a532 to your computer and use it in GitHub Desktop.
Automates process of installation and configuration of WSL
@echo off
echo - This scripts automates process of installing Windows Subsystem for Linux (WSL)
REM Due some limitations, this script has to be can't be executed under PowerShell
Get-ChildItem >nul 2>&1
if %errorLevel% == 0 (
echo # Swich to CMD
cmd /c start "" %0
exit 0
)
echo ------------------
echo During this script you will be asked few times to input some data, stay tuned.
echo ------------------
echo Do you want to install PulseAudio Windows service?
echo It will allow you to hear a music from Linux world
set INSTALL_PULSE=
set /P INSTALL_PULSE=Install and register pulseaudio? (y/n): %=%
echo ------------------
echo Do you want to install XmingServer service?
echo It allows to connect to WSL's XServer
set INSTALL_XMING=
set /P INSTALL_XMING=Install and register XmingServer? (y/n): %=%
REM --------------------------------
set BASH=C:\Windows\System32\bash.exe
if not exist %BASH% call :install_wsl
call :get_linux_user
echo Main Linux user is: %linux_user%
call :fix_wsl
call :install_base_packages
call :fix_libpulse0
If /I "%INSTALL_PULSE%"=="y" call :install_pulse
If /I "%INSTALL_XMING%"=="y" call :install_xserver
echo -
echo - Done
pause
goto :eof
:check_permissions
net session >nul 2>&1
if %errorLevel% == 0 (
goto :eof
) else (
call :missing_admin
)
goto :eof
:get_linux_user
%BASH% -c "whoami > ./tmpFile"
if not %errorLevel% == 0 call :missing_admin
set /p linux_user= < tmpFile
del tmpFile
goto :eof
goto :eof
:install_pulse
REM installs third party soft what allows to connect to Linux subsystem and pass audio stream to Windows.
SET "LINUXTMP=$(echo '%TMP:\=\\%' | sed -e 's|\\|/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/mnt/\L\1\E/\2|')"
echo LINUXTMP = "%LINUXTMP%"
ECHO --- PulseAudio...
%BASH% -c "rm -fr '%LINUXTMP%/pulseaudio.zip'"
%BASH% -c "wget -cO '%LINUXTMP%/pulseaudio.zip' 'http://bosmans.ch/pulseaudio/pulseaudio-1.1.zip'"
ECHO --- Extracting PulseAudio
md "%TMP%\pulseaudio"
%BASH% -c "unzip -o '%LINUXTMP%/pulseaudio.zip' -d '%LINUXTMP%/pulseaudio'"
ECHO --- Installing PulseAudio
xcopy /e /Y "%TMP%\pulseaudio" "%AppData%\PulseAudio\"
ECHO --- Setting PulseAudio to run at startup
echo set ws=wscript.createobject("wscript.shell") > "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_pulseaudio.vbe"
echo ws.run "%AppData%\PulseAudio\bin\pulseaudio.exe --exit-idle-time=-1",0 >> "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_pulseaudio.vbe"
REM Recomended/required settings
echo load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1 >> "%AppData%\PulseAudio\etc\pulse\default.pa"
ECHO --- Setting PulseAudio
"%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_pulseaudio.vbe"
ECHO When prompted, DO NOT allow 'pulseaudio' access to any of your networks. It doesn't need access.
goto :eof
:install_xserver
REM installs third party soft what allows to connect to Linux subsystem and pass audio stream to Windows.
SET "LINUXTMP=$(echo '%TMP:\=\\%' | sed -e 's|\\|/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/mnt/\L\1\E/\2|')"
echo LINUXTMP = "%LINUXTMP%"
ECHO --- XServer ...
%BASH% -c "rm -fr '%LINUXTMP%/xserver.exe'"
%BASH% -c "wget -cO '%LINUXTMP%/xserver.exe' 'http://downloads.sourceforge.net/project/xming/Xming/6.9.0.31/Xming-6-9-0-31-setup.exe'"
%TMP%\xserver.exe /NORESTART /SILENT /SUPPRESSMSGBOXES /DIR="%AppData%\Xming\"
ECHO --- Setting Xming to run at startup
echo set ws=wscript.createobject("wscript.shell") > "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_xming.vbe"
echo ws.run "%AppData%\Xming\Xming.exe -multiwindow",0 >> "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_xming.vbe"
ECHO --- Setting Xming
"%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start_xming.vbe"
goto :eof
:install_wsl
call :check_permissions
echo -- Enable developer mode in Windows
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"
echo -- Install WSL
powershell -noprofile -command "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux"
echo -- Install Ubuntu
lxrun /install /y
echo
echo - Please Reboot your computer before continue
set /p in= Do you want to reboot now? (y/n)
if "%in%" == "y" (
shutdown -r -f -t 10
)
exit 0
:wsl_start_sudo
if not "%linux_user%" == "root" (
echo -- Switch linux user to root
lxrun /setdefaultuser root
)
goto :eof
:wsl_stop_sudo
if not "%linux_user%" == "root" (
echo -- Switch linux user to `%linux_user%`
lxrun /setdefaultuser %linux_user%
)
goto :eof
:fix_wsl
echo -- Fix WSL installation
call :wsl_start_sudo
echo -- Fix settings for root user
call :fix_user_wsl_settings
echo --- Fix sudo command
%BASH% -c "if cat /etc/hosts | grep `hostname` >> /dev/null ; then echo OK; else echo '127.0.0.1 `hostname`' >> /etc/hosts; fi"
REM %BASH% -c "echo '127.0.0.1 `hostname`' >> /etc/hosts"
echo --- Update packages
%BASH% -c "apt update && apt -y upgrade"
call :wsl_stop_sudo
if not "%linux_user%" == "root" (
echo -- Fix settings for main user
call :fix_user_wsl_settings
)
goto :eof
:fix_user_wsl_settings
echo --- Fix default language
%BASH% -c "if cat ~/.bashrc | grep LANG=en_US.utf8 >> /dev/null ; then echo OK; else echo LANG=en_US.utf8 >> ~/.bashrc; fi"
REM %BASH% -c "echo LANG=en_US.utf8 >> ~/.bashrc"
echo --- Setup default X display
%BASH% -c "if cat ~/.bashrc | grep 'export DISPLAY=:0.0' >> /dev/null ; then echo OK; else echo 'export DISPLAY=:0.0' >> ~/.bashrc; fi"
REM %BASH% -c "echo 'export DISPLAY=:0.0' >> ~/.bashrc"
goto :eof
:install_base_packages
echo -- Install base packages
call :wsl_start_sudo
%BASH% -c "apt install -y unzip git-core zsh vim-gnome"
call :wsl_stop_sudo
goto :eof
:fix_libpulse0
call :wsl_start_sudo
echo -- Fix pulse audio
%BASH% -c "add-apt-repository ppa:therealkenc/wsl-pulseaudio -y"
%BASH% -c "apt-get update && apt-get upgrade && apt-get install -y libpulse0"
call :wsl_stop_sudo
%BASH% -c "if cat ~/.bashrc | grep 'export PULSE_SERVER=tcp:localhost' >> /dev/null ; then echo OK; else echo 'export PULSE_SERVER=tcp:localhost' >> ~/.bashrc; fi"
goto :eof
:missing_admin
echo Please run this script as an administrator
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment