mirror of
https://github.com/suiryc/archlinux-initrd-ssh-cryptsetup.git
synced 2026-07-27 20:27:00 +03:00
Refactor messages printing/logging
Add option to also log to ksmg.
This commit is contained in:
@@ -8,18 +8,18 @@ sshcs_net_start() {
|
||||
# we must have an 'ip' setting, and a device in it
|
||||
[ -z "${ip}" ] && [ -n "${nfsaddrs}" ] && ip="${nfsaddrs}"
|
||||
[ -z "${ip}" ] && {
|
||||
dbg "No ip setting to setup network"
|
||||
log_dbg "No ip setting to setup network"
|
||||
return 1
|
||||
}
|
||||
|
||||
net_device=$(echo ${ip} | cut -d: -f6)
|
||||
[ -z "${net_device}" ] && {
|
||||
dbg "No network device to setup"
|
||||
log_dbg "No network device to setup"
|
||||
return 1
|
||||
}
|
||||
|
||||
if [ "${sshcs_opt_net_wol:-d}" != "d" ]; then
|
||||
dbg "Setting network device=${net_device} wol=${sshcs_opt_net_wol}"
|
||||
log_dbg "Setting network device=${net_device} wol=${sshcs_opt_net_wol}"
|
||||
ethtool -s "${net_device}" wol "${sshcs_opt_net_wol}"
|
||||
fi
|
||||
|
||||
@@ -35,7 +35,7 @@ sshcs_net_start() {
|
||||
# ipconfig manual: https://git.kernel.org/pub/scm/libs/klibc/klibc.git/tree/usr/kinit/ipconfig/README.ipconfig
|
||||
ipconfig_out=$(ipconfig -t "${sshcs_opt_timeout_ipconfig}" "ip=${ip}")
|
||||
if [ $? -ne 0 ]; then
|
||||
err "IP configuration timeout!"
|
||||
log_err "IP configuration timeout!"
|
||||
echo "Devices probing:"
|
||||
ipconfig -n -t 5 -c none all
|
||||
return 1
|
||||
@@ -58,7 +58,7 @@ sshcs_net_start() {
|
||||
. "${net_env}"
|
||||
rm -f "${net_env}"
|
||||
|
||||
echo "IP-Config: device=${net_device} ip=${net_address}/${net_netmask} gw=${net_gateway} dns0=${net_dns0} dns1=${net_dns1}"
|
||||
log_msg "IP-Config: device=${net_device} ip=${net_address}/${net_netmask} gw=${net_gateway} dns0=${net_dns0} dns1=${net_dns1}"
|
||||
|
||||
[ -n "${net_address}" ]
|
||||
}
|
||||
@@ -66,14 +66,14 @@ sshcs_net_start() {
|
||||
sshcs_net_done() {
|
||||
# we are done with the network
|
||||
if [ -n "${net_device}" ]; then
|
||||
dbg "Setting network device=${net_device} down"
|
||||
log_dbg "Setting network device=${net_device} down"
|
||||
ip addr flush dev "${net_device}"
|
||||
ip link set dev "${net_device}" down
|
||||
fi
|
||||
}
|
||||
|
||||
sshcs_trapped_timeout() {
|
||||
err "Timeout reached! Powering off."
|
||||
log_err "Timeout reached! Powering off."
|
||||
poweroff -f
|
||||
exit
|
||||
}
|
||||
@@ -105,7 +105,7 @@ sshcs_untrap_timeout() {
|
||||
proc_parse_stat ${pid_timeout} && kill ${pid_timeout}
|
||||
pid_timeout=
|
||||
trap - SIGALRM
|
||||
msg "Timeout cleared."
|
||||
log_dbg "Timeout cleared."
|
||||
}
|
||||
|
||||
sshcs_shell_run() {
|
||||
@@ -155,7 +155,7 @@ EOF
|
||||
[ ! -d "/var/log" ] && mkdir -p "/var/log"
|
||||
touch "/var/log/lastlog"
|
||||
|
||||
msg "Starting dropbear ..."
|
||||
log_dbg "Starting dropbear ..."
|
||||
dropbear -Esgjk -P "${path_dropbear_pid}" ${sshcs_opt_listen}
|
||||
|
||||
# Actual unlocking shell
|
||||
@@ -190,7 +190,7 @@ sshcs_cryptpart_process() {
|
||||
cryptdev_orig=${cryptdev}
|
||||
if cryptdev=$(resolve_device "${cryptdev_orig}" ${rootdelay}); then
|
||||
if cryptsetup isLuks "${cryptdev}" >/dev/null 2>&1; then
|
||||
dbg "Adding crypt device=${cryptdev} type=${crypttype} name=${cryptname} args=<${cryptargs}> in setup script"
|
||||
log_dbg "Adding crypt device=${cryptdev} type=${crypttype} name=${cryptname} args=<${cryptargs}> in setup script"
|
||||
|
||||
# update script used to unlock device either in console or SSH
|
||||
[ -s "${sshcs_cryptsetup_script}" ] || cat <<'EOF' > "${sshcs_cryptsetup_script}"
|
||||
@@ -222,9 +222,9 @@ EOF
|
||||
killall cryptsetup > /dev/null 2>&1
|
||||
break
|
||||
fi
|
||||
err "Device still not mapped! Please wait or retry."
|
||||
log_err "Device still not mapped! Please wait or retry."
|
||||
elif [ ! -e "/dev/mapper/${cryptname}" ]; then
|
||||
err "cryptsetup failed! Please retry."
|
||||
log_err "cryptsetup failed! Please retry."
|
||||
else
|
||||
break
|
||||
fi
|
||||
@@ -233,7 +233,7 @@ EOF
|
||||
done
|
||||
EOF
|
||||
else
|
||||
err "Failed to manage encrypted device ${cryptdev_orig}: not a LUKS volume."
|
||||
log_err "Failed to manage encrypted device ${cryptdev_orig}: not a LUKS volume."
|
||||
fi
|
||||
fi
|
||||
}
|
||||
@@ -272,7 +272,7 @@ else
|
||||
return
|
||||
fi
|
||||
echo ""
|
||||
err "Device resources missing! Please retry."
|
||||
log_err "Device resources missing! Please retry."
|
||||
fi
|
||||
EOF
|
||||
chmod a+x "${sshcs_cryptsetup_script}"
|
||||
@@ -290,7 +290,7 @@ run_hook() {
|
||||
|
||||
# sanity check: crypttab should be present
|
||||
[ ! -e "${etc_crypttab}" ] && {
|
||||
dbg "No crypttab configuration to process"
|
||||
log_dbg "No crypttab configuration to process"
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -307,13 +307,13 @@ run_hook() {
|
||||
# Setup script used to unlock device either in console or SSH
|
||||
sshcs_cryptpart_setup
|
||||
if [ ! -e "${sshcs_cryptsetup_script}" ]; then
|
||||
err "No encrypted device found! Skipping crypt remote unlocking."
|
||||
log_err "No encrypted device found! Skipping crypt remote unlocking."
|
||||
return 0
|
||||
fi
|
||||
|
||||
# start and check network
|
||||
if ! sshcs_net_start; then
|
||||
err "Net interface not available! Skipping crypt remote unlocking."
|
||||
log_err "Net interface not available! Skipping crypt remote unlocking."
|
||||
# We still allow to unlock locally with timeout
|
||||
sshcs_shell_run
|
||||
return 0
|
||||
|
||||
@@ -2,13 +2,35 @@
|
||||
|
||||
. "/init_functions"
|
||||
|
||||
dbg () {
|
||||
[ ${sshcs_opt_debug} != 0 ] && echo "$@"
|
||||
# Log debug message, if debug enabled.
|
||||
# Also log as kmsg if enabled.
|
||||
log_dbg() {
|
||||
[ ${sshcs_opt_debug} -eq 0 ] && return 0
|
||||
echo "$@"
|
||||
[ ${sshcs_opt_log_kmsg} -eq 0 ] && return 0
|
||||
echo "<31>sshcs: $@" > /dev/kmsg
|
||||
}
|
||||
|
||||
# Log information message, even if quiet.
|
||||
# Also log as kmsg if enabled.
|
||||
log_msg() {
|
||||
echo "$@"
|
||||
[ ${sshcs_opt_log_kmsg} -eq 0 ] && return 0
|
||||
echo "<30>sshcs: $@" > /dev/kmsg
|
||||
}
|
||||
|
||||
# Log error message (prefixed with "ERROR: " in console).
|
||||
# Also log as kmsg if enabled.
|
||||
log_err() {
|
||||
err "$@"
|
||||
[ ${sshcs_opt_log_kmsg} -eq 0 ] && return 0
|
||||
echo "<27>sshcs: $@" > /dev/kmsg
|
||||
}
|
||||
|
||||
sshcs_env_load() {
|
||||
local sshcs_env="/etc/initcpio/sshcs_env"
|
||||
|
||||
local log_kmsg_default=1
|
||||
local debug_default=0
|
||||
local net_wol_default=g
|
||||
local timeout_ipconfig_default=10
|
||||
@@ -18,6 +40,7 @@ sshcs_env_load() {
|
||||
[ ${sshcs_env_loaded:-0} -eq 1 ] && return
|
||||
[ -e "${sshcs_env}" ] && . "${sshcs_env}"
|
||||
sshcs_env_loaded=1
|
||||
[ -z "${sshcs_opt_log_kmsg}" ] && sshcs_opt_log_kmsg=${log_kmsg_default}
|
||||
[ -z "${sshcs_opt_debug}" ] && sshcs_opt_debug=${debug_default}
|
||||
[ -z "${sshcs_opt_net_wol}" ] && sshcs_opt_net_wol=${net_wol_default}
|
||||
[ -z "${sshcs_opt_timeout_ipconfig}" ] && sshcs_opt_timeout_ipconfig=${timeout_ipconfig_default}
|
||||
@@ -89,7 +112,7 @@ sshcs_cleanup() {
|
||||
|
||||
# cleanup dropbear
|
||||
if [ -f "${path_dropbear_pid}" ]; then
|
||||
msg "Stopping dropbear ..."
|
||||
log_dbg "Stopping dropbear ..."
|
||||
kill $(cat "${path_dropbear_pid}")
|
||||
rm -f "${path_dropbear_pid}"
|
||||
fi
|
||||
@@ -99,13 +122,13 @@ sshcs_cleanup() {
|
||||
# This is necessary to properly terminate both these shells and the spawned
|
||||
# SSH processes.
|
||||
# Reminder: "... | ..." does fork a subshell
|
||||
dbg "Searching SSH shells ..."
|
||||
log_dbg "Searching SSH shells ..."
|
||||
pgrep_output=$(pgrep /usr/bin/ash)
|
||||
pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$")
|
||||
echo "${pgrep_output}" | while read pid; do
|
||||
proc_parse_stat ${pid} || continue
|
||||
proc_find_parent_cmd ${pid} "dropbear" || continue
|
||||
dbg "Killing SSH shell pid=${pid}"
|
||||
log_dbg "Killing SSH shell pid=${pid}"
|
||||
kill -SIGHUP ${pid}
|
||||
done
|
||||
fi
|
||||
@@ -130,16 +153,18 @@ sshcs_cleanup() {
|
||||
# Note: as a side effect, this will also kill the shell that was forked to
|
||||
# trigger a timeout, which is fine (we want to kill it, either from here or
|
||||
# from the init shell).
|
||||
dbg "Searching console shells ..."
|
||||
log_dbg "Searching console shells ..."
|
||||
pgrep_output=$(pgrep /usr/bin/ash)
|
||||
pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$")
|
||||
echo "${pgrep_output}" | while read pid; do
|
||||
proc_find_parent_cmd ${pid} "dropbear" && continue
|
||||
dbg "Killing console shell pid=${pid}"
|
||||
log_dbg "Killing console shell pid=${pid}"
|
||||
kill -SIGHUP ${pid}
|
||||
done
|
||||
fi
|
||||
# else: when in SSH shell script, we have nothing else to do other than exit.
|
||||
|
||||
log_dbg "All done"
|
||||
}
|
||||
|
||||
sshcs_check_done() {
|
||||
@@ -161,12 +186,12 @@ sshcs_check_done() {
|
||||
echo ""
|
||||
# When finalizing in console, power off
|
||||
if [ ${finalize} -eq 1 ] && proc_is_console; then
|
||||
err "Devices are still locked! Powering off."
|
||||
log_err "Devices are still locked! Powering off."
|
||||
poweroff -f
|
||||
fi
|
||||
|
||||
# When in shell or SSH, let user try again
|
||||
err "Devices are still locked! Please retry."
|
||||
log_err "Devices are still locked! Please retry."
|
||||
return
|
||||
fi
|
||||
|
||||
@@ -179,7 +204,7 @@ sshcs_check_done() {
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "cryptsetup succeeded! Boot sequence should go on."
|
||||
log_msg "cryptsetup succeeded! Boot sequence should go on."
|
||||
proc_is_console || echo "Please wait and reconnect to nominal SSH service."
|
||||
|
||||
sshcs_cleanup
|
||||
|
||||
Reference in New Issue
Block a user