r554 - x86/trunk/scripts

justin at linuxfromscratch.org justin at linuxfromscratch.org
Mon Aug 8 16:38:26 PDT 2005


Author: justin
Date: 2005-08-08 17:38:25 -0600 (Mon, 08 Aug 2005)
New Revision: 554

Modified:
   x86/trunk/scripts/net-setup
Log:
Updated net-setup to utilize dialog forms, to not require a gateway and error correction with dialog messages.

Modified: x86/trunk/scripts/net-setup
===================================================================
--- x86/trunk/scripts/net-setup	2005-08-05 06:21:48 UTC (rev 553)
+++ x86/trunk/scripts/net-setup	2005-08-08 23:38:25 UTC (rev 554)
@@ -1,191 +1,221 @@
 #!/bin/bash
 #
-# Script for interactively choosing a network service
-# and starting eth0 on the LFS LiveCD. First implemented
+# Script for interactively choosing a network interface, 
+# service and configuring this service.  First implemented
 # in the x86-6.0-1 CD.
 #
 # Written by Jeremy Huntwork 09.17.2004
+# Additional features by Justin Knierim with the help
+#   of Alexander Patrakov 08.08.05
 #
 
+# Locations of network configs, ifup and ifdown
 DIR=/etc/sysconfig/network-devices
-ETH0CFG=$DIR/ifconfig.eth0
 IFUP=$DIR/ifup
 IFDOWN=$DIR/ifdown
-LINKTEST=`ip link show eth0 2> /dev/null`
-GREETING="Let's configure your network device!"
 
-# Function to set nameservers in /etc/resolv.conf
-# Allows user to enter any number of nameservers,
-# exits when user types "done"
+# Set variables after network device is selected
+set_devvars(){
+	ETHXCFG=$DIR/ifconfig.$DEV
+	LINKTEST=`/sbin/ip link show $DEV 2> /dev/null`
+}
 
+# Form to enter nameservers for /etc/resolv.conf
 set_dns(){
+	DLG_COMMAND="dialog --title \"DNS Servers - /etc/resolv.conf\" --no-cancel \
+        --form \"Please enter the DNS IP addresses below:\" 0 0 3 \
+	\"Server 1:\" 1 1 \"$F_DNS1\" 1 25 15 0 \
+	\"Server 2:\" 2 1 \"$F_DNS2\" 2 25 15 0 \
+	\"Server 3:\" 3 1 \"$F_DNS3\" 3 25 15 0"
+	SELECTION=`eval $DLG_COMMAND 3>&2 2>&1 1>&3`
 
-	echo "Enter a DNS server: "
-	echo "(Enter server addresses, one per line in order of preference, and type \"done\" when finished."
-	read DNS
-	counter=0
-	while [ $DNS != "done" ]
-	do
-		# Check that user enters four numbers separated by '.'
-		if [ -z "$DNS" -o -n "${DNS##?*.?*.?*.?*}" ] ; then
-			echo "$DNS is not a valid nameserver address."
-		else
-			# If this is the first entry, overwrite any existing
-			# /etc/resolv.conf, else append.
-			if [ $counter -eq 0 ] ; then
-				echo "nameserver $DNS" > /etc/resolv.conf
-			else 
-				echo "nameserver $DNS" >> /etc/resolv.conf
-			fi
-		fi
-		read DNS
-		counter=`expr $counter + 1`
-	done
-}		
+	STATUS=$?
+	if [ "$STATUS" -ne 0 ] ; then
+		exit 1
+	fi
 
