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

nsenter: cannot open /proc/320/ns/time: No such file or directory

jototland opened this issue · comments

I get this after running your script.
nsenter: cannot open /proc/320/ns/time: No such file or directory

Luckily, I can "wsl -e bash --norc" and revert the changes to /etc/bash.bashrc. But what is wrong with my /proc since nsenter doesn't like it?

Windows version 19041.388
WSL 2
Pengwin

Ok, I tested again with ubuntu instead of pengwin, and the script worked just fine. I guess that might be the reason the script has ubuntu in it's name :-)

I would still like to make it work for pengwin, though...

exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a \

this error because nsenter CMD -a maybe include time namespaces, you can diff ubuntu and pengwin nsenter --help

The time namespace support has been merged in the 5.6 kernel.

TL;DR: Change options of nsenter from -a to -m -p

Thank you @eternalphane for the information, but this could be confusing for some people.

Based on that data, this is how to fix it:

  • Replace (copy and paste) the following lines of enter-systemd-namespace file. You can do it on Windows side.
USER_HOME="$(getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}')"
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    if [ -n "$1" ] && [ "$1" != "bash --login" ] && [ "$1" != "/bin/bash --login" ]; then
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /usr/bin/sudo -H -u "$SUDO_USER" \
            /bin/bash -c 'set -a; [ -f "$HOME/.systemd-env" ] && source "$HOME/.systemd-env"; set +a; exec bash -c '"$(printf "%q" "$@")"
    else
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /bin/login -p -f "$SUDO_USER" \
            $([ -f "$USER_HOME/.systemd-env" ] && /bin/cat "$USER_HOME/.systemd-env" | xargs printf ' %q')
    fi
    echo "Existential crisis"
    exit 1
fi
  • Get inside the broken Linux distribution:
> wsl bash --norc
  • Reinstall it:
$ bash ubuntu-wsl2-systemd-script.sh --force
  • Worked on:
    • WSL2 + Ubuntu-20.10

Worket on Debian 11 release! Thank you!

Thanks @pablorq !
This was needed for me to get in working on WSL2+Ubuntu after release upgrade to 21.04!

I'm glad it helps!

🙂

@s4pfyr How exactly did you do it? After upgrading, mine started throwing Failed to enable unit: Transport endpoint is not connected (Docker) and nsenter: cannot open /proc/9286/ns/time: No such file or directory when running exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof -s systemd) -a su - $LOGNAME

This was messy for me before, so I enabled systemd by doing this

sudo apt-get install -yqq daemonize dbus-user-session fontconfig
sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target

@hyoretsu I did not have the nsenter problem after switching the flags like @pablorq suggested.
Regarding docker: I had problems with iptables and needed to do sudo update-alternatives --config iptables and select legancy mode. Try starting the docker daemon with sudo dockerd --debug to figure out whats going on with docker.

I'm even unable to run the install script with --force:

nsenter: cannot open /proc/830/ns/time: No such file or directory

I'm even unable to run the install script with --force:

nsenter: cannot open /proc/830/ns/time: No such file or directory

There are two commands, they each work in different cases:
exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof systemd) -a su - $LOGNAME
exec sudo nsenter -m -u -i -n -p -C -r -w -t $(pidof systemd) -m -p su - $LOGNAME (just noticed mp being repeated, might not be necessary)

If that doesn't work, I believe you need to do it manually

I managed to successfully update to 21.10 btw
microsoft/WSL#5126 (comment)

ok so have I understood it correctly.
I need to replace the LOGNAME with my home user name and pidof systemd.
But how can I find the PID of systemd if systemd doesnt work? (Or even if it works)

ok so have I understood it correctly. I need to replace the LOGNAME with my home user name and pidof systemd. But how can I find the PID of systemd if systemd doesnt work? (Or even if it works)

top systemd/htop systemd though keep in mind that the PID changes sometimes.

I try

wsl bash --norc
nsenter: cannot open /proc/26/ns/time: No such file or directory

it does not work, but

wsl --exec /usr/bin/sh
$

work and then I used @pablorq's method.

I successfully changed enter-systemd-namespace, but even after reinstalling the script I get the same error. What am I supposed to do?

Check your the endfile /usr/sbin/enter-systemd-namespace, if need change it.

Apparently, changing it before rebooting (so before the error happens) and not reinstalling the script as opposed to what @pablorq suggested works like a charm (at least for me). Thank you all very much.

As I seen in: https://www.csa.iisc.ac.in/~vg/teaching/E0-256/slides/lxc-slide-deck.pdf
/proc/pid/ns/time is new, so I think that there may be patch for update, or just downgrade some services.

Also: when running: lsns -l, you should see the namespaces list for linux kernel.
If you cannot see 'time', then you should provide support them.

