Differences between revisions 2 and 3
Revision 2 as of 2013-01-12 20:17:23
Size: 831
Comment: Add link to french translation + import script from English master
Revision 3 as of 2015-12-12 16:02:26
Size: 1359
Comment: sync with English master
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
~-[[DebianWiki/EditorGuide#translation|Translation(s)]]: [[ConvertNetToStatic|English]] - [[fr/ConvertNetToStatic|Français]] - Italiano-~ ~-[[DebianWiki/EditorGuide#translation|Traduzioni]]: [[ConvertNetToStatic|English]] - [[fr/ConvertNetToStatic|Français]] - Italiano-~
Line 7: Line 7:
Accetta anche un argomento "--bond" per la conversione in un'interfaccia statica bonded.
Questa versione aggiornata dello script ora supporta anche sistemi basati su RPM... scusate!

Accetta anche un argomento "--bond" per la conversione in un'interfaccia statica bonded. In modo predefinito lo script assume che eth0 sia l'interfaccia primaria e, se con bonding, eth1 sia l'interfaccia secondaria, ma ciò può essere modificato dalla riga di comando.
Line 11: Line 14:
== Uso: ==

mk-net-static [--debug] [--bond] [interfaccia_primaria [interfaccia_secondaria]]

es: per creare bond0 a partire da em1 em2 p1p1 e p1p2 usando l'indirizzo IP assegnato da dhcp a em1 eseguire:

mk-net-static --bond em1 em2 p1p1 p1p2

Traduzioni: English - Français - Italiano


Esiste un bug segnalato da molto tempo (267838, segnato come bug che non verrà risolto) riguardandte l'installatore Debian che riguarda la possibilità di avviare una macchina usando DHCP durante l'installazione, ma creare il sistema installato con una configurazione di rete statica.

Questo è uno script scritto da ?AlexOwen da eseguire al primo avvio per fare il cambiamento per conto dell'utente.

Questa versione aggiornata dello script ora supporta anche sistemi basati su RPM... scusate!

Accetta anche un argomento "--bond" per la conversione in un'interfaccia statica bonded. In modo predefinito lo script assume che eth0 sia l'interfaccia primaria e, se con bonding, eth1 sia l'interfaccia secondaria, ma ciò può essere modificato dalla riga di comando.

L'autore lo rende disponibile nella speranza che sia di aiuto ad altri.

Uso:

mk-net-static [--debug] [--bond] [interfaccia_primaria [interfaccia_secondaria]]

es: per creare bond0 a partire da em1 em2 p1p1 e p1p2 usando l'indirizzo IP assegnato da dhcp a em1 eseguire:

mk-net-static --bond em1 em2 p1p1 p1p2

   1 #!/bin/bash
   2 set -e
   3 
   4 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
   5 
   6 #variables
   7 SYSCONFIG=/etc/sysconfig
   8 SCRIPTS=/etc/sysconfig/network-scripts
   9 BACKUPD=$SCRIPTS/backup-files
  10 INTERFACES=/etc/network/interfaces
  11 
  12 #functions
  13 
  14 cidr2mask() {
  15   local i mask=""
  16   local full_octets=$(($1/8))
  17   local partial_octet=$(($1%8))
  18 
  19   for ((i=0;i<4;i+=1)); do
  20     if [ $i -lt $full_octets ]; then
  21       mask+=255
  22     elif [ $i -eq $full_octets ]; then
  23       mask+=$((256 - 2**(8-$partial_octet)))
  24     else
  25       mask+=0
  26     fi  
  27     test $i -lt 3 && mask+=.
  28   done
  29 
  30   echo $mask
  31 }
  32 
  33 
  34 get-current(){ #$1 =  optional ethernet device (defaults to eth0)
  35 #Use sed magic and function cidr2mask to convert ip command output to bash variables
  36 
  37 DEVICE=${1:-eth0}
  38 
  39 IPADDR=$(ip addr show $DEVICE|\
  40 sed -n  -e's%.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)/\([0-9]*\) brd.*scope global.*%\1%p')
  41 BROADCAST=$(ip addr show $DEVICE|\
  42 sed -n  -e's%.*inet.*brd \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) scope global.*%\1%p')
  43 NETMASK=$(ip addr show $DEVICE |\
  44 sed -n  -e's%.*inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/\([0-9]*\) brd.*scope global.*%\1\n%p' |\
  45         (read mask; if [ -n "$mask" ] ; then printf "%s\n" `cidr2mask $mask` ; fi))
  46 GATEWAY=$(ip route show |sed -n  -e's%default via \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\) dev \(\w*\)\ .*%\1%p')
  47 #DOMAIN=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%domain \(\w*\)%\1%p')
  48 DOMAIN=$(dnsdomainname)
  49 I present this here in the hope it helps someone else.
  50 NAMESERVERS=$(cat /etc/resolv.conf | sed -e '/^#.*$/d' -e's/#.*//' | sed -n -e's%nameserver \(\w*\)%\1%p')
  51 set -- $NAMESERVERS
  52 DNS1=$1
  53 DNS2=$2
  54 }
  55 
  56 get-hwaddr(){ #$1 =  optional ethernet device (defaults to eth0)
  57 DEVICE=${1:-eth0}
  58 ip addr show $DEVICE | sed -n  -e's%.*link/ether \([0-9A-F:].*\) brd.*%\1%p' | tr 'a-z' 'A-Z'
  59 }
  60 
  61 
  62 backup-files(){
  63 if [ -d $SCRIPTS ]; then #redhat
  64     cd $SYSCONFIG
  65     [ -f "$SYSCONFIG/network.orig" ] || cp -p "$SYSCONFIG/network" "$SYSCONFIG/network.orig"
  66     cp -p "$SYSCONFIG/network" "$SYSCONFIG/network-`date +%F-%T`"
  67 
  68     [ -d $BACKUPD ]  || mkdir $BACKUPD
  69     cd $SCRIPTS/
  70     for name in ifcfg-* ; do 
  71     [ -f "$BACKUPD/${name}.orig" ] || cp -p "$name" "$BACKUPD/${name}.orig"
  72     cp -p "$name" "$BACKUPD/`date +%F-%T`-${name}"
  73     done
  74 fi
  75 if [ -f "${INTERFACES}" ] ; then #debian
  76     [ -f "${INTERFACES}.orig" ] || cp -p  "${INTERFACES}" "${INTERFACES}.orig" 
  77     cp -p "${INTERFACES}" "${INTERFACES}-`date +%F-%T`" 
  78 fi
  79 }
  80 
  81 fix-zeroconf(){
  82 if [ -d $SCRIPTS ]; then #redhat
  83     if ! grep -q "NOZEROCONF=yes" $SYSCONFIG/network ; then 
  84         echo "NOZEROCONF=yes" >>$SYSCONFIG/network
  85     fi
  86 fi
  87 }
  88 
  89 fix-networkmanager(){
  90 local RUN
  91 RUN=""
  92 if $DEBUG ; then
  93    RUN="echo DEBUG: "
  94 fi
  95 if [ -d $SCRIPTS ]; then #redhat
  96     $RUN chkconfig NetworkManager off
  97     $RUN service   NetworkManager stop
  98     $RUN chkconfig network on
  99 fi
 100 }
 101 
 102 gen-ifcfg-dev-static(){ #$1 =  optional ethernet device (defaults to eth0)
 103 DEVICE=${1:-eth0}
 104 HWADDR=$(get-hwaddr $DEVICE) 
 105 
 106 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
 107 DEVICE=$DEVICE
 108 
 109 NM_CONTROLLED=no
 110 #TYPE="Ethernet"
 111 #HWADDR=$HWADDR
 112 
 113 IPV6INIT=no
 114 
 115 ONBOOT=yes
 116 USERCTL=no
 117 BOOTPROTO=none
 118 
 119 IPADDR=$IPADDR
 120 NETMASK=$NETMASK
 121 BROADCAST=$BROADCAST
 122 GATEWAY=$GATEWAY
 123 DOMAIN=$DOMAIN
 124 DNS1=$DNS1
 125 DNS2=$DNS2
 126 EOF
 127 }
 128 
 129 
 130 gen-ifcfg-bond-static(){ #$1 = optional bond device (default bond0)
 131 DEVICE=${1:-bond0}
 132 
 133 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
 134 DEVICE=$DEVICE
 135 BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer3+4"
 136 
 137 NM_CONTROLLED=no
 138 #TYPE=Bond
 139 #BONDING_MASTER=yes
 140 
 141 IPV6INIT=no
 142 
 143 ONBOOT=yes
 144 USERCTL=no
 145 BOOTPROTO=none
 146 
 147 IPADDR=$IPADDR
 148 NETMASK=$NETMASK
 149 BROADCAST=$BROADCAST
 150 GATEWAY=$GATEWAY
 151 DOMAIN=$DOMAIN
 152 DNS1=$DNS1
 153 DNS2=$DNS2
 154 EOF
 155 
 156 }
 157 
 158 gen-ifcfg-bond-slave(){ #$1 =  optional ethernet device (defaults to eth0)
 159 DEVICE=${1:-eth0}
 160 HWADDR=$(get-hwaddr $DEVICE)
 161 
 162 if $DEBUG ; then
 163   echo DEBUG: gen-ifcfg-bond-slave dev $DEVICE mac $HWADDR
 164 fi
 165 
 166 cat <<EOF >$SCRIPTS/ifcfg-$DEVICE
 167 DEVICE=$DEVICE
 168 MASTER=bond0
 169 SLAVE=yes
 170 
 171 NM_CONTROLLED=no
 172 
 173 ONBOOT=yes
 174 USERCTL=no
 175 BOOTPROTO=none
 176 #HWADDR=$HWADDR
 177 #TYPE=Ethernet
 178 EOF
 179 }
 180 
 181 
 182 
 183 
 184 gen-interfaces-static(){
 185 DEVICE=${1:-eth0}
 186 cat <<EOF >"${INTERFACES}"
 187 # This file describes the network interfaces available on your system
 188 # and how to activate them. For more information, see interfaces(5).
 189 
 190 #source /etc/network/interfaces.d/*
 191 
 192 # The loopback network interface
 193 auto lo
 194 iface lo inet loopback
 195 
 196 # The primary network interface
 197 auto $DEVICE
 198 iface $DEVICE inet static
 199     address $IPADDR
 200     broadcast $BROADCAST
 201     netmask $NETMASK
 202     gateway $GATEWAY
 203     #dns-nameservers $DNS1 $DNS2
 204 
 205 EOF
 206 
 207 }
 208 
 209 gen-interfaces-bond-static(){
 210 DEVICE=${1:-bond0}
 211 cat <<EOF >"${INTERFACES}"
 212 # This file describes the network interfaces available on your system
 213 # and how to activate them. For more information, see interfaces(5).
 214 
 215 #source /etc/network/interfaces.d/*
 216 
 217 # The loopback network interface
 218 auto lo
 219 iface lo inet loopback
 220 
 221 # The primary network interface
 222 auto $DEVICE
 223 iface $DEVICE inet static
 224     address $IPADDR
 225     broadcast $BROADCAST
 226     netmask $NETMASK
 227     gateway $GATEWAY
 228     #dns-nameservers $DNS1 $DNS2
 229     slaves $slaves
 230     bond_mode 802.3ad
 231     bond_miimon 100
 232     bond_updelay 200
 233     bond_downdelay 200
 234     bond_xmit_hash_policy layer3+4
 235 EOF
 236 }
 237 
 238 #Main
 239 primary=eth0
 240 slaves="eth0 eth1"
 241 
 242 DEBUG=false
 243 RUN=""
 244 if [ "$1" = "--debug" ] ; then
 245     DEBUG=true
 246     shift
 247 
 248     RUN="echo DEUBG: "
 249     SYSCONFIG=/tmp/sysconfig
 250     SCRIPTS=/tmp/sysconfig/network-scripts
 251     BACKUPD=$SCRIPTS/backup-files
 252     INTERFACES=/tmp/network/interfaces
 253 
 254     echo "DEBUG MODE: altering files under /tmp not /etc"
 255 fi
 256 
 257 BOND=0
 258 if [ "$1" = "--bond" ] ; then
 259         BOND=1
 260         shift
 261 fi
 262 
 263 #Any further command line args are primary and secondary interfaces
 264 if [ $# -gt 0 ] ; then
 265     primary=$1
 266     shift
 267 fi
 268 
 269 if [ $# -gt 0 ] ; then
 270     secondaries="$*"
 271     slaves="$primary $secondaries"
 272 fi
 273 
 274 if $DEBUG ; then
 275    echo DEBUG: bond $BOND, primary $primary, secondaries $secondaries.
 276 fi
 277 
 278 
 279 
 280 backup-files
 281 get-current $primary
 282 fix-zeroconf
 283 fix-networkmanager
 284 
 285 $RUN ifdown $primary
 286 
 287 if [ -d $SCRIPTS ]; then #redhat
 288     if [ "$BOND" = "0"  ] ; then
 289         #single static device
 290         gen-ifcfg-dev-static $primary
 291     else
 292         #bonded static device
 293         gen-ifcfg-bond-static
 294         for dev in $slaves ; do 
 295             gen-ifcfg-bond-slave $dev
 296         done
 297         $RUN ifconfig $primary 0.0.0.0
 298     fi
 299     #Start new network config
 300     $RUN service network restart
 301 fi
 302 
 303 if [ -f "${INTERFACES}" ] ; then #debian
 304     if [ "$BOND" = "0"  ] ; then
 305         #single static device
 306         gen-interfaces-static
 307         $RUN ifup $primary
 308     else
 309         #bonded static device
 310         gen-interfaces-bond-static
 311         $RUN ifconfig $primary 0.0.0.0
 312         $RUN ifup bond0
 313     fi
 314 fi