From 4addcbbd510bb003ad2cd2c3e034252cb73b7dee Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 8 Feb 2023 22:16:39 +0000 Subject: [PATCH] turn run-qemu.sh script into mips-vm buildEnv command --- README.md | 30 ++++++++++--------- default.nix | 1 + overlay.nix | 1 + pkgs/mips-vm/default.nix | 11 +++++++ .../run-qemu.sh => pkgs/mips-vm/mips-vm.sh | 25 ++++++++++++---- tests/pppoe/getaddress.expect | 2 +- tests/pppoe/test.nix | 15 ++++------ 7 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 pkgs/mips-vm/default.nix rename scripts/run-qemu.sh => pkgs/mips-vm/mips-vm.sh (56%) diff --git a/README.md b/README.md index 2c6fb4a..1d06f26 100644 --- a/README.md +++ b/README.md @@ -93,23 +93,25 @@ in the right way ### Running Liminix in Qemu -`./scripts/run-qemu.sh` accepts a kernel vmlinux image and a squashfs -and runs qemu with appropriate config for two ethernet interfaces -hooked up to "lan" and "access" respectively. It connects the Liminix serial console -and the [QEMU monitor](https://www.qemu.org/docs/master/system/monitor.html) to -stdin/stdout. Use ^P (not ^A) to switch to the monitor. +In a `buildEnv` nix-shell, you can use the `mips-vm` command +to run Qemu with appropriate config for two ethernet interfaces +hooked up to "lan" and "access" respectively. It connects the Liminix +serial console and the [QEMU monitor](https://www.qemu.org/docs/master/system/monitor.html) to stdin/stdout. Use ^P (not ^A) to switch to the monitor. -If you run with `--background /path/to/unix/socket` it will fork into -the background and open a Unix socket at that pathname to communicate -on. Use `./scripts/connect-qemu.sh` to connect to it, and ^O to -disconnect. + nix-shell -A buildEnv --arg device '(import ./devices/qemu)' --run "mips-vm result/vmlinux result/squashfs" + +If you run with `--background /path/to/some/directory` as the first +parameter, it will fork into the background and open Unix sockets in +that directory for console and monitor. Use +`./scripts/connect-qemu.sh` to connect to either of these sockets, and +^O to disconnect. ### Emulated upstream connection -In the tests/support/ppp-server directory there is a derivation -to install and configure [Mikrotik RouterOS](https://mikrotik.com/software) as -a PPPoE access concentrator connected to the `access` and `world` -networks, so that Liminix PPPoE client support can be tested. +In pkgs/routeros there is a derivation to install and configure +[Mikrotik RouterOS](https://mikrotik.com/software) as a PPPoE access +concentrator connected to the `access` and `world` networks, so that +Liminix PPPoE client support can be tested. This is made available in the `buildEnv`, so you can do something like @@ -132,8 +134,8 @@ you can run all of the tests by evaluating `ci.nix`: nix-build --argstr liminix `pwd` --argstr nixpkgs `pwd`/../nixpkgs --argstr unstable `pwd`/../unstable-nixpkgs/ ci.nix +or to run a named test, use the `-A` flag. For example, `-A pppoe` -Some of the tests require the emulated upstream connection to be running. ## Hardware diff --git a/default.nix b/default.nix index 4a3b6c0..6ec720f 100644 --- a/default.nix +++ b/default.nix @@ -102,6 +102,7 @@ in { tufted routeros.routeros routeros.ros-exec-script + mips-vm ]; }; } diff --git a/overlay.nix b/overlay.nix index e00aac4..0d046b3 100644 --- a/overlay.nix +++ b/overlay.nix @@ -25,6 +25,7 @@ final: prev: { nettle = null; }; + mips-vm = final.callPackage ./pkgs/mips-vm {}; pppoe = final.callPackage ./pkgs/pppoe {}; ppp = (prev.ppp.override { diff --git a/pkgs/mips-vm/default.nix b/pkgs/mips-vm/default.nix new file mode 100644 index 0000000..7c1b22e --- /dev/null +++ b/pkgs/mips-vm/default.nix @@ -0,0 +1,11 @@ +{ + qemu +, writeShellScriptBin +, stdenv +, lib +}: +writeShellScriptBin "mips-vm" + '' + export PATH="${lib.makeBinPath [qemu]}:$PATH" + ${builtins.readFile ./mips-vm.sh} + '' diff --git a/scripts/run-qemu.sh b/pkgs/mips-vm/mips-vm.sh similarity index 56% rename from scripts/run-qemu.sh rename to pkgs/mips-vm/mips-vm.sh index 24e3b8f..9409981 100755 --- a/scripts/run-qemu.sh +++ b/pkgs/mips-vm/mips-vm.sh @@ -1,16 +1,29 @@ -#!/usr/bin/env nix-shell -#! nix-shell -i bash -p qemu +#!/usr/bin/env bash + +usage(){ + echo "usage: $(basename $0) [--background /path/to/state_directory] kernel rootimg" + echo -e "\nWithout --background, use C-p c (not C-a c) to switch to the monitor" + exit 1 +} if test "$1" = "--background" ; then - socket=$2 - pid="`dirname $socket`/`basename $socket .sock`.pid" + statedir=$2 + if test -z "$statedir" || ! test -d $statedir ; then + usage + fi + pid="${statedir}/pid" + socket="${statedir}/console" + monitor="${statedir}/monitor" echo "running in background, socket is $socket, pid $pid" - flags="--daemonize --pidfile $pid -chardev socket,id=sock,path=$2,server=on,wait=off,mux=on -mon chardev=sock,mode=readline -serial chardev:sock " + flags="--daemonize --pidfile $pid -serial unix:$socket,server,nowait -monitor unix:$monitor,server,nowait" shift;shift else flags="-serial mon:stdio" fi +test -n "$2" || usage + + INIT=${INIT-/bin/init} echo $QEMU_OPTIONS qemu-system-mips \ @@ -22,4 +35,4 @@ qemu-system-mips \ -device virtio-net-pci,disable-legacy=on,disable-modern=off,netdev=access,mac=ba:ad:1d:ea:21:02 \ -netdev socket,id=lan,mcast=230.0.0.1:1235,localaddr=127.0.0.1 \ -device virtio-net-pci,disable-legacy=on,disable-modern=off,netdev=lan,mac=ba:ad:1d:ea:21:01 \ - -kernel $1 -display none $flags ${QEMU_OPTIONS} + -kernel $1 -display none $flags ${QEMU_OPTIONS} diff --git a/tests/pppoe/getaddress.expect b/tests/pppoe/getaddress.expect index bb88202..ea283e9 100644 --- a/tests/pppoe/getaddress.expect +++ b/tests/pppoe/getaddress.expect @@ -1,6 +1,6 @@ set timeout 60 -spawn socat unix-connect:foo.sock - +spawn socat unix-connect:vm/console - send "\r\n" expect "login:" { send "root\r\n" } expect "/ #" diff --git a/tests/pppoe/test.nix b/tests/pppoe/test.nix index 258bb47..f5ef219 100644 --- a/tests/pppoe/test.nix +++ b/tests/pppoe/test.nix @@ -7,20 +7,15 @@ let img = (import liminix { liminix-config = ./configuration.nix; }).outputs.default; pkgs = import { overlays = [(import ../../overlay.nix)]; }; - ros = pkgs.pkgsBuildBuild.routeros; - run-qemu = pkgs.writeShellScriptBin "run-qemu" '' - export PATH="${pkgs.lib.makeBinPath [pkgs.qemu]}:$PATH" - ${builtins.readFile ../../scripts/run-qemu.sh} - ''; - + inherit (pkgs.pkgsBuildBuild) routeros mips-vm; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ python3Packages.scapy expect jq socat - ros.routeros - run-qemu + routeros.routeros + mips-vm ] ; } '' serverstatedir=$(mktemp -d -t routeros-XXXXXX) @@ -50,8 +45,8 @@ fatal(){ trap fatal ERR routeros $serverstatedir - -run-qemu --background foo.sock ${img}/vmlinux ${img}/squashfs +mkdir vm +mips-vm --background ./vm ${img}/vmlinux ${img}/squashfs expect ${./getaddress.expect} set -o pipefail