#!/bin/bash # inject-ssh.sh — Add SSH (dropbear) to initramfs for debugging # Usage: ./hack/inject-ssh.sh [path-to-kubesolo-os.gz] # # This adds a minimal SSH server to the initramfs so you can SSH into the # running KubeSolo OS for debugging. NOT for production use. # # Prerequisites: dropbear binaries (statically compiled) or tcz packages set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" ROOTFS_DIR="${ROOTFS_DIR:-$PROJECT_ROOT/build/rootfs-work}" ROOTFS="$ROOTFS_DIR/rootfs" INITRAMFS="${1:-$ROOTFS_DIR/kubesolo-os.gz}" if [ ! -d "$ROOTFS" ]; then echo "ERROR: Rootfs not found: $ROOTFS" echo "Run 'make rootfs' first." exit 1 fi SSH_PUBKEY="${SSH_PUBKEY:-$HOME/.ssh/id_rsa.pub}" if [ ! -f "$SSH_PUBKEY" ]; then SSH_PUBKEY="$HOME/.ssh/id_ed25519.pub" fi if [ ! -f "$SSH_PUBKEY" ]; then echo "ERROR: No SSH public key found." echo "Set SSH_PUBKEY=/path/to/key.pub or generate one with: ssh-keygen" exit 1 fi echo "==> Injecting SSH support into rootfs..." echo " Public key: $SSH_PUBKEY" # Create SSH directories mkdir -p "$ROOTFS/root/.ssh" mkdir -p "$ROOTFS/etc/dropbear" # Install authorized key cp "$SSH_PUBKEY" "$ROOTFS/root/.ssh/authorized_keys" chmod 700 "$ROOTFS/root/.ssh" chmod 600 "$ROOTFS/root/.ssh/authorized_keys" # Create a startup script for dropbear cat > "$ROOTFS/usr/lib/kubesolo-os/init.d/85-ssh.sh" << 'EOF' #!/bin/sh # 85-ssh.sh — Start SSH server for debugging (dev only) if command -v dropbear >/dev/null 2>&1; then # Generate host keys if missing if [ ! -f /etc/dropbear/dropbear_rsa_host_key ]; then dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key >/dev/null 2>&1 fi if [ ! -f /etc/dropbear/dropbear_ed25519_host_key ]; then dropbearkey -t ed25519 -f /etc/dropbear/dropbear_ed25519_host_key >/dev/null 2>&1 fi dropbear -R -p 22 2>/dev/null log_ok "SSH server (dropbear) started on port 22" else log_warn "dropbear not found — SSH not available" log_warn "To add SSH, install dropbear statically compiled binary to /usr/sbin/dropbear" fi EOF chmod +x "$ROOTFS/usr/lib/kubesolo-os/init.d/85-ssh.sh" echo "==> SSH stage added (85-ssh.sh)" echo "" echo "==> NOTE: You still need the dropbear binary in the rootfs." echo " Option 1: Download a static dropbear build:" echo " wget -O $ROOTFS/usr/sbin/dropbear " echo " chmod +x $ROOTFS/usr/sbin/dropbear" echo "" echo " Option 2: Build from source with CGO_ENABLED=0 equivalent" echo "" echo "==> After adding dropbear, rebuild:" echo " make initramfs iso" echo "" echo "==> Then connect with:" echo " ssh -p 2222 root@localhost (when using hack/dev-vm.sh)"