Skip to content

Instantly share code, notes, and snippets.

@NyaMisty
Created May 4, 2022 22:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaMisty/418b06a427a976ade266cf7a700019a8 to your computer and use it in GitHub Desktop.
Save NyaMisty/418b06a427a976ade266cf7a700019a8 to your computer and use it in GitHub Desktop.
WSL Systemd Configuration: in 2022

WSL2 Systemd, 2022 (Windows 11) version

Install genie

https://github.com/arkane-systems/genie

Fix genie on latest Windows 11

See Issue arkane-systems/genie#267

Patch /usr/lib/binfmt.d/WSLInterop.conf:

  • From: :WSLInterop:M::MZ::/init:F
  • To: :WSLInterop:M::MZ::/init:PF

Auto Start Init Script & Systemd

TLDR: Use /etc/wsl.conf

Add these lines:

[boot] command = /bin/bash /opt/wsl2_init.sh

Add systemctl wrapper

Write the following into /usr/local/bin/systemctl

#!/bin/bash
export PATH=/usr/bin:$PATH # avoid genie recursively call this wrap
genie -c /usr/bin/systemctl "$@"
#!/bin/bash
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
exec > >(tee -a -i /var/log/wsl2_init.log)
# Without this, only stdout would be captured - i.e. your
# log file would not contain any error messages.
# SEE (and upvote) the answer by Adam Spiers, which keeps STDERR
# as a separate stream - I did not want to steal from him by simply
# adding his answer to mine.
exec 2>&1
echo "---------- WSL booted at $(/bin/date +'%Y-%m-%d %H:%M:%S') ----------"
echo "Environments:"
declare -px
echo "Preparing basic environments..."
export LOGNAME='misty'
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:$PATH
echo "Booting genie systemd..."
/usr/bin/genie -v -i
echo genie returns: $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment