Merge pull request #34442 from rnhmjoj/virtual

Fix virtualType for network-interfaces-scripted
This commit is contained in:
Jörg Thalheim 2018-02-01 10:35:13 +00:00 committed by GitHub
commit 57d72d4140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 21 deletions

View File

@ -230,9 +230,7 @@ let
RemainAfterExit = true; RemainAfterExit = true;
}; };
script = '' script = ''
ip tuntap add dev "${i.name}" \ ip tuntap add dev "${i.name}" mode "${i.virtualType}" user "${i.virtualOwner}"
${optionalString (i.virtualType != null) "mode ${i.virtualType}"} \
user "${i.virtualOwner}"
''; '';
postStop = '' postStop = ''
ip link del ${i.name} || true ip link del ${i.name} || true

View File

@ -74,21 +74,17 @@ in
networks."99-main" = genericNetwork mkDefault; networks."99-main" = genericNetwork mkDefault;
} }
(mkMerge (flip map interfaces (i: { (mkMerge (flip map interfaces (i: {
netdevs = mkIf i.virtual ( netdevs = mkIf i.virtual ({
let "40-${i.name}" = {
devType = if i.virtualType != null then i.virtualType netdevConfig = {
else (if hasPrefix "tun" i.name then "tun" else "tap"); Name = i.name;
in { Kind = i.virtualType;
"40-${i.name}" = {
netdevConfig = {
Name = i.name;
Kind = devType;
};
"${devType}Config" = optionalAttrs (i.virtualOwner != null) {
User = i.virtualOwner;
};
}; };
}); "${i.virtualType}Config" = optionalAttrs (i.virtualOwner != null) {
User = i.virtualOwner;
};
};
});
networks."40-${i.name}" = mkMerge [ (genericNetwork mkDefault) { networks."40-${i.name}" = mkMerge [ (genericNetwork mkDefault) {
name = mkDefault i.name; name = mkDefault i.name;
DHCP = mkForce (dhcpStr DHCP = mkForce (dhcpStr

View File

@ -273,11 +273,13 @@ let
}; };
virtualType = mkOption { virtualType = mkOption {
default = null; default = if hasPrefix "tun" name then "tun" else "tap";
type = with types; nullOr (enum [ "tun" "tap" ]); defaultText = literalExample ''if hasPrefix "tun" name then "tun" else "tap"'';
type = with types; enum [ "tun" "tap" ];
description = '' description = ''
The explicit type of interface to create. Accepts tun or tap strings. The type of interface to create.
Also accepts null to implicitly detect the type of device. The default is TUN for an interface name starting
with "tun", otherwise TAP.
''; '';
}; };

View File

@ -433,6 +433,49 @@ let
$client2->succeed("ip addr show dev vlan >&2"); $client2->succeed("ip addr show dev vlan >&2");
''; '';
}; };
virtual = {
name = "Virtual";
machine = {
networking.interfaces."tap0" = {
ip4 = [ { address = "192.168.1.1"; prefixLength = 24; } ];
ip6 = [ { address = "2001:1470:fffd:2096::"; prefixLength = 64; } ];
virtual = true;
};
networking.interfaces."tun0" = {
ip4 = [ { address = "192.168.1.2"; prefixLength = 24; } ];
ip6 = [ { address = "2001:1470:fffd:2097::"; prefixLength = 64; } ];
virtual = true;
};
};
testScript = ''
my $targetList = <<'END';
tap0: tap UNKNOWN_FLAGS:800 user 0
tun0: tun UNKNOWN_FLAGS:800 user 0
END
# Wait for networking to come up
$machine->start;
$machine->waitForUnit("network.target");
# Test interfaces set up
my $list = $machine->succeed("ip tuntap list | sort");
"$list" eq "$targetList" or die(
"The list of virtual interfaces does not match the expected one:\n",
"Result:\n", "$list\n",
"Expected:\n", "$targetList\n"
);
# Test interfaces clean up
$machine->succeed("systemctl stop network-addresses-tap0");
$machine->succeed("systemctl stop network-addresses-tun0");
my $residue = $machine->succeed("ip tuntap list");
$residue eq "" or die(
"Some virtual interface has not been properly cleaned:\n",
"$residue\n"
);
'';
};
}; };
in mapAttrs (const (attrs: makeTest (attrs // { in mapAttrs (const (attrs: makeTest (attrs // {