newer-initramfs/mkbase.sh

52 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
set -e
if [ $# -lt 1 ]; then
echo "usage: $0 <image> [cmdline] [arch]"
exit 1
fi
image="$1"
cmdline="$2"
arch="$3"
staging=$(mktemp -d)
trap "rm -rf '$staging'" EXIT
if [ -z "$arch" ]; then
arch=$(apk --print-arch)
fi
apk add --root="$staging" --arch "$arch" --repositories-file /etc/apk/repositories --keys-dir /etc/apk/keys --initdb \
alpine-baselayout busybox busybox-suid cryptsetup zfs dropbear
cp -R rootfs/* "$staging"
cc -static askpass.c -o "$staging"/bin/askpass
git_tag="$(git describe --tags --exact-match HEAD 2>/dev/null || true)"
git_branch="$(git symbolic-ref HEAD 2>/dev/null || true)"
git_latestcommit="$(git log -1 --date='short' --format='%h-%ad' || true)"
if [ -n "$git_tag" ]; then
version="$git_tag"
elif [ -n "$git_latestcommit" ]; then
version="$git_branch-$git_latestcommit"
elif [ -n "$git_branch" ]; then
version="$git_branch-???"
else
version="???"
fi
echo "$version" > "$staging"/VERSION
cp secrets/authorized_keys "$staging"
mkdir -p "$staging"/etc/dropbear
for x in rsa ecdsa; do
hostkey=secrets/dropbear_${x}_host_key
if ! [ -f "$hostkey" ]; then
"$staging"/usr/bin/dropbearkey -t $x -f "$hostkey"
fi
cp "$hostkey" "$staging"/etc/dropbear
done
echo " $cmdline" >> "$staging"/cmdline
(cd "$staging" && find . | cpio -H newc -o | gzip -9) > "$image"