From 863e8311cfc2ec7dc98fdb43abaa8499dd3414a4 Mon Sep 17 00:00:00 2001 From: Judd Vinet Date: Thu, 30 Jun 2005 23:57:54 +0000 Subject: added new netcfg stuff for roaming network profiles --- functions | 1 + minilogd.c | 1 + netcfg | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ netfs | 2 + network | 130 ++++++++++++++++++++--------- profile-template | 27 ++++++ rc.conf | 10 +++ rc.local | 1 + rc.multi | 4 + rc.shutdown | 1 + rc.single | 1 + rc.sysinit | 28 ++++++- 12 files changed, 407 insertions(+), 43 deletions(-) create mode 100755 netcfg create mode 100644 profile-template diff --git a/functions b/functions index 347c4de..0ae7b62 100644 --- a/functions +++ b/functions @@ -102,3 +102,4 @@ ck_daemon() { } # End of file +# vim: set ts=2 noet: diff --git a/minilogd.c b/minilogd.c index 76e4a9c..9e38094 100644 --- a/minilogd.c +++ b/minilogd.c @@ -182,3 +182,4 @@ int main(int argc, char **argv) { exit(5); } } +/* vim: set ts=2 noet: */ diff --git a/netcfg b/netcfg new file mode 100755 index 0000000..d9166f9 --- /dev/null +++ b/netcfg @@ -0,0 +1,244 @@ +#!/bin/bash + +. /etc/rc.conf +. /etc/rc.d/functions +[ -f /etc/conf.d/dhcpcd ] && . /etc/conf.d/dhcpcd + +NETCFG_VER=0.1 +PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH" + +PROFILE_DIR="/etc/network-profiles" +STATE_DIR="/var/run/net" + +version() +{ + echo "netcfg v$NETCFG_VER" +} + +usage() +{ + version + echo + echo "usage: netcfg " + echo " netcfg --stop " + echo " netcfg --menu [--timeout ]" + echo " netcfg --stopall" + echo + echo "Network profiles are stored in $PROFILE_DIR" + echo +} + +stop_profile() +{ + if [ "$1" = "" ]; then + echo "error: missing interface name (eg, eth0)" + exit 1 + fi + INTERFACE=$1 + [ -f $STATE_DIR/$INTERFACE ] || return + + unset GATEWAY + . $STATE_DIR/$INTERFACE + + stat_busy "Shutting down interface: $INTERFACE" + + # bring down the default route (gateway) + [ "$GATEWAY" ] && route del default gw $GATEWAY + + # bring down the interface + ifconfig $INTERFACE down + + rm -f $STATE_DIR/$INTERFACE + + stat_done +} + +stop_all() +{ + [ -d $STATE_DIR ] || return + for prof in `ls $STATE_DIR`; do + unset INTERFACE + . $prof + stop_profile $INTERFACE + done +} + +start_profile() +{ + if [ "$1" = "" ]; then + echo "error: missing profile name" + exit 1 + fi + if [ ! -f $PROFILE_DIR/$1 ]; then + echo "error: $PROFILE_DIR/$1 is missing" >&2 + exit 1 + fi + + # Read the profile + . $PROFILE_DIR/$1 + + # Shut down any profiles tied to this interface + stop_profile $INTERFACE + + stat_busy "Starting network profile: $1" + + # Re-read the profile (stop_profile might have overwritten our settings) + unset DESCRIPTION INTERFACE IFOPTS IWOPTS WIFI_INTERFACE + unset GATEWAY HOSTNAME DOMAIN DNS1 DNS2 + . $PROFILE_DIR/$1 + + # Configure wireless settings, if necessary + [ "$WIFI_INTERFACE" ] || WIFI_INTERFACE=$INTERFACE + if [ "$IWOPTS" ]; then + iwconfig $WIFI_INTERFACE $IWOPTS + [ $? -ne 0 ] && stat_fail && return + fi + + if [ "$IFOPTS" = "dhcp" -o "$IFOPTS" = "DHCP" ]; then + dhcpcd $DHCPCD_ARGS $INTERFACE + [ $? -ne 0 ] && stat_fail && return + else + # bring up the interface + ifconfig $INTERFACE $IFOPTS up + [ $? -ne 0 ] && stat_fail && return + + # bring up the default route (gateway) + if [ "$GATEWAY" ]; then + route add default gw $GATEWAY + [ $? -ne 0 ] && stat_fail && return + fi + fi + + # set the hostname + if [ "$HOSTNAME" ]; then + hostname $HOSTNAME + [ $? -ne 0 ] && stat_fail && return + fi + + # Generate a new resolv.conf + if [ "$DNS1" ]; then + : >/etc/resolv.conf + [ $? -ne 0 ] && stat_fail && return + [ "$DOMAIN" ] && echo "domain $DOMAIN" >>/etc/resolv.conf + [ "$DNS1" ] && echo "nameserver $DNS1" >>/etc/resolv.conf + [ "$DNS2" ] && echo "nameserver $DNS2" >>/etc/resolv.conf + fi + + # Save the info in /var/run so we can shut it down later + mkdir -p $STATE_DIR + cp $PROFILE_DIR/$1 $STATE_DIR/$INTERFACE + stat_done +} + +menu() +{ + if [ "`ls $PROFILE_DIR 2>/dev/null | grep -v ^template$`" = "" -o ! -d $PROFILE_DIR ]; then + echo "No profiles found. Add profiles in $PROFILE_DIR" + return + fi + # scan all profiles + unset profiles + DEFAULT= + i=0 + for prof in `ls $PROFILE_DIR`; do + # ignore the template + [ "$prof" = "template" ] && continue + NAME=$prof + + # if there's a profile called "main", use that as default + [ "$NAME" = "main" ] && DEFAULT=$NAME + unset DESCRIPTION + . $PROFILE_DIR/$NAME + if [ "$DESCRIPTION" ]; then + profiles[$i]=$NAME + i=$((i+1)) + profiles[$i]=$DESCRIPTION + i=$((i+1)) + fi + done + + if [ ${#profiles} -eq 0 ]; then + echo "No profiles were found in $PROFILE_DIR" + return + fi + + # if no default yet, use the first entry + [ "$DEFAULT" = "" ] && DEFAULT=${profiles[0]} + + ANSWER=`mktemp` + + if [ "$TIMEOUT" != "" ]; then + dialog \ + --timeout $TIMEOUT \ + --default-item $DEFAULT \ + --menu "Select the network profile you wish to use\n\n (timeout in $TIMEOUT seconds)" \ + 13 50 6 \ + "${profiles[@]}" 2>$ANSWER + ret=$? + else + dialog \ + --default-item $DEFAULT \ + --menu "Select the network profile you wish to use" \ + 13 50 6 \ + "${profiles[@]}" 2>$ANSWER + ret=$? + fi + + case $ret in + 1) ;; # cancel - do nothing + 255) start_profile $DEFAULT ;; # timeout - use default + 0) start_profile `cat $ANSWER` ;; # user selection + # abnormal + *) echo "abnormal ret code from dialog: $ret" ;; + esac + + rm $ANSWER +} + +# +# Begin +# + +if [ "`id -u`" != "0" ]; then + echo "This script should be run as root." + exit 1 +fi + +# Parse command line +MODE="profile" +PROFILE= +IFACE= +TIMEOUT= +while [ $# -ne 0 ]; do + case $1 in + --version) MODE="ver" ;; + --help) MODE="usage" ;; + --menu) MODE="menu" ;; + --stopall) MODE="stopall" ;; + --stop) MODE="stop" + shift + IFACE=$1 ;; + --timeout) shift + TIMEOUT=$1 ;; + --*) MODE="usage" ;; + -*) MODE="usage" ;; + *) PROFILE=$1 ;; + esac + shift +done + +if [ "$MODE" = "profile" -a "$PROFILE" = "" ]; then + MODE="usage" +fi + +# Figure out what we're doing... +[ "$MODE" = "ver" ] && version +[ "$MODE" = "usage" ] && usage +[ "$MODE" = "profile" ] && start_profile $PROFILE +[ "$MODE" = "stop" ] && stop_profile $IFACE +[ "$MODE" = "stopall" ] && stop_all +[ "$MODE" = "menu" ] && menu + +exit 0 + +# vim: set ts=2 noet: diff --git a/netfs b/netfs index 19974d2..b89f515 100755 --- a/netfs +++ b/netfs @@ -33,3 +33,5 @@ case "$1" in *) echo "usage: $0 {start|stop|restart}" esac + +# vim: set ts=2 noet: diff --git a/network b/network index 1513c58..056e515 100755 --- a/network +++ b/network @@ -3,13 +3,13 @@ . /etc/rc.conf . /etc/rc.d/functions -# look for wireless settings +# wireless settings [ -f /etc/conf.d/wireless ] && . /etc/conf.d/wireless - -# look for ethernet bonding settings +# ethernet bonding settings [ -f /etc/conf.d/bonding ] && . /etc/conf.d/bonding - -# look for dhcpcd settings +# bridge settings +[ -f /etc/conf.d/bridges ] && . /etc/conf.d/bridges +# dhcpcd settings [ -f /etc/conf.d/dhcpcd ] && . /etc/conf.d/dhcpcd # Special wrapper for hotplug ifup calls @@ -31,22 +31,18 @@ ifup() echo "usage: $0 ifup " return 1 fi - for wif in ${WLAN_INTERFACES[@]}; do - if [ "$wif" = "${1}" ]; then - wvarname="\$wlan_${1}" - eval wif_line=$wvarname - /usr/sbin/iwconfig $wif_line - /bin/sleep 2 - fi - done - varname="\$${1}" - eval new_ifline=$varname - if [ "$new_ifline" = "dhcp" ]; then + eval iwcfg="\$wlan_${1}" + if [ "$iwcfg" != "" ]; then + /usr/sbin/iwconfig $iwcfg + /bin/sleep 2 + fi + eval ifcfg="\$${1}" + if [ "$ifcfg" = "dhcp" ]; then # remove the .pid file if it exists rm -f /etc/dhcpc/dhcpcd-${1}.{pid,cache} >/dev/null 2>&1 - /usr/sbin/dhcpcd $DHCPCD_ARGS $1 + /usr/sbin/dhcpcd $DHCPCD_ARGS ${1} else - /sbin/ifconfig $new_ifline + /sbin/ifconfig $ifcfg fi return $? } @@ -57,17 +53,16 @@ ifdown() echo "usage: $0 ifdown " return 1 fi - varname="\$${1}" - eval new_ifline=$varname - if [ "$new_ifline" = "dhcp" ]; then + eval ifcfg="\$${1}" + if [ "$ifcfg" = "dhcp" ]; then if [ -f /etc/dhcpc/dhcpcd-${1}.pid ]; then /bin/kill `cat /etc/dhcpc/dhcpcd-${1}.pid` else # No .pid file, just bring the interface itself down - /sbin/ifconfig $1 down + /sbin/ifconfig ${1} down fi else - /sbin/ifconfig $new_ifline down + /sbin/ifconfig $ifcfg down fi return $? } @@ -91,9 +86,8 @@ rtup() echo "usage: $0 rtup " return 1 fi - varname="\$${1}" - eval new_rtline=$varname - /sbin/route add $new_rtline + eval routecfg="\$${1}" + /sbin/route add $routecfg return $? } @@ -103,9 +97,8 @@ rtdown() echo "usage: $0 rtdown " return 1 fi - varname="\$${1}" - eval new_rtline=$varname - /sbin/route del $new_rtline + eval routecfg="\$${1}" + /sbin/route del $routecfg return $? } @@ -122,28 +115,79 @@ rtlist() done } +bond_up() +{ + for ifline in ${BOND_INTERFACES[@]}; do + if [ "$ifline" = "${ifline#!}" ]; then + eval bondcfg="\$bond_${ifline}" + /sbin/ifenslave $ifline $bondcfg || error=1 + fi + done +} + +bridge_up() +{ + for br in ${BRIDGE_INTERFACES[@]}; do + if [ "$br" = "${br#!}" ]; then + # if the bridge already exists, remove it + if [ "`/sbin/ifconfig $br 2>/dev/null`" ]; then + /sbin/ifconfig $br down + /usr/sbin/brctl delbr $br + fi + /usr/sbin/brctl addbr $br + eval brifs="\$bridge_${br}" + for brif in $brifs; do + if [ "$brif" = "${brif#!}" ]; then + /usr/sbin/brctl addif $br $brif || error=1 + fi + done + fi + done +} + +bridge_down() +{ + for br in ${BRIDGE_INTERFACES[@]}; do + if [ "$br" = "${br#!}" ]; then + /usr/sbin/brctl delbr $br + fi + done +} + + case "$1" in start) if ! ck_daemon network; then echo "Network is already running. Try 'network restart'" exit fi + + # See if we're using network profiles + if [ "$NET" ]; then + # This env var is passed from the kernel boot line + /usr/bin/netcfg $NET + elif [ "$NET_PROFILES" ]; then + if [ "$NET_PROFILES" = "menu" ]; then + /usr/bin/netcfg --menu --timeout 5 + else + for prof in ${NET_PROFILES[@]}; do + /usr/bin/netcfg $prof + done + fi + fi + stat_busy "Starting Network" error=0 - # bring up interfaces + # bring up bridge interfaces + bridge_up + # bring up ethernet interfaces for ifline in ${INTERFACES[@]}; do if echo $ifline | grep '^[^\!]' >/dev/null 2>&1; then ifup $ifline || error=1 fi done # bring up bond interfaces - for ifline in ${BOND_INTERFACES[@]}; do - if echo $ifline | grep '^[^\!]' 2>&1 >/dev/null; then - bvarname="\$bond_${ifline}" - eval bif_line=$bvarname - /sbin/ifenslave $ifline $bif_line || error=1 - fi - done + bond_up # bring up routes for rtline in "${ROUTES[@]}"; do if echo $rtline | grep '^[^\!]' 2>&1 >/dev/null; then @@ -158,10 +202,14 @@ case "$1" in fi ;; stop) - if ck_daemon network; then - echo "Network is not running. Try 'network start'" - exit + #if ck_daemon network; then + # echo "Network is not running. Try 'network start'" + # exit + #fi + if [ "$NET_PROFILES" ]; then + /usr/bin/netcfg --stopall fi + stat_busy "Stopping Network" rm_daemon network error=0 @@ -175,6 +223,8 @@ case "$1" in ifdown $ifline || error=1 fi done + # bring down bridge interfaces + bridge_down if [ $error -eq 0 ]; then stat_done else diff --git a/profile-template b/profile-template new file mode 100644 index 0000000..70d2d7f --- /dev/null +++ b/profile-template @@ -0,0 +1,27 @@ +# +# Network Profile +# + +DESCRIPTION="Default Network Profile" + +# Network Settings +INTERFACE=eth0 +HOSTNAME=myhost + +# Interface Settings (use IFOPTS="dhcp" for DHCP) +IFOPTS="192.168.0.2 netmask 255.255.255.0 broadcast 192.168.0.255" +GATEWAY=192.168.0.1 + +# DNS Settings (optional) +DOMAIN=localdomain +DNS1=192.168.0.1 +DNS2= + +# Wireless Settings (optional) +#ESSID=default +#KEY= +#IWOPTS="mode managed essid $ESSID channel 6 key restricted $KEY" + +#WIFI_INTERFACE=wlan0 # use this if you have a special wireless interface + # that is linked to the real $INTERFACE + diff --git a/rc.conf b/rc.conf index 9063a91..a66e0ee 100644 --- a/rc.conf +++ b/rc.conf @@ -52,6 +52,16 @@ INTERFACES=(lo eth0) gateway="default gw 192.168.0.1" ROUTES=(!gateway) +# +# Enable these network profiles at boot-up. These are only useful +# if you happen to need multiple network configurations (ie, laptop users) +# (set to 'menu' to present a menu during boot-up) +# (prefix with a ! to disable) +# +# Network profiles are found in /etc/network-profiles +# +#NET_PROFILES=(main) + # # Daemons to start at boot-up (in this order) # (prefix a daemon with a ! to disable it) diff --git a/rc.local b/rc.local index b8e8ef5..f177298 100755 --- a/rc.local +++ b/rc.local @@ -4,3 +4,4 @@ # # End of file +# vim: set ts=2 noet: diff --git a/rc.multi b/rc.multi index 3f34941..783a89e 100755 --- a/rc.multi +++ b/rc.multi @@ -18,8 +18,12 @@ for daemon in "${DAEMONS[@]}"; do fi done +# Load sysctl variables if sysctl.conf is present +[ -r /etc/sysctl.conf ] && /sbin/sysctl -q -p &>/dev/null + if [ -x /etc/rc.local ]; then /etc/rc.local fi # End of file +# vim: set ts=2 noet: diff --git a/rc.shutdown b/rc.shutdown index b18699d..41daa97 100755 --- a/rc.shutdown +++ b/rc.shutdown @@ -96,3 +96,4 @@ else fi # End of file +# vim: set ts=2 noet: diff --git a/rc.single b/rc.single index 0634e6f..3870380 100755 --- a/rc.single +++ b/rc.single @@ -63,3 +63,4 @@ if [ "$RUNLEVEL" = "1" ]; then fi # End of file +# vim: set ts=2 noet: diff --git a/rc.sysinit b/rc.sysinit index 822b78f..29b928d 100755 --- a/rc.sysinit +++ b/rc.sysinit @@ -113,6 +113,7 @@ stat_busy "Removing Leftover Files" /bin/rm -f /var/lock/* &>/dev/null /bin/rm -f /var/run/*.pid &>/dev/null /bin/rm -f /var/run/daemons/* &>/dev/null +/bin/rm -f /var/run/net/* &>/dev/null /bin/rm -rf /tmp/* /tmp/.* &>/dev/null /bin/rm -f /forcefsck &>/dev/null : > /var/run/utmp @@ -145,6 +146,29 @@ if [ "$KEYMAP" != "" ]; then status "Loading Keyboard Map: $KEYMAP" /bin/loadkeys -q $KEYMAP fi + +# Set user defined locale +if [ "$LOCALE" != "" ]; then + stat_busy "Setting Locale: $LOCALE" + echo "export LANG=$LOCALE" >/etc/profile.d/locale.sh + /bin/chmod 755 /etc/profile.d/locale.sh + + # If locale is *.utf set console to Unicode mode + if [ `echo $LOCALE | /bin/grep -i utf` != "" ]; then + /usr/bin/kbd_mode -u + /usr/bin/dumpkeys | /bin/loadkeys --unicode + for i in `seq 1 12`; do + echo -ne "\033%G" > /dev/vc/${i}; + done + if [ "$CONSOLEFONT" = "" ]; then + CONSOLEFONT="LatArCyrHeb-16" + fi + fi + stat_done +else + rm -f /etc/profile.d/locale.sh +fi + if [ "$CONSOLEFONT" != "" ]; then stat_busy "Loading Console Font: $CONSOLEFONT" for i in `seq 1 12`; do @@ -172,10 +196,8 @@ if [ -f /proc/modules ]; then stat_done fi -# Load sysctl variables if sysctl.conf is present -[ -r /etc/sysctl.conf ] && /sbin/sysctl -q -p - # Screen blanks after 15 minutes idle time /usr/bin/setterm -blank 15 # End of file +# vim: set ts=2 noet: -- cgit v1.2.3-54-g00ecf