Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Talks on Little-Known Random Facts about WSL2

Talks on Little-Known Random Facts about WSL2

presentation at Kernel/VM expedition online part4 (https://kernelvm.connpass.com/event/225573/) by @nullpo_head (https://twitter.com/nullpo_head).

Random Talks!
Won’t cover basics of WSL, nor anything explained in the official documentation. Will talk only about lesser-known facts about WSL2

WSL2(Windows Subsystem for Linux 2)に関するいろいろなことを話します.
Japanese 日本語文字起こし記事 「ここが変だよ『WSL2』自作ディストロ開発で発見した知られざる“バグ”と“事実” - logmi」https://logmi.jp/tech/articles/326106

Takaya Saeki

November 20, 2021
Tweet

More Decks by Takaya Saeki

Other Decks in Technology

Transcript

  1. Talks on Little-Known Random Facts about WSL2 Kernel/VM online part

    4 Takaya Saeki (@nullpo_head) https://fuel.edby.coffee
  2. Outline •Random Talks! • Won’t cover basics of WSL, nor

    something explained in the official documentation. (e.g., WSLg • Will talk only about lesser-known facts
  3. @nullpo_head ( Takaya Saeki ) Software Engineer ( https://fule.edby.coffee )

    Hobby: Software development • Distrod • WSL2 Meta-distro that runs system • WSL Hello sudo • Sudo with Windows Hello • Dbgee • Start VSCode debug session from the command line side • Noah • Thin hypervisor that translates native systemcalls to Linux systemcalls • CPU experiment (2014) • Run XV6 on your own CPU
  4. Context: Distrod is now available! https://github.com/nullpo-head/wsl-distrod/ • Presented at Kernel/VM

    online part 3 • Install Ubuntu, Arch Linux, Gentoo and more with systemd in a minute, or make your current distro run systemd. • Auto-start on Windows startup • Built-in port forwarding service • Will talk about random little- known facts about WSL2 I learned from developing Distrod
  5. Topics • WSL instance is a container • Interop Internals

    • WSL_INTEROP, subreaper • Network • DNS Performance Issue • Bugs • Autostart and Admin Privileges
  6. WSL2 – VM “and” Container • WSL2 is a light-weight

    VM => Correct • A WSL2 distro is a container => Also correct!
  7. WSL distro is running in a container You cannot see

    kernel threads since you’re in a PID namespace
  8. Enter another distro’s namespaces • /mnt/wsl is mounted with MS_SHARED

    • You can do # mount --bind /proc /mnt/wsl/myproc
  9. How WSL launches .exe file? • Each WSL session listens

    on a socket to accept requests to launch .exe files on `/run/WSL` • Think of it as one Windows Terminal tab with one *_interop socket.
  10. How /init finds corresponding *_interop Two patterns 1. WSL_INTEROP: If

    a Linux process has the WSL_INTEROP environment variable, /init will use the path of that variable to launch the Windows process requested by that Linux process. 2. Look for ANCESTOR_PID_INTEROP Otherwise, /init enumerates all the PIDs of the ancestor processes of the Linux process and checks for the existence of a file named PID_INTEROP 1. You can confirm this behavior by checking # strace –f cmd.exe
  11. Admin Privilege If you run a Windows Terminal instance with

    the admin privilege, the corresponding *_interop also has the admin privilege
  12. WSL2 Network is a bit unique • All distros share

    the same network namespace • /init assigns a static IP address to eth0 • No DHCP • The IP address changes on every boot • Seems to have special DNS implementation only for WSL • Because it has much poorer performance than the Hyper-V’s network adapter. • ...I don’t know why 🤔
  13. DNS Performance Issue Let’s kill your WSL2’s DNS! Of course,

    normal Hyper-V VM DNS and Windows DNS will not die with such small traffic. This happens when you launch a medium-sized k8s cluster. Set 8.8.8.8 as your primary name server as a workaround. $ for i in {1..10}; do dig fooo1 > /dev/null & dig fooo2 > /dev/null & dig fooo3 > /dev/null; done & $ ping wwww.google.com ping: wwww.google.com: Name or service not known
  14. WSL users struggling with auto-startup For example, to start ssh

    at Windows startup All available Instructions on the Internet say 1. Make a task to run “wsl.exe” at Windows startup by Task Scheduler 2. You need to enable “Run with highest privileges” => Actually, Not
  15. “highest privilege” requirement is a bug in wsl.exe, not a

    WSL specification. • Hit WslLaunchInteractive directly. You don’t need the highest privilege. Good for security. $WslLaunchInteractive = @' [DllImport("wslapi.dll")] public static extern uint WslLaunchInteractive( [MarshalAs(UnmanagedType.LPWStr)]string DistributionName, [MarshalAs(UnmanagedType.LPWStr)]string Command, [MarshalAs(UnmanagedType.U1)]bool UseCurrentWorkingDirectory, out uint ExitCode ); ‘@; $exitcode=256; $wslapi = Add-Type -MemberDefinition $WslLaunchInteractive -Name 'WslApi' - Namespace 'Win32' -PassThru; $wslapi::WslLaunchInteractive('{{DISTRO_NAME}}', 'exit', $false, [ref]$exitcode);
  16. WSL2 is (still) anyway WSL1 was great, but WSL2 is

    also cool. Technically, • Memory reclaim • WSLg • Lightweight VM • Containers And best of all, WSL is convenient!