subsystemd: Run systemd in WSL2
Run systemd under Linux namespace in WSL2. Heavily inspired by arkane-systems/genie, but written in Rust.
Difference with arkane-systems/genie
Slightly following genie's behavior, but noted below...
- Interface
- Command line interface is not compatible.
- Behavior
- Hostname altertion is optional with
--hostname,--hostname-suffix/etc/hostsare not updated. Users are encouraged to usenss-myhostname.
- Uses
machinectl shellto launch a user shell; this allows running systemd user session
- Hostname altertion is optional with
- Internal
- Removed dependency to
unshare,daemonize,nsentercommand line tools - systemd-wide environment variables are set via
systemd.confdrop-in, usingDefaultEnvironment= - systemd PID from root namespace is stored at
/run/subsystemctl/systemd.pid
- Removed dependency to
Install
Arch Linux AUR
https://aur.archlinux.org/packages/subsystemctl
Self build
Usage
subsystemctl start: Start systemd environment
PS> wsl -u root -- subsystemctl start
subsystemctl shell: shell login to systemd-enabled environment
PS> wsl subsystemctl shell
Connected to the local host. Press ^] three times within 1s to exit session.
someone@hostname$ ...
Specifying uid to login
PS> wsl -u root -- subsystemctl --uid=1000 shell
Connected to the local host. Press ^] three times within 1s to exit session.
someone@hostname$ ...
subsystemctl exec: Raw nsenter like interface
PS> wsl subsystemctl exec id
uid=1000(sorah) gid=1000(sorah) groups=1000(sorah),116(admin)
Specifying uid (and gid)
PS> wsl -u root -- subsystemctl exec id
uid=0(root) gid=0(root) groups=0(root)
PS> wsl -u root -- subsystemctl exec --uid=1000 id
uid=1000(sorah) gid=1000(sorah) groups=1000(sorah),116(admin)
PS> wsl -u root -- subsystemctl exec --uid=1000 --gid=116 id
uid=1000(sorah) gid=116(admin) groups=116(admin)
subsystemctl is-running
#!/bin/bash
if ; then
else
fi
subsystemctl is-inside
#!/bin/bash
if ; then
else
fi
Tips
systemd-resolved, networkd are recommended to be disabled
otherwise /etc/resolv.conf might get overwritten to resolved stub-resolver.
Author
Sorah Fukumori https://sorah.jp/
License
MIT