windows WSL 初始化起步设置脚本

14 min read
#!/usr/bin/env bash
# vim: noet ft=sh sw=4 ts=4:
#
# Author: Augusto Pascutti <[email protected]>
#
# This will help you bootstrap a Windows machine with WSL with Docker
# and some other utilities. This is heavily customized for my own use.
#
# Requirements (or things that should already be installed):
#   - WSL (with Debian or Ubuntu): `Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux`
#   - Docker for Windows

EXTRA_APT_PACKAGES_TO_INSTALL="git vim curl jq gnupg2 tmux lsof htop par tree bash bash-completion wget"

indent()
{
	sed 's/^/   /'
}

# https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly
install_docker()
{
	sudo apt-get update
	sudo apt-get remove -y docker docker-engine docker.io containerd runc

	sudo apt-get install -y \
		apt-transport-https \
		ca-certificates \
		curl \
		gnupg2 \
		software-properties-common

	curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - 2>&1
	sudo apt-key fingerprint 0EBFCD88 2>&1

	sudo add-apt-repository \
		"deb [arch=amd64] https://download.docker.com/linux/debian \
		$(lsb_release -cs) \
		stable"

	sudo apt-get update
	sudo apt-get install -y \
		docker-ce docker-ce-cli containerd.io
	sudo usermod -aG docker $USER
	echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc

	sudo cat <<-EOF > /tmp/wsl.conf
	[automount]
	root = /
	options = "metadata"
	EOF
	sudo mv /tmp/wsl.conf /etc/wsl.conf
	cat <<-EOT
	>>> Restart the terminal and run "docker run hellow-world" to test Docker installation.
	EOT
}

install_extra()
{
	sudo apt-get update
	sudo apt-get upgrade -y
	sudo apt-get install -y \
		${EXTRA_APT_PACKAGES_TO_INSTALL}

}

install_updates()
{
	sudo apt-get update
	sudo apt-get upgrade -y
}

# https://gist.github.com/augustohp/0b0f96249e399d4ec731830280fbe776
install_castles()
{
	if [ -d "~/.homesick/repos" ]
	then
		echo "Skipping as there are castles already cloned and this is intended as a bootstrap."
		return 0
	fi

	bash <(curl --silent -L https://git.io/fNMTH)
}

main()
{
	echo "Updating installed packages..."
	install_updates | indent
	echo "Installing extra packages..."
	install_extra | indent
	echo "Installing Docker (removing old one) which will connect to Docker-CE for Windows..."
	install_docker | indent
	echo "Install castles..."
	install_castles
}

main