-set_static_ip(){
-	echo "Enter your IP address:"
-	read IP
-	if [ -z "$IP" -o -n "${IP##?*.?*.?*.?*}" ] ; then
-		echo ""
-		echo "$IP is not a valid IP address."
-		set_static_ip;
+	unset F_DNS1 F_DNS2 F_DNS3
+	eval "`echo "$SELECTION" | sed -e '1s,^,F_DNS1=",' -e '2s,^,F_DNS2=",' \
+	      -e '3s,^,F_DNS3=",' | sed -e 's,$,",'`"
+
+	# Validate DNS IP addresses
+	DNS_OK=1
+	if [ -n "$F_DNS1" -a "${F_DNS1##?*.?*.?*.?*}" ] || \
+	   [ -n "$F_DNS2" -a "${F_DNS2##?*.?*.?*.?*}" ] || \
+	   [ -n "$F_DNS3" -a "${F_DNS3##?*.?*.?*.?*}" ] ; then
+                DNS_OK=0
+		DNS_ERRMSG="One of the entered IP addresses is not valid."
+        fi
+
+	if [ "$DNS_OK" -eq 1 ] ; then
+		rm -f /etc/resolv.conf
+		[ -n "$F_DNS1" ] && echo "nameserver $F_DNS1" >> /etc/resolv.conf
+		[ -n "$F_DNS2" ] && echo "nameserver $F_DNS2" >> /etc/resolv.conf
+		[ -n "$F_DNS3" ] && echo "nameserver $F_DNS3" >> /etc/resolv.conf
+
+		unset F_DNS1 F_DNS2 F_DNS3 SELECTION DLG_COMMAND STATUS
+		unset DNS_OK DNS_ERRMSG
 	else
-		echo "IP=$IP" >> $ETH0CFG
+		DLG_COMMAND="dialog --title \"DNS IP Validation Error\" \
+		--msgbox \"$DNS_ERRMSG\" 6 60"
+		eval $DLG_COMMAND
+		set_dns;	
 	fi
-}
+}		
 
