Skip to content

Commit e263862

Browse files
authored
Merge pull request #18 from macmpi/dev
Release 0.9
2 parents f7571ac + 03eb12b commit e263862

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ From there, actual system install can be performed as usual with `setup-alpine`
2222
Extra files may be added next to `headless.apkovl.tar.gz` to customise boostrapping configuration (check sample files):
2323
- `wpa_supplicant.conf`[^3] (*mandatory for wifi usecase*): define wifi SSID & password.
2424
- `interfaces`[^3] (*optional*): define network interfaces at will, if defaults DCHP-based are not suitable.
25-
- `ssh_host_*_key*` (*optional*): provide custom ssh keys to be injected (may be stored), instead of using bundled ones[^2] (not stored). Providing an empty key file will trigger new keys generation (ssh server may take longer to start).
25+
- `authorized_keys` (*optional*): provide client's public SSH key to secure `root` ssh login.
26+
- `ssh_host_*_key*` (*optional*): provide server's custom ssh keys to be injected (may be stored), instead of using bundled ones[^2] (not stored). Providing an empty key file will trigger new keys generation (ssh server may take longer to start).
2627
- `unattended.sh`[^3] (*optional*): create custom automated deployment script to further tune & extend actual setup (backgrounded).
2728

2829

headless.apkovl.tar.gz

514 Bytes
Binary file not shown.

overlay/etc/local.d/headless.start

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright 2022 - 2023, macmpi
44
# SPDX-License-Identifier: MIT
55

6-
VERSION="0.8"
6+
VERSION="0.9"
77

88

