From 6f7aaf10a5beb6cc10ebe75674d9c8664bd187fa Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 7 May 2014 17:00:46 +0200 Subject: Containers: Use systemd-nspawn's --network-veth flag Note that this causes the name of the host-side interface to change from c- to ve-. --- nixos/modules/virtualisation/containers.nix | 93 ++++++++++++++--------------- nixos/modules/virtualisation/run-in-netns.c | 50 ---------------- 2 files changed, 45 insertions(+), 98 deletions(-) delete mode 100644 nixos/modules/virtualisation/run-in-netns.c (limited to 'nixos/modules/virtualisation') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 7a8c8a4594b5..08c234b94133 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -4,16 +4,6 @@ with lib; let - runInNetns = pkgs.stdenv.mkDerivation { - name = "run-in-netns"; - unpackPhase = "true"; - buildPhase = '' - mkdir -p $out/bin - gcc ${./run-in-netns.c} -o $out/bin/run-in-netns - ''; - installPhase = "true"; - }; - nixos-container = pkgs.substituteAll { name = "nixos-container"; dir = "bin"; @@ -23,6 +13,28 @@ let inherit (pkgs) socat; }; + # The container's init script, a small wrapper around the regular + # NixOS stage-2 init script. + containerInit = pkgs.writeScript "container-init" + '' + #! ${pkgs.stdenv.shell} -e + + # Initialise the container side of the veth pair. + if [ "$PRIVATE_NETWORK" = 1 ]; then + ip link set host0 name eth0 + ip link set dev eth0 up + if [ -n "$HOST_ADDRESS" ]; then + ip route add $HOST_ADDRESS dev eth0 + ip route add default via $HOST_ADDRESS + fi + if [ -n "$LOCAL_ADDRESS" ]; then + ip addr add $LOCAL_ADDRESS dev eth0 + fi + fi + + exec "$1" + ''; + system = config.nixpkgs.system; in @@ -70,7 +82,7 @@ in Whether to give the container its own private virtual Ethernet interface. The interface is called eth0, and is hooked up to the interface - c-container-name + ve-container-name on the host. If this option is not set, then the container shares the network interfaces of the host, and can bind to any port on any interface. @@ -176,39 +188,8 @@ in "/nix/var/nix/profiles/per-container/$INSTANCE" \ "/nix/var/nix/gcroots/per-container/$INSTANCE" - if [ -f "/etc/containers/$INSTANCE.conf" ]; then - . "/etc/containers/$INSTANCE.conf" - fi - - # Cleanup from last time. - ifaceHost=c-$INSTANCE - ifaceCont=ctmp-$INSTANCE - ns=net-$INSTANCE - ip netns del $ns 2> /dev/null || true - ip link del $ifaceHost 2> /dev/null || true - ip link del $ifaceCont 2> /dev/null || true - if [ "$PRIVATE_NETWORK" = 1 ]; then - # Create a pair of virtual ethernet devices. On the host, - # we get ‘c- -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char * * argv) -{ - if (argc < 3) { - fprintf(stderr, "%s: missing arguments\n", argv[0]); - return 1; - } - - char nsPath[PATH_MAX]; - - sprintf(nsPath, "/run/netns/%s", argv[1]); - - int fd = open(nsPath, O_RDONLY); - if (fd == -1) { - fprintf(stderr, "%s: opening network namespace: %s\n", argv[0], strerror(errno)); - return 1; - } - - if (setns(fd, CLONE_NEWNET) == -1) { - fprintf(stderr, "%s: setting network namespace: %s\n", argv[0], strerror(errno)); - return 1; - } - - umount2(nsPath, MNT_DETACH); - if (unlink(nsPath) == -1) { - fprintf(stderr, "%s: unlinking network namespace: %s\n", argv[0], strerror(errno)); - return 1; - } - - /* FIXME: Remount /sys so that /sys/class/net reflects the - interfaces visible in the network namespace. This requires - bind-mounting /sys/fs/cgroups etc. */ - - execv(argv[2], argv + 2); - fprintf(stderr, "%s: running command: %s\n", argv[0], strerror(errno)); - return 1; -} -- cgit 1.4.1