'What is the entry point for WSL 1?

Imagine we have a statically linked Linux executable.

How should I name it in the imported tar.gz so the WSL 1 will run it by default, when created and started like:

# import an archive as a WSL distro
wsl --import static tmp-root-dir static.tar.gz
# boot distro to a default app??
wsl -d static

PS WSL uses own proprietary boot process and seems doesn't use traditional Unix /sbin/init.



Solution 1:[1]

I managed to get it working. Initially I missed an executable bit on the app when created TAR archive.

Take standard 64-bit assembly:

.data
msg:
  .ascii "Hello, world!\n"
  .set len, . - msg

.text

.globl _start
_start:
  # write
  mov  $1,   %rax
  mov  $1,   %rdi
  mov  $msg, %rsi
  mov  $len, %rdx
  syscall

  # exit
  mov  $60, %rax
  xor  %rdi, %rdi
  syscall

and create a minimal WSL system:

wsl as -64 -o minimal.o minimal.s
wsl ld -melf_x86_64 -o minimal minimal.o
tar czf minimal.tar.gz \
  --mode=a=rx \
  --xform='s#^minimal#/\0#' minimal
wsl --import minimal rootfs-minimal minimal.tar.gz --version 1
wsl --list
wsl -d minimal -e /minimal

To make executable default (shorten wsl -d minimal -e /minimal to wsl -d minimal) we need an extra file /etc/passwd:

root:x:0:0:root:/root:/minimal

First line of this file determine a default user and so path to the executable (entry point) unless you override the user with /etc/wsl.conf:

[user]
default=user

Basically WSL 1 treats only 2 files as magical (in addition to ignoring /sbin/init):

  • /etc/wsl.conf
  • /etc/passwd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gavenkoa