Convert "nework-interfaces"

svn path=/nixos/branches/fix-style/; revision=14403
This commit is contained in:
Marc Weber 2009-03-06 12:27:38 +00:00
parent ed8bfc1c78
commit 4963abf63e
3 changed files with 94 additions and 90 deletions

View File

@ -423,6 +423,7 @@ in
(import ../upstart-jobs/filesystems.nix) # Mount file systems. (import ../upstart-jobs/filesystems.nix) # Mount file systems.
(import ../upstart-jobs/swap.nix) (import ../upstart-jobs/swap.nix)
(import ../upstart-jobs/network-interfaces.nix)
# security # security

View File

@ -71,12 +71,6 @@ let
jobs = map makeJob jobs = map makeJob
([ ([
# Network interfaces.
(import ../upstart-jobs/network-interfaces.nix {
inherit modprobe config;
inherit (pkgs) nettools wirelesstools bash writeText;
})
# Name service cache daemon. # Name service cache daemon.
(import ../upstart-jobs/nscd.nix { (import ../upstart-jobs/nscd.nix {
inherit (pkgs) glibc; inherit (pkgs) glibc;

View File

@ -1,7 +1,11 @@
{nettools, modprobe, wirelesstools, bash, writeText, config}: {pkgs, config, ...}:
###### implementation
let let
inherit (pkgs) nettools wirelesstools bash writeText;
cfg = config.networking; cfg = config.networking;
# !!! use XML # !!! use XML
@ -10,91 +14,96 @@ let
subnetMasks = map (i: if i ? subnetMask then i.subnetMask else "default") cfg.interfaces; subnetMasks = map (i: if i ? subnetMask then i.subnetMask else "default") cfg.interfaces;
essids = map (i: if i ? essid then i.essid else "default") cfg.interfaces; essids = map (i: if i ? essid then i.essid else "default") cfg.interfaces;
wepKeys = map (i: if i ? wepKey then i.wepKey else "nokey") cfg.interfaces; wepKeys = map (i: if i ? wepKey then i.wepKey else "nokey") cfg.interfaces;
modprobe = config.system.sbin.modprobe;
in in
{ {
name = "network-interfaces"; services = {
extraJobs = [{
name = "network-interfaces";
job = '' job = ''
start on udev start on udev
stop on shutdown stop on shutdown
start script start script
export PATH=${modprobe}/sbin:$PATH export PATH=${modprobe}/sbin:$PATH
modprobe af_packet || true modprobe af_packet || true
for i in $(cd /sys/class/net && ls -d *); do for i in $(cd /sys/class/net && ls -d *); do
echo "Bringing up network device $i..." echo "Bringing up network device $i..."
${nettools}/sbin/ifconfig $i up || true ${nettools}/sbin/ifconfig $i up || true
done done
# Configure the manually specified interfaces. # Configure the manually specified interfaces.
names=(${toString names}) names=(${toString names})
ipAddresses=(${toString ipAddresses}) ipAddresses=(${toString ipAddresses})
subnetMasks=(${toString subnetMasks}) subnetMasks=(${toString subnetMasks})
essids=(${toString essids}) essids=(${toString essids})
wepKeys=(${toString wepKeys}) wepKeys=(${toString wepKeys})
for ((n = 0; n < ''${#names[*]}; n++)); do for ((n = 0; n < ''${#names[*]}; n++)); do
name=''${names[$n]} name=''${names[$n]}
ipAddress=''${ipAddresses[$n]} ipAddress=''${ipAddresses[$n]}
subnetMask=''${subnetMasks[$n]} subnetMask=''${subnetMasks[$n]}
essid=''${essids[$n]} essid=''${essids[$n]}
wepKey=''${wepKeys[$n]} wepKey=''${wepKeys[$n]}
# Set wireless networking stuff. # Set wireless networking stuff.
if test "$essid" != default; then if test "$essid" != default; then
${wirelesstools}/sbin/iwconfig "$name" essid "$essid" || true ${wirelesstools}/sbin/iwconfig "$name" essid "$essid" || true
fi fi
if test "$wepKey" != nokey; then if test "$wepKey" != nokey; then
${wirelesstools}/sbin/iwconfig "$name" key "$(cat "$wepKey")" || true ${wirelesstools}/sbin/iwconfig "$name" key "$(cat "$wepKey")" || true
fi fi
# Set IP address / netmask. # Set IP address / netmask.
if test "$ipAddress" != dhcp; then if test "$ipAddress" != dhcp; then
echo "Configuring interface $name..." echo "Configuring interface $name..."
extraFlags= extraFlags=
if test "$subnetMask" != default; then if test "$subnetMask" != default; then
extraFlags="$extraFlags netmask $subnetMask" extraFlags="$extraFlags netmask $subnetMask"
fi fi
${nettools}/sbin/ifconfig "$name" "$ipAddress" $extraFlags || true ${nettools}/sbin/ifconfig "$name" "$ipAddress" $extraFlags || true
fi fi
done done
# Set the nameservers. # Set the nameservers.
if test -n "${toString cfg.nameservers}"; then if test -n "${toString cfg.nameservers}"; then
rm -f /etc/resolv.conf rm -f /etc/resolv.conf
if test -n "${cfg.domain}"; then if test -n "${cfg.domain}"; then
echo "domain ${cfg.domain}" >> /etc/resolv.conf echo "domain ${cfg.domain}" >> /etc/resolv.conf
fi fi
for i in ${toString cfg.nameservers}; do for i in ${toString cfg.nameservers}; do
echo "nameserver $i" >> /etc/resolv.conf echo "nameserver $i" >> /etc/resolv.conf
done done
fi fi
# Set the default gateway. # Set the default gateway.
if test -n "${cfg.defaultGateway}"; then if test -n "${cfg.defaultGateway}"; then
${nettools}/sbin/route add default gw "${cfg.defaultGateway}" || true ${nettools}/sbin/route add default gw "${cfg.defaultGateway}" || true
fi fi
# Run any user-specified commands. # Run any user-specified commands.
${bash}/bin/sh ${writeText "local-net-cmds" cfg.localCommands} || true ${bash}/bin/sh ${writeText "local-net-cmds" cfg.localCommands} || true
end script end script
# Hack: Upstart doesn't yet support what we want: a service that # Hack: Upstart doesn't yet support what we want: a service that
# doesn't have a running process associated with it. # doesn't have a running process associated with it.
respawn sleep 100000 respawn sleep 100000
stop script
for i in $(cd /sys/class/net && ls -d *); do
echo "Taking down network device $i..."
${nettools}/sbin/ifconfig $i down || true
done
end script
'';
stop script
for i in $(cd /sys/class/net && ls -d *); do
echo "Taking down network device $i..."
${nettools}/sbin/ifconfig $i down || true
done
end script
'';
}];
};
} }