Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Added uninstall.sh #43

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
TMPDIR ?= /var/tmp

all:
@echo Run \'sudo make install\' to install.

install:
@echo Online install for Ubuntu WSL2 systemd Hack
curl https://raw.githubusercontent.com/alanmburr/ubuntu-wsl2-systemd-script/master/ubuntu-wsl2-systemd-script.sh | bash
@echo Run \'sudo make disable\' to disable systemd hack
@echo Run \'sudo make uninstall\' to uninstall systemd hack

uninstall:
@echo Online uninstall for Ubuntu WSL2 systemd Hack
curl https://raw.githubusercontent.com/alanmburr/ubuntu-wsl2-systemd-script/master/uninstall.sh | bash

disable:
@echo Offline systemd Hack disable
@sudo grep -v "source /usr/sbin/start-systemd-namespace" /etc/bash.bashrc > $(TMPDIR)/tmpfile && mv $(TMPDIR)/tmpfile /etc/bash.bashrc
@echo Run \'sudo make uninstall\' to fully uninstall

23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# ubuntu-wsl2-systemd-script
Script to enable systemd support on current Ubuntu WSL2 images from the Windows store.
Tested on 18.04, 20.04 and the versionless (current) version of Ubuntu from the Windows Store.
Tested on 18.04, 20.04, 20.10 versions of Ubuntu from the Windows Store.
I am not responsible for broken installations, fights with your roommates and police ringing your door ;-).

*Note: I don't have the right tools to test/chagne this script at the moment. I you like to be an active maintainer, testing out PR's and updating code when necesary, please mail me (see profile).

Instructions from [the snapcraft forum](https://forum.snapcraft.io/t/running-snaps-on-wsl2-insiders-only-for-now/13033) turned into a script. Thanks to [Daniel](https://forum.snapcraft.io/u/daniel) on the Snapcraft forum!

## Usage
Expand All @@ -15,7 +13,7 @@ sudo apt install git
to do so.
### Run the script and commands
```sh
git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
git clone https://github.com/alanmburr/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script/
bash ubuntu-wsl2-systemd-script.sh
# Enter your password and wait until the script has finished
Expand All @@ -27,4 +25,19 @@ systemctl
```
If you don't get an error and see a list of units, the script worked.

Have fun using systemd on your Ubuntu WSL2 image. You may use and change and distribute this script in whatever way you'd like.
### Disabling from a broken shell
To disable this script (in any case), just press <kbd>Win</kbd> + <kbd>R</kbd> and type `wsl.exe -u root` and press <kbd>Enter</kbd>.
Then, continue the steps in the [Uninstalling section](#Uninstalling)


### Uninstalling
To uninstall open your distro and type:
```sh
git clone https://github.com/alanmburr/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script
bash uninstall.sh
# Enter your [sudo] password to uninstall.
```
Hopefully it works. If it don't, leave an issue :)

Have fun using systemd on your Ubuntu WSL2 image.
7 changes: 3 additions & 4 deletions enter-systemd-namespace
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash --norc

if [ "$LOGNAME" != "root" ]; then
echo "You need to run $0 through sudo"
exit 1
if [ "$(id -u)" != "0" ]; then
exec sudo "$0" "$1"
fi

if [ -x /usr/sbin/daemonize ]; then
Expand All @@ -24,7 +23,7 @@ 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"
Expand Down
2 changes: 1 addition & 1 deletion start-systemd-namespace
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

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 [ "$LOGNAME" != "root" ] && ( [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ] ); then
export | sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}' | \
Expand Down
16 changes: 11 additions & 5 deletions ubuntu-wsl2-systemd-script.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#!/bin/bash

if [ -f /usr/sbin/start-systemd-namespace ] && [ "$1" != "--force" ]; then
echo "It appears you have already installed the systemd hack."
echo "To forcibly reinstall, run this script with the \`--force\` parameter."
exit
if [ "$1" != "--force" ]; then
if [ -f /usr/sbin/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}')"
Expand Down
13 changes: 13 additions & 0 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
exec sudo "$0" "$1"
fi

cd2=$(pwd)
#sudo apt update -yqqqq
echo "Uninstalling ubuntu-wsl2-systemd-hack"
sudo rm -f /usr/sbin/start-systemd-namespace
sudo rm -f /usr/sbin/enter-systemd-namespace
cd /var/tmp
sudo grep -v "source /usr/sbin/start-systemd-namespace" /etc/bash.bashrc > tmpfile && mv tmpfile /etc/bash.bashrc
cd $cd2