4dac9e5814
machine can now declare an option `virtualisation.vlans' that causes it to have network interfaces connected to each listed virtual network. For instance, virtualisation.vlans = [ 1 2 ]; causes the machine to have two interfaces (in addition to eth0, used by the test driver to control the machine): eth1 connected to network 1 with IP address 192.168.1.<i>, and eth2 connected to network 2 with address 192.168.2.<i> (where <i> is the index of the machine in the `nodes' attribute set). On the other hand, virtualisation.vlans = [ 2 ]; causes the machine to only have an eth1 connected to network 2 with address 192.168.2.<i>. So each virtual network <n> is assigned the IP range 192.168.<n>.0/24. Each virtual network is implemented using a separate multicast address on the host, so guests really cannot talk to networks to which they are not connected. * Added a simple NAT test to demonstrate this. * Added an option `virtualisation.qemu.options' to specify QEMU command-line options. Used to factor out some commonality between the test driver script and the interactive test script. svn path=/nixos/trunk/; revision=21928
138 lines
4.7 KiB
Nix
138 lines
4.7 KiB
Nix
{ nixpkgs, services, system }:
|
|
|
|
let pkgs = import nixpkgs { config = {}; inherit system; }; in
|
|
|
|
with pkgs;
|
|
|
|
rec {
|
|
|
|
inherit pkgs;
|
|
|
|
|
|
# Build a virtual network from an attribute set `{ machine1 =
|
|
# config1; ... machineN = configN; }', where `machineX' is the
|
|
# hostname and `configX' is a NixOS system configuration. The
|
|
# result is a script that starts a QEMU instance for each virtual
|
|
# machine. Each machine is given an arbitrary IP address in the
|
|
# virtual network.
|
|
buildVirtualNetwork =
|
|
{ nodes }:
|
|
|
|
let nodes_ = lib.mapAttrs (n: buildVM nodes_) (assignIPAddresses nodes); in
|
|
|
|
stdenv.mkDerivation {
|
|
name = "vms";
|
|
buildCommand =
|
|
''
|
|
ensureDir $out/vms
|
|
${
|
|
lib.concatMapStrings (vm:
|
|
''
|
|
ln -sn ${vm.config.system.build.vm} $out/vms/${vm.config.networking.hostName}
|
|
''
|
|
) (lib.attrValues nodes_)
|
|
}
|
|
|
|
ensureDir $out/bin
|
|
cat > $out/bin/run-vms <<EOF
|
|
#! ${stdenv.shell}
|
|
port=8080
|
|
for i in $out/vms/*; do
|
|
port2=\$((port++))
|
|
echo "forwarding localhost:\$port2 to \$(basename \$i):80"
|
|
QEMU_OPTS="-redir tcp:\$port2::80" \$i/bin/run-*-vm &
|
|
done
|
|
EOF
|
|
chmod +x $out/bin/run-vms
|
|
''; # */
|
|
};
|
|
|
|
|
|
buildVM =
|
|
nodes: configurations:
|
|
|
|
import ./eval-config.nix {
|
|
inherit nixpkgs services system;
|
|
modules = configurations ++
|
|
[ ../modules/virtualisation/qemu-vm.nix
|
|
../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs
|
|
{ key = "no-manual"; services.nixosManual.enable = false; }
|
|
];
|
|
extraArgs = { inherit nodes; };
|
|
};
|
|
|
|
|
|
# Given an attribute set { machine1 = config1; ... machineN =
|
|
# configN; }, sequentially assign IP addresses in the 192.168.1.0/24
|
|
# range to each machine, and set the hostname to the attribute name.
|
|
assignIPAddresses = nodes:
|
|
|
|
let
|
|
|
|
machines = lib.attrNames nodes;
|
|
|
|
machinesNumbered = zip machines (lib.range 1 254);
|
|
|
|
nodes_ = lib.flip map machinesNumbered (m: lib.nameValuePair m.first
|
|
[ ( { config, pkgs, nodes, ... }:
|
|
let
|
|
interfacesNumbered = zip config.virtualisation.vlans (lib.range 1 255);
|
|
interfaces =
|
|
lib.flip map interfacesNumbered ({ first, second }:
|
|
{ name = "eth${toString second}";
|
|
ipAddress = "192.168.${toString first}.${toString m.second}";
|
|
}
|
|
);
|
|
in
|
|
{ key = "ip-address";
|
|
config =
|
|
{ networking.hostName = m.first;
|
|
|
|
networking.interfaces = interfaces;
|
|
|
|
networking.primaryIPAddress =
|
|
lib.optionalString (interfaces != []) (lib.head interfaces).ipAddress;
|
|
|
|
# Put the IP addresses of all VMs in this machine's
|
|
# /etc/hosts file. If a machine has multiple
|
|
# interfaces, use the IP address corresponding to
|
|
# the first interface (i.e. the first network in its
|
|
# virtualisation.vlans option).
|
|
networking.extraHosts = lib.flip lib.concatMapStrings machines
|
|
(m: let config = (lib.getAttr m nodes).config; in
|
|
lib.optionalString (config.networking.primaryIPAddress != "")
|
|
("${config.networking.primaryIPAddress} " +
|
|
"${config.networking.hostName}\n"));
|
|
|
|
virtualisation.qemu.options =
|
|
lib.flip lib.concatMapStrings interfacesNumbered ({ first, second }:
|
|
"-net nic,vlan=${toString second},model=virtio " +
|
|
# Use 232.0.1.<vlan> as the multicast address to
|
|
# connect VMs on the same vlan, but allow it to
|
|
# be overriden using the $QEMU_MCAST_ADDR_<vlan>
|
|
# environment variable. The test driver sets
|
|
# this variable to prevent collisions between
|
|
# parallel builds.
|
|
"-net socket,vlan=${toString second},mcast=" +
|
|
"\${QEMU_MCAST_ADDR_${toString first}:-232.0.1.${toString first}:1234} "
|
|
);
|
|
|
|
};
|
|
}
|
|
)
|
|
(lib.getAttr m.first nodes)
|
|
] );
|
|
|
|
in lib.listToAttrs nodes_;
|
|
|
|
|
|
# Zip two lists together. Should be moved to pkgs.lib.
|
|
zip = xs: ys:
|
|
if xs != [] && ys != [] then
|
|
[ {first = lib.head xs; second = lib.head ys;} ]
|
|
++ zip (lib.tail xs) (lib.tail ys)
|
|
else [];
|
|
|
|
|
|
}
|