Translation(s): English - Fran├žais - Italiano


There is a long standing bug (267838, marked wontfix) against the debian-installer, to allow a machine to boot via DHCP during install, but create the installed system with a static network config.

Here is a script I have written to run on first boot to make that change for me. It also accepts a "--bond" argument to convert to a static bonded interface.

I present this here in the hope it helps someone else.

   1 #!/bin/bash
   2 set -e
   3 
   4 #functions
   5 
   6 cidr2mask() {
   7   local i mask=""
   8   local full_octets=$(($1/8))
   9   local partial_octet=$(($1%8))
  10 
  11   for ((i=0;i<4;i+=1)); do
  12     if [ $i -lt $full_octets ]; then
  13       mask+=255
  14     elif [ $i -eq $full_octets ]; then
  15       mask+=$((256 - 2**(8-$partial_octet)))
  16     else
  17       mask+=0
  18     fi  
  19     test $i -lt 3 && mask+=.
  20   done
  21 
  22   echo $mask
  23 }
  24 
  25 
  26 gen-interfaces(){
  27 
  28 cat <<EOF | $FILTER
  29 # The loopback network interface
  30 auto lo
  31 iface lo inet loopback
  32 
  33 # The primary network interface
  34 auto eth0
  35 EOF
  36 
  37 #Use sed magic and function cidr2mask to convert ip command output into  output of this form
  38 # eth0-1 iface eth0 inet static
  39 # eth0-2        address nnn.nnn.50.203
  40 # eth0-4        broadcast nnn.nnn.51.255
  41 # eth0-3        netmask nnn.nnn.254.0
  42 # eth0-5        gateway 138.37.51.254
  43 #In preparation for further filtering
  44 
  45 (
  46 ip addr show |\
  47 sed -n  -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global \(.*\)%'\
  48 '\4-1 iface \4 inet static\n\4-2 \taddress \1\n\4-4 \tbroadcast \3%p'
  49 ip addr show |\
  50 sed -n  -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global \(.*\)%'\
  51 '\4-3 \2\n%p' | (read key mask; if [ -n "$mask" ] ; then printf "%s \t%s %s\n" $key  netmask `cidr2mask $mask` ; fi)
  52 ip route show |sed -n  -e's%default via \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) dev \(.*\) %\2-5 \t\gateway \1%p'
  53 )| sort | cut -f2- -d" " | $FILTER
  54 
  55 if [ $BOND = 1 ] ; then
  56 cat <<EOF
  57         slaves eth0 eth1
  58         bond_mode 802.3ad
  59         bond_miimon 100
  60         bond_updelay 200
  61         bond_downdelay 200
  62         bond_xmit_hash_policy layer2+3
  63 EOF
  64 fi
  65 }
  66 
  67 
  68 #Main
  69 
  70 BOND=0
  71 if [ "$1" = "--bond" ] ; then
  72         BOND=1
  73 fi
  74 
  75 FILTER=cat
  76 if [ $BOND = 1 ] ; then
  77         FILTER='sed -e s/eth0/bond0/ '
  78 fi
  79 
  80 [ -f /etc/network/interfaces.orig ] || cp -p  /etc/network/interfaces /etc/network/interfaces.orig 
  81  
  82 gen-interfaces  >/etc/network/interfaces.new
  83 
  84 ifdown eth0
  85 
  86 cp -p /etc/network/interfaces /etc/network/interfaces-"`date +%F-%T`"
  87 mv /etc/network/interfaces.new /etc/network/interfaces
  88 
  89 if [ $BOND = 1 ] ; then
  90         ifconfig eth0 0.0.0.0
  91         ifup bond0
  92 else
  93         ifup eth0
  94 fi