-set_static_gateway(){
-	echo "Enter your default gateway:"
-	read GTWY
-	if [ -z "$GTWY" -o -n "${GTWY##?*.?*.?*.?*}" ] ; then
-		echo ""
-		echo "$GTWY is not a valid default gateway."
-		set_static_gateway;
-	else
-		echo "GATEWAY=$GTWY" >> $ETH0CFG
+# Setup IP address, prefix, gateway and broadcast
+set_static_cfg(){
+	# Default values (only apply on first run-through, meaning SIP_OK doesn't exist)
+	if [ -z "$SIP_OK" ] ; then
+		F_PREFIX=24
 	fi
-}
 
-set_static_prefix(){
-	echo "Enter your prefix: "
-	echo "(This determines your subnet mask. For example, a prefix of 24 would give you a subnet mask of 255.255.255.0)"
-	read PRFX
-	# First verify that user has entered a prefix, and not just pressed 'Enter'
-	if [ $PRFX ] ; then
-		# Check that prefix is a value between 8 & 30
-		if [ "$PRFX" -lt 8 -o "$PRFX" -gt 30 ] ; then
-			echo ""
-			echo "$PRFX is not a valid prefix."
-			set_static_prefix;
-		else
-			echo "PREFIX=$PRFX" >> $ETH0CFG
-		fi
-	else
-		echo "Prefix cannot be empty."
-		echo ""
-		set_static_prefix;
+        DLG_COMMAND="dialog --title \"Static IP Configuration\" --no-cancel \
+        --form \"Please enter the network connection details:\" 0 0 4 \
+        \"IP Address:\" 1 1 \"$F_IPADDR\" 1 25 15 0 \
+        \"Prefix:\" 2 1 \"$F_PREFIX\" 2 25 2 0 \
+        \"Broadcast:\" 3 1 \"$F_BROADCAST\" 3 25 15 0 \
+	\"Gateway:\" 4 1 \"$F_GATEWAY\" 4 25 15 0"
+        SELECTION=`eval $DLG_COMMAND 3>&2 2>&1 1>&3`
+
+        STATUS=$?
+        if [ "$STATUS" -ne 0 ] ; then
+                exit 1
+        fi
+
+	unset F_IPADDR F_PREFIX F_BROADCAST F_GATEWAY
+        eval "`echo "$SELECTION" | sed -e '1s,^,F_IPADDR=",' -e '2s,^,F_PREFIX=",' \
+              -e '3s,^,F_BROADCAST=",' -e '4s,^,F_GATEWAY=",' | sed -e 's,$,",'`"
+
+        # Validate entries (Must give IP, prefix and broadcast.  Gateway optional.)
+        SIP_OK=1
+        if [ -n "$F_IPADDR" -a "${F_IPADDR##?*.?*.?*.?*}" ] || \
+           [ -n "$F_BROADCAST" -a "${F_BROADCAST##?*.?*.?*.?*}" ] || \
+           [ -n "$F_GATEWAY" -a "${F_GATEWAY##?*.?*.?*.?*}" ] ; then
+                SIP_OK=0
+		SIP_ERRMSG="One of the entered IP addresses or prefix is not valid."
 	fi
-}
+	if [ "$F_PREFIX" -lt 8 -o "$F_PREFIX" -gt 30 ] ; then
+		SIP_OK=0
+		SIP_ERRMSG="The prefix is not valid.  Please enter a value from 8 to 30."
+	fi
+	if [ -z "$F_IPADDR" -o -z "$F_BROADCAST" -o -z "$F_PREFIX" ] ; then
+		SIP_OK=0
+		SIP_ERRMSG="Please enter an IP address, broadcast and prefix."		
+        fi
 
-set_static_broadcast(){
-	echo "Enter your broadcast address: "
-	read BRDCST
-	if [ -z "$BRDCST" -o -n "${BRDCST##?*.?*.?*.?*}" ] ; then
-		echo ""
-		echo "$BRDCST is not a valid broadcast address."
-		set_static_broadcast;
+        if [ "$SIP_OK" -eq 1 ] ; then
+                echo "IP=$F_IPADDR" >> $ETHXCFG
+		echo "PREFIX=$F_PREFIX" >> $ETHXCFG
+		echo "BROADCAST=$F_BROADCAST" >> $ETHXCFG
+		[ -n "$F_GATEWAY" ] && echo "GATEWAY=$F_GATEWAY" >> $ETHXCFG
+
+                unset F_IPADDR F_PREFIX F_BROADCAST F_GATEWAY DLG_COMMAND STATUS
+                unset SELECTION SIP_OK SIP_ERRMSG
 	else
-		echo "BROADCAST=$BRDCST" >> $ETH0CFG
+                DLG_COMMAND="dialog --title \"Static IP Validation Error\" \
+                --msgbox \"$SIP_ERRMSG\" 6 60"
+                eval $DLG_COMMAND
+                set_static_cfg;
 	fi
 }
 
-# Main function. Presents user with a choice of
-# static or dhcp and attempts to bring up eth0
+# 
+net_service_menu(){
+	# Remove previous config, so ifup won't get confused.
+	rm -f $ETHXCFG
 
-choose_service(){
+	unset ARGS
+	ARGS="$ARGS dhcp \"Acquire IP address through dhcp.\""
+	ARGS="$ARGS static \"Enter a static IP Address.\""
+	ARGS="$ARGS pppoe \"Setup an ADSL connection.\""
 
-	# Remove previous config, so ifup won't get confused.
-	rm -f $ETH0CFG
+	DLG_COMMAND="dialog --title \"Select network service\" --default-item dhcp \
+		--menu \"Select the network service to use for the $DEV interface:\" \
+		0 0 0 $ARGS"
+	SERVICE=`eval $DLG_COMMAND 3>&2 2>&1 1>&3`
 
-	echo ""
-	echo "What type of service should eth0 use?"
-	echo "(static, dhcp or pppoe)"
-	read SERVICE
+	unset DLG_COMMAND ARGS
 
-case "$SERVICE" in
+	case "$SERVICE" in
 
-	dhcp) 
-		echo "SERVICE=dhcpcd" > $ETH0CFG
-		echo "DHCP_START=\"\"" >> $ETH0CFG
-		echo "DHCP_STOP=\"-k\"" >> $ETH0CFG
+		dhcp) 
+			echo "SERVICE=dhcpcd" > $ETHXCFG
+			echo "DHCP_START=\"\"" >> $ETHXCFG
+			echo "DHCP_STOP=\"-k\"" >> $ETHXCFG
 
-		# Start the service
-		$IFUP eth0
+			# Start the service
+			$IFUP $DEV
 
-		# Test if dhcp worked and did not set DNS.
-		# If so run set_dns function.
-		if echo $LINKTEST | grep -q UP ; then			
-			if [ ! -f /etc/resolv.conf ] ; then
-				echo "DHCP did not configure DNS."
-				set_dns;
+			# Test if dhcp worked and did not set DNS.
+			# If so run set_dns function.
+			if echo "$LINKTEST" | grep -q UP ; then			
+				if [ ! -f /etc/resolv.conf ] ; then
+					set_dns;
+				fi
+				ip addr show $DEV
 			fi
-			ip addr show eth0
-		fi
-		;;
-
-	static)
-		echo "SERVICE=ipv4-static" > $ETH0CFG
-		set_static_ip;
-		set_static_gateway;
-		set_static_prefix;
-		set_static_broadcast;
-		set_dns;
-		# Start the service
-		$IFUP eth0
-		;;
-
-	pppoe)
-		adsl-setup
-		;;
-
-	*)
-		# Error message in case user entered something unusable, like "waldo"
-		echo ""
-		echo "--------------------------"
-		echo " Invalid option: $SERVICE"
-		echo " Please try again."
-		echo "--------------------------"
-		choose_service;
-		;;
-esac
+			;;
+	
+		static)
+			echo "SERVICE=ipv4-static" > $ETHXCFG
+			set_static_cfg;
+			set_dns;
+			$IFUP $DEV
+			;;
+	
+		pppoe)
+			adsl-setup
+			;;
+		*)
+			exit 0;
+			;;
+	esac
 }
 
