~razzi/ronin

5950bebb261cce950534527f0aeee810d7e20cbd — Razzi Abuissa 6 months ago 37770f5
Clean up scripts, including retry for ssh
4 files changed, 78 insertions(+), 41 deletions(-)

M Makefile
M guest-setup.sh
R ronin.sh => ronin
M setup-ssh.sh
M Makefile => Makefile +11 -3
@@ 1,10 1,18 @@
all: ronin

ronin: ronin.sh
	./ronin.sh
.PHONY: *

ronin:
	./ronin

lint:
	shellcheck ronin.sh
	shellcheck ronin

ssh:
	@./ronin ssh

launch:
	./ronin launch

clean:
	-pkill -i qemu

M guest-setup.sh => guest-setup.sh +2 -1
@@ 4,11 4,12 @@ ssh-keygen -A
mkdir -p /run/sshd

systemctl reset-failed sshd

systemctl start sshd

adduser ronin --gecos "" --disabled-password
echo ronin:ronin | chpasswd

usermod -aG sudo ronin

mkdir -p /home/ronin/.ssh
chown ronin:ronin /home/ronin/.ssh

R ronin.sh => ronin +57 -32
@@ 1,48 1,73 @@
#!/bin/sh
# Create a virtual machine and log in

set -ex
test ! -z "$RONIN_DEBUG" && set -ex

mkdir -p ~/.cache/ronin && \
  mkdir -p ~/.ronin
RONIN_DISK="$HOME/.ronin/ronin-disk.qcow2"

DEBIAN_VERSION=12
ronin_setup() {
  mkdir -p ~/.cache/ronin && \
    mkdir -p ~/.ronin

DEBIAN_CLOUD_IMAGE="debian-$DEBIAN_VERSION-nocloud-amd64.qcow2"
DEBIAN_IMAGE_URL="https://cloud.debian.org/images/cloud/bookworm/latest/$DEBIAN_CLOUD_IMAGE"
  DEBIAN_VERSION=12

CACHED_DEBIAN_IMAGE="$HOME/.cache/ronin/$DEBIAN_CLOUD_IMAGE"
  DEBIAN_CLOUD_IMAGE="debian-$DEBIAN_VERSION-nocloud-amd64.qcow2"
  DEBIAN_IMAGE_URL="https://cloud.debian.org/images/cloud/bookworm/latest/$DEBIAN_CLOUD_IMAGE"

test ! -f "$CACHED_DEBIAN_IMAGE" && \
  wget "$DEBIAN_IMAGE_URL" --output-document "$CACHED_DEBIAN_IMAGE"
  CACHED_DEBIAN_IMAGE="$HOME/.cache/ronin/$DEBIAN_CLOUD_IMAGE"

RONIN_DEBIAN_IMAGE="$HOME/.ronin/$DEBIAN_CLOUD_IMAGE"
test ! -f "$RONIN_DEBIAN_IMAGE" &&
  cp "$CACHED_DEBIAN_IMAGE" "$RONIN_DEBIAN_IMAGE"
  test ! -f "$CACHED_DEBIAN_IMAGE" && \
    wget "$DEBIAN_IMAGE_URL" --output-document "$CACHED_DEBIAN_IMAGE"

RONIN_DISK="$HOME/.ronin/ronin-disk.qcow2"
  RONIN_DEBIAN_IMAGE="$HOME/.ronin/$DEBIAN_CLOUD_IMAGE"
  test ! -f "$RONIN_DEBIAN_IMAGE" &&
    cp "$CACHED_DEBIAN_IMAGE" "$RONIN_DEBIAN_IMAGE"


  test ! -f "$RONIN_DISK" && \
    cp "$RONIN_DEBIAN_IMAGE" "$RONIN_DISK" && \
    qemu-img resize "$RONIN_DISK" 20G \

  RONIN_PRIVATE_KEY="$HOME/.ronin/ronin_key.priv"

  # Could be encapsulated... ssh-keygen cli is quite unwieldy
  test ! -f "$HOME/.ronin/ronin_key.priv" && \
    ssh-keygen -t ed25519 -f "$RONIN_PRIVATE_KEY" -N '' && \
    mv "$HOME/.ronin/ronin_key.priv.pub" "$HOME/.ronin/ronin_key.pub"

  RONIN_GUEST_SSHD_KEY="$HOME/.ronin/guest_ssh_host_ed25519_key.pub"
  test ! -f "$RONIN_GUEST_SSHD_KEY" && ./setup-ssh.sh

test ! -f "$RONIN_DISK" && \
  cp "$RONIN_DEBIAN_IMAGE" "$RONIN_DISK" && \
  qemu-img resize "$RONIN_DISK" 20G \
  return 0
}

