wsl2-systemd script
This commit is contained in:
51
enter-systemd-namespace
Executable file
51
enter-systemd-namespace
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/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=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"
|
||||
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
|
||||
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
|
||||
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" "$@")"
|
||||
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')
|
||||
fi
|
||||
echo "Existential crisis"
|
||||
exit 1
|
||||
fi
|
@@ -39,6 +39,11 @@ if [ "$ADDITIONALS" == "true" ]; then
|
||||
echo "Additional install has started"
|
||||
sh /additional_install.sh
|
||||
echo "Additional install has finished"
|
||||
else if [ "$WSL2" == "true" ]; then
|
||||
echo "WSL2 systemd script has started"
|
||||
sh /wsl2-systemd-script.sh
|
||||
#systemctl
|
||||
echo "WSL2 systemd script has finished"
|
||||
else
|
||||
if [[ "$SMARTHOST_PROXY" == "YES" || "$SMARTHOST_PROXY" == "TRUE" ]]; then
|
||||
PROXY_TYPE=smarthost-proxy" "$PROXY_TYPE;
|
||||
|
15
install.sh
15
install.sh
@@ -648,6 +648,21 @@ if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ] || [ "$ANSWER" == "" ]; then
|
||||
registry.format.hu/installer-tool
|
||||
fi
|
||||
|
||||
# enable systemd support on current images
|
||||
echo "Would you like to install and enable systemd support? (Y/n)";
|
||||
read -r ANSWER;
|
||||
if [ "$ANSWER" == "y" ] || [ "$ANSWER" == "Y" ] || [ "$ANSWER" == "" ] ; then
|
||||
|
||||
# Run installer tool
|
||||
$SUDO_CMD docker run \
|
||||
--env WSL2=true \
|
||||
--env SUDO_CMD=$SUDO_CMD \
|
||||
--volume $HOME/.ssh/installer:/root/.ssh/id_rsa \
|
||||
--volume /etc/user/:/etc/user/ \
|
||||
--volume /etc/system/:/etc/system/ \
|
||||
registry.format.hu/installer-tool
|
||||
fi;
|
||||
|
||||
shopt -s expand_aliases
|
||||
source $HOME/.bash_aliases
|
||||
|
||||
|
22
start-systemd-namespace
Executable file
22
start-systemd-namespace
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/bin/sh
|
||||
|
||||
SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
|
||||
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
|
||||
if [ "$LOGNAME" != "root" ] && ( [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ] ); then
|
||||
export | sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}' | \
|
||||
grep -E -v "^(BASH|BASH_ENV|DIRSTACK|EUID|GROUPS|HOME|HOSTNAME|\
|
||||
IFS|LANG|LOGNAME|MACHTYPE|MAIL|NAME|OLDPWD|OPTERR|\
|
||||
OSTYPE|PATH|PIPESTATUS|POSIXLY_CORRECT|PPID|PS1|PS4|\
|
||||
SHELL|SHELLOPTS|SHLVL|SYSTEMD_PID|UID|USER|_)(=|\$)" > "$HOME/.systemd-env"
|
||||
export PRE_NAMESPACE_PATH="$PATH"
|
||||
export PRE_NAMESPACE_PWD="$(pwd)"
|
||||
exec $SUDO_CMD /enter-systemd-namespace "$BASH_EXECUTION_STRING"
|
||||
fi
|
||||
if [ -n "$PRE_NAMESPACE_PATH" ]; then
|
||||
export PATH="$PRE_NAMESPACE_PATH"
|
||||
unset PRE_NAMESPACE_PATH
|
||||
fi
|
||||
if [ -n "$PRE_NAMESPACE_PWD" ]; then
|
||||
cd "$PRE_NAMESPACE_PWD"
|
||||
unset PRE_NAMESPACE_PWD
|
||||
fi
|
90
wsl2-systemd-script.sh
Executable file
90
wsl2-systemd-script.sh
Executable file
@@ -0,0 +1,90 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "$1" != "--force" ]; then
|
||||
if [ -f /start-systemd-namespace ]; then
|
||||
echo "It appears you have already installed the systemd hack."
|
||||
echo "To forcibly reinstall, run this script with the \`--force\` parameter."
|
||||
exit
|
||||
fi
|
||||
if [ -z "$WSL_DISTRO_NAME" ]; then
|
||||
echo "It appears that you are not running on WSL."
|
||||
echo "To forcibly install anyway, run this script with the \`--force\` parameter."
|
||||
exit
|
||||
fi
|
||||
fi
|
||||
|
||||
#self_dir="$(dirname $0)"
|
||||
|
||||
function interop_prefix {
|
||||
win_location="/mnt/"
|
||||
if [ -f /etc/wsl.conf ]; then
|
||||
tmp="$(awk -F '=' '/root/ {print $2}' /etc/wsl.conf | awk '{$1=$1;print}')"
|
||||
[ "$tmp" == "" ] || win_location="$tmp"
|
||||
unset tmp
|
||||
fi
|
||||
echo "$win_location"
|
||||
|
||||
unset win_location
|
||||
}
|
||||
|
||||
function sysdrive_prefix {
|
||||
win_location="$(interop_prefix)"
|
||||
hard_reset=0
|
||||
for pt in $(ls "$win_location"); do
|
||||
if [ $(echo "$pt" | wc -l) -eq 1 ]; then
|
||||
if [ -d "$win_location$pt/Windows/System32" ]; then
|
||||
hard_reset=1
|
||||
win_location="$pt"
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $hard_reset -eq 0 ]; then
|
||||
win_location="c"
|
||||
fi
|
||||
|
||||
echo "$win_location"
|
||||
|
||||
unset win_location
|
||||
unset hard_reset
|
||||
}
|
||||
|
||||
$SUDO_CMD hwclock -s
|
||||
$SUDO_CMD apt-get update && $SUDO_CMD apt-get install -yqq daemonize dbus-user-session fontconfig
|
||||
|
||||
#$SUDO_CMD cp "$self_dir/start-systemd-namespace" /start-systemd-namespace
|
||||
#$SUDO_CMD cp "$self_dir/enter-systemd-namespace" /enter-systemd-namespace
|
||||
$SUDO_CMD chmod +x /enter-systemd-namespace
|
||||
|
||||
$SUDO_CMD tee /etc/$SUDO_CMDers.d/systemd-namespace >/dev/null <<EOF
|
||||
Defaults env_keep += WSLPATH
|
||||
Defaults env_keep += WSLENV
|
||||
Defaults env_keep += WSL_INTEROP
|
||||
Defaults env_keep += WSL_DISTRO_NAME
|
||||
Defaults env_keep += PRE_NAMESPACE_PATH
|
||||
Defaults env_keep += PRE_NAMESPACE_PWD
|
||||
%$SUDO_CMD ALL=(ALL) NOPASSWD: /enter-systemd-namespace
|
||||
EOF
|
||||
|
||||
if ! grep 'start-systemd-namespace' /etc/bash.bashrc >/dev/null; then
|
||||
$SUDO_CMD sed -i 2a"# Start or enter a PID namespace in WSL2\nsource /start-systemd-namespace\n" /etc/bash.bashrc
|
||||
fi
|
||||
|
||||
$SUDO_CMD rm -f /etc/systemd/user/sockets.target.wants/dirmngr.socket
|
||||
$SUDO_CMD rm -f /etc/systemd/user/sockets.target.wants/gpg-agent*.socket
|
||||
$SUDO_CMD rm -f /lib/systemd/system/sysinit.target.wants/proc-sys-fs-binfmt_misc.automount
|
||||
$SUDO_CMD rm -f /lib/systemd/system/sysinit.target.wants/proc-sys-fs-binfmt_misc.mount
|
||||
$SUDO_CMD rm -f /lib/systemd/system/sysinit.target.wants/systemd-binfmt.service
|
||||
|
||||
if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ] && [ "$(head -n1 /proc/sys/fs/binfmt_misc/WSLInterop)" == "enabled" ]; then
|
||||
"$(interop_prefix)$(sysdrive_prefix)"/Windows/System32/cmd.exe /C setx WSLENV BASH_ENV/u
|
||||
"$(interop_prefix)$(sysdrive_prefix)"/Windows/System32/cmd.exe /C setx BASH_ENV /etc/bash.bashrc
|
||||
else
|
||||
echo
|
||||
echo "You need to manually run the following two commands in Windows' cmd.exe:"
|
||||
echo
|
||||
echo " setx WSLENV BASH_ENV/u"
|
||||
echo " setx BASH_ENV /etc/bash.bashrc"
|
||||
echo
|
||||
fi
|
Reference in New Issue
Block a user