-# Script starts here. Tests first for link on eth0
-
+# Script starts here. Check for root user.
 if [ `whoami` != "root" ] ; then
 	echo "You must be root to use $0"
 	exit 1;
 fi
 
-if [ -n "$LINKTEST" ] ; then
+# Get list of devices and prompt user to select one
+if [ ! -d /sys/class/net ] ; then
+	echo "No devices found or sysfs not mounted!"
+	exit 1;
+fi
 
-	# If link is not up, start main function choose_service.
-
-	if ! echo "$LINKTEST" | grep -q UP ; then
-		echo ""
-		echo $GREETING
-		choose_service;
+for EDEV in "/sys/class/net/eth* /sys/class/net/wlan*" ; do
+	EDEV=`basename $EDEV`
+	if echo `/sbin/ip link show $EDEV 2> /dev/null` | grep -q UP ; then
+		EDEVSTATUS="UP"
 	else
+		EDEVSTATUS="DOWN"
+	fi
+	DEVLIST="$DEVLIST `basename $EDEV` \"$EDEVSTATUS\""
+done		
 
-	# If link is up, user likely has run this script already.
-	# Take down eth0 so we can set it up cleanly.
+DLG_COMMAND="dialog --title \"Network Configuration\" --default-item eth0 \
+       	--timeout 20 --menu \"Select the network device to configure:\" \
+	0 0 0 $DEVLIST"
+DEV=`eval $DLG_COMMAND 3>&2 2>&1 1>&3`
+unset EDEV EDEVSTATUS DEVLIST DLG_COMMAND
 
-		$IFDOWN eth0
-		echo ""
-		echo $GREETING
-		choose_service;
+# Continue if device was selected, exit if otherwise.
+if [ -n "$DEV" ] ; then
+	set_devvars;
+
+	if [ -n "$LINKTEST" ] ; then
+		# Link is up. Bring it down and start service menu.
+		if echo "$LINKTEST" | grep -q UP ; then
+			$IFDOWN $DEV
+		fi
+
+		net_service_menu;
+	else
+		DLG_COMMAND="dialog --title \"Device Not Found\" \
+                --msgbox \"Device $DEV is not present on this system.\" 6 60"
+                eval $DLG_COMMAND
+		exit 1
 	fi
-else
-	echo "Device eth0 is not present on this system!"
 fi




More information about the livecd mailing list