From ebd014b41d7fffcdca982ca2b05796274b8c0449 Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Wed, 29 Jul 2020 01:33:31 +0100 Subject: [PATCH 1/2] Remove useless part of login command * VSCode WSL-remote fails: issue #33 * `/bin/login` does not support environment variable setting via the command line when used with the `-f` flag that allows login to operate without requesting a password. Signed-off-by: Daniel Llewellyn --- enter-systemd-namespace | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/enter-systemd-namespace b/enter-systemd-namespace index 8304fb1..0f1af04 100755 --- a/enter-systemd-namespace +++ b/enter-systemd-namespace @@ -42,9 +42,7 @@ if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then /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" -a \ - /bin/login -p -f "$SUDO_USER" \ - $([ -f "$USER_HOME/.systemd-env" ] && /bin/cat "$USER_HOME/.systemd-env" | xargs printf ' %q') + exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a /bin/login -p -f "$SUDO_USER" fi echo "Existential crisis" exit 1 From ad455038dfc1705826cf1020ad95d8f94bb1421c Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Wed, 29 Jul 2020 02:47:36 +0100 Subject: [PATCH 2/2] Use /bin/sh wherever possible This reduces the proliferation of recursion that can become difficult to understand or reason about. By using `/bin/sh` in as many places as possible we prevent the bash rcfile from kicking-in and performing the namespace stuff many times over. Signed-off-by: Daniel Llewellyn --- enter-systemd-namespace | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/enter-systemd-namespace b/enter-systemd-namespace index 0f1af04..b2b0cb0 100755 --- a/enter-systemd-namespace +++ b/enter-systemd-namespace @@ -24,10 +24,10 @@ if ! command -v /usr/bin/unshare > /dev/null; then exit 1 fi -SYSTEMD_EXE="/lib/systemd/systemd --system-unit=basic.target" +SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target" SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')" if [ -z "$SYSTEMD_PID" ]; then - "$DAEMONIZE" /usr/bin/unshare --fork --pid --mount-proc bash -c 'export container=wsl; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; exec '"$SYSTEMD_EXE" + "$DAEMONIZE" /usr/bin/unshare --fork --pid --mount-proc sh -c 'export container=wsl; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; exec '"$SYSTEMD_EXE" while [ -z "$SYSTEMD_PID" ]; do echo "Sleeping for 1 second to let systemd settle" sleep 1 @@ -37,10 +37,10 @@ fi 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 + if [ -n "$1" ]; then exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a \ /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" "$@")" + /bin/sh -c 'set -a; [ -f "$HOME/.systemd-env" ] && . "$HOME/.systemd-env"; set +a; exec '"$*" else exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a /bin/login -p -f "$SUDO_USER" fi