Files
zolliker bedc498a32 move hostkeys directory to /home/l_samenv/.config
else it would be overwritten each time
+ remove boot_tiny after use
2026-10-29 09:05:50 +01:00

129 lines
4.2 KiB
Bash

#!/bin/bash
# Build a custom core.gz for the BOOT_TINY stick:
# - nano, lz4, openssh (and their dependencies) are built in, so no
# network and no tce-load are needed at boot
# - /opt/bootlocal.sh starts sshd and sets up ~/.ssh
# - the ssh/ directory (authorized_keys, id_rsa, ...) is staged in /opt/ssh
# - the stick gets a hostname and its own, ssh host keys (depending on stick name)
#
# Needs: root, cpio, gzip, unsquashfs (package squashfs-tools), ssh-keygen
#
# Put these next to this script:
# core.gz.orig the original, unmodified core.gz (keep it, never overwrite)
# tcz/*.tcz extensions fetched on the Tiny Core box with
# "tce-load -w nano lz4 openssh", copied from tce/optional
# tmp/ssh_for_boxes/ files for ~/.ssh of user tc (authorized_keys, ...)
# bootlocal.sh
# ../hostkeys/<name>/ ssh host keys of the stick called <name>. Used if it
# exists, created (and filled with new keys) if it does not.
# Keep it, then a rebuilt stick keeps its identity.
# Result: tmp/core.gz (to be copied to the stick)
set -e
cd "$(dirname "$0")"
ORIG=core.gz.orig
[ "$EUID" -eq 0 ] || { echo "Please run as root"; exit 1; }
for f in "$ORIG" bootlocal.sh; do
[ -f "$f" ] || { echo "missing $f"; exit 1; }
done
ls tcz/*.tcz > /dev/null 2>&1 || { echo "no tcz/*.tcz found"; exit 1; }
for c in cpio gzip unsquashfs ssh-keygen; do
command -v $c > /dev/null || { echo "need $c"; exit 1; }
done
file "$ORIG" | grep -q gzip || { echo "$ORIG is not gzip compressed:"; file "$ORIG"; exit 1; }
if [ -z "$1" ]; then
read -p "stick name [apu-boot]: " stickname
stickname=${stickname:-apu-boot}
else
stickname=$1
fi
label=${stickname//-/_}
label=${label^^}
# the name ends up in a directory name and in a script: allow only safe characters
case "$stickname" in
*[!A-Za-z0-9-]*|.*) echo "invalid hostname '$stickname' (use letters, digits and dash only)"; exit 1 ;;
esac
# ssh host keys: use previous hostkeys if present, create missing keys otherwise
KEYDIR=${2:hostkeys}
# temporary files: needs ssh_for_boxes here before and puts core.gz as a result
TMPDIR=tmp
SSHDIR=$TMPDIR/ssh_for_boxes
if [ -d "$KEYDIR" ]; then
echo "using ssh host keys from $KEYDIR"
else
echo "creating $KEYDIR with new ssh host keys"
mkdir -p "$KEYDIR"
fi
for t in dsa rsa ecdsa ed25519; do
if [ ! -f "$KEYDIR/ssh_host_${t}_key" ]; then
echo " generate $t key"
rm -f "$KEYDIR/ssh_host_${t}_key.pub"
ssh-keygen -q -t $t -N '' -f "$KEYDIR/ssh_host_${t}_key"
fi
done
cat << EOF > $TMPDIR/bootsync.sh
#!/bin/sh
# put other system startup commands here, the boot process will wait until they complete.
# Use bootlocal.sh for system startup commands that can run in the background
# and therefore not slow down the boot process.
/usr/bin/sethostname $stickname
/opt/bootlocal.sh &
EOF
WORK=$(mktemp -d)
TMP=$(mktemp -d)
trap 'rm -rf "$WORK" "$TMP"' EXIT
echo "unpack $ORIG"
zcat "$ORIG" | (cd "$WORK" && cpio -idm --quiet)
for t in tcz/*.tcz; do
echo "add $t"
rm -rf "$TMP/x"
unsquashfs -d "$TMP/x" "$t" > /dev/null
cp -a "$TMP/x/." "$WORK/"
done
echo "add ssh files from $SSHDIR for user tc host"
if [ -d "$SSHDIR" ]; then
echo "stage $SSHDIR in /opt/ssh"
mkdir -p "$WORK/opt/ssh"
cp -a "$SSHDIR"/. "$WORK/opt/ssh/"
fi
echo "add ssh host keys from $KEYDIR"
mkdir -p "$WORK/usr/local/etc/ssh"
for k in "$KEYDIR"/ssh_host_*_key; do
install -m 600 -o root -g root "$k" "$WORK/usr/local/etc/ssh/"
install -m 644 -o root -g root "$k.pub" "$WORK/usr/local/etc/ssh/"
done
install -m 755 -o root -g root bootlocal.sh "$WORK/opt/bootlocal.sh"
install -m 755 -o root -g root $TMPDIR/bootsync.sh "$WORK/opt/bootsync.sh"
# commands whose scripts live on the stick: small wrappers in /usr/local/bin
STICKCMDS="clone write_to_ssd"
mkdir -p "$WORK/usr/local/bin"
for c in $STICKCMDS; do
printf '#!/bin/sh\ncd %s && exec bash ./%s "$@"\n' "/media/$label" "$c" > "$WORK/usr/local/bin/$c"
chmod 755 "$WORK/usr/local/bin/$c"
done
OUT=core.gz
echo "pack $TMPDIR/$OUT"
(cd "$WORK" && find . | cpio -o -H newc --quiet) | gzip -9 > "$TMPDIR/${OUT}"
# change ownership of generated files to the owner of the current directory
chown -R --reference=. $KEYDIR $TMPDIR
ls -l "$TMPDIR/$OUT"
echo "done."