RONIN_PRIVATE_KEY="$HOME/.ronin/ronin_key.priv"
ronin_launch() {
  qemu-system-x86_64 \
    -hda "$RONIN_DISK" \
    -m 4096 \
    -nic hostfwd=tcp::2022-:22 \
    -daemonize \
    -display none  # Comment out this line for a graphical window into the guest. Login as root.
}

# Could be encapsulated... ssh-keygen cli is quite unwieldy
test ! -f "$HOME/.ronin/ronin_key.priv" && \
  ssh-keygen -t ed25519 -f "$RONIN_PRIVATE_KEY" -N '' && \
  mv "$HOME/.ronin/ronin_key.priv.pub" "$HOME/.ronin/ronin_key.pub"
ronin_ssh() {
  ssh -o 'ConnectionAttempts 3' ronin@localhost -p 2022 -i "$HOME/.ronin/ronin_key.priv"
}

# TODO could leave known_hosts in weird state
GUEST_SSHD_KEY="$HOME/.ronin/guest_ssh_host_ed25519_key.pub"
test ! -f "$GUEST_SSHD_KEY" && ./setup-ssh.sh
ronin_main() {
  ronin_setup
  ronin_launch
  ronin_ssh
}

# Launch the VM for prime time
qemu-system-x86_64 \
  -hda "$RONIN_DISK" \
  -m 4096 \
  -nic hostfwd=tcp::2022-:22 \
  -daemonize \
  -display none  # Comment out this line for a graphical window into the guest. Login as root.

ssh ronin@localhost -p 2022 -i "$HOME/.ronin/ronin_key.priv"
if test -z "$@"; then
  ronin_main
elif test "$@" = launch; then
  ronin_launch
elif test "$@" = ssh; then
  ronin_ssh
else
  echo "ronin: unrecognized argument $*"
  return 1
fi

M setup-ssh.sh => setup-ssh.sh +8 -5
@@ 1,13 1,16 @@
#!/bin/sh

BASEDIR=$(dirname "$0")
ABSOLUTE_SCRIPT_PATH=$(readlink -f "$BASEDIR")

cp $ABSOLUTE_SCRIPT_PATH/guest-setup.sh ~/.ronin/
cp "$ABSOLUTE_SCRIPT_PATH/guest-setup.sh" "$HOME/.ronin/"

"$ABSOLUTE_SCRIPT_PATH"/setup-ssh.exp

# Add guest ssh key to host known_hosts
echo -n 'localhost ' >> ~/.ssh/known_hosts
cat $HOME/.ronin/guest_ssh_host_ed25519_key.pub >> ~/.ssh/known_hosts
ronin_guest_sshd_key="$HOME/.ronin/guest_ssh_host_ed25519_key.pub"

ssh_key_contents=$(cat "$ronin_guest_sshd_key")

echo "localhost $ssh_key_contents" >> "$HOME/.ssh/known_hosts"

ssh-keygen -Hf ~/.ssh/known_hosts
ssh-keygen -Hf "$HOME/.ssh/known_hosts"