2021-03-08 06:11:15 -08:00
|
|
|
qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
|
|
|
|
|
2022-08-16 12:00:00 -07:00
|
|
|
# shellcheck disable=SC1091
|
2021-03-08 06:11:16 -08:00
|
|
|
source "$qemuDir/wait-until.sh"
|
|
|
|
|
2021-03-08 06:11:15 -08:00
|
|
|
tmpDir=/tmp/nix-bitcoin-qemu-vm
|
2022-08-16 12:00:00 -07:00
|
|
|
mkdir -p "$tmpDir"
|
2021-03-08 06:11:15 -08:00
|
|
|
|
|
|
|
# Cleanup on exit
|
|
|
|
cleanup() {
|
|
|
|
set +eu
|
|
|
|
if [[ $qemuPID ]]; then
|
2022-08-16 12:00:00 -07:00
|
|
|
kill -9 "$qemuPID"
|
2021-03-08 06:11:15 -08:00
|
|
|
fi
|
2022-08-16 12:00:00 -07:00
|
|
|
rm -rf "$tmpDir"
|
2021-03-08 06:11:15 -08:00
|
|
|
}
|
|
|
|
trap "cleanup" EXIT
|
|
|
|
|
|
|
|
identityFile=$qemuDir/id-vm
|
2022-08-16 12:00:00 -07:00
|
|
|
chmod 0600 "$identityFile"
|
2021-03-08 06:11:15 -08:00
|
|
|
|
|
|
|
runVM() {
|
|
|
|
vm=$1
|
|
|
|
vmNumCPUs=$2
|
|
|
|
vmMemoryMiB=$3
|
|
|
|
sshPort=$4
|
|
|
|
|
2022-08-16 12:00:00 -07:00
|
|
|
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 &
|
2021-03-08 06:11:15 -08:00
|
|
|
qemuPID=$!
|
|
|
|
}
|
|
|
|
|
|
|
|
vmWaitForSSH() {
|
|
|
|
echo
|
|
|
|
printf "Waiting for SSH connection..."
|
2021-03-08 06:11:16 -08:00
|
|
|
waitUntil "c : 2>/dev/null" 500
|
2021-03-08 06:11:15 -08:00
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run command in VM
|
|
|
|
c() {
|
2022-08-16 12:00:00 -07:00
|
|
|
ssh -p "$sshPort" -i "$identityFile" -o ConnectTimeout=1 \
|
2021-03-08 06:11:15 -08:00
|
|
|
-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
|