nullpo-head / wsl-distrod

Distrod is a meta-distro for WSL 2 which installs Ubuntu, Arch, Debian, Gentoo, etc. with systemd in a minute for you. Distrod also has built-in auto-start feature on Windows startup and port forwarding ability.
MIT License
1.9k stars 90 forks source link

[Bug]: `network-online.target` not available #52

Open siennathesane opened 2 years ago

siennathesane commented 2 years ago

Describe the bug

Some unit configurations won't be able to exist due to missing network target.

systemctl status
● DESKTOP-S8SIUD6
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Sat 2022-05-21 12:31:34 MDT; 5min ago
   CGroup: /
           ├─user.slice
           │ └─user-1000.slice
           │   ├─user@1000.service
           │   │ ├─init.scope
           │   │ │ ├─867 /lib/systemd/systemd --user
           │   │ │ └─868 (sd-pam)
           │   │ └─dbus.service
           │   │   └─1125 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
           │   └─session-c1.scope
           │     ├─ 714 login -f
           │     ├─ 874 -zsh
           │     ├─1159 systemctl status
           │     └─1160 less
           ├─init.scope
           │ └─1 /sbin/init systemd.setenv=WSL_DISTRO_NAME=Ubuntu systemd.setenv=WSL_INTEROP=/run/WSL/11_interop systemd.setenv=WSLENV=WT_SESSION::WT_PROFILE_ID --unit=multi-user.target
           └─system.slice
             ├─snap-lxd-19188.mount
             │ └─101 snapfuse /var/lib/snapd/snaps/lxd_19188.snap /snap/lxd/19188 -o ro,nodev,allow_other,suid
             ├─systemd-udevd.service
             │ └─89 /lib/systemd/systemd-udevd
             ├─cron.service
             │ └─160 /usr/sbin/cron -f
             ├─polkit.service
             │ └─169 /usr/lib/policykit-1/polkitd --no-debug
             ├─systemd-timedated.service
             │ └─1158 /lib/systemd/systemd-timedated
             ├─networkd-dispatcher.service
             │ └─167 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
             ├─ModemManager.service
             │ └─288 /usr/sbin/ModemManager
             ├─systemd-journald.service
             │ └─69 /lib/systemd/systemd-journald
             ├─atd.service
             │ └─190 /usr/sbin/atd -f
             ├─unattended-upgrades.service
             │ └─282 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
             ├─ssh.service
             │ └─290 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
             ├─snapd.service
             │ └─178 /usr/lib/snapd/snapd
             ├─rsyslog.service
             │ └─173 /usr/sbin/rsyslogd -n -iNONE
             ├─snap-core18-1988.mount
             │ └─102 snapfuse /var/lib/snapd/snaps/core18_1988.snap /snap/core18/1988 -o ro,nodev,allow_other,suid
             ├─snap-snapd-11036.mount
             │ └─103 snapfuse /var/lib/snapd/snaps/snapd_11036.snap /snap/snapd/11036 -o ro,nodev,allow_other,suid
             ├─dbus.service
             │ └─161 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
             └─systemd-logind.service
               └─182 /lib/systemd/systemd-logind

I don't think WSL2 needs a network-online.target, but it might help to simulate it for units which need it.

Steps to reproduce

Given this service file:

# consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target

[Service]
User=consul
Group=consul
ExecStart=/opt/homebrew/opt/consul/bin/consul agent -server -ui -datacenter dc1 -bind 127.0.0.1
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

