Initial work for building alpine image.

We are downloading a base alpine filesystem and launching a jail inside of it. Still need to install docker, build an initfs, set up EFI, and actually dump it all into an image.
This commit is contained in:
Tom Alexander 2022-02-05 14:24:32 -05:00
parent 8b1169a684
commit d5b80b6a03
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -0,0 +1,175 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
############## Setup #########################
function cleanup {
for f in "${folders[@]}"; do
>&2 echo "Deleting $f"
rm -rf "$f"
done
}
folders=()
for sig in EXIT INT QUIT HUP TERM; do
trap "set +e; cleanup" "$sig"
done
function die {
local status_code="$1"
shift
>&2 echo "${@}"
exit "$status_code"
}
function log {
>&2 echo "${@}"
}
############## Config Settings ###############
# These are below the Setup in case I end up creating things that need
# to be cleaned up
# Cache for local development to avoid stressing alpine servers
: ${NETIZEN_CACHE:="/tmp"}
: ${LOCAL_DEV:="false"}
: ${ALPINE_TARBALL:="alpine-minirootfs-3.15.0-x86_64.tar.gz"}
: ${ALPINE_SHA256:="ec7ec80a96500f13c189a6125f2dbe8600ef593b87fc4670fe959dc02db727a2"}
: ${ALPINE_SIGNATURE:="-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEEBILYQCL1LfHE581DKTrNCQfZSVoFAmGeGqEACgkQKTrNCQfZ
SVp4Ww/+MzjBfj+33+0QxesuW/4ZGTShK6E+Hi7bXH04kparCEL77pdBnyYTfTjW
Myt2BdPPze6upbsTJjDezpoKkza47ODD5MwsT22Utm4+zJUcHVAUiRKkm57lUuF0
60DldhNj1K5sUrZgfPq+QIkJLsKZtUEoCx3lf1Ou0cRb+5P6nxnRNqqqpcwXaoJr
mhoH/YCH6f+HydbXRcHX9hwdAlCj3LnOn2m6kwppdl6sWSp7yUXkjn1grKN7bnzy
lzevA2aJJGc1S4qATYflaQcsWCcBJZ/Y1cAAl8OPLB+SYtmXbEJyluH5O7PsMGR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fO40X3R/J5YM6SqG7y2uwjVCHFBeO2Nkkr
8nOno+Rbn2b03c9MapMT4ll8jJds4xwhhpIjzPLWd2ZcX/ZGqmsnKPiroe9p1VPo
lN72Ohr9lS+OXfvOPV2N+Ar5rCObmhnYbXGgU/qyhk1qkRu+w2bBZOOQIdaCfh5A
Hbn3ZGGGQskgWZDFP4xZ3DWXFSWMPuvEjbmUn2xrh9oYsjsOGy9tyBFFySU2vyZP
Mkc=
=FcYC
-----END PGP PUBLIC KEY BLOCK-----"}
############## Main Script ###################
function precheck {
# Checks to run before building the image
if ! grep -q 'linux.ko' <<<"$(kldstat)"; then
die 1 "Need linux kernel module for building initramfs."
fi
for bin in gpg sha256; do
if ! command -V "$bin" &> /dev/null; then
die 1 "Need $bin installed."
fi
done
}
function make_chroot {
bsdtar -C "$chroot" -xpf "${download_directory}/${ALPINE_TARBALL}"
# Steal the DNS info from the host
(umask 022 && resolvconf -l > "${chroot}/etc/resolv.conf")
# Enter the jail
# install mkinitfs
sudo jail -c path="$chroot" ip4=inherit ip6=inherit host=inherit allow.raw_sockets=true command=/bin/sh
# Remove the resolv.conf file since it should get populated via dhcp
rm "${chroot}/etc/resolv.conf"
}
function download_alpine {
if [ "$LOCAL_DEV" == "true" ] && [ -e "${NETIZEN_CACHE}/${ALPINE_TARBALL}" ]; then
# Cache for local development to avoid stressing alpine servers
fetch -o "${download_directory}/" "file://${NETIZEN_CACHE}/${ALPINE_TARBALL}"
else
fetch -o "${download_directory}/" "https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/x86_64/${ALPINE_TARBALL}"
if [ "$LOCAL_DEV" == "true" ]; then
cp "${download_directory}/${ALPINE_TARBALL}" /tmp/
fi
fi
# Validate
sha256 -c "$ALPINE_SHA256" "${download_directory}/${ALPINE_TARBALL}"
local keyring="$work_directory/keyring"
gpg --no-default-keyring --keyring "$keyring" --trust-model always --import <<<"$ALPINE_KEY"
gpg --no-default-keyring --keyring "$keyring" --trust-model always --verify <(cat <<<"$ALPINE_SIGNATURE") "${download_directory}/${ALPINE_TARBALL}"
}
function main {
precheck
work_directory=$(mktemp -d -t dib)
folders+=("$work_directory")
download_directory="${work_directory}/downloads"
mkdir "$download_directory"
download_alpine
chroot="${work_directory}/chroot"
mkdir "$chroot"
log "Building chroot in $chroot"
make_chroot
}
main