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

module-based-network
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 ### Running Liminix in Qemu
`./scripts/run-qemu.sh` accepts a kernel vmlinux image and a squashfs In a `buildEnv` nix-shell, you can use the `mips-vm` command
and runs qemu with appropriate config for two ethernet interfaces to run Qemu with appropriate config for two ethernet interfaces
hooked up to "lan" and "access" respectively. It connects the Liminix serial console hooked up to "lan" and "access" respectively. It connects the Liminix
and the [QEMU monitor](https://www.qemu.org/docs/master/system/monitor.html) to 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.
stdin/stdout. Use ^P (not ^A) to switch to the monitor.
If you run with `--background /path/to/unix/socket` it will fork into nix-shell -A buildEnv --arg device '(import ./devices/qemu)' --run "mips-vm result/vmlinux result/squashfs"
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 If you run with `--background /path/to/some/directory` as the first
disconnect. 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 ### Emulated upstream connection
In the tests/support/ppp-server directory there is a derivation In pkgs/routeros there is a derivation to install and configure
to install and configure [Mikrotik RouterOS](https://mikrotik.com/software) as [Mikrotik RouterOS](https://mikrotik.com/software) as a PPPoE access
a PPPoE access concentrator connected to the `access` and `world` concentrator connected to the `access` and `world` networks, so that
networks, so that Liminix PPPoE client support can be tested. Liminix PPPoE client support can be tested.
This is made available in the `buildEnv`, so you can do something like 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 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 ## Hardware

View File

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

View File

@ -25,6 +25,7 @@ final: prev: {
nettle = null; nettle = null;
}; };
mips-vm = final.callPackage ./pkgs/mips-vm {};
pppoe = final.callPackage ./pkgs/pppoe {}; pppoe = final.callPackage ./pkgs/pppoe {};
ppp = ppp =
(prev.ppp.override { (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 #!/usr/bin/env bash
#! nix-shell -i bash -p qemu
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 if test "$1" = "--background" ; then
socket=$2 statedir=$2
pid="`dirname $socket`/`basename $socket .sock`.pid" 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" 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 shift;shift
else else
flags="-serial mon:stdio" flags="-serial mon:stdio"
fi fi
test -n "$2" || usage
INIT=${INIT-/bin/init} INIT=${INIT-/bin/init}
echo $QEMU_OPTIONS echo $QEMU_OPTIONS
qemu-system-mips \ 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 \ -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 \ -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 \ -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 set timeout 60
spawn socat unix-connect:foo.sock - spawn socat unix-connect:vm/console -
send "\r\n" send "\r\n"
expect "login:" { send "root\r\n" } expect "login:" { send "root\r\n" }
expect "/ #" expect "/ #"

View File

@ -7,20 +7,15 @@ let img = (import liminix {
liminix-config = ./configuration.nix; liminix-config = ./configuration.nix;
}).outputs.default; }).outputs.default;
pkgs = import <nixpkgs> { overlays = [(import ../../overlay.nix)]; }; pkgs = import <nixpkgs> { overlays = [(import ../../overlay.nix)]; };
ros = pkgs.pkgsBuildBuild.routeros; inherit (pkgs.pkgsBuildBuild) routeros mips-vm;
run-qemu = pkgs.writeShellScriptBin "run-qemu" ''
export PATH="${pkgs.lib.makeBinPath [pkgs.qemu]}:$PATH"
${builtins.readFile ../../scripts/run-qemu.sh}
'';
in pkgs.runCommand "check" { in pkgs.runCommand "check" {
nativeBuildInputs = with pkgs; [ nativeBuildInputs = with pkgs; [
python3Packages.scapy python3Packages.scapy
expect expect
jq jq
socat socat
ros.routeros routeros.routeros
run-qemu mips-vm
] ; ] ;
} '' } ''
serverstatedir=$(mktemp -d -t routeros-XXXXXX) serverstatedir=$(mktemp -d -t routeros-XXXXXX)
@ -50,8 +45,8 @@ fatal(){
trap fatal ERR trap fatal ERR
routeros $serverstatedir routeros $serverstatedir
mkdir vm
run-qemu --background foo.sock ${img}/vmlinux ${img}/squashfs mips-vm --background ./vm ${img}/vmlinux ${img}/squashfs
expect ${./getaddress.expect} expect ${./getaddress.expect}
set -o pipefail set -o pipefail