Targeting Windows 10’s Windows Subsystem for Linux with Delphi for Linux

by Oct 10, 2019

More information on Targeting Windows 10’s Windows Subsystem for Linux with Delphi for Linux.

Currently this is all the content from the slides, I'll add a link for the replay later.

  1. What's is the Windows Subsystem for Linux
  2. Benefits & use cases Why do you care?
  3. Timeline When were Subsystems introduced?
  4. Distributions Which Linux distros work with WSL?
  5. Installation and configuration
  6. Targeting WSL from Delphi The reason you came here today
  7. FMXLinux on WSL Working with GUI apps
  8. More Resources Find out more!

Microsoft and Windows Love Linux

What is the Windows Subsystem for Linux (WSL)

  • Windows Subsystem for Linux (WSL) is a compatibility layer for running Linux binary executables (in ELF format) natively on Windows 10 and Windows Server 2019
  • Multiple distributions are available via the Microsoft Store (or load your own)
  • Mostly focused on command-line interface with limited support for GUI/Desktop apps via external X11 server
  • Collaborative between Microsoft and Linux
  • Less abstraction and better Windows integration than a traditional Virtual Machine
  • Kind of like a reverse WINE (the Linux compatibility layer for Windows)

[ top ]

Benefits and Use Cases

  • Ability to run unmodified ELF64 Linux binaries, expand toolkit and capabilities
  • Flexibility of combining Windows and Linux tool chain on one computer
  • Less overhead than a traditional virtual machine means better performance
  • Local build environment with containers, etc.
  • Testing server applications from Windows without additional infrastructure
  • No need to mess with dual boot or 3rd party VM installation
  • Invoke Linux binaries from Windows and Windows executables from Linux
  • Runs as you need it, less management
  • Memory and drive space are dynamically shared

[ top ]

Timeline

  • July 1993 – Microsoft Windows NT introduced support for subsystems, allowing the NT kernel to support Win32, POSIX, OS/2, etc.
  • February 1999 – POSIX replaced with Windows Services for UNIX (SFU)  
  • August 2001 – Windows XP brought Windows NT architecture to consumer editions of Windows
  • January 2004 – EOL for Windows Services for UNIX
  • August 2016 – Initial WSL release (Windows 10 Anniversary Update)
    • User mode session manager service that handles the Linux instance life cycle
    • Pico provider drivers (lxss.sys, lxcore.sys) that emulate a Linux kernel by translating Linux syscalls
    • Pico processes that host the unmodified user mode Linux (e.g. /bin/bash)
    • Initially only included Ubuntu – later other distros were added
    • Emulated the Linux kernel 
  • Many minor improvements and bug fixes to WSL since initial release
  • June 2019 – WSL2 released to the “Insider Build” (Windows 10 builds 18917)
    • Full Native Linux Kernel – no more emulation/simulation/abstraction – uses optimized subset of Hyper-V
    • Significant performance improvements

[ top ]

Available DistributionsWindows Subsystem for Linux Distributions

  • Ubuntu, Kali, & Pengwin are in the Debian family (using .deb pkgs – apt)
    • Kali is focused on security
    • Pengwin is designed around WSL
  • SUSE and Fedora are in the RPM family with Red Hat
  • Alpine is an independent, lightweight, security-oriented, Linux distribution
  • Some distros include multiple versions. The "Ubuntu" one is always the latest LTS, while the others are version specific.

WSL Installation Guide

  • Fall Creators Update Windows build 16215 from 2017 (current release is 1903 Build 18362)
  • Enable "Windows Subsystem for Linux" optional feature (via Admin PowerShell or Windows Features)
    • Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-WindowsSubsystem-Linux
  • Install Linux Distro of Choice 
    • Windows Store or manually via script
  • Launch Linux Distro to initialize it, provide password, etc.
  • You can also build a custom distro
  • Launch WSL 4 ways
    • Icon on start menuTurn Windows Subsystem for Linux feature on
    • [distro], for example ubuntu
    • wsl.exe or bash.exe
    • wsl [command] or  bash -c [command]

