Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service script #169

Merged
merged 12 commits into from
Dec 6, 2022
Merged

Fix service script #169

merged 12 commits into from
Dec 6, 2022

Conversation

josesa-xx
Copy link
Contributor

Rework service init script without using buggy start-stop-daemon
Script has error checks enables so there is no need to set ret variable
Redirect debug messages to stderr

- Fixed init service script to allow proper 'start' and 'stop' actions consistently.
  I kept struggling that service start only worked after killing the distro because
  sometimes it didn't start/stop properly leaving processes running and without pid
  file
- Added service 'status' action
- Added additional service debug by setting environment variable DEBUG, ie:
  wsl.exe -d wsl-vpnkit DEBUG=1 service wsl-vpnkit stop
- Changed order in startup welcome screen having stop followed by start,
  this makes it easier for users to just copy/paste from welcome screen to have
  service running
- Added log check command to see only the latest service execution logs
- Added to README how to do start with status firt and how to enable DEBUG
- Run tests twice with and without DEBUG enabled
@F21
Copy link

F21 commented Nov 20, 2022

It's still glitchy for me.

Note: I have wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit status >/dev/null || wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit start in my .zshrc, which explains Service wsl-vpnkit is running being printed in the first line.

I installed your patch, rebooted and it worked.
However, I rebooted again, and it didn't start properly:

Service wsl-vpnkit is running
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit ps -a
service: unrecognized option: a
Usage: rc-service [options] [-i] <service> <cmd>...
   or: rc-service [options] -e <service>
   or: rc-service [options] -l
   or: rc-service [options] -r <service>

Options: [ cdDe:ilr:INsSZChqVv ]
  -d, --debug                       set xtrace when running the command
  -D, --nodeps                      ignore dependencies
  -e, --exists <arg>                tests if the service exists or not
  -c, --ifcrashed                   if the service is crashed run the command
  -i, --ifexists                    if the service exists run the command
  -I, --ifinactive                  if the service is inactive run the command
  -N, --ifnotstarted                if the service is not started run the command
  -s, --ifstarted                   if the service is started run the command
  -S, --ifstopped                   if the service is stopped run the command
  -l, --list                        list all available services
  -r, --resolve <arg>               resolve the service name to an init script
  -Z, --dry-run                     dry run (show what would happen)
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors)
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit status
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
12
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   78 root      0:00 /init
   79 root      0:00 /init
   80 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit status
   82 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 12
+ echo Service wsl-vpnkit is not running
Service wsl-vpnkit is not running
+ return 1
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit status
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
12
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   85 root      0:00 /init
   86 root      0:00 /init
   87 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit status
   89 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 12
+ echo Service wsl-vpnkit is not running
Service wsl-vpnkit is not running
+ return 1
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit start
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
12
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   92 root      0:00 /init
   93 root      0:00 /init
   94 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit start
   96 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 12
+ echo Service wsl-vpnkit is not running
+ return 1
+ echo 99
+ sleep 1
+ nohup wsl-vpnkit
+ status
+ _debug_check
+ '[' -n 1 ]
+ set -x
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
99
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   92 root      0:00 /init
   93 root      0:00 /init
   94 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit start
   99 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  140 root      0:00 wsl-vm -path /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe -iface eth1 -subnet 192.168.67.0/24 -gateway-ip 192.168.67.1 -host-ip 192.168.67.2 -vm-ip 192.168.67.3 -debug=false
  146 root      0:00 {wsl-gvproxy.exe} /init /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe -subnet 192.168.67.0/24 -gateway-ip 192.168.67.1 -hos
  185 root      0:00 nslookup -type A example.com 192.168.67.1
  187 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 99
+ echo Service wsl-vpnkit is running
Service wsl-vpnkit is running
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit status
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
99
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   93 root      0:00 /init
   99 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  196 root      0:00 ping -4 -c 1 example.com
  197 root      0:00 /init
  198 root      0:00 /init
  199 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit status
  201 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 99
+ echo Service wsl-vpnkit is running
Service wsl-vpnkit is running
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit status
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
99
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   93 root      0:00 /init
   99 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  196 root      0:00 ping -4 -c 1 example.com
  204 root      0:00 /init
  205 root      0:00 /init
  206 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit status
  208 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 99
+ echo Service wsl-vpnkit is running
Service wsl-vpnkit is running
$ ssh boostport
ssh: connect to host 10.0.0.1 port 9922: Network is unreachable
$ ssh boostport
ssh: connect to host 10.0.0.1 port 9922: Network is unreachable
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 ps -a
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
  253 root      0:00 /init
  254 root      0:00 /init
  255 root      0:00 ps -a
