Skip to content

Instantly share code, notes, and snippets.

@arp242
Created April 8, 2021 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arp242/c4a38dce5ee6165b044887fe46d80892 to your computer and use it in GitHub Desktop.
Save arp242/c4a38dce5ee6165b044887fe46d80892 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -u
if [ "$(id -u)" = "0" ]; then
echo "You really don't need to run this as root."
exit 1
fi
if [ "$(lsb_release -cs)" != "void" ]; then
echo "This doesn't look like a Void Linux system?"
lsb_release -a
exit 1
fi
# Some settings.
repo="https://alpha.de.repo.voidlinux.org/current" # GNU libc; add /musl for musl libc.
cache="/tmp/void-go-cache" # xbps cache dir.
img="$(readlink -f ./go.tar)" # Image archive to create.
root="/tmp/void-go" # Directory to install Void to.
# Can be removed afterwards, but kept for manual frobbing.
# Bootstrap xbps.
mkdir -p "$root/var/db/xbps/keys"
cp "/var/db/xbps/keys/"*.plist "$root/var/db/xbps/keys/"
xbps-install -y -c "$cache" -r "$root" -R "$repo" -S
# A very minimal base; we don't need a lot of stuff in base-minimal. This ~45M.
xbps-install -y -c "$cache" -r "$root" -R "$repo" base-files glibc busybox-static ca-certificates
(
cd "$root/bin"
for t in $(./busybox.static --list); do [ -h $t ] || ln -s busybox.static $t; done
)
# We want Go and gcc (required for cgo and race detector).
xbps-install -y -c "$cache" -r "$root" -R "$repo" go gcc # ~560M
# Point Go cache to the right directory.
# go env -w AR=x
echo 'GOCACHE=/cache/go-build' > "$root/root/.config/go/env"
echo 'GOMODCACHE=/cache/go-mod' >> "$root/root/.config/go/env"
# Remove xbps cache, and some other files we (probably) don't need.
# This also removes C++ and ADA support from gcc.
rm -r \
"$root/var/cache/xbps" \
"$root/var/db/xbps/"*"/x86_64-repodata" \
"$root/usr/share/man" \
"$root/usr/share/info" \
"$root/usr/share/go/" \
"$root/usr/bin/"*c++* \
"$root/usr/bin/"*g++* \
"$root/usr/include/c++" \
"$root/usr/lib/gcc/"*/?.?/cc1plus \
"$root/usr/lib/gcc/"*/?.?/gnat1
# Strip binaries and libraries to reduce the size a bit.
find "$root" -executable -exec strip {} \+ >/dev/null 2>&1
# Create the image archive.
(cd "$root" && tar cf "$img" .)
echo "Done; created:"
(cd / && ls -lh "$img")
# End result: ~480M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment