Skip to content

Instantly share code, notes, and snippets.

@novium
Last active November 28, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novium/fa877a4b2cd8013dfde2d8739a80bdcb to your computer and use it in GitHub Desktop.
Save novium/fa877a4b2cd8013dfde2d8739a80bdcb to your computer and use it in GitHub Desktop.
IOOPM Windows Subsystem for Linux

WSL Installer for IOOPM

Note: these scripts were written for WSL1 / Ubuntu 18.04, today it's easier to download Ubuntu from the Store and follow the linux instructions on the course-web!

Best of luck with the course! (and make sure to have fun!)

These scripts automatically install most of what is needed for the course IOOPM @ Uppsala University.

Remember that this is not an officially supported environment - only the Linux terminals at the university are. Thus, always check your code before turning in an assignment on the terminals, git is super handy for that, and is also a learning goal! Also good if you experience any problems.

Installing

Step 0

Update Windows to the latest version preferably build 1709 and later, see FAQ for builds tested. You can find the build number in About your PC. Make sure your internet connection is stable, so it is probably a bad idea to do this during a lab! If something goes wrong, check the FAQ.

Also, if something is unclear you can refer to this YouTube video.

Step 3

Download install.ps1 by right-clicking this link and selecting "save as" or by clicking "Raw" in the right corner of the file and saving the file to disk.

Remember, it is good practice skimming thru any script you download from the internet! Including this one.

Step 4

Close and save all running applications since your computer will reboot during the installation. Then use cd in Powershell (as an admin) to navigate to the directory where you saved the file and run . .\install.ps1 (you don't need to download resume.ps1 since it is downloaded from here by the script).

Important During the installation after the restart it will say that Ubuntu is installing, after that it will ask you to provide a username and a password. These are the ones used inside bash when using commands such as sudo.

Step 5

After entering your username and password type the command exit in the powershell window, it will ask for the password once more after that and then continue installing! Be sure to get a cup of coffee since after typing your secure password 3 times you're probably in a dire need (and installing all the packages can take a while)!

You're done when you see the prompt PS C:\Windows\system32>, phew! Now you can use Linux by opening up Powershell and using the command bash or by running the Windows app Ubuntu.

From bash you have access to the entire Windows filesystem!

FAQ

1. Error ...\install.ps1 cannot be loaded

You need to update your security policy, to do that open a Powershell window as an administrator and type this command Set-ExecutionPolicy -ExecutionPolicy RemoteSigned. This option allows scripts to be run. You can change it back to AllSigned when you're done.

I recommend installing Chocolatey as a package manager for Windows, then you can leave the policy as RemoteSigned :)

2. Error appx package can't be sideloaded.

Go to Settings, Update & Security, For Developers, and press "Sideload apps" to allow apps to be sideloaded.

3. Minimum Windows Version

Definitely requires Windows 10. Tested with build 1803 (April 2018 Update) and 1709 (Fall Creators Update).

4. Temporary files

As long as the installation finishes all the temporary files should be deleted, otherwise they should be deleted by Windows automatically during the next disk cleanup. If you want to make sure - check that the directory %temp%\ioopm doesn't exist.

5. adduser: Please enter a username matching...

Ubuntu needs a lowercase username (think just letters an numbers) due to certain constraints. Try again with a lowercase username!

6. Uninstalling

If you want to uninstall you just need to uninstall the Ubuntu app on Windows.

7. AFL

This program is a bit special, if it doesn't work, use the terminals or move the code and app to the Linux homefolder!

8. Restarted but nothing happened

First, wait a bit! It seems like it can take a few minutes before some computers continue the script!

Try opening Powershell as an administrator and type cd $env:TEMP\ioopm, from there, run the command . .\resume.ps1 and continue from step 5 in the instruction. If this directory doesn't exist the installation probably finished, try running bash again in a command prompt.

Anything else? Catch Alexander at a lab!

# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
workflow IOOPM-Install {
#
# Download files
#
New-Item "$env:TEMP\ioopm" -Force -ItemType directory
InlineScript {
$powerline_url = "https://github.com/powerline/fonts/archive/master.zip"
$powerline_output = "$env:TEMP\ioopm\powerline.zip"
$ubuntu_url = "https://aka.ms/wsl-ubuntu-1804"
$ubuntu_output = "$env:TEMP\ioopm\Ubuntu.appx"
Import-Module BitsTransfer
# Download and extract powerline fonts from repo
Start-BitsTransfer -Source $powerline_url -Destination $powerline_output
Expand-Archive -Force -Path $powerline_output -Destination "$env:TEMP\ioopm\powerline"
# Download Ubuntu appx from Microsoft
Start-BitsTransfer -Source $ubuntu_url -Destination $ubuntu_output
# Download resume script (from gist)
Start-BitsTransfer -Source "https://gist.githubusercontent.com/novium/fa877a4b2cd8013dfde2d8739a80bdcb/raw/ede86e2a6c9574f5f03e695ceb891390ac593f79/resume.ps1" -Destination "$env:TEMP\ioopm\resume.ps1"
}
#
# Install
#
# Install powerline fonts
Invoke-Expression "$env:TEMP\ioopm\powerline\fonts-master\install.ps1 DejaVu*"
# Allows app sideloading (so that we can install the Ubuntu app
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowAllTrustedApps" /d "1"
# Allows us to execute the resume-script
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
# WSL (-NoRestart since ps workflows don't support user interaction)
Enable-WindowsOptionalFeature -Online -NoRestart -FeatureName Microsoft-Windows-Subsystem-Linux
Restart-Computer # Restart is required to continue
}
# Before we begin, remind user to exit everything
Write-Host
Write-Host "Remember to save and exit all your applications" -BackgroundColor DarkRed
Write-Host "Your computer will restart during the installation" -BackgroundColor DarkRed
Write-Host
Read-Host -Prompt "Press ENTER to continue"
# This is to resume the install after restart
# Check that task doesn't exist
Get-ScheduledTask -TaskName IOOPMInstallResume | Unregister-ScheduledTask -Confirm:$false
# Build the command to run powershell from a task
# We need it since a powershell job again doesn't allow user interaction
$scriptPath = "$env:TEMP\ioopm\resume.ps1"
$actionscript = "-WindowStyle Normal -NoLogo -NoProfile -executionpolicy bypass -NoExit -file $scriptPath"
$pstart = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$act = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
$trigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -TaskName IOOPMInstallResume `
-RunLevel Highest `
-Trigger $trigger `
-Action $act `
-Force
# Install!
IOOPM-Install -JobName IOOPMInstall
# Install ubuntu appx we downloaded earlier
Add-AppxPackage -Path "$env:TEMP\ioopm\Ubuntu.appx"
# This part requires user input
# Username + Password + Exit
ubuntu1804.exe install --root
ubuntu1804.exe run "DEBIAN_FRONTEND=noninteractive sudo apt-get update && sudo apt-get install -y gcc clang libclang-dev lldb emacs25 vim tree make tmux curl astyle zsh linux-tools-common valgrind git libcunit1-dev doxygen xclip global zip unzip openjdk-8-jdk linux-tools-generic cscope cproto gcovr tig htop junit gnuplot graphviz cmake indent ubuntu-desktop"
ubuntu1804.exe run "sudo adduser ioopm --gecos \"\" --disabled-password"
ubuntu1804.exe run "sudo usermod -aG sudo ioopm"
ubuntu1804.exe config --default-user ioopm
ubuntu1804.exe run "echo \"export DISPLAY=localhost:0.0\" >> ~/.bashrc"
# Cleanup (%temp%/ioopm)
Get-ChildItem -Path "$env:TEMP\ioopm" -Recurse | Remove-Item -force -recurse
Remove-Item "$env:TEMP\ioopm" -Force
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowAllTrustedApps" /d "0"
Set-ExecutionPolicy -ExecutionPolicy AllSigned
# Double check that the resume-task is removed
Get-ScheduledTask -TaskName IOOPMInstallResume | Unregister-ScheduledTask -Confirm:$false
@novium
Copy link
Author

novium commented Sep 5, 2018

TODO:

  • Rewrite download and extract to run in parallell to save a few seconds before restart.
  • Pause before starting install asking to save and exit all apps
  • Test on 1703 Redstone 2 / Fall Creators Update
  • Look into creating custom distro for even quicker install (primarily due to somewhat slow fs on WSL, but also it being 1.5GB of packages) + extend to be able to either install thru WSL or VirtualBox/Hyper-V

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment