From 9df22a2764a550f0f9a94f759a091d1d8cdd1135 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Sun, 29 Mar 2020 18:21:04 +0200 Subject: [PATCH] add deploy-qemu-vm.sh example --- README.md | 18 ++++++-- examples/deploy-qemu-vm.sh | 93 ++++++++++++++++++++++++++++++++++++++ examples/qemu-vm/id-vm | 7 +++ examples/qemu-vm/id-vm.pub | 1 + 4 files changed, 115 insertions(+), 4 deletions(-) create mode 100755 examples/deploy-qemu-vm.sh create mode 100644 examples/qemu-vm/id-vm create mode 100644 examples/qemu-vm/id-vm.pub diff --git a/README.md b/README.md index 51b1989..28a0c83 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,7 @@ It should be a reproducible and extensible platform for applications building on Example --- -The easiest way is to try out nix-bitcoin is to use one of the provided examples. -This requires that you either have NixOS installed or another Linux distribution with [Nix](https://nixos.org/nix/) and [VirtualBox](https://www.virtualbox.org). +The easiest way to try out nix-bitcoin is to use one of the provided examples. ``` git clone https://github.com/fort-nix/nix-bitcoin @@ -33,8 +32,19 @@ cd examples/ nix-shell ``` -Now you can run `./deploy-nixops.sh` to install nix-bitcoin in a VirtualBox or `./deploy-container.sh` to install in a [NixOS container](https://github.com/erikarvstedt/extra-container). -This will set up a nix-bitcoin according to `examples/configuration.nix` and then shut down immediately. +The following example scripts set up a nix-bitcoin node according to `examples/configuration.nix` and then +shut down immediately. They leave no traces (outside of `/nix/store`) on the host system. + +- `./deploy-container.sh` creates a [NixOS container](https://github.com/erikarvstedt/extra-container).\ + This is the fastest way to set up a node.\ + Requires: [NixOS](https://nixos.org/) + +- `./deploy-qemu-vm.sh` creates a QEMU VM.\ + Requires: [Nix](https://nixos.org/nix/) + +- `./deploy-nixops.sh` creates a VirtualBox VM via [NixOps](https://github.com/NixOS/nixops).\ + NixOps can be used to deploy to various other backends like cloud providers.\ + Requires: [Nix](https://nixos.org/nix/), [VirtualBox](https://www.virtualbox.org) Available modules --- diff --git a/examples/deploy-qemu-vm.sh b/examples/deploy-qemu-vm.sh new file mode 100755 index 0000000..9267c7b --- /dev/null +++ b/examples/deploy-qemu-vm.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +set -euo pipefail + +# This script demonstrates how to run a nix-bitcoin node in QEMU. +# Running this script leaves no traces on your host system. + +# This demo is a template for your own experiments. +# Feel free to modify or to run nix-shell and execute individual statements of this +# script in the interactive shell. + +# MAKE SURE TO REPLACE the SSH identity file if you use this script for +# anything serious. + +if [[ ! -v IN_NIX_SHELL ]]; then + echo "Running script in nix shell env..." + exec nix-shell --run "${BASH_SOURCE[0]}" +fi + +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 + +echo "Building VM" +nix-build --out-link $tmpDir/vm - < { + configuration = { + imports = [ + + + ]; + virtualisation.graphics = false; + services.mingetty.autologinUser = "root"; + users.users.root = { + openssh.authorizedKeys.keys = [ "$(cat $identityFile.pub)" ]; + }; + }; +}).vm +EOF + +vmMemoryMiB=2048 +vmNumCPUs=4 +sshPort=60734 + +export NIX_DISK_IMAGE=$tmpDir/img +export QEMU_NET_OPTS=hostfwd=tcp::$sshPort-:22 +/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 +echo "Waiting until services are ready..." +c ' +attempts=300 +while ! systemctl is-active clightning &> /dev/null; do + ((attempts-- == 0)) && { echo "timeout"; exit 1; } + sleep 0.2 +done +' +echo +echo "Bitcoind service:" +c systemctl status bitcoind +echo +echo "Bitcoind network:" +c bitcoin-cli getnetworkinfo +echo +echo "lightning-cli state:" +c lightning-cli getinfo +echo +echo "Node info:" +c nodeinfo + +# Cleanup happens at exit (see above) diff --git a/examples/qemu-vm/id-vm b/examples/qemu-vm/id-vm new file mode 100644 index 0000000..ce39161 --- /dev/null +++ b/examples/qemu-vm/id-vm @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACBbda93/QYwDf4PGJp2KgJ1xwKPgKhEHeJrubXAeZWPpgAAAIgDA7dzAwO3 +cwAAAAtzc2gtZWQyNTUxOQAAACBbda93/QYwDf4PGJp2KgJ1xwKPgKhEHeJrubXAeZWPpg +AAAECuRSsNFRQgAOid87b/2kIhgycAH5nPgZwkY4bUpq7LQFt1r3f9BjAN/g8YmnYqAnXH +Ao+AqEQd4mu5tcB5lY+mAAAABG5vbmUB +-----END OPENSSH PRIVATE KEY----- diff --git a/examples/qemu-vm/id-vm.pub b/examples/qemu-vm/id-vm.pub new file mode 100644 index 0000000..114cbff --- /dev/null +++ b/examples/qemu-vm/id-vm.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFt1r3f9BjAN/g8YmnYqAnXHAo+AqEQd4mu5tcB5lY+m none