#!/usr/bin/env bash # rnr5090: a second sshd (port 2222) that lives entirely in RAM, so you can log in and reboot the machine # while the root NVMe is stalled. Owner-run (needs root, writes /run and /etc/systemd/system). # # sudo bash rescue-sshd.sh --build # (re)build the RAM chroot in /run/rescue and start the daemon once, now # sudo bash rescue-sshd.sh --install # build + install a systemd unit that rebuilds it at every boot # sudo bash rescue-sshd.sh --check # prove that the running daemon touches no disk file # sudo bash rescue-sshd.sh --netconsole LAPTOP_IP # also send kernel messages over UDP to the laptop (listen: nc -ul 6666) # sudo bash rescue-sshd.sh --remove # stop and remove the unit and the chroot # # Use from the laptop when the normal ssh (port 22) hangs: # ssh -p 2222 root@192.168.1.196 (keys copied from user para; the host key is a new one) # rboot # sync, remount read-only, reboot now (sysrq); runs inside the rescue shell # dmesg | tail; cat /proc/pressure/io # what the kernel says about the stall # # Design: sshd, bash, busybox and every shared library they need are copied into tmpfs (/run), the daemon runs # chrooted there with UsePAM=no, its log goes to a file in tmpfs (not journald), /dev is a bind mount of devtmpfs # (RAM) and /proc is mounted inside, so a login needs no file from the NVMe. With swap enabled tmpfs pages could be # swapped out to the same NVMe: run server-hang-autoreboot.sh --swapoff too. set -euo pipefail R=${RESCUE_ROOT:-/run/rescue} PORT=${RESCUE_PORT:-2222} KEY_USER=${RESCUE_KEY_USER:-para} # whose authorized_keys are copied; the login inside the rescue chroot is root mode=""; laptop_ip="" while [ $# -gt 0 ]; do case "$1" in --build) mode=build ;; --install) mode=install ;; --check) mode=check ;; --remove) mode=remove ;; --stop) mode=stop ;; --copytest) mode=copytest ;; --netconsole) mode=netconsole; laptop_ip=${2:?laptop ip}; shift ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac shift done if [ -z "$mode" ]; then sed -n '2,20p' "$0"; exit 0; fi if [ "$mode" != copytest ] && [ "$(id -u)" -ne 0 ]; then echo "run as root (sudo)" >&2; exit 1; fi copy_bin() { # copy a binary and its shared libraries into the chroot, same paths local b; b=$(readlink -f "$1") install -D "$b" "$R$b" [ "$b" = "$1" ] || { install -d "$R$(dirname "$1")"; ln -sf "$b" "$R$1"; } { ldd "$b" 2>/dev/null || true; } | awk '/=> \//{print $3} /^[[:space:]]*\//{print $1}' | while read -r l; do local rl; rl=$(readlink -f "$l"); install -D "$rl" "$R$rl" [ "$rl" = "$l" ] || { install -d "$R$(dirname "$l")"; ln -sf "$rl" "$R$l"; } done } teardown() { pkill -f "sshd_config -E /[r]escue.log" 2>/dev/null || true sleep 1 pkill -9 -f "sshd_config -E /[r]escue.log" 2>/dev/null || true local t while read -r t; do mount --make-rprivate "$t" 2>/dev/null || true; done < <(awk -v r="$R" 'index($2, r)==1 {print $2}' /proc/mounts) while read -r t; do umount -l "$t" 2>/dev/null || true; done < <(awk -v r="$R" 'index($2, r)==1 {print $2}' /proc/mounts | sort -r) if awk -v r="$R" 'index($2, r)==1' /proc/mounts | grep -q .; then echo "refusing to delete $R: something is still mounted under it" >&2; awk -v r="$R" 'index($2, r)==1' /proc/mounts >&2; exit 1 fi rm -rf --one-file-system "$R" } build() { teardown install -d -m 755 "$R"/{etc/ssh,bin,sbin,usr/sbin,usr/bin,run/sshd,var/empty,proc,dev,root/.ssh,tmp,lib,lib64} # binaries: sshd (+ privsep helpers), shell, busybox applets, sysrq helper for b in /usr/sbin/sshd /usr/bin/bash /usr/bin/busybox /usr/lib/openssh/sftp-server /usr/lib/openssh/sshd-session /usr/lib/openssh/sshd-auth; do [ -e "$b" ] && copy_bin "$b" done # NSS: getpwnam needs libnss_files inside the chroot for l in $(ldconfig -p 2>/dev/null | awk '/libnss_files\.so/{print $NF}' | head -1); do rl=$(readlink -f "$l"); install -D "$rl" "$R$rl"; [ "$rl" = "$l" ] || { install -d "$R$(dirname "$l")"; ln -sf "$rl" "$R$l"; } done chroot "$R" /usr/bin/busybox --install -s /bin 2>/dev/null || { for a in ls cat dmesg ps free df sync mount umount dd grep tail head sleep sh sysctl; do ln -sf /usr/bin/busybox "$R/bin/$a"; done; } ln -sf /usr/bin/bash "$R/bin/bash" # identity and keys (copied once, no disk access later) { echo 'root:x:0:0:root:/root:/bin/bash'; getent passwd sshd; } > "$R/etc/passwd" { echo 'root:x:0:'; getent group nogroup sshd; } > "$R/etc/group" 2>/dev/null || true printf 'passwd: files group: files shadow: files ' > "$R/etc/nsswitch.conf" install -m 600 "$(getent passwd "$KEY_USER" | cut -d: -f6)/.ssh/authorized_keys" "$R/root/.ssh/authorized_keys" chmod 700 "$R/root" "$R/root/.ssh" ssh-keygen -q -t ed25519 -N '' -f "$R/etc/ssh/ssh_host_ed25519_key" cat > "$R/etc/ssh/sshd_config" < "$R/bin/rboot" <<'EOF' #!/bin/sh # sync, remount read-only, reboot immediately (sysrq s,u,b) echo "sysrq: sync, remount ro, reboot" echo s > /proc/sysrq-trigger; sleep 1; echo u > /proc/sysrq-trigger; sleep 1; echo b > /proc/sysrq-trigger EOF chmod 755 "$R/bin/rboot" mount -t proc proc "$R/proc" # own device nodes and a NEW devpts instance (never bind /dev: shared propagation would let our umount hit the host) # /run is mounted nodev, so device nodes need their own fresh tmpfs mount mount -t tmpfs -o mode=755,size=1m tmpfs "$R/dev" mknod -m 666 "$R/dev/null" c 1 3; mknod -m 666 "$R/dev/zero" c 1 5; mknod -m 666 "$R/dev/full" c 1 7 mknod -m 666 "$R/dev/random" c 1 8; mknod -m 666 "$R/dev/urandom" c 1 9; mknod -m 666 "$R/dev/tty" c 5 0 install -d "$R/dev/pts" mount -t devpts devpts "$R/dev/pts" -o newinstance,gid=5,mode=620,ptmxmode=666 ln -sf pts/ptmx "$R/dev/ptmx" : > "$R/rescue.log" chroot "$R" /usr/sbin/sshd -t -f /etc/ssh/sshd_config nohup chroot "$R" /usr/sbin/sshd -D -e -f /etc/ssh/sshd_config -E /rescue.log >/dev/null 2>&1 & sleep 1 echo "rescue sshd listening on :$PORT (login: root, with the keys of $KEY_USER, RAM chroot $R, log $R/rescue.log)" echo "first connect: ssh -p $PORT root@ (new host key); then: rboot" } case "$mode" in copytest) # no root, no mounts, no daemon: only checks that every needed file can be copied (use RESCUE_ROOT=/some/dir) rm -rf "$R"; install -d "$R" for b in /usr/sbin/sshd /usr/bin/bash /usr/bin/busybox /usr/lib/openssh/sftp-server /usr/lib/openssh/sshd-session /usr/lib/openssh/sshd-auth; do [ -e "$b" ] && copy_bin "$b" && echo "copied $b" done for l in $(ldconfig -p 2>/dev/null | awk '/libnss_files\.so/{print $NF}' | head -1); do rl=$(readlink -f "$l"); install -D "$rl" "$R$rl"; echo "copied $rl"; done echo "copytest ok: $(find "$R" -type f | wc -l) files, $(du -sh "$R" | cut -f1)" ;; build) build ;; install) install -d /usr/local/sbin install -m 755 "$0" /usr/local/sbin/rescue-sshd.sh cat > /etc/systemd/system/leleka-rescue-sshd.service </dev/null || stat -f -c %T "$f" 2>/dev/null || echo unknown) case "$fs" in tmpfs|proc|devtmpfs|devpts|sockfs) ;; *) echo " $f ($fs)";; esac done echo "done" ;; netconsole) ifc=$(ip -o route get "$laptop_ip" | sed -n 's/.* dev \([^ ]*\).*/\1/p'); src=$(ip -o route get "$laptop_ip" | sed -n 's/.* src \([^ ]*\).*/\1/p') modprobe netconsole "netconsole=6665@$src/$ifc,6666@$laptop_ip/" echo "kernel messages now go by UDP to $laptop_ip:6666 (on the laptop: any UDP listener, e.g. python -c ... or ncat -ul 6666)" echo "to keep it after reboot: echo 'netconsole netconsole=6665@$src/$ifc,6666@$laptop_ip/' > /etc/modules-load.d/netconsole.conf" ;; stop) # used by the unit's ExecStop: only tear the chroot down; the unit must stay enabled for the next boot teardown ;; remove) systemctl disable --now leleka-rescue-sshd.service 2>/dev/null || true rm -f /etc/systemd/system/leleka-rescue-sshd.service; systemctl daemon-reload 2>/dev/null || true teardown; echo removed ;; esac