Skip to content

Commit fbfa487

Browse files
authored
Merge pull request #17 from kristapsk/finalize-more-options
More options and features for finalize-nakamochi.sh
2 parents 8d9fc3e + 6dcf3d6 commit fbfa487

File tree

2 files changed

+115
-8
lines changed

2 files changed

+115
-8
lines changed

tools/finalize-nakamochi.sh

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ patch_lnd_conf()
3434

3535
run_main()
3636
{
37-
if [[ -z $2 ]]; then
38-
echo "Usage: $(basename "$0") [--skip-mkp224o] /dev/sda2 /dev/sdb1 [/mnt/usd [/mnt/ssd]]"
37+
if [[ -z $2 ]] || [[ "$1" == "--help" ]]; then
38+
echo "Usage: $(basename "$0") [options] /dev/sda2 /dev/sdb1 [/mnt/usd [/mnt/ssd]]"
3939
echo "Where:"
4040
echo " /dev/sda2 - uSD card root partition (use '-' to not mount)"
4141
echo " /dev/sdb1 - SSD data partition (use '-' to not mount)"
4242
echo " /mnt/usd - uSD card mount point / directory"
4343
echo " /mnt/ssd - SSD mount point / directory"
4444
echo "Options:"
4545
echo " --skip-mkp224o - skip generating onion keys (requires them to be already present on SSD)"
46+
echo " --test-image - configure image for testing (allows ssh root access with password)"
47+
# FixMe: --update-update is temporary hack, remove after https://github.com/nakamochi/sysupdates/pull/8 is merged.
48+
echo " --update-update - update /sysupdates/update.sh on SSD from local copy"
4649
echo "Example: $(basename "$0") /dev/sdc2 /dev/sdd1"
4750
exit 1
4851
fi
@@ -52,12 +55,29 @@ run_main()
5255
exit 1
5356
fi
5457

55-
if [[ "$1" == "--skip-mkp224o" ]]; then
56-
shift
57-
skip_mkp224o=1
58-
else
59-
skip_mkp224o=0
60-
fi
58+
skip_mkp224o=0
59+
test_image=0
60+
update_update=0
61+
while [[ "$1" == --* ]]; do
62+
case "$1" in
63+
--skip-mkp224o)
64+
skip_mkp224o=1
65+
shift
66+
;;
67+
--test-image)
68+
test_image=1
69+
shift
70+
;;
71+
--update-update)
72+
update_update=1
73+
shift
74+
;;
75+
*)
76+
echo "Error: unknown option $1"
77+
exit 1
78+
;;
79+
esac
80+
done
6181

6282
if [[ "$skip_mkp224o" -eq 0 ]]; then
6383
if ! check_exists mkp224o; then
@@ -240,6 +260,33 @@ run_main()
240260
chown -R "$lnd_user_group" "$SSD_MOUNT_POINT"/lnd
241261
echo "done."
242262

263+
# finalize image for testing or production
264+
if [[ "$test_image" -eq 1 ]]; then
265+
echo -n "Finalizing image for testing ... "
266+
sed -i "s/^#?PermitRootLogin.*/PermitRootLogin yes/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
267+
sed -i "s/^#?PasswordAuthentication.*/PasswordAuthentication yes/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
268+
root_pass="nakamochi"
269+
crypted_root_pass="$(mkpasswd "$root_pass")"
270+
sed -i "s/^root:[^:]*:/root:$crypted_root_pass:/" "$USD_MOUNT_POINT"/etc/shadow
271+
echo "done."
272+
echo "Test image root password is $root_pass, ssh root login allowed."
273+
else
274+
echo -n "Finalizing image for production ... "
275+
sed -i "s/^#?PermitRootLogin.*/PermitRootLogin no/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
276+
sed -i "s/^#?PasswordAuthentication.*/PasswordAuthentication no/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
277+
crypted_root_pass="$(mkpasswd "$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 13; echo)")"
278+
sed -i "s/^root:[^:]*:/root:$crypted_root_pass:/" "$USD_MOUNT_POINT"/etc/shadow
279+
echo "done."
280+
fi
281+
282+
if [[ "$update_update" -eq 1 ]]; then
283+
# update /sysupdates/update.sh on SSD from local copy
284+
echo -n "Updating /sysupdates/update.sh on SSD ... "
285+
cp "$(dirname "$0")/update.sh" "$SSD_MOUNT_POINT"/sysupdates/update.sh
286+
chmod +x "$SSD_MOUNT_POINT"/sysupdates/update.sh
287+
echo "done."
288+
fi
289+
243290
sync
244291
echo "All DONE, Nakamochi uSD and SSD should be ready!"
245292
}

tools/update.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/sh
2+
# shellcheck disable=SC2181
3+
4+
# https://github.com/nakamochi/sysupdates
5+
# pull changes from a remote git repo and run the "apply" script.
6+
# commits are expected to be signed by gpg keys with a sufficient
7+
# trust level to satisfy git pull --verify-signatures.
8+
# the script is expected to be run as root, to allow making changes to the
9+
# operating system.
10+
# in the future, the plan is to provide an on-screen git diff and apply updates
11+
# after user confirmation.
12+
13+
# git branch to pull from. defaults to master.
14+
# another value is "dev", for a development aka unstable version.
15+
BRANCH="${1:-master}"
16+
REMOTE_URL="${2:-https://github.com/nakamochi/sysupdates.git}"
17+
# output everything to a temp file and print its contents only in case of an error,
18+
# so that when run via a cronjob, the output is empty on success which prevents
19+
# needless emails, were any configured.
20+
LOGFILE="${LOGFILE:-/var/log/sysupdate.log}"
21+
# a local git repo dir where to pull the updates into.
22+
REPODIR="${REPODIR:-/ssd/sysupdates}"
23+
24+
# multiple running instances of the script would certainly result in race conditions.
25+
# so, we serialize runs using a lock file, timing out with an error after 15min.
26+
if [ -z "$NAKAMOCHI_SYSUPDATE_LOCK" ]; then
27+
# use the script itself as the lock file
28+
lockfile=$0
29+
exec env NAKAMOCHI_SYSUPDATE_LOCK=1 \
30+
flock --exclusive --timeout 900 "$lockfile" "$0" "$@"
31+
fi
32+
33+
# start of the sysupdate; trim prevously logged runs
34+
date > "$LOGFILE"
35+
36+
# fetch updates from remote
37+
cd "$REPODIR" || exit 1
38+
{
39+
echo "Fetching updates from $REMOTE_URL, branch $BRANCH"
40+
git remote set-url origin "$REMOTE_URL"
41+
git fetch origin # in case the refspec is unknown locally yet
42+
git reset --hard HEAD # remove local changes
43+
git clean -fd # force-delete untracked files
44+
git checkout "$BRANCH"
45+
git pull --verify-signatures
46+
} >> "$LOGFILE" 2>&1
47+
if [ $? -ne 0 ]; then
48+
echo "ERROR: git pull failed"
49+
cat "$LOGFILE"
50+
exit 1
51+
fi
52+
53+
# run repo's update script
54+
export SYSUPDATES_ROOTDIR="$REPODIR"
55+
export SYSUPDATES_CHANNEL="$BRANCH"
56+
if ! ./apply.sh >> "$LOGFILE" 2>&1; then
57+
echo "ERROR: apply failed"
58+
cat "$LOGFILE"
59+
exit 1
60+
fi

0 commit comments

Comments
 (0)