2022-07-09 12:34:58 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2023-05-12 12:04:05 +00:00
|
|
|
# Copyright 2022 - 2023, macmpi
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-05-17 17:13:41 +00:00
|
|
|
VERSION="0.8"
|
2022-12-05 09:09:59 +00:00
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
|
|
|
|
_apk() {
|
|
|
|
local cmd="$1"
|
|
|
|
local pkg="$2"
|
|
|
|
|
|
|
|
case $cmd in
|
|
|
|
add) # install only if not already present
|
|
|
|
if ! apk info | grep -wq "${pkg}"; then
|
|
|
|
apk add "$pkg" && printf "${pkg} " >> /tmp/.trash/installed
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
del) # delete only if previously installed
|
|
|
|
if grep -wq "$pkg" /tmp/.trash/installed; then
|
|
|
|
apk del "$pkg" && sed -i 's/\b'"${pkg}"'\b//' /tmp/.trash/installed
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "only add/del: wrong usage"; exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
_preserve() {
|
|
|
|
[ -f "$1" ] && cp "$1" "${1}.orig"
|
|
|
|
}
|
|
|
|
|
|
|
|
_restore() {
|
|
|
|
if [ -f "${1}.orig" ]; then
|
|
|
|
mv -- "${1}.orig" "${1}"
|
|
|
|
else
|
|
|
|
rm -rf "${1}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-08-14 00:16:40 +00:00
|
|
|
##### End of part to be duplicated into post-cleanup (do not alter!)
|
2023-05-16 12:36:41 +00:00
|
|
|
|
|
|
|
|
2022-07-09 12:34:58 +00:00
|
|
|
# Redirect stdout and errors to console as rc.local does not log anything
|
|
|
|
exec 1>/dev/console 2>&1
|
|
|
|
|
2022-12-05 09:09:59 +00:00
|
|
|
logger -st ${0##*/} "Alpine Linux headless bootstrap v$VERSION by macmpi"
|
|
|
|
|
2022-07-09 12:34:58 +00:00
|
|
|
mkdir /tmp/.trash
|
|
|
|
|
2023-05-16 07:28:58 +00:00
|
|
|
# grab used ovl filename from dmesg
|
|
|
|
ovl="$( dmesg | grep -o 'Loading user settings from .*:' | awk '{print $5}' | sed 's/:.*$//' )"
|
|
|
|
ovlpath="$( dirname "$ovl" )"
|
|
|
|
|
|
|
|
# Help randomness for wpa_supplicant and ssh server
|
2023-05-12 07:26:50 +00:00
|
|
|
rc-service seedrng start
|
2022-07-09 12:34:58 +00:00
|
|
|
|
|
|
|
## Setup Network interfaces
|
|
|
|
if [ -f "${ovlpath}/wpa_supplicant.conf" ]; then
|
|
|
|
logger -st ${0##*/} "Wifi setup found !"
|
2023-05-16 12:36:41 +00:00
|
|
|
_apk add wpa_supplicant
|
|
|
|
_preserve "/etc/wpa_supplicant/wpa_supplicant.conf"
|
2023-05-11 15:53:42 +00:00
|
|
|
install -m600 "${ovlpath}/wpa_supplicant.conf" /etc/wpa_supplicant/wpa_supplicant.conf
|
2022-07-09 12:34:58 +00:00
|
|
|
else
|
|
|
|
logger -st ${0##*/} "Wifi setup not found !"
|
|
|
|
fi
|
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
_preserve "/etc/network/interfaces"
|
2023-05-11 15:53:42 +00:00
|
|
|
if ! install -m644 "${ovlpath}/interfaces" /etc/network/interfaces; then
|
2022-07-09 12:34:58 +00:00
|
|
|
# set default interfaces if not specified by interface file on boot storage
|
|
|
|
logger -st ${0##*/} "No interfaces file supplied, building default interfaces..."
|
|
|
|
for dev in $(ls /sys/class/net)
|
|
|
|
do
|
|
|
|
case ${dev%%[0-9]*} in
|
|
|
|
lo)
|
|
|
|
cat <<-EOF >> /etc/network/interfaces
|
|
|
|
auto $dev
|
|
|
|
iface $dev inet loopback
|
|
|
|
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
eth)
|
|
|
|
cat <<-EOF >> /etc/network/interfaces
|
|
|
|
auto $dev
|
|
|
|
iface $dev inet dhcp
|
|
|
|
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
wlan)
|
|
|
|
[ -f /etc/wpa_supplicant/wpa_supplicant.conf ] && cat <<-EOF >> /etc/network/interfaces
|
|
|
|
auto $dev
|
|
|
|
iface $dev inet dhcp
|
|
|
|
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
usb)
|
|
|
|
cat <<-EOF >> /etc/network/interfaces
|
|
|
|
auto $dev
|
|
|
|
iface $dev inet static
|
|
|
|
address 10.42.0.2/24
|
|
|
|
gateway 10.42.0.1
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat <<-EOF > /etc/resolv.conf
|
|
|
|
nameserver 208.67.222.222
|
|
|
|
nameserver 208.67.220.220
|
|
|
|
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Using following network interfaces:"
|
|
|
|
cat /etc/network/interfaces
|
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
_preserve "/etc/hostname"
|
2022-07-18 15:01:07 +00:00
|
|
|
echo "alpine-headless" > /etc/hostname
|
|
|
|
hostname -F /etc/hostname
|
2022-12-05 08:57:08 +00:00
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
grep -q "wlan" /etc/network/interfaces && \
|
|
|
|
[ -f /etc/wpa_supplicant/wpa_supplicant.conf ] && \
|
|
|
|
rc-service wpa_supplicant start
|
2022-07-09 12:34:58 +00:00
|
|
|
rc-service networking start
|
|
|
|
|
|
|
|
|
|
|
|
## Setup temporary SSH server (root login, no password)
|
2023-05-16 12:36:41 +00:00
|
|
|
## We use some bundled (or optionaly provided) keys to avoid generation at startup and save time
|
|
|
|
_apk add openssh
|
|
|
|
_preserve "/etc/ssh/sshd_config"
|
|
|
|
_preserve "/etc/conf.d/sshd"
|
2023-05-12 06:41:41 +00:00
|
|
|
|
|
|
|
cat <<-EOF >> /etc/ssh/sshd_config
|
|
|
|
AuthenticationMethods none
|
|
|
|
PermitEmptyPasswords yes
|
|
|
|
PermitRootLogin yes
|
|
|
|
Banner /tmp/.trash/banner
|
|
|
|
EOF
|
|
|
|
|
2023-05-13 06:30:34 +00:00
|
|
|
# Banner file
|
2023-05-16 07:28:58 +00:00
|
|
|
warn=""
|
|
|
|
grep -q "${ovlpath}.*[[:space:]]ro[[:space:],]" /proc/mounts; RO=$?
|
2023-05-16 12:36:41 +00:00
|
|
|
[ "$RO" -eq "0" ] && warn="(remount partition rw!)"
|
2023-05-16 07:28:58 +00:00
|
|
|
|
2023-05-11 18:41:37 +00:00
|
|
|
cat <<-EOF > /tmp/.trash/banner
|
|
|
|
|
|
|
|
Alpine Linux headless bootstrap v$VERSION by macmpi
|
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
You may want to delete/rename .apkovl file before reboot ${warn}:
|
2023-05-16 07:28:58 +00:00
|
|
|
$ovl
|
|
|
|
(can be done automatically with unattended script - see sample snippet)
|
|
|
|
|
2023-05-16 12:36:41 +00:00
|
|
|
|
2023-05-11 18:41:37 +00:00
|
|
|
EOF
|
|
|
|
|
2023-05-13 06:30:34 +00:00
|
|
|
# Bundled temporary keys are moved in RAM /tmp so they won't be stored
|
2023-05-16 12:36:41 +00:00
|
|
|
# within permanent config later (new ones will then be generated at reboot)
|
2023-05-13 06:30:34 +00:00
|
|
|
KEYGEN_STANCE="sshd_disable_keygen=yes"
|
2023-05-11 15:53:42 +00:00
|
|
|
mv /etc/ssh/ssh_host_*_key* /tmp/.trash/.
|
2023-05-13 06:30:34 +00:00
|
|
|
|
|
|
|
# Inject optional custom keys (those might be stored)
|
|
|
|
if install -m600 "${ovlpath}"/ssh_host_*_key* /etc/ssh/; then
|
2023-05-16 12:36:41 +00:00
|
|
|
# check for empty key within injected ones: if found, generate new keys
|
2023-05-13 06:30:34 +00:00
|
|
|
if find /etc/ssh/ -maxdepth 1 -type f -name 'ssh_host_*_key*' -empty | grep -q .; then
|
|
|
|
rm /etc/ssh/ssh_host_*_key*
|
|
|
|
KEYGEN_STANCE=""
|
|
|
|
logger -st ${0##*/} "Will generate new SSH keys..."
|
|
|
|
else
|
|
|
|
chmod 644 /etc/ssh/ssh_host_*_key.pub
|
|
|
|
logger -st ${0##*/} "Using injected SSH keys..."
|
|
|
|
fi
|
|
|
|
else
|
2023-05-12 06:41:41 +00:00
|
|
|
logger -st ${0##*/} "Using bundled ssh keys from RAM..."
|
|
|
|
cat <<-EOF >> /etc/ssh/sshd_config
|
|
|
|
HostKey /tmp/.trash/ssh_host_ed25519_key
|
|
|
|
HostKey /tmp/.trash/ssh_host_rsa_key
|
|
|
|
EOF
|
2023-05-11 15:53:42 +00:00
|
|
|
fi
|
|
|
|
|
2023-05-13 06:30:34 +00:00
|
|
|
echo "$KEYGEN_STANCE" >> /etc/conf.d/sshd
|
2022-07-09 12:34:58 +00:00
|
|
|
rc-service sshd start
|
|
|
|
|
|
|
|
## Prep for final post-cleanup
|
2023-02-24 15:00:54 +00:00
|
|
|
## clears any installed packages and settings
|
2023-05-16 12:36:41 +00:00
|
|
|
# copy begininng of this file to keep functions
|
|
|
|
sed -n '/^#* End .*alter!)$/q;p' /etc/local.d/headless.start > /tmp/.trash/post-cleanup
|
|
|
|
|
|
|
|
cat <<-EOF >> /tmp/.trash/post-cleanup
|
|
|
|
|
2022-07-09 12:34:58 +00:00
|
|
|
logger -st ${0##*/} "Cleaning-up..."
|
2023-05-16 12:36:41 +00:00
|
|
|
_restore "/etc/ssh/sshd_config"
|
|
|
|
_restore "/etc/conf.d/sshd"
|
|
|
|
_apk del openssh
|
|
|
|
_restore "/etc/wpa_supplicant/wpa_supplicant.conf"
|
|
|
|
_apk del wpa_supplicant
|
|
|
|
_restore "/etc/network/interfaces"
|
|
|
|
_restore "/etc/hostname"
|
2022-07-09 12:34:58 +00:00
|
|
|
rm /etc/modules-load.d/g_ether.conf
|
|
|
|
rm /etc/modprobe.d/g_ether.conf
|
|
|
|
rc-update del local default
|
|
|
|
rm /etc/local.d/headless.start
|
|
|
|
|
2022-07-24 21:15:11 +00:00
|
|
|
if [ -f "${ovlpath}/unattended.sh" ]; then
|
|
|
|
install -m755 "${ovlpath}/unattended.sh" /tmp/unattended.sh
|
|
|
|
/tmp/unattended.sh >/dev/console 2>&1 &
|
|
|
|
logger -st ${0##*/} "/tmp/unattended.sh script launched in the background with PID \$!"
|
|
|
|
fi
|
|
|
|
|
2022-07-09 12:34:58 +00:00
|
|
|
logger -st ${0##*/} "Done !!"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
chmod +x /tmp/.trash/post-cleanup
|
|
|
|
exec /tmp/.trash/post-cleanup
|
|
|
|
|