Skip to content

Commit

Permalink
Rework systemd setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sileshn committed Nov 17, 2022
1 parent e7659f4 commit 74310be
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ Archlinux on WSL2 (Windows 10 FCU or later) based on [wsldl](https://github.com/
ArchWSL2 has the following features during the installation stage.
* Increase virtual disk size from the default 256GB
* Create a new user and set the user as default
* ArchWSL2 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. You will need the [store version of wsl](https://apps.microsoft.com/store/detail/windows-subsystem-for-linux-preview/9P9TQF7MRM4R) for native systemd support. For previous versions of wsl, systemd is supported using diddledani's [one-script-wsl2-systemd](https://github.com/diddledani/one-script-wsl2-systemd). You can do a minimal setup using [this](https://github.com/diddledani/one-script-wsl2-systemd#minimal-manual-installation) procedure using an older version of the script as the latest changes are not working. Use the commands below.
```cmd
sudo wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
sudo sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
sudo wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
sudo sed -i 's/\\nSystemd/Systemd/g' /etc/profile.d/00-wsl2-systemd.sh
```
* ArchWSL2 Supports systemd natively if you are running wsl v0.67.6 (more details here) and above. For earlier versions of wsl, systemd is supported using diddledani's one-script-wsl2-systemd. This is done automatically during initial setup.
* ArchWSL2 includes a wsl.conf file which only has [section headers](https://i.postimg.cc/MZ4DC1Fw/Screenshot-2022-02-02-071533.png). Users can use this file 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).

## Requirements
Expand Down
61 changes: 49 additions & 12 deletions bash_profile
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,21 @@ txtrst=$(tput sgr0)
diskvol=$(mount | grep -m1 ext4 | cut -f 1 -d " ")
sudo resize2fs $diskvol >/dev/null 2>&1
disksize=$(sudo blockdev --getsize64 $diskvol)
wslversion=$(wsl.exe --version | tr -d '\0' | sed -n 1p | cut -f3 -d " " | cut -f2-3 -d ".")
width=$(echo $COLUMNS)

if [ "$width" -gt 120 ]; then
width=120
fi

if (($(echo $wslversion '<' 67.6|bc))); then
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'\""
else
commandline="systemd=true"
fi
echo "$commandline" >> /etc/wsl.conf

test -e /mnt/c/Users/Public/vhdresize.txt && rm /mnt/c/Users/Public/vhdresize.txt
test -e /mnt/c/Users/Public/shutdown.cmd && rm /mnt/c/Users/Public/shutdown.cmd
test -e ~/vhdresize.txt && rm ~/vhdresize.txt
test -e ~/shutdown.cmd && rm ~/shutdown.cmd
figlet -t -k -f /usr/share/figlet/fonts/mini.flf "Welcome to ArchWSL" | lolcat
figlet -t -k -f /usr/share/figlet/fonts/mini.flf "Welcome to ArchWSL2" | lolcat
echo -e "\033[33;7mDo not interrupt or close the terminal window till script finishes execution!!!\n\033[0m"

if [ "$disksize" -le 274877906944 ]; then
echo -e ${grn}"ArchWSL'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
echo -e ${grn}"ArchWSL2'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
select yn in "Yes" "No"; do
case $yn in
Yes)
Expand Down Expand Up @@ -99,7 +91,7 @@ if [ "$disksize" -le 274877906944 ]; then
done

secs=3
printf ${ylw}"\nPlease grant diskpart elevated permissions when requested. ArchWSL will restart after disk resize.\n"${txtrst}
printf ${ylw}"\nPlease grant diskpart elevated permissions when requested. ArchWSL2 will restart after disk resize.\n"${txtrst}
printf ${red}"Warning!!! Any open wsl distros will be shutdown.\n\n"${txtrst}
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
Expand Down Expand Up @@ -154,8 +146,20 @@ select yn in "Yes" "No"; do
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
cp ~/shutdown.cmd /mnt/c/Users/Public

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
commandline="systemd=true"
echo "$commandline" >>/etc/wsl.conf
else
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'\""
echo "$commandline" >>/etc/wsl.conf
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
fi

secs=3
printf ${ylw}"\nTo set the new user as the default user, ArchWSL will shutdown and restart!!!\n\n"${txtrst}
printf ${ylw}"\nTo set the new user as the default user, ArchWSL2 will shutdown and restart!!!\n\n"${txtrst}
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
sleep 1
Expand All @@ -174,3 +178,36 @@ select yn in "Yes" "No"; do
;;
esac
done

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
commandline="systemd=true"
echo "$commandline" >>/etc/wsl.conf
else
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'\""
echo "$commandline" >>/etc/wsl.conf
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/main/src/sudoers -O /etc/sudoers.d/wsl2-systemd
sed -i 's/%sudo/%wheel/g' /etc/sudoers.d/wsl2-systemd
wget https://raw.githubusercontent.com/diddledani/one-script-wsl2-systemd/4dc64fba72251f1d9804ec64718bb005e6b27b62/src/00-wsl2-systemd.sh -P /etc/profile.d/
sed -i '/\\nSystemd/d' /etc/profile.d/00-wsl2-systemd.sh
fi

echo "@echo off" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
echo "wsl.exe --terminate $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
if env | grep "WT_SESSION" >/dev/null 2>&1; then
echo "wt.exe -w 0 nt wsl.exe -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
else
echo "cmd /c start \"$WSL_DISTRO_NAME\" wsl.exe --cd ~ -d $WSL_DISTRO_NAME" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
fi
echo "del C:\Users\Public\shutdown.cmd" | sudo tee -a ~/shutdown.cmd >/dev/null 2>&1
cp ~/shutdown.cmd /mnt/c/Users/Public

secs=3
printf ${ylw}"\nArchWSL2 will shutdown and restart to setup systemd!!!\n\n"${txtrst}
while [ $secs -gt 0 ]; do
printf "\r\033[KShutting down in %.d seconds. " $((secs--))
sleep 1
done

rm ~/.bash_profile
powershell.exe -command "Start-Process -Verb Open -FilePath 'shutdown.cmd' -WorkingDirectory 'C:\Users\Public' -WindowStyle Hidden"
exec sleep 0

0 comments on commit 74310be

Please sign in to comment.