99
_apk() {
@@ -39,37 +39,39 @@ _restore() {
3939
fi
4040
}
4141

42-
##### End of part to be dupplicated into post-cleanup (do not alter!)
42+
##### End of part to be duplicated into post-cleanup (do not alter!)
4343

4444

4545
# Redirect stdout and errors to console as rc.local does not log anything
4646
exec 1>/dev/console 2>&1
4747

4848
logger -st ${0##*/} "Alpine Linux headless bootstrap v$VERSION by macmpi"
4949

50-
mkdir /tmp/.trash
50+
install -dm 0700 /tmp/.trash
5151

5252
# grab used ovl filename from dmesg
5353
ovl="$( dmesg | grep -o 'Loading user settings from .*:' | awk '{print $5}' | sed 's/:.*$//' )"
54-
ovlpath="$( dirname "$ovl" )"
54+
ovl="$( basename "${ovl}" )"
55+
# search path again as mountpoint may have been changed later in the boot process...
56+
ovlpath=$( find /media -maxdepth 2 -type d -path '*/.*' -prune -o -type f -name "${ovl}" -exec dirname {} \; | head -1 )
5557

5658
# Help randomness for wpa_supplicant and ssh server
5759
rc-service seedrng start
5860

5961
## Setup Network interfaces
6062
if [ -f "${ovlpath}/wpa_supplicant.conf" ]; then
61-
logger -st ${0##*/} "Wifi setup found !"
63+
logger -st ${0##*/} "Configuring wifi..."
6264
_apk add wpa_supplicant
6365
_preserve "/etc/wpa_supplicant/wpa_supplicant.conf"
6466
install -m600 "${ovlpath}/wpa_supplicant.conf" /etc/wpa_supplicant/wpa_supplicant.conf
6567
else
66-
logger -st ${0##*/} "Wifi setup not found !"
68+
logger -st ${0##*/} "No wifi setup supplied !"
6769
fi
6870

6971
_preserve "/etc/network/interfaces"
7072
if ! install -m644 "${ovlpath}/interfaces" /etc/network/interfaces; then
7173
# set default interfaces if not specified by interface file on boot storage
72-
logger -st ${0##*/} "No interfaces file supplied, building default interfaces..."
74+
logger -st ${0##*/} "No interfaces file supplied, building defaults..."
7375
for dev in $(ls /sys/class/net)
7476
do
7577
case ${dev%%[0-9]*} in
@@ -132,12 +134,26 @@ _apk add openssh
132134
_preserve "/etc/ssh/sshd_config"
133135
_preserve "/etc/conf.d/sshd"
134136

135-
cat <<-EOF >> /etc/ssh/sshd_config
136-
AuthenticationMethods none
137-
PermitEmptyPasswords yes
137+
cat <<-EOF > /etc/ssh/sshd_config
138138
PermitRootLogin yes
139139
Banner /tmp/.trash/banner
140140
EOF
141+
142+
if install -m600 "${ovlpath}/authorized_keys" /tmp/.trash/authorized_keys; then
143+
logger -st ${0##*/} "Enabling public key SSH authentication..."
144+
cat <<-EOF >> /etc/ssh/sshd_config
145+
AuthenticationMethods publickey
146+
AuthorizedKeysFile /tmp/.trash/authorized_keys
147+
# relax strict mode as authorized_keys are inside /tmp
148+
StrictModes no
149+
EOF
150+
else
151+
logger -st ${0##*/} "No SSH authentication."
152+
cat <<-EOF >> /etc/ssh/sshd_config
153+
AuthenticationMethods none
154+
PermitEmptyPasswords yes
155+
EOF
156+
fi
141157

142158
# Banner file
143159
warn=""
@@ -149,7 +165,7 @@ cat <<-EOF > /tmp/.trash/banner
149165
Alpine Linux headless bootstrap v$VERSION by macmpi
150166
151167
You may want to delete/rename .apkovl file before reboot ${warn}:
152-
$ovl
168+
${ovlpath}/${ovl}
153169
(can be done automatically with unattended script - see sample snippet)
154170
155171
@@ -189,6 +205,16 @@ sed -n '/^#* End .*alter!)$/q;p' /etc/local.d/headless.start > /tmp/.trash/post-
189205

190206
cat <<-EOF >> /tmp/.trash/post-cleanup
191207
208+
_tst_inet() {
209+
## Tested URL redirects to github project page: is.gd shortener provides basic analytics.
210+
## Analytics are public and can be checked at https://is.gd/stats.php?url=apkovl_run
211+
## Privacy policy: https://is.gd/privacy.php
212+
INET="failed"
213+
wget -q -T 10 --spider https://is.gd/apkovl_run > /dev/null 2>&1 &&
214+
INET="success"
215+
logger -st ${0##*/} "Internet access: \$INET"
216+
}
217+
192218
logger -st ${0##*/} "Cleaning-up..."
193219
_restore "/etc/ssh/sshd_config"
194220
_restore "/etc/conf.d/sshd"
@@ -202,6 +228,11 @@ cat <<-EOF >> /tmp/.trash/post-cleanup
202228
rc-update del local default
203229
rm /etc/local.d/headless.start
204230
231+
# Internet connectivity test
232+
# Can be skipped by creating a 'opt-out'-named dummy file aside apkovl file
233+
[ -f "${ovlpath}/opt-out" ] || _tst_inet &
234+
235+
# Run unattended script if available
205236
if [ -f "${ovlpath}/unattended.sh" ]; then
206237
install -m755 "${ovlpath}/unattended.sh" /tmp/unattended.sh
207238
/tmp/unattended.sh >/dev/console 2>&1 &

sample_unattended.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ sleep 60
1414

1515

1616
## This snippet removes apkovl file on volume after initial boot
17+
# grab used ovl filename from dmesg
1718
ovl="$( dmesg | grep -o 'Loading user settings from .*:' | awk '{print $5}' | sed 's/:.*$//' )"
18-
ovlpath="$( dirname "$ovl" )"
19+
ovl="$( basename "${ovl}" )"
20+
# search path again as mountpoint may have been changed later in the boot process...
21+
ovlpath=$( find /media -maxdepth 2 -type d -path '*/.*' -prune -o -type f -name "${ovl}" -exec dirname {} \; | head -1 )
1922

2023
# also works in case volume is mounted read-only
2124
grep -q "${ovlpath}.*[[:space:]]ro[[:space:],]" /proc/mounts; RO=$?
2225
[ "$RO" -eq "0" ] && mount -o remount,rw "${ovlpath}"
23-
rm -f "${ovl}"
26+
rm -f "${ovlpath}/${ovl}"
2427
[ "$RO" -eq "0" ] && mount -o remount,ro "${ovlpath}"
2528

2629
########################################################

0 commit comments

Comments
 (0)