bootstrap_ssh.sh 1.4 KB

123456789101112131415161718192021222324
  1. #!/bin/bash
  2. # bootstrap_ssh.sh <vm> <port> <layout us|de> [password] - for a VM installed from an ISO that has no ssh: serves a setup script
  3. # from this host ($PORT, pick one nobody else uses) and runs it in a terminal that is already open and focused in the guest
  4. # (the QEMU user network reaches the host as 10.0.2.2). The script enables sshd, adds the rig key, passwordless sudo,
  5. # autologin is distro specific and not done here. The guest password is typed by the script's sudo (default labbalab1).
  6. HERE=$(cd "$(dirname "$0")" && pwd)
  7. LINUXVM=${LINUXVM:-$HOME/.local/share/linuxvm}
  8. vm=$1 port=$2 layout=$3 pw=${4:-labbalab1}
  9. dir=$(mktemp -d)
  10. cat > "$dir/s" <<EOS
  11. #!/bin/bash
  12. echo $pw | sudo -S true 2>/dev/null
  13. sudo systemctl enable --now sshd 2>/dev/null || sudo systemctl enable --now ssh
  14. mkdir -p ~/.ssh; chmod 700 ~/.ssh; echo '$(cat "$LINUXVM/id_ed25519.pub")' >> ~/.ssh/authorized_keys; chmod 600 ~/.ssh/authorized_keys
  15. echo 'lab ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/lab >/dev/null
  16. sudo chsh -s /bin/bash lab
  17. sudo ufw allow 22 2>/dev/null; sudo firewall-cmd --add-service=ssh --permanent 2>/dev/null
  18. echo SETUP-DONE
  19. EOS
  20. (cd "$dir" && python3 -m http.server "$port" --bind 0.0.0.0 >/dev/null 2>&1 & echo $! > "$dir/pid")
  21. sleep 1
  22. q() { "$HERE/qtype.sh" "$vm" "$layout" "$1"; }
  23. q "curl -sO 10.0.2.2:$port/s"; q ret; sleep 4; q "bash s"; q ret; sleep 15
  24. kill "$(cat "$dir/pid")"; rm -rf "$dir"