From 8ce36ffb3ac5773ea91c1cc183a13c5a1c7fc58f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 11 Mar 2011 14:50:11 +0000 Subject: [PATCH] * Use "ip" instead of "ifconfig" for setting up network interfaces, since the latter is rather deprecated and has been unmaintained since 2001. Note that "ip" doesn't know about classful addressing, so you can no longer get away with not specifying the subnet mask for explicitly configured interfaces. So if you had networking.interfaces = [ { name = "eth0"; ipAddress = "192.168.1.1"; } ]; this should be changed to networking.interfaces = [ { name = "eth0"; ipAddress = "192.168.1.1"; subnetMask = "255.255.255.0"; } ]; otherwise you end up with a subnet mask of 255.255.255.255. svn path=/nixos/trunk/; revision=26279 --- lib/build-vms.nix | 1 + modules/tasks/network-interfaces.nix | 47 +++++++++------------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/lib/build-vms.nix b/lib/build-vms.nix index 32d0a4811ebd..8d4592b48bd3 100644 --- a/lib/build-vms.nix +++ b/lib/build-vms.nix @@ -51,6 +51,7 @@ rec { lib.flip map interfacesNumbered ({ first, second }: { name = "eth${toString second}"; ipAddress = "192.168.${toString first}.${toString m.second}"; + subnetMask = "255.255.255.0"; } ); in diff --git a/modules/tasks/network-interfaces.nix b/modules/tasks/network-interfaces.nix index 138a11cf21c9..f6343815a95e 100644 --- a/modules/tasks/network-interfaces.nix +++ b/modules/tasks/network-interfaces.nix @@ -4,12 +4,8 @@ with pkgs.lib; let - inherit (pkgs) nettools; - cfg = config.networking; - ifconfig = "${nettools}/sbin/ifconfig"; - in { @@ -166,33 +162,28 @@ in preStart = '' - ${pkgs.lib.concatMapStrings (i: - if i.macAddress != "" then + ${flip concatMapStrings cfg.interfaces (i: + optionalString (i.macAddress != "") '' - echo "Configuring interface ${i.name}..." - ${ifconfig} "${i.name}" down || true - ${ifconfig} "${i.name}" hw ether "${i.macAddress}" || true - '' - else "") cfg.interfaces + echo "Setting MAC address of ${i.name} to ${i.macAddress}..." + ip link set "${i.name}" address "${i.macAddress}" || true + '') } for i in $(cd /sys/class/net && ls -d *); do echo "Bringing up network device $i..." - ${ifconfig} $i up || true + ip link set "$i" up || true done # Configure the manually specified interfaces. - ${pkgs.lib.concatMapStrings (i: - if i.ipAddress != "" then + ${flip concatMapStrings cfg.interfaces (i: + optionalString (i.ipAddress != "") '' echo "Configuring interface ${i.name}..." - extraFlags= - if test -n "${i.subnetMask}"; then - extraFlags="$extraFlags netmask ${i.subnetMask}" - fi - ${ifconfig} "${i.name}" "${i.ipAddress}" $extraFlags || true - '' - else "") cfg.interfaces} + ip addr add "${i.ipAddress}""${optionalString (i.subnetMask != "") ("/" + i.subnetMask)}" \ + dev "${i.name}" || true + '') + } # Set the nameservers. if test -n "${toString cfg.nameservers}"; then @@ -206,9 +197,9 @@ in fi # Set the default gateway. - if test -n "${cfg.defaultGateway}"; then - ${nettools}/sbin/route add default gw "${cfg.defaultGateway}" || true - fi + ${optionalString (cfg.defaultGateway != "") '' + ip route add default via "${cfg.defaultGateway}" || true + ''} # Run any user-specified commands. ${pkgs.stdenv.shell} ${pkgs.writeText "local-net-cmds" cfg.localCommands} || true @@ -218,14 +209,6 @@ in initctl emit -n ip-up ''} ''; - - postStop = - '' - #for i in $(cd /sys/class/net && ls -d *); do - # echo "Taking down network device $i..." - # ${ifconfig} $i down || true - #done - ''; }; # Set the host name in the activation script. Don't clear it if