Managing WSL Distros (Version 1903 and later)

  • List distributions
    • wsl –list –all or wsl –list –running
  • Set default distribution
    • wsl –setdefault <DistributionName>  or wsl -s <DistributionName> 
  • Run a specific distro
    • wsl –distribution <DistributionName>
  • Run as specified user 
    • wsl –user <Username> (or use the specific distro name)
  • Unregister and uninstall a distribution
    • wsl –unregister <DistributionName>
  • Prior to 1903 use wslconfig.exe instead of wsl.exe
  • WSL files are stored in %userprofile%\AppData\Local\Packages (But don’t modify them from Windows!)

top ]

Targeting from Delphi for Linux

  • Ubuntu LTS (18.04) is the official recommended distro – but should work with others
  • Install the developer tools that Delphi needs
    • sudo apt update # Update the package lists
    • sudo apt full-upgrade # Upgrade the system by removing/installing/upgrading packages
    • sudo apt install libcurl4-gnutls-dev build-essential
    • sudo apt autoremove # clean things up
  • Install paserver http://docwiki.embarcadero.com/RADStudio/en/Installing_the_Platform_Assistant_on_Linux
    • cd ~
    • tar -xf /mnt/c/Program Files (x86)/Embarcadero/Studio/20.0/PAServer/LinuxPAServer20.0.tar.gz
    • mv PAServer-20.0/ PAServer
    • cd PAServer
    • ./paserver
  • Launch PAServer on Linux from Windows
    • ubuntu run ~/PAServer/paserver or wsl ~/PAServer/paserver

top ]

Connecting from the IDE

  • The IP address and ports are shared with the host
  • So you can connect to 127.0.0.1
  • Just keep in mind that you can only use each port only with one running instance (even if you have multiple distros setup)
  • So use different port numbers for multiple instances if you want to run them simultaneously 

Connection Profile Manager

[ top ]

X Windows Server Options

To deploy and debug FMXLinux desktop applications you need to setup a X Windows Server on Windows.

  • For sale in Microsoft Store
  • Free & Open Source
  • Install required packages in WSL (including the xfce4 desktop environment)
    sudo apt install xorg libgl1-mesa-dev libosmesa-dev libgtk-3-bin xfce4 xfce4-terminal
  • Launch your X Server on Windows
    • This launches x410 in desktop mode (as opposed to floating window mode)
      start /B x410.exe /desktop
  • Start desktop environment
    ubuntu.exe run "if [ -z \”$(pidof xfce4-session)\” ]; then export DISPLAY=127.0.0.1:0.0; cd ~; xfce4-session; pkill '(gpg|ssh)-agent'; fi;"
  • Launch/debug GUI apps 

[ top ]

Maximum Automation

  • Delphi IDE -> Tools -> Configure Tools
  • Ubuntu Terminal 
  • For GUI launch this batch file
    • @echo off
    • start /B x410.exe /desktop
    • ubuntu.exe run "if [ -z \”$(pidof xfce4-session)\” ]; then export DISPLAY=127.0.0.1:0.0; cd ~; xfce4-session; pkill '(gpg|ssh)-agent'; fi;"

Calling Windows Subsystem for Linux from Delphi IDE

  • In Xfce4 to auto launch PAServer (This will be different for other window managers!)
    • Applications -> Settings -> Settings Manager -> Session and Startup -> Application Autostart -> Add
    • Command: exo-open –launch TerminalEmulator /home/delphi/PAServer/paserver -password=

Setting Xfce4 to auto launch PAServer

More Resources

Check out the full series for replays and more https://embt.co/Windows10ModernizeWebinarSeries 

FMXLinux Resources

Windows Subsystem for Linux Resources

[ top ]