Package: network-manager
Severity: normal
Tags: patch

I have attached a new version of ifblacklist_migrate.sh that will
comment out any wireless stanzas configured by netcfg when run in
netcfg's new finish-install hook.

I have tested this with a live-installer image built with a squeeze d-i,
a patched netcfg (including the couple of fixes I have noted on
#614884), and a patched NM. After running the installer and configuring
wlan0 on an open network, the installed system's NM no longer shows
wlan0 as "not managed" and the wlan0 stanza is correctly commented out
with "#NetworkManager#". All other kinds of wireless network should be
handled too, so long as at least one "wireless-" option is found {in the
stanza.

Ben
#!/bin/sh

# (C) 2007 Canonical Ltd.
# Author: Alexander Sack <[email protected]>
# License: GNU General Public License, version 2 or any later version

if test x$NIF_FILE = x; then
   NIF_FILE=/etc/network/interfaces
fi

backup_suffix=0
while test -e ${NIF_FILE}.bak-${backup_suffix}; do
   backup_suffix=$(expr $backup_suffix + 1)
done

# Migrate wireless interfaces configured by netcfg
if test x$NETCFG = x1; then
  NIF_FILE_BACKUP="$NIF_FILE.bak-${backup_suffix}"
  cp $NIF_FILE $NIF_FILE_BACKUP
  awk '
    function print_stanza() {
      for (i in stanza)
	print (wireless ? "#NetworkManager#" : "") stanza[i];
      delete stanza;
      stanza_lines=wireless=0;
    }
    stanza_lines>1 && /[ \t]+wireless-/ {wireless=1}
    /^(iface|auto|allow-|mapping)/ {new_stanza=1}
    stanza_lines>0 && (/^$/ || new_stanza) {print_stanza()}
    new_stanza || stanza_lines>0 {
      stanza[stanza_lines]=$0;
      stanza_lines+=1;
      new_stanza=0;
    }
    stanza_lines==0 {print}
    END {print_stanza()}' $NIF_FILE_BACKUP > $NIF_FILE
  exit 0
fi

auto_ifs=$(cat $NIF_FILE | \
    egrep "^auto|^allow-" | \
    sed -e 's/auto//' | \
    sed -e 's/allow-[^ ].* //')

ifaces_to_disable=""

echo Auto interfaces found: $auto_ifs

# iterate over all auto interfaces
for i in $auto_ifs; do
  IFS_old=$IFS; IFS=""

  NIF_FILE_content=$(cat $NIF_FILE | \
      sed -e 's/^[ \t]*auto.*$//' | \
      sed -e 's/^[ \t]*allow-.*$//' | \
      sed -e 's/^[ \t]*#.*$//' | grep -v ^$)

  # '--' is inserted by grep -A1 if there are multiple iface blocks
  lines=$(echo $NIF_FILE_content | grep -A1 "^iface.*$i.*dhcp" | grep -v '\--')
  IFS="
"

  # if there is no iface line for that interface, we would still get a line
  # count of 1 ... so use word_count 0 below to exclude ifaces that have no
  # configuration at all.
  word_count=$(echo $lines | wc -w)
  line_count=0
  for line in $lines; do
      nulled_line=$(echo "$line" | sed -e 's/[# ]//' | grep -v ^iface)
      if test x$nulled_line != x; then
	  line_count=$(expr $line_count + 1)
      fi
  done  

  if test $line_count -eq 0 -a $word_count -gt 0; then
     ifaces_to_disable="$ifaces_to_disable $i"
     echo iface to disable = $i
  fi
  IFS=$IFS_old
done

if [ -n "$ifaces_to_disable" ]; then
    cp $NIF_FILE "$NIF_FILE.bak-${backup_suffix}"
    for i in $ifaces_to_disable; do
	echo -n "Disabling interface: $i ... "
	sed -i -e "s/^\([ \t]*iface.*$i.*\)$/#NetworkManager#\1/" $NIF_FILE
	echo done.
    done
fi

Reply via email to