Refactor messages printing/logging

Add option to also log to ksmg.
This commit is contained in:
Julien Coloos
2026-07-12 20:08:40 +02:00
parent 32f45a9121
commit 2335b859fa
2 changed files with 52 additions and 27 deletions

View File

@@ -8,18 +8,18 @@ sshcs_net_start() {
# we must have an 'ip' setting, and a device in it # we must have an 'ip' setting, and a device in it
[ -z "${ip}" ] && [ -n "${nfsaddrs}" ] && ip="${nfsaddrs}" [ -z "${ip}" ] && [ -n "${nfsaddrs}" ] && ip="${nfsaddrs}"
[ -z "${ip}" ] && { [ -z "${ip}" ] && {
dbg "No ip setting to setup network" log_dbg "No ip setting to setup network"
return 1 return 1
} }
net_device=$(echo ${ip} | cut -d: -f6) net_device=$(echo ${ip} | cut -d: -f6)
[ -z "${net_device}" ] && { [ -z "${net_device}" ] && {
dbg "No network device to setup" log_dbg "No network device to setup"
return 1 return 1
} }
if [ "${sshcs_opt_net_wol:-d}" != "d" ]; then 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}" ethtool -s "${net_device}" wol "${sshcs_opt_net_wol}"
fi 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 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}") ipconfig_out=$(ipconfig -t "${sshcs_opt_timeout_ipconfig}" "ip=${ip}")
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
err "IP configuration timeout!" log_err "IP configuration timeout!"
echo "Devices probing:" echo "Devices probing:"
ipconfig -n -t 5 -c none all ipconfig -n -t 5 -c none all
return 1 return 1
@@ -58,7 +58,7 @@ sshcs_net_start() {
. "${net_env}" . "${net_env}"
rm -f "${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}" ] [ -n "${net_address}" ]
} }
@@ -66,14 +66,14 @@ sshcs_net_start() {
sshcs_net_done() { sshcs_net_done() {
# we are done with the network # we are done with the network
if [ -n "${net_device}" ]; then 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 addr flush dev "${net_device}"
ip link set dev "${net_device}" down ip link set dev "${net_device}" down
fi fi
} }
sshcs_trapped_timeout() { sshcs_trapped_timeout() {
err "Timeout reached! Powering off." log_err "Timeout reached! Powering off."
poweroff -f poweroff -f
exit exit
} }
@@ -105,7 +105,7 @@ sshcs_untrap_timeout() {
proc_parse_stat ${pid_timeout} && kill ${pid_timeout} proc_parse_stat ${pid_timeout} && kill ${pid_timeout}
pid_timeout= pid_timeout=
trap - SIGALRM trap - SIGALRM
msg "Timeout cleared." log_dbg "Timeout cleared."
} }
sshcs_shell_run() { sshcs_shell_run() {
@@ -155,7 +155,7 @@ EOF
[ ! -d "/var/log" ] && mkdir -p "/var/log" [ ! -d "/var/log" ] && mkdir -p "/var/log"
touch "/var/log/lastlog" touch "/var/log/lastlog"
msg "Starting dropbear ..." log_dbg "Starting dropbear ..."
dropbear -Esgjk -P "${path_dropbear_pid}" ${sshcs_opt_listen} dropbear -Esgjk -P "${path_dropbear_pid}" ${sshcs_opt_listen}
# Actual unlocking shell # Actual unlocking shell
@@ -190,7 +190,7 @@ sshcs_cryptpart_process() {
cryptdev_orig=${cryptdev} cryptdev_orig=${cryptdev}
if cryptdev=$(resolve_device "${cryptdev_orig}" ${rootdelay}); then if cryptdev=$(resolve_device "${cryptdev_orig}" ${rootdelay}); then
if cryptsetup isLuks "${cryptdev}" >/dev/null 2>&1; 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 # update script used to unlock device either in console or SSH
[ -s "${sshcs_cryptsetup_script}" ] || cat <<'EOF' > "${sshcs_cryptsetup_script}" [ -s "${sshcs_cryptsetup_script}" ] || cat <<'EOF' > "${sshcs_cryptsetup_script}"
@@ -222,9 +222,9 @@ EOF
killall cryptsetup > /dev/null 2>&1 killall cryptsetup > /dev/null 2>&1
break break
fi 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 elif [ ! -e "/dev/mapper/${cryptname}" ]; then
err "cryptsetup failed! Please retry." log_err "cryptsetup failed! Please retry."
else else
break break
fi fi
@@ -233,7 +233,7 @@ EOF
done done
EOF EOF
else 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
fi fi
} }
@@ -272,7 +272,7 @@ else
return return
fi fi
echo "" echo ""
err "Device resources missing! Please retry." log_err "Device resources missing! Please retry."
fi fi
EOF EOF
chmod a+x "${sshcs_cryptsetup_script}" chmod a+x "${sshcs_cryptsetup_script}"
@@ -290,7 +290,7 @@ run_hook() {
# sanity check: crypttab should be present # sanity check: crypttab should be present
[ ! -e "${etc_crypttab}" ] && { [ ! -e "${etc_crypttab}" ] && {
dbg "No crypttab configuration to process" log_dbg "No crypttab configuration to process"
return 0 return 0
} }
@@ -307,13 +307,13 @@ run_hook() {
# Setup script used to unlock device either in console or SSH # Setup script used to unlock device either in console or SSH
sshcs_cryptpart_setup sshcs_cryptpart_setup
if [ ! -e "${sshcs_cryptsetup_script}" ]; then 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 return 0
fi fi
# start and check network # start and check network
if ! sshcs_net_start; then 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 # We still allow to unlock locally with timeout
sshcs_shell_run sshcs_shell_run
return 0 return 0

View File

@@ -2,13 +2,35 @@
. "/init_functions" . "/init_functions"
dbg () { # Log debug message, if debug enabled.
[ ${sshcs_opt_debug} != 0 ] && echo "$@" # 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() { sshcs_env_load() {
local sshcs_env="/etc/initcpio/sshcs_env" local sshcs_env="/etc/initcpio/sshcs_env"
local log_kmsg_default=1
local debug_default=0 local debug_default=0
local net_wol_default=g local net_wol_default=g
local timeout_ipconfig_default=10 local timeout_ipconfig_default=10
@@ -18,6 +40,7 @@ sshcs_env_load() {
[ ${sshcs_env_loaded:-0} -eq 1 ] && return [ ${sshcs_env_loaded:-0} -eq 1 ] && return
[ -e "${sshcs_env}" ] && . "${sshcs_env}" [ -e "${sshcs_env}" ] && . "${sshcs_env}"
sshcs_env_loaded=1 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_debug}" ] && sshcs_opt_debug=${debug_default}
[ -z "${sshcs_opt_net_wol}" ] && sshcs_opt_net_wol=${net_wol_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} [ -z "${sshcs_opt_timeout_ipconfig}" ] && sshcs_opt_timeout_ipconfig=${timeout_ipconfig_default}
@@ -89,7 +112,7 @@ sshcs_cleanup() {
# cleanup dropbear # cleanup dropbear
if [ -f "${path_dropbear_pid}" ]; then if [ -f "${path_dropbear_pid}" ]; then
msg "Stopping dropbear ..." log_dbg "Stopping dropbear ..."
kill $(cat "${path_dropbear_pid}") kill $(cat "${path_dropbear_pid}")
rm -f "${path_dropbear_pid}" rm -f "${path_dropbear_pid}"
fi fi
@@ -99,13 +122,13 @@ sshcs_cleanup() {
# This is necessary to properly terminate both these shells and the spawned # This is necessary to properly terminate both these shells and the spawned
# SSH processes. # SSH processes.
# Reminder: "... | ..." does fork a subshell # Reminder: "... | ..." does fork a subshell
dbg "Searching SSH shells ..." log_dbg "Searching SSH shells ..."
pgrep_output=$(pgrep /usr/bin/ash) pgrep_output=$(pgrep /usr/bin/ash)
pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$") pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$")
echo "${pgrep_output}" | while read pid; do echo "${pgrep_output}" | while read pid; do
proc_parse_stat ${pid} || continue proc_parse_stat ${pid} || continue
proc_find_parent_cmd ${pid} "dropbear" || continue proc_find_parent_cmd ${pid} "dropbear" || continue
dbg "Killing SSH shell pid=${pid}" log_dbg "Killing SSH shell pid=${pid}"
kill -SIGHUP ${pid} kill -SIGHUP ${pid}
done done
fi fi
@@ -130,16 +153,18 @@ sshcs_cleanup() {
# Note: as a side effect, this will also kill the shell that was forked to # 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 # trigger a timeout, which is fine (we want to kill it, either from here or
# from the init shell). # from the init shell).
dbg "Searching console shells ..." log_dbg "Searching console shells ..."
pgrep_output=$(pgrep /usr/bin/ash) pgrep_output=$(pgrep /usr/bin/ash)
pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$") pgrep_output=$(echo "${pgrep_output}" | grep -E -v "^(|1|$$)$")
echo "${pgrep_output}" | while read pid; do echo "${pgrep_output}" | while read pid; do
proc_find_parent_cmd ${pid} "dropbear" && continue proc_find_parent_cmd ${pid} "dropbear" && continue
dbg "Killing console shell pid=${pid}" log_dbg "Killing console shell pid=${pid}"
kill -SIGHUP ${pid} kill -SIGHUP ${pid}
done done
fi fi
# else: when in SSH shell script, we have nothing else to do other than exit. # else: when in SSH shell script, we have nothing else to do other than exit.
log_dbg "All done"
} }
sshcs_check_done() { sshcs_check_done() {
@@ -161,12 +186,12 @@ sshcs_check_done() {
echo "" echo ""
# When finalizing in console, power off # When finalizing in console, power off
if [ ${finalize} -eq 1 ] && proc_is_console; then 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 poweroff -f
fi fi
# When in shell or SSH, let user try again # 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 return
fi fi
@@ -179,7 +204,7 @@ sshcs_check_done() {
fi fi
echo "" 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." proc_is_console || echo "Please wait and reconnect to nominal SSH service."
sshcs_cleanup sshcs_cleanup