Even when an identity file (i.e. a private key) is specified, SSH tries all keys loaded by the SSH agent first for authentication (if an agent is running). When the agent returns many keys this can lead to a "Too Many Failures" auth error, because none of the agent keys are accepted by the VM SSH server. Fix this by only using the provided key.
53 lines
1.1 KiB
Bash
53 lines
1.1 KiB
Bash
qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
|
|
|
|
# shellcheck disable=SC1091
|
|
source "$qemuDir/wait-until.sh"
|
|
|
|
tmpDir=/tmp/nix-bitcoin-qemu-vm
|
|
mkdir -p "$tmpDir"
|
|
|
|
# Cleanup on exit
|
|
cleanup() {
|
|
set +eu
|
|
if [[ $qemuPID ]]; then
|
|
kill -9 "$qemuPID"
|
|
fi
|
|
rm -rf "$tmpDir"
|
|
}
|
|
trap "cleanup" EXIT
|
|
|
|
identityFile=$qemuDir/id-vm
|
|
chmod 0600 "$identityFile"
|
|
|
|
runVM() {
|
|
vm=$1
|
|
vmNumCPUs=$2
|
|
vmMemoryMiB=$3
|
|
sshPort=$4
|
|
|
|
export NIX_DISK_IMAGE="$tmpDir/img"
|
|
export QEMU_NET_OPTS="hostfwd=tcp::${sshPort}-:22"
|
|
# shellcheck disable=SC2211
|
|
</dev/null "$vm"/bin/run-*-vm -m "$vmMemoryMiB" -smp "$vmNumCPUs" &>/dev/null &
|
|
qemuPID=$!
|
|
}
|
|
|
|
vmWaitForSSH() {
|
|
echo
|
|
printf "Waiting for SSH connection..."
|
|
waitUntil "c : 2>/dev/null" 500
|
|
echo
|
|
}
|
|
|
|
# Run command in VM
|
|
c() {
|
|
ssh -p "$sshPort" -i "$identityFile" -o IdentitiesOnly=yes -o ConnectTimeout=1 \
|
|
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
|
|
-o ControlMaster=auto -o ControlPath=$tmpDir/ssh-connection -o ControlPersist=60 \
|
|
root@127.0.0.1 "$@"
|
|
}
|
|
export identityFile
|
|
export sshPort
|
|
export tmpDir
|
|
export -f c
|