examples: extract common features for running QEMU VMs
Needed by commit: "examples: add deploy-krops.sh"
This commit is contained in:
parent
cb3d5446c8
commit
ccba86a0f0
@ -17,61 +17,27 @@ if [[ ! -v IN_NIX_SHELL ]]; then
|
||||
exec nix-shell --run "./${BASH_SOURCE[0]##*/} $*"
|
||||
fi
|
||||
|
||||
cd "${BASH_SOURCE[0]%/*}"
|
||||
|
||||
tmpDir=/tmp/nix-bitcoin-qemu-vm
|
||||
mkdir -p $tmpDir
|
||||
|
||||
# Cleanup on exit
|
||||
cleanup() {
|
||||
set +eu
|
||||
kill -9 $qemuPID
|
||||
rm -rf $tmpDir
|
||||
}
|
||||
trap "cleanup" EXIT
|
||||
|
||||
identityFile=qemu-vm/id-vm
|
||||
chmod 0600 $identityFile
|
||||
source qemu-vm/run-vm.sh
|
||||
|
||||
echo "Building VM"
|
||||
nix-build --out-link $tmpDir/vm - <<EOF
|
||||
nix-build --out-link $tmpDir/vm - <<'EOF'
|
||||
(import <nixpkgs/nixos> {
|
||||
configuration = {
|
||||
imports = [
|
||||
<configuration.nix>
|
||||
<qemu-vm/vm-config.nix>
|
||||
<nix-bitcoin/modules/secrets/generate-secrets.nix>
|
||||
];
|
||||
virtualisation.graphics = false;
|
||||
services.mingetty.autologinUser = "root";
|
||||
users.users.root = {
|
||||
openssh.authorizedKeys.keys = [ "$(cat $identityFile.pub)" ];
|
||||
};
|
||||
};
|
||||
}).vm
|
||||
EOF
|
||||
|
||||
vmMemoryMiB=2048
|
||||
vmNumCPUs=4
|
||||
vmMemoryMiB=2048
|
||||
sshPort=60734
|
||||
runVM $tmpDir/vm $vmNumCPUs $vmMemoryMiB $sshPort
|
||||
|
||||
export NIX_DISK_IMAGE=$tmpDir/img
|
||||
export QEMU_NET_OPTS=hostfwd=tcp::$sshPort-:22
|
||||
</dev/null $tmpDir/vm/bin/run-*-vm -m $vmMemoryMiB -smp $vmNumCPUs &>/dev/null &
|
||||
qemuPID=$!
|
||||
|
||||
# Run command in VM
|
||||
c() {
|
||||
ssh -p $sshPort -i $identityFile -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 "$@"
|
||||
}
|
||||
|
||||
echo
|
||||
echo "Waiting for SSH connection..."
|
||||
while ! c : 2>/dev/null; do :; done
|
||||
|
||||
echo
|
||||
vmWaitForSSH
|
||||
echo "Waiting until services are ready..."
|
||||
c '
|
||||
attempts=300
|
||||
@ -99,4 +65,4 @@ case ${1:-} in
|
||||
;;
|
||||
esac
|
||||
|
||||
# Cleanup happens at exit (see above)
|
||||
# Cleanup happens at exit (defined in qemu-vm/run-vm.sh)
|
||||
|
48
examples/qemu-vm/run-vm.sh
Normal file
48
examples/qemu-vm/run-vm.sh
Normal file
@ -0,0 +1,48 @@
|
||||
qemuDir=$(cd "${BASH_SOURCE[0]%/*}" && pwd)
|
||||
|
||||
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
|
||||
</dev/null $vm/bin/run-*-vm -m $vmMemoryMiB -smp $vmNumCPUs &>/dev/null &
|
||||
qemuPID=$!
|
||||
}
|
||||
|
||||
vmWaitForSSH() {
|
||||
echo
|
||||
printf "Waiting for SSH connection..."
|
||||
while ! c : 2>/dev/null; do :; done
|
||||
echo
|
||||
}
|
||||
|
||||
# Run command in VM
|
||||
c() {
|
||||
ssh -p $sshPort -i $identityFile -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
|
7
examples/qemu-vm/vm-config.nix
Normal file
7
examples/qemu-vm/vm-config.nix
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
virtualisation.graphics = false;
|
||||
services.mingetty.autologinUser = "root";
|
||||
users.users.root = {
|
||||
openssh.authorizedKeys.keyFiles = [ ./id-vm.pub ];
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user