When this command is run under a user slice (see #13):

# install homebrew for linux: https://brew.sh
# install consul with `brew install consul`
brew services start -v consul --file consul.service

Then this is the expected result:

/usr/bin/systemctl --user daemon-reload
/usr/bin/systemctl --user start homebrew.consul
Failed to start homebrew.consul.service: Unit network-online.target not found.
Error: Failure while executing; `/usr/bin/systemctl --user start homebrew.consul` exited with 5.

Expected behavior

The unit consul.service should run.

Windows version

19044.1706

Linux kernel version

Linux DESKTOP-S8SIUD6 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Distro

Ubuntu 20.04

How did you install that distro?

Enabled distrod in an existing WSL2 distro

Logs

12,414,1322381248,-;Distrod: [DEBUG] distrod-exec: exec_command_in_distro
12,415,1322381360,-;Distrod: [DEBUG] Executing a command in the distro.
12,416,1322381596,-;Distrod: [DEBUG] Failed to ignore signal Sys(EINVAL)
12,417,1322381894,-;Distrod: [DEBUG] Failed to ignore signal Sys(EINVAL)
12,418,1322382025,-;Distrod: [DEBUG] Distro::exec_command.
12,419,1322385124,-;Distrod: [DEBUG] Container::exec_command.
12,420,1322385724,-;Distrod: [DEBUG] Triple fork done.
12,421,1322386068,-;Distrod: [DEBUG] dropping privilege. kmsg logging in the child ends here.
12,423,1341357189,-;Distrod: [DEBUG] distrod-exec: exec_command

additional comment

Interestingly, if you remove the network-online.target and then run the command again, brew thinks it starts, but the unit isn't actually running:

$ brew services start -v consul --file ref-configs/consul.service
/usr/bin/systemctl --user daemon-reload
/usr/bin/systemctl --user start homebrew.consul
/usr/bin/systemctl --user enable homebrew.consul
Created symlink /home/sienna/.config/systemd/user/multi-user.target.wants/homebrew.consul.service → /home/sienna/.config/systemd/user/homebrew.consul.service.
==> Successfully started `consul` (label: homebrew.consul)
$ brew services list
Name   Status User   File
consul none   sienna
$ systemctl status
● DESKTOP-S8SIUD6
    State: running
     Jobs: 0 queued
   Failed: 0 units
    Since: Sat 2022-05-21 12:31:34 MDT; 24min ago
   CGroup: /
           ├─user.slice
           │ └─user-1000.slice
           │   ├─user@1000.service
           │   │ ├─init.scope
           │   │ │ ├─2788 /lib/systemd/systemd --user
           │   │ │ └─2789 (sd-pam)
           │   │ └─dbus.service
           │   │   └─3040 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
           │   └─session-c3.scope
           │     ├─2733 login -f
           │     ├─2794 -zsh
           │     ├─3288 systemctl status
           │     └─3289 less
           ├─init.scope
           │ └─1 /sbin/init systemd.setenv=WSL_DISTRO_NAME=Ubuntu systemd.setenv=WSL_INTEROP=/run/WSL/11_interop systemd.setenv=WSLENV=WT_SESSION::WT_PROFILE_ID --unit=multi-user.target
           └─system.slice
             ├─snap-lxd-19188.mount
             │ └─101 snapfuse /var/lib/snapd/snaps/lxd_19188.snap /snap/lxd/19188 -o ro,nodev,allow_other,suid
             ├─systemd-udevd.service
             │ └─89 /lib/systemd/systemd-udevd
             ├─cron.service
             │ └─160 /usr/sbin/cron -f
             ├─polkit.service
             │ └─169 /usr/lib/policykit-1/polkitd --no-debug
             ├─networkd-dispatcher.service
             │ └─167 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
             ├─ModemManager.service
             │ └─288 /usr/sbin/ModemManager
             ├─systemd-journald.service
             │ └─69 /lib/systemd/systemd-journald
             ├─atd.service
             │ └─190 /usr/sbin/atd -f
             ├─unattended-upgrades.service
             │ └─282 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
             ├─ssh.service
             │ └─290 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
             ├─snapd.service
             │ └─178 /usr/lib/snapd/snapd
             ├─rsyslog.service
             │ └─173 /usr/sbin/rsyslogd -n -iNONE
             ├─snap-core18-1988.mount
             │ └─102 snapfuse /var/lib/snapd/snaps/core18_1988.snap /snap/core18/1988 -o ro,nodev,allow_other,suid
             ├─snap-snapd-11036.mount
             │ └─103 snapfuse /var/lib/snapd/snaps/snapd_11036.snap /snap/snapd/11036 -o ro,nodev,allow_other,suid
             ├─dbus.service
             │ └─161 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
             └─systemd-logind.service
               └─182 /lib/systemd/systemd-logind
$ ps uax
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 169424 13428 ?        Ss   12:31   0:00 /sbin/init systemd.setenv=WSL_DISTRO_NAME=Ubuntu systemd.setenv=WSL_INTEROP=/run/WSL/11_interop systemd.setenv=WSLENV=WT_SESSION::WT_PROFILE_ID --unit=multi-user.target
sienna         3  0.0  0.0  10536  2532 pts/0    S    12:31   0:00 distrod-exec -- /usr/bin/zsh -zsh
sienna         4  0.0  0.0  15612  7564 pts/0    S    12:31   0:00 -zsh
root          69  0.0  0.0  53248 20804 ?        S<s  12:31   0:00 /lib/systemd/systemd-journald
root          89  0.0  0.0  21616  6956 ?        Ss   12:31   0:00 /lib/systemd/systemd-udevd
root         101  0.0  0.0   3704  1592 ?        Ss   12:31   0:00 snapfuse /var/lib/snapd/snaps/lxd_19188.snap /snap/lxd/19188 -o ro,nodev,allow_other,suid
root         102  0.0  0.0   3760  1780 ?        Ss   12:31   0:00 snapfuse /var/lib/snapd/snaps/core18_1988.snap /snap/core18/1988 -o ro,nodev,allow_other,suid
root         103  0.0  0.0   3644  1604 ?        Ss   12:31   0:00 snapfuse /var/lib/snapd/snaps/snapd_11036.snap /snap/snapd/11036 -o ro,nodev,allow_other,suid
root         160  0.0  0.0   8540  2796 ?        Ss   12:31   0:00 /usr/sbin/cron -f
message+     161  0.0  0.0   7560  4696 ?        Ss   12:31   0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         167  0.0  0.0  29900 18316 ?        Ss   12:31   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root         169  0.0  0.0 232716  6724 ?        Ssl  12:31   0:00 /usr/lib/policykit-1/polkitd --no-debug
syslog       173  0.0  0.0 224348  6512 ?        Ssl  12:31   0:00 /usr/sbin/rsyslogd -n -iNONE
root         178  0.0  0.1 1390152 47600 ?       Ssl  12:31   0:00 /usr/lib/snapd/snapd
root         182  0.0  0.0  16820  7808 ?        Ss   12:31   0:00 /lib/systemd/systemd-logind
daemon       190  0.0  0.0   3796  2352 ?        Ss   12:31   0:00 /usr/sbin/atd -f
root         282  0.0  0.0 108116 20432 ?        Ssl  12:31   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root         288  0.0  0.0 314332 10576 ?        Ssl  12:31   0:00 /usr/sbin/ModemManager
root         290  0.0  0.0  12176  6852 ?        Ss   12:31   0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root        2732  0.0  0.0  11032  4592 pts/0    S    12:55   0:00 sudo login -f sienna
root        2733  0.0  0.0  10440  4680 pts/0    S    12:55   0:00 login -f
sienna      2788  0.1  0.0  21360 10868 ?        Ss   12:55   0:00 /lib/systemd/systemd --user
sienna      2789  0.0  0.0 170784  5540 ?        S    12:55   0:00 (sd-pam)
sienna      2794  0.1  0.0  15088  7104 pts/0    S    12:55   0:00 -zsh
sienna      3040  0.0  0.0   7108  3920 ?        Ss   12:55   0:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
sienna      4171  0.0  0.0  10616  3260 pts/0    R+   12:57   0:00 ps uax

Not quite sure why it's not registered, but it doesn't error out.

siennathesane commented 2 years ago

Update: I did get it working, just not with the network/user/group configs. I'm going to leave this open for now since network-online.target isn't available, even though I was able to work through my issue.

Given this configuration:

[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/

[Service]
ExecStart=/home/linuxbrew/.linuxbrew/bin/consul agent -server -ui -datacenter dc1 -data-dir /tmp/pleiades -bind 127.0.0.1
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

And this command to setup symlinks:

sudo ln -s $PWD/consul.service /etc/systemd/system/consul.service

When this command is run:

sudo systemctl enable consul

Then this is the result:

$ sudo systemctl start consul
$ sudo systemctl status consul
● consul.service - "HashiCorp Consul - A service mesh solution"
     Loaded: loaded (/etc/systemd/system/consul.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-05-21 13:08:34 MDT; 4s ago
       Docs: https://www.consul.io/
   Main PID: 6956 (consul)
      Tasks: 13 (limit: 30700)
     Memory: 28.5M
     CGroup: /system.slice/consul.service
             └─6956 /home/linuxbrew/.linuxbrew/bin/consul agent -server -ui -datacenter dc1 -data-dir /tmp/pleiades -bind 127.0.0.1

May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.265-0600 [INFO]  agent.router: Initializing LAN area manager
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.266-0600 [INFO]  agent.server: Adding LAN server: server="DESKTOP-S8SIUD6 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.266-0600 [INFO]  agent.server.autopilot: reconciliation now disabled
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.266-0600 [INFO]  agent.server: Handled event for server in area: event=member-join server=DESKTOP-S8SIUD6.dc1 area=wan
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.266-0600 [WARN]  agent: [core]grpc: addrConn.createTransport failed to connect to {dc1-127.0.0.1:8300 DESKTOP-S8SIUD6.dc1 <nil> 0 <nil>}. Err: connection error: desc = "transport: Error while dialing dia>
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.267-0600 [INFO]  agent: Started DNS server: address=127.0.0.1:8600 network=udp
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.267-0600 [INFO]  agent: Started DNS server: address=127.0.0.1:8600 network=tcp
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.267-0600 [INFO]  agent: Starting server: address=127.0.0.1:8500 network=tcp protocol=http
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.267-0600 [INFO]  agent: started state syncer
May 21 13:08:34 DESKTOP-S8SIUD6 consul[6956]: 2022-05-21T13:08:34.267-0600 [INFO]  agent: Consul agent running!

And it was verified to be available via curl:

curl -v localhost:8500
*   Trying 127.0.0.1:8500...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8500 (#0)
> GET / HTTP/1.1
> Host: localhost:8500
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=utf-8
< Location: /ui/
< Date: Sat, 21 May 2022 19:08:55 GMT
< Content-Length: 39
<
<a href="/ui/">Moved Permanently</a>.

* Connection #0 to host localhost left intact

And the browser:

image