pmaports/main/postmarketos-mkinitfs-hook-debug-shell/20-debug-shell.sh
Caleb Connolly 57744b984e
postmarketos-initramfs: export logs on boot failure (MR 4646)
Currently, when postmarketOS fails to boot up, retrieving any
information necessary to root cause the issue is non-trivial, requiring
building a custom initramfs with debug-shell enabled and then manually
copying out data.

Let's improve the situation by exporting logs automatically on boot
failure. This is safer than just exposing a root shell but still
provides a whole lot of useful info that should make duplicating and
triaging issues much easier for developers.

This commit implements the functionality and includes some generally
useful data. We then generate a README and expose files for each command/log
as well as an archive that can be easily attached to a GitLab issue.

To help with triaging, also record the version of the postmarketos-initramfs
package and hash the init.sh and init_functions.sh files.

For testing purposes, you can trigger the log recovery mode on-time by
creating an empty file named ".pmos_export_logs" in the /boot partition.

Signed-off-by: Caleb Connolly <caleb@connolly.tech>
2024-01-05 17:29:45 +00:00

68 lines
1.8 KiB
Bash

#!/bin/sh
# shellcheck disable=SC1091
. ./init_functions.sh
. /usr/share/misc/source_deviceinfo
TELNET_PORT=23
setup_usb_network
start_unudhcpd
show_splash "WARNING: debug-shell is active\\nhttps://postmarketos.org/debug-shell"
echo "Create 'pmos_continue_boot' script"
{
echo "#!/bin/sh"
#Disable any active usb mass storage
echo "if [ -d /config/usb_gadget/g1/functions/mass_storage.0 ]; then setup_usb_storage; fi"
echo "pkill -9 -f pmos_shell"
echo "pkill -f pmos_fail_halt_boot"
echo "pkill -f telnetd.*:${TELNET_PORT}"
} >/usr/bin/pmos_continue_boot
chmod +x /usr/bin/pmos_continue_boot
echo "Create 'pmos_shell' script"
{
echo "#!/bin/sh"
echo "sh"
} >/usr/bin/pmos_shell
chmod +x /usr/bin/pmos_shell
echo "Create 'pmos_fail_halt_boot' script"
{
echo "#!/bin/sh"
echo '. /init_functions.sh'
echo "fail_halt_boot"
} >/usr/bin/pmos_fail_halt_boot
chmod +x /usr/bin/pmos_fail_halt_boot
echo "Start the telnet daemon"
{
echo "#!/bin/sh"
echo "echo \"Type 'pmos_continue_boot' to continue booting:\""
echo "sh"
} >/telnet_connect.sh
chmod +x /telnet_connect.sh
host_ip="${unudhcpd_host_ip:-172.16.42.1}"
telnetd -b "${host_ip}:${TELNET_PORT}" -l /telnet_connect.sh
# mount pstore, if possible
if [ -d /sys/fs/pstore ]; then
mount -t pstore pstore /sys/fs/pstore || true
fi
# mount debugfs - very helpful for debugging
mount -t debugfs none /sys/kernel/debug || true
# make a symlink like Android recoveries do
ln -s /sys/kernel/debug /d
echo "---"
echo "WARNING: debug-shell is active on ${host_ip}:${TELNET_PORT}."
echo "This is a security hole! Only use it for debugging, and"
echo "uninstall the debug-shell hook afterwards!"
echo "You can expose storage devices using 'setup_usb_storage /dev/DEVICE'"
echo "---"
pmos_shell
# Show "Loading" splash again when continuing
show_splash "Loading..."