$ nano .zshrc
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 ps -a
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
    6 root      0:00 /init
    7 root      0:00 /init
    8 root      0:00 ps -a
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit start
+ test -f /var/run/wsl-vpnkit.pid
+ true
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
    9 root      0:00 /init
   10 root      0:00 /init
   11 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit start
   12 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ echo Service wsl-vpnkit is not running
+ return 1
+ echo 13
+ sleep 1
+ nohup wsl-vpnkit
+ status
+ _debug_check
+ '[' -n 1 ]
+ set -x
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
13
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
    9 root      0:00 /init
   10 root      0:00 /init
   11 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit start
   13 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
   54 root      0:00 wsl-vm -path /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe -iface eth1 -subnet 192.168.67.0/24 -gateway-ip 192.168.67.1 -host-ip 192.168.67.2 -vm-ip 192.168.67.3 -debug=false
   60 root      0:00 {wsl-gvproxy.exe} /init /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe /mnt/c/Users/user/wsl-vpnkit/wsl-gvproxy.exe -subnet 192.168.67.0/24 -gateway-ip 192.168.67.1 -hos
  111 root      0:00 nslookup -type AAAA example.com 1.1.1.1
  113 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 13
+ echo Service wsl-vpnkit is running
Service wsl-vpnkit is running
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 ps -a
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   10 root      0:00 /init
   13 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  116 root      0:00 ping -6 -c 1 example.com
  117 root      0:00 /init
  118 root      0:00 /init
  119 root      0:00 ps -a
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 ps -a
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   10 root      0:00 /init
   13 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  116 root      0:00 ping -6 -c 1 example.com
  120 root      0:00 /init
  121 root      0:00 /init
  122 root      0:00 ps -a
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 ps -a
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
   10 root      0:00 /init
   13 root      0:00 {wsl-vpnkit} /bin/sh /usr/sbin/wsl-vpnkit
  116 root      0:00 ping -6 -c 1 example.com
  123 root      0:00 /init
  124 root      0:00 /init
  125 root      0:00 ps -a
$ wsl.exe -d wsl-vpnkit --cd /app DEBUG=1 service wsl-vpnkit status
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
13
+ ps
PID   USER     TIME  COMMAND
    1 root      0:00 /init
    3 root      0:00 {init} plan9 --control-socket 5 --log-level 4 --server-fd 6 --pipe-fd 8 --log-truncate
  158 root      0:00 /init
  159 root      0:00 /init
  160 root      0:00 {wsl-vpnkit} /bin/sh -e /etc/init.d/wsl-vpnkit status
  162 root      0:00 ps
+ test -f /var/run/wsl-vpnkit.pid
+ cat /var/run/wsl-vpnkit.pid
+ pgrep -P 13
+ echo Service wsl-vpnkit is not running
Service wsl-vpnkit is not running
+ return 1

I wonder why wsl-gvproxy.exe suddenly disappears.

@josesa-xx josesa-xx marked this pull request as draft November 22, 2022 10:23
@josesa-xx
Copy link
Contributor Author

Yes it is still flacky.

Although it helps in case start-stop-daemon is not working at all, it still doesn't solve the problem in Windows 10 where wsl-gvproxy.exe dies on windows side shortly after it starts.

I'm keeping this open as alternative, but this needs more refinement.

- stop and restart actions are performed consistently
Improved build.sh
- able to build inside vpn if using http_proxy
@josesa-xx josesa-xx marked this pull request as ready for review December 1, 2022 08:22
@josesa-xx
Copy link
Contributor Author

I've reviewed the service script and now it behaves consistently to on all actions without breaking wsl-gvproxy connection.

@F21
Copy link

F21 commented Dec 1, 2022

Just tested this and I can confirm that it now stops and starts correctly.

Closes #165

Allow build with podman
Improve automated test timings
@josesa-xx
Copy link
Contributor Author

This also closes #171 and #160

@josesa-xx
Copy link
Contributor Author

@sakai135 please review.

./test.sh
```

Optionally you may build with `podman` instead of `docker` (default) by overriding environment variable `DOCKER`:
```sh
DOCKER=podman ./build.sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -8,7 +8,7 @@ RUN GOOS=windows go build -ldflags '-H=windowsgui' -o bin/wsl-gvproxy.exe ./cmd/
GOOS=linux CGO_ENABLED=0 go build -ldflags '-s -w' -o bin/wsl-vm ./cmd/vm && \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the gvisor-tap-vsock repo I maintain there is an additional comment to explain why -ldflags '-H=windowsgui' was added; as it allows backgrounding of the process. This got lost in the import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added as comment

@sakai135 sakai135 merged commit 939233d into sakai135:main Dec 6, 2022
@sakai135
Copy link
Owner

sakai135 commented Dec 6, 2022

Thanks @josesa-xx @F21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants