commit 4addcbbd510bb003ad2cd2c3e034252cb73b7dee
parent 103d85bdeab0333217b6882a7714da779ac6ef8e
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 8 Feb 2023 22:16:39 +0000
turn run-qemu.sh script into mips-vm buildEnv command
Diffstat:
8 files changed, 73 insertions(+), 50 deletions(-)
diff --git 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
@@ -102,6 +102,7 @@ in {
tufted
routeros.routeros
routeros.ros-exec-script
+ mips-vm
];
};
}
diff --git 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
@@ -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/pkgs/mips-vm/mips-vm.sh b/pkgs/mips-vm/mips-vm.sh
@@ -0,0 +1,38 @@
+#!/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
+ 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 -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 \
+ -M malta -m 256 \
+ -echr 16 \
+ -append "default console=ttyS0,38400n8 panic=10 oops=panic init=$INIT loglevel=8 root=/dev/vda" \
+ -drive file=$2,format=raw,readonly=on,if=virtio \
+ -netdev socket,id=access,mcast=230.0.0.1:1234,localaddr=127.0.0.1 \
+ -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}
diff --git a/scripts/run-qemu.sh b/scripts/run-qemu.sh
@@ -1,25 +0,0 @@
-#!/usr/bin/env nix-shell
-#! nix-shell -i bash -p qemu
-
-if test "$1" = "--background" ; then
- socket=$2
- pid="`dirname $socket`/`basename $socket .sock`.pid"
- 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 "
- shift;shift
-else
- flags="-serial mon:stdio"
-fi
-
-INIT=${INIT-/bin/init}
-echo $QEMU_OPTIONS
-qemu-system-mips \
- -M malta -m 256 \
- -echr 16 \
- -append "default console=ttyS0,38400n8 panic=10 oops=panic init=$INIT loglevel=8 root=/dev/vda" \
- -drive file=$2,format=raw,readonly=on,if=virtio \
- -netdev socket,id=access,mcast=230.0.0.1:1234,localaddr=127.0.0.1 \
- -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}
diff --git 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
@@ -7,20 +7,15 @@ let img = (import liminix {
liminix-config = ./configuration.nix;
}).outputs.default;
pkgs = import <nixpkgs> { 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