basementhud/overlay/usr/local/libexec/init-localstorage.sh

42 lines
841 B
Bash
Executable File

#!/bin/sh
die() {
remount_ro
echo "ERROR: $1" >&2
exit 1
}
gen_ssh_keys() {
unset kf x
mkdir -p /run/storage/ssh || die 'Could not create /run/storage/ssh'
for x in rsa ecdsa ed25519; do
kf=/run/storage/ssh/ssh_host_${x}_key
if [ ! -f ${kf} ]; then
ssh-keygen -t ${x} -f ${kf} -N '' -C '' \
|| die "Failed to generate ${x} SSH host key"
fi
done
}
remount_ro() {
if [ -w /run/storage ]; then
mount -oremount,ro /run/storage
fi
}
remount_rw() {
if [ ! -w /run/storage ]; then
mount -o remount,rw /run/storage \
|| die 'Could not remount /run/storage read-write'
fi
}
mountpoint -q /run/storage || die '/run/storage is not mounted'
if [ ! -d /run/storage/ssh ]; then
remount_rw
gen_ssh_keys
fi
remount_ro