Skip to content

Instantly share code, notes, and snippets.

@loopyd
Last active October 27, 2023 00:24
Show Gist options
  • Save loopyd/a10908a7c67b0ab68ab9805c442c0bf2 to your computer and use it in GitHub Desktop.
Save loopyd/a10908a7c67b0ab68ab9805c442c0bf2 to your computer and use it in GitHub Desktop.
[WSL2] Configuration injector
#!/bin/bash
# wsl-init.sh | Version 1.0.0
# License: Unilicense <https://unlicense.org/>
# Author: loopyd <loopyd@github.com>
#
# Simple bash script that injects preferred WSL configuration, and updates the system.
#
# USAGE INSTRUCTIONS
#
# 1. Clicking "RAW", and copy all to the clipboard by pressing Ctrl + A, and then Ctrl + C
#
# 2. Move to your terminal and type the following commands (in your WSL shell, at the bash
# prompt:
#
# cd $HOME
# touch wsl-init.sh
# chmod +x ./wsl-init.sh
# nano ./wsl-init.sh
#
# 3. You're in the nano editor now, so paste the contents of the clipboard and press Ctrl + X,
# and then Y [enter key] to apply the changes to the file.
#
# 4. Now you can run:
#
# ./wsl-init.sh
#
# To configure your WSL instance and default WSL configuration.
#
# CUSTOMIZATION
#
# For advanced users, you can edit the WSL configuration that gets
# injected by editing the here documents.
#
# If you'd like to add any additional packages to the installation
# you can modify the apt-get install line.
#
# THANK YOU!
#
# If you like this utility, give this gist a star.
#########################################################
# Pre-flight checks
if [ "$EUID" -ne 0 ]; then
echo "🆙 Auto-elevating script to root..."
exec sudo "$0" "$@"
fi
#########################################################
# Update system
echo '⏳ Updating system...'
{
apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y
apt-get install wslu wget dos2unix -y
} >/dev/null || {
echo '❌ Failed to update / install dependencies'
exit 1
}
echo '✅ Updated system successfully.'
#########################################################
# Update local WSL configuration
cat >/etc/wsl.conf <<'EOF'
[boot]
systemd=true
[automount]
enabled = true
root = /mnt/
options = "metadata,uid=1000,gid=1000,umask=077,fmask=11,case=off"
mountFsTab = true
[network]
hostname = SABER7OOTH-PC
generateHosts = true
generateResolvConf = true
[user]
default = saber7ooth
[interop]
enabled = true
appendWindowsPath = false
EOF
echo '✅ Injected local WSL configuration'
#########################################################
# Local wsl configuration.
{
wsl_USERPROFILE=`wslpath -au $(wslvar USERPROFILE)`
if [[ -z "$wsl_USERPROFILE" ]]; then
echo '❌ The C:\ drive is not mounted, please re-run this script after logging back in.' >&2
exit 1
fi
cat >"$wsl_USERPROFILE/.wslconfig" <<'EOF'
[wsl2]
memory = 8GB
localhostForwarding = true
swap = 16GB
swapFile = C:\\tools\\wsl\\swap.vhdx
guiApplications = true
nestedVirtualization = true
vmIdleTimeout = 120000
EOF
unix2dos "$wsl_USERPROFILE/.wslconfig" 2>/dev/null
} >/dev/null || {
exit 1
}
echo '✅ Injected windows WSL configuration'
#########################################################
# System tweaks
{
touch "$HOME/.hushlogin"
} >/dev/null || {
exit 1
}
echo "✅ Applied system tweaks."
echo '⚠️ Please log out, and restart this WSL instance to apply the changes'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment