view usr/src/cmd/initpkg/arm/net-physical @ 0:c9caec207d52 b86

Initial porting based on b86
author Koji Uno <koji.uno@sun.com>
date Tue, 02 Jun 2009 18:56:50 +0900
parents
children
line wrap: on
line source

#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
# All rights reserved.
#
#
# Copyright (c) 2006-2007 NEC Corporation
#

# Print warnings to console
warn_failed_ifs() {
	echo "Failed to $1 interface(s): $2" >/dev/console
}

#
# inet_process_hostname processor [ args ]
#
# Process an inet hostname file.  The contents of the file
# are taken from standard input. Each line is passed
# on the command line to the "processor" command.
# Command line arguments can be passed to the processor.
#
# Examples:
#       inet_process_hostname /sbin/ifconfig hme0 < /etc/hostname.hme0
#       
#       inet_process_hostname /sbin/ifparse -f < /etc/hostname.hme0
#
# If there is only line in an hostname file we assume it contains
# the old style address which results in the interface being brought up 
# and the netmask and broadcast address being set.
#
# If there are multiple lines we assume the file contains a list of
# commands to the processor with neither the implied bringing up of the
# interface nor the setting of the default netmask and broadcast address.
#
# Return non-zero if any command fails so that the caller may alert
# users to errors in the configuration.
#
inet_process_hostname()
{
	#
	# Redirecting input from a file results in a sub-shell being
	# used, hence this outer loop surrounding the "multiple_lines"
	# and "ifcmds" variables.
	#
	while :; do
		multiple_lines=false
		ifcmds=""
		retval=0

		while read line; do
			if [ -n "$ifcmds" ]; then
				#
				# This handles the first N-1
				# lines of a N-line hostname file.
				#
				$* $ifcmds || retval=$?
				multiple_lines=true
			fi
			ifcmds="$line"
		done

		#
		# If the hostname file is empty or consists of only
		# blank lines, break out of the outer loop without
		# configuring the newly plumbed interface.
		#
		[ -z "$ifcmds" ] && return $retval
		if [ $multiple_lines = false ]; then
			# The traditional single-line hostname file.
			ifcmds="$ifcmds netmask + broadcast + up"
		fi

		#
		# This handles either the single-line case or
		# the last line of the N-line case.
		#
		$* $ifcmds || return $?
		return $retval
	done
}


case "$1" in
'start')
	# Fall through -- rest of script is the initialization code.
	;;

'stop')
	# No operation
	exit 0
	;;

*)
	echo "Usage : $0 { start | stop }"
	exit 1
	;;
esac

#
# Get the list of IPv4 interfaces to configure by breaking
# /etc/hostname.* into separate args by using "." as a shell separator
# character.
#
interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
	ORIGIFS="$IFS"
	IFS="$IFS."
	set -- $interface_names
	IFS="$ORIGIFS"
	while [ $# -ge 2 ]; do
		shift
		if [ "$1" = "xx0" ]; then
			#
			# For some unknown historical reason the xx0
			# ifname is ignored.
			#
			shift
			continue
		fi
		if [ $# -gt 1 -a "$2" != "/etc/hostname" ]; then
			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
				shift
			done
		else
			# Check if this interface is already plumbed.
			/sbin/ifconfig $1 >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				inet_list="$inet_list $1"
				shift
			fi
		fi
	done
fi

#
# Step through the IPv4 interface list and try to plumb every interface.
# Generate list of plumbed and failed IPv4 interfaces.
#
if [ -n "$inet_list" ]; then
	set -- $inet_list
	while [ $# -gt 0 ]; do
		/sbin/ifconfig $1 plumb
		if /sbin/ifconfig $1 inet >/dev/null 2>&1; then
			inet_plumbed="$inet_plumbed $1"
		else
			inet_failed="$inet_failed $1"
		fi
		shift
	done
	[ -n "$inet_failed" ] && warn_failed_ifs "plumb IPv4" $inet_failed
fi

#
# Process the /etc/hostname.* files of plumbed IPv4 interfaces.
#
# If /etc/hostname.lo0 exists the loop below will do additional
# configuration of lo0.
#
if [ -n "$inet_plumbed" ]; then
	i4s_fail=
	set -- $inet_plumbed
	while [ $# -gt 0 ]; do
		inet_process_hostname /sbin/ifconfig $1 inet \
		    </etc/hostname.$1 >/dev/null
		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
		shift
	done
	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
fi

exit 0