ops: rescue sshd: reliable teardown (process title, recursive unmount, no rm under a mount), single build via unit restart
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
cf728876db
commit
cc88c5357c
+17
-11
@@ -48,12 +48,21 @@ copy_bin() { # copy a binary and its shared libraries into the chroot, same pat
|
||||
done
|
||||
}
|
||||
|
||||
teardown() {
|
||||
pkill -f "sshd_config -E /rescue.log" 2>/dev/null || true
|
||||
sleep 1
|
||||
pkill -9 -f "sshd_config -E /rescue.log" 2>/dev/null || true
|
||||
local t
|
||||
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() {
|
||||
pkill -f "^/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config -E /rescue.log" 2>/dev/null || true
|
||||
mountpoint -q "$R/dev/pts" && umount "$R/dev/pts" || true
|
||||
mountpoint -q "$R/proc" && umount "$R/proc" || true
|
||||
mountpoint -q "$R/dev" && umount -l "$R/dev" || true
|
||||
rm -rf "$R"; install -d -m 755 "$R"/{etc/ssh,bin,sbin,usr/sbin,usr/bin,run/sshd,var/empty,proc,dev,root,root/.ssh,tmp,lib,lib64}
|
||||
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"
|
||||
@@ -120,7 +129,6 @@ case "$mode" in
|
||||
echo "copytest ok: $(find "$R" -type f | wc -l) files, $(du -sh "$R" | cut -f1)" ;;
|
||||
build) build ;;
|
||||
install)
|
||||
build
|
||||
install -d /usr/local/sbin
|
||||
install -m 755 "$0" /usr/local/sbin/rescue-sshd.sh
|
||||
cat > /etc/systemd/system/leleka-rescue-sshd.service <<EOF
|
||||
@@ -135,9 +143,9 @@ ExecStop=/usr/local/sbin/rescue-sshd.sh --remove
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
systemctl daemon-reload; systemctl enable --now leleka-rescue-sshd.service ;;
|
||||
systemctl daemon-reload; systemctl enable leleka-rescue-sshd.service; systemctl restart leleka-rescue-sshd.service ;;
|
||||
check)
|
||||
pid=$(pgrep -f "^/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config -E /rescue.log" | head -1)
|
||||
pid=$(pgrep -f "sshd_config -E /rescue.log" | head -1)
|
||||
[ -n "$pid" ] || { echo "rescue sshd is not running"; exit 1; }
|
||||
echo "rescue sshd pid $pid; files it maps or holds open outside tmpfs/proc/dev (must be empty):"
|
||||
{ awk '{print $6}' /proc/$pid/maps | grep '^/' | sort -u; ls -l /proc/$pid/fd | awk '{print $NF}' | grep '^/' | sort -u; } \
|
||||
@@ -154,7 +162,5 @@ EOF
|
||||
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
|
||||
pkill -f "^/usr/sbin/sshd -D -e -f /etc/ssh/sshd_config -E /rescue.log" 2>/dev/null || true
|
||||
mountpoint -q "$R/proc" && umount "$R/proc" || true; mountpoint -q "$R/dev" && umount -l "$R/dev" || true
|
||||
rm -rf "$R"; echo removed ;;
|
||||
teardown; echo removed ;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user