For me - I don't know how can I downgrade the version, and which one? (Or running a new patch), but I presume this can be solved by simple commands.

I ran this script ( ubuntu-wsl2-systemd-script) originally but did not have success. So I waited for upgrade to 22.04 like most suggested and after upgrading I run into this error:
nsenter: cannot open /proc/27/ns/time: No such file or directory

[process exited with code 1 (0x00000001)]

wsl -e /usr/libexec/nslogin /bin/bash in command prompt allows me to the enter the terminal. How can I get my linux terminal to open without error now?

Thanks @pablorq, worked for me!

Hi there,

I updated from Ubuntu 20.04 to 22.04 via console (do-release-upgrade).

Unfortunately the solution of @pablorq only works partly. The system starts now, but only it is not possible to login as normal user. wsl -u root works totally fine and I can login. Systemd runs fine.

root@MADAGASKAR:/mnt/c/Users/myuser# ps aux | grep systemd
root          43  0.0  0.0   6140   932 ?        Ss   21:05   0:00 /usr/bin/unshare --fork --pid --mount-proc bash -c export container=wsl; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; exec /lib/systemd/systemd --unit=basic.target
root          45 99.4  0.0  21940 11544 ?        Rs   21:05   1:37 /lib/systemd/systemd --unit=basic.target
root          93 32.8  0.9 187216 147168 ?       S<s  21:05   0:32 /lib/systemd/systemd-journald
root         119  0.2  0.0  22820  6136 ?        Ss   21:05   0:00 /lib/systemd/systemd-udevd
systemd+     140  0.0  0.0  16064  6548 ?        Ss   21:05   0:00 /lib/systemd/systemd-networkd
root         305  0.0  0.0  11992  5704 pts/1    Ss+  21:06   0:00 sudo /usr/sbin/enter-systemd-namespace
root         314  0.0  0.0  11992   884 pts/2    Ss   21:06   0:00 sudo /usr/sbin/enter-systemd-namespace
root         315  0.0  0.0   7740  3556 pts/2    S+   21:06   0:00 /bin/bash --norc /usr/sbin/enter-systemd-namespace
root         319  0.0  0.0   7740  1572 pts/2    S+   21:06   0:00 /bin/bash --norc /usr/sbin/enter-systemd-namespace
root         323  0.0  0.0   6868  2376 pts/0    S+   21:06   0:00 grep --color=auto systemd

tail -f /var/log/syslog gives

systemd-fstab-generator[14]: Failed to create unit file /run/systemd/generator/-.mount, as it already exists. Duplicate entry in /etc/fstab?
systemd-sysv-generator[22]: Failed to create unit file /run/systemd/generator.late/hddtemp.service: File exists
systemd-sysv-generator[22]: Failed to create unit file /run/systemd/generator.late/apport.service: File exists
systemd-journald[32]: File /var/log/journal/cad812440ce02ca8f59723955f259f43/system.journal corrupted or uncleanly shut down, renaming and replacing.
systemd-journald[32]: Failed to read journal file /var/log/journal/cad812440ce02ca8f59723955f259f43/user-1000.journal for rotation, trying to move it out of the way: Device or resource busy
systemd-journald[32]: Failed to read journal file /var/log/journal/cad812440ce02ca8f59723955f259f43/user-1000.journal for rotation, trying to move it out of the way: Device or resource busy

File system is not full (332G free):

 Dateisystem    Größe Benutzt Verf.     Verw% Eingehängt auf
 /dev/sdc          251G   88G      151G   37%      /
 drvfs                953G   621G     332G   66%     /mnt/c

After login (myuser is my sudo user):

PS C:\Users\myuser> wsl -u myuser
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64)
[...]
System information as of Do 12. Mai 20:39:56 CEST 2022
System load:  1.38037109375       Processes:             11
Usage of /:   35.5% of 250.98GB   Users logged in:       0
Memory usage: 2%                  IPv4 address for eth0: 172.21.148.45
Swap usage:   0%

Letzte Anmeldung: Donnerstag, 12. Mai 2022, 20:35:47 CEST auf pts/0

Login nach 60 Sekunden wegen 
Zeitüberschreitung abgebrochen.
# Login canceld after 60 seconds

What I found: the variable $HOME_USER which is used in /usr/sbin/enter-systemd-namespace is empty, if used in shell.

That#s why getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}'returns nothing.

I also tried to "trick" the script by setting the user and users home manually ...

# USER_HOME="$(getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}')"
export USER_HOME="/home/myuser"
export SUDO_USER="myuser"

... also here: the sytem starts, but login with normal user (!) is canceled after 60 seconds without success.

With the last method, $HOME/.systemd-env is created correct:

root@MADAGASKAR:/home/apos# cat .systemd-env
DISPLAY=":0"
HOSTTYPE="x86_64"
PULSE_SERVER="/mnt/wslg/PulseServer"
PWD="/home/apos"
TERM="xterm-256color"
WAYLAND_DISPLAY="wayland-0"
WSLENV="WT_SESSION:BASH_ENV/u:WT_PROFILE_ID"
WSL_DISTRO_NAME="Ubuntu-20.04"
WSL_INTEROP="/run/WSL/1027_interop"
WT_PROFILE_ID="{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}"
WT_SESSION="b342d5dc-46b4-4253-a7ac-df7a4d0a6f7b"
XDG_RUNTIME_DIR="/mnt/wslg/runtime-dir"

If I edit /etc/bash.bashrc and alter the first line to
[ "$USER" == "root" ] && source /usr/sbin/start-systemd-namespace

start wsl -u root once, so systemd is running - I can start alls apps via Startmenü. Also Xterm.

Any idea?

Cheers Axel

TL;DR: Change options of nsenter from -a to -m -p

This works perfectly after using do-release-upgrade to 21.10 Impish.
No need to reinstall though, that made it break again for me.

commented
USER_HOME="$(getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}')"
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    if [ -n "$1" ] && [ "$1" != "bash --login" ] && [ "$1" != "/bin/bash --login" ]; then
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /usr/bin/sudo -H -u "$SUDO_USER" \
            /bin/bash -c 'set -a; [ -f "$HOME/.systemd-env" ] && source "$HOME/.systemd-env"; set +a; exec bash -c '"$(printf "%q" "$@")"
    else
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /bin/login -p -f "$SUDO_USER" \
            $([ -f "$USER_HOME/.systemd-env" ] && /bin/cat "$USER_HOME/.systemd-env" | xargs printf ' %q')
    fi
    echo "Existential crisis"
    exit 1
fi

it works on wsl2 + Ubuntu 22.04.1

#36 (comment) working for me !!! THANK"S you save my life

bash ubuntu-wsl2-systemd-script.sh --force

@pablorq thanks for the save, however worked for me after running bash ubuntu-wsl2-systemd-script.sh --force with sudo

Thanks @jototland for opening the ticket, and thanks @pablorq for the simplification of the fix!
Had to use this fix on WSL2+Ubuntu after release upgrade to 22.04.

Hi guys
I've upgraded 20.04 to 22.04,
I've got the problem with /proc/xx/ns/time,
I've changed the enter-systemd-namespace ; the problem /proc ... was fixed
but when I start wsl,

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.79.1-microsoft-standard-WSL2 x86_64)

System information as of sam. 31 déc. 2022 18:33:48 CET

System load: 0.830078125 Processes: 9
Usage of /: 6.7% of 250.92GB Users logged in: 0
Memory usage: 4% IPv4 address for eth0: 172.23.118.163
Swap usage: 0%

3 mises à jour peuvent être appliquées immédiatement.
Pour afficher ces mises à jour supplémentaires, exécuter : apt list --upgradable

Last login: Sat Dec 31 18:23:02 CET 2022 on pts/0

then I've got "Login timed out after 60 seconds."

after some investigations, i've found the command "getent password" never end
any idea ?
thanks

Was fixed in nsenter, see util-linux/util-linux#2059

Worked on WSL2 + Ubuntu 22.04, also
Thanks @pablorq

commented

TL;DR: Change options of nsenter from -a to -m -p

Thank you @eternalphane for the information, but this could be confusing for some people.

Based on that data, this is how to fix it:

  • Replace (copy and paste) the following lines of enter-systemd-namespace file. You can do it on Windows side.
USER_HOME="$(getent passwd | awk -F: '$1=="'"$SUDO_USER"'" {print $6}')"
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    if [ -n "$1" ] && [ "$1" != "bash --login" ] && [ "$1" != "/bin/bash --login" ]; then
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /usr/bin/sudo -H -u "$SUDO_USER" \
            /bin/bash -c 'set -a; [ -f "$HOME/.systemd-env" ] && source "$HOME/.systemd-env"; set +a; exec bash -c '"$(printf "%q" "$@")"
    else
        exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
            /bin/login -p -f "$SUDO_USER" \
            $([ -f "$USER_HOME/.systemd-env" ] && /bin/cat "$USER_HOME/.systemd-env" | xargs printf ' %q')
    fi
    echo "Existential crisis"
    exit 1
fi
  • Get inside the broken Linux distribution:
> **wsl -e bash --norc**
  • Reinstall it:
$ **sudo** bash ubuntu-wsl2-systemd-script.sh --force
  • Worked on:

    • WSL2 + Ubuntu-20.10