DamionGans / ubuntu-wsl2-systemd-script

[Does not work anymore!] Script to enable systemd support on current Ubuntu WSL2 images

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Systemd starts but services don't

pablorq opened this issue · comments

After several tests, I can't find out how to do for systemd starts all the services.

If I start it without entering bash, I've got this:

>wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

>wsl /bin/false
Sleeping for 1 second to let systemd settle

>wsl ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  2 14:36 ?        00:00:00 /lib/systemd/systemd --unit=basic.target
root          46       1  2 14:36 ?        00:00:00 /lib/systemd/systemd-journald
root          63       1  2 14:36 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      67       1  1 14:36 ?        00:00:00 /lib/systemd/systemd-networkd
root         240       1  0 14:36 ?        00:00:00 snapfuse /var/lib/snapd/snaps/lxd_18150.snap /snap/lxd/18150 -o ro,n
message+     246       1  0 14:36 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfil
root         247       1  1 14:36 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
root         255       1  0 14:36 ?        00:00:00 /lib/systemd/systemd-hostnamed
systemd+     266       1  1 14:36 ?        00:00:00 /lib/systemd/systemd-resolved
root         316      63  2 14:36 ?        00:00:00 /lib/systemd/systemd-udevd
root         317      63  2 14:36 ?        00:00:00 /lib/systemd/systemd-udevd
root         318      63  0 14:36 ?        00:00:00 /lib/systemd/systemd-udevd
root         319       0  0 14:36 pts/0    00:00:00 /usr/bin/sudo -H -u [USER] /bin/bash -c set -a; [ -f "$HOME/.system
[USER]      320     319  0 14:36 pts/0    00:00:00 ps -ef

The /bin/false command is for trigger your script to enter the systemd namespace, without doing any else.

In case I enter the shell, I've got this:

>wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

>wsl
Sleeping for 1 second to let systemd settle
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of vie 27 nov 2020 14:37:24 CET

  System load:  0.6                Processes:             39
  Usage of /:   2.6% of 250.98GB   Users logged in:       0
  Memory usage: 6%                 IPv4 address for eth0: 192.168.26.186
  Swap usage:   0%

 * Introducing self-healing high availability clustering for MicroK8s!
   Super simple, hardened and opinionated Kubernetes for production.

     https://microk8s.io/high-availability

0 actualizaciones se pueden instalar inmediatamente.
0 de estas actualizaciones son una actualización de seguridad.


Last login: Fri Nov 27 14:37:06 CET 2020 on pts/0

$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  1 14:37 ?        00:00:00 /lib/systemd/systemd --unit=basic.target
root          46       1  0 14:37 ?        00:00:00 /lib/systemd/systemd-journald
root          63       1  0 14:37 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      70       1  0 14:37 ?        00:00:00 /lib/systemd/systemd-networkd
root         180       0  0 14:37 pts/0    00:00:00 /bin/login -p -f         'HOSTTYPE=x86_64' 'PWD=/mnt/d/apps/System'
root         256       1  0 14:37 ?        00:00:00 snapfuse /var/lib/snapd/snaps/lxd_18150.snap /snap/lxd/18150 -o ro,n
message+     262       1  0 14:37 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfil
root         263       1  0 14:37 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
systemd+     283       1  0 14:37 ?        00:00:00 /lib/systemd/systemd-resolved
root         366       1  0 14:37 ?        00:00:00 /lib/systemd/systemd-logind
[USER]      368       1  0 14:37 ?        00:00:00 /lib/systemd/systemd --user
[USER]      369     368  0 14:37 ?        00:00:00 (sd-pam)
[USER]      374     368  0 14:37 ?        00:00:00 /usr/bin/pulseaudio --daemonize=no --log-target=journal
[USER]      376     368  0 14:37 ?        00:00:00 /usr/libexec/tracker-miner-fs
[USER]      377     180  0 14:37 pts/0    00:00:00 -bash
[USER]      385     368  0 14:37 ?        00:00:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfi
rtkit        394       1  0 14:37 ?        00:00:00 /usr/libexec/rtkit-daemon
root         398       1  0 14:37 ?        00:00:00 /usr/lib/policykit-1/polkitd --no-debug
[USER]      406     368  0 14:37 ?        00:00:00 /usr/libexec/gvfsd
[USER]      421     368  0 14:37 ?        00:00:00 /usr/libexec/gvfsd-fuse /run/user/1000/gvfs -f -o big_writes
[USER]      427     368  0 14:37 ?        00:00:00 /usr/libexec/gvfs-udisks2-volume-monitor
root         431       1  0 14:37 ?        00:00:00 /usr/lib/udisks2/udisksd
[USER]      456     368  0 14:37 ?        00:00:00 /usr/libexec/gvfs-goa-volume-monitor
[USER]      460     368  0 14:37 ?        00:00:00 /usr/libexec/goa-daemon
[USER]      467     368  0 14:37 ?        00:00:00 /usr/libexec/goa-identity-service
[USER]      473     368  0 14:37 ?        00:00:00 /usr/libexec/gvfs-afc-volume-monitor
[USER]      478     368  0 14:37 ?        00:00:00 /usr/libexec/gvfs-mtp-volume-monitor
[USER]      482     368  0 14:37 ?        00:00:00 /usr/libexec/gvfs-gphoto2-volume-monitor
root         487       1  0 14:37 ?        00:00:00 /usr/lib/upower/upowerd
[USER]      513     377  0 14:37 pts/0    00:00:00 ps -ef

In any case I can start the services manually, but not all of them. For example:

>wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-20.04           Stopped         2
  docker-desktop-data    Running         2
  docker-desktop         Running         2

>wsl /bin/false
Sleeping for 1 second to let systemd settle

>wsl ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  1 14:41 ?        00:00:00 /lib/systemd/systemd --unit=basic.target
root          46       1  1 14:41 ?        00:00:00 /lib/systemd/systemd-journald
root          63       1  1 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      70       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-networkd
root         242       1  0 14:41 ?        00:00:00 snapfuse /var/lib/snapd/snaps/lxd_18150.snap /snap/lxd/18150 -o ro,n
message+     248       1  0 14:41 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfil
root         249       1  0 14:41 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
root         254       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-hostnamed
root         258       1  0 14:41 ?        00:00:00 /usr/lib/NetworkManager/nm-dispatcher
systemd+     269       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-resolved
root         322      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         323      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         324      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         325      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         327      63  1 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         330       0  0 14:41 pts/0    00:00:00 /usr/bin/sudo -H -u [USER] /bin/bash -c set -a; [ -f "$HOME/.system
[USER]      331     330  0 14:41 pts/0    00:00:00 ps -ef

>wsl sudo systemctl start cron.service
[sudo] password for [USER]:

>wsl ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 14:41 ?        00:00:00 /lib/systemd/systemd --unit=basic.target
root          46       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-journald
root          63       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      70       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-networkd
root         242       1  0 14:41 ?        00:00:00 snapfuse /var/lib/snapd/snaps/lxd_18150.snap /snap/lxd/18150 -o ro,n
message+     248       1  0 14:41 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfil
root         249       1  0 14:41 ?        00:00:00 /usr/sbin/NetworkManager --no-daemon
systemd+     269       1  0 14:41 ?        00:00:00 /lib/systemd/systemd-resolved
root         339      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         340      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         341      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         342      63  0 14:41 ?        00:00:00 /lib/systemd/systemd-udevd
root         347       1  0 14:41 ?        00:00:00 /usr/sbin/cron -f
root         348       0  0 14:42 pts/0    00:00:00 /usr/bin/sudo -H -u [USER] /bin/bash -c set -a; [ -f "$HOME/.system
[USER]      349     348  0 14:42 pts/0    00:00:00 ps -ef

Any idea how to do for starting all the missing services?

[SOLVED] All enabled systemd services are run on boot

Doing a lot of research I was thinking about to read all enabled services and start all of them with an script, after systemd was running as PID 1.

But this is the work that systemd has to do... when going to multi-user.target.

So that's all. Change from --unit=basic.target to --unit=multi-user.target in both scripts and reboot wsl (no the windows machine). After that you will have all services running, like cron or snap. In fact, this fix the "all snap packages broken after boot" issue.

Summary
Changes to do:

File start-systemd-namespace

#SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
SYSTEMD_EXE="/lib/systemd/systemd --unit=multi-user.target"

File enter-systemd-namespace

#SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
SYSTEMD_EXE="/lib/systemd/systemd --unit=multi-user.target"

WARINING
A collateral effect is related to DNS resolution, and the WSL original name resolution file is overwritten by a new one with wrong nameservers.

Easy fix: disable systemd name resolution:

$ sudo systemctl disable systemd-resolved.service