This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
forked from DamionGans/ubuntu-wsl2-systemd-script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
enter-systemd-namespace
executable file
·57 lines (51 loc) · 1.91 KB
/
enter-systemd-namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash --norc
if [ "$LOGNAME" != "root" ]; then
echo "You need to run $0 through sudo"
exit 1
fi
if [ -x /usr/sbin/daemonize ]; then
DAEMONIZE=/usr/sbin/daemonize
elif [ -x /usr/bin/daemonize ]; then
DAEMONIZE=/usr/bin/daemonize
else
echo "Cannot execute daemonize to start systemd."
exit 1
fi
if ! command -v /lib/systemd/systemd > /dev/null; then
echo "Cannot execute /lib/systemd/systemd."
exit 1
fi
if ! command -v /usr/bin/unshare > /dev/null; then
echo "Cannot execute /usr/bin/unshare."
exit 1
fi
SYSTEMD_EXE="/lib/systemd/systemd --unit=multi-user.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"
while [ -z "$SYSTEMD_PID" ]; do
#echo "Sleeping for 1 second to let systemd settle"
sleep 1
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
done
if [ "${USE_WSLG_SOCKET}" != "false" ]; then
if [ ! -L "/tmp/.X11-unix" ]; then
rm -rf "/tmp/.X11-unix"
ln -s /mnt/wslg/.X11-unix /tmp/.X11-unix
fi
fi
fi
USER_USERNAME=${SUDO_USER:-$LOGNAME}
USER_HOME="$(getent passwd | awk -F: '$1=="'"$USER_USERNAME"'" {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 "$USER_USERNAME" \
/bin/bash --norc -c "set -a; [ -f \"\$HOME/.systemd-env\" ] && source \"\$HOME/.systemd-env\"; set +a; $@"
else
exec /usr/bin/nsenter -t "$SYSTEMD_PID" -m -p \
/bin/login -p -f "$USER_USERNAME"
fi
echo "Existential crisis"
exit 1
fi