turn run-qemu.sh script into mips-vm buildEnv command

This commit is contained in:
Daniel Barlow 2023-02-08 22:16:39 +00:00
parent 103d85bdea
commit 4addcbbd51
7 changed files with 54 additions and 31 deletions

View file

@ -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

View file

@ -102,6 +102,7 @@ in {
tufted
routeros.routeros
routeros.ros-exec-script
mips-vm
];
};
}

View file

@ -25,6 +25,7 @@ final: prev: {
nettle = null;
};
mips-vm = final.callPackage ./pkgs/mips-vm {};
pppoe = final.callPackage ./pkgs/pppoe {};
ppp =
(prev.ppp.override {

11
pkgs/mips-vm/default.nix Normal file
View file

@ -0,0 +1,11 @@
{
qemu
, writeShellScriptBin
, stdenv
, lib
}:
writeShellScriptBin "mips-vm"
''
export PATH="${lib.makeBinPath [qemu]}:$PATH"
${builtins.readFile ./mips-vm.sh}
''

View file

@ -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}

View file

@ -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 "/ #"

View file

@ -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