xref: /minix/minix/commands/netconf/netconf.sh (revision e3b78ef1)
1#!/bin/sh
2#
3#	netconf 0.1 - Configure network
4#
5# Changes:
6#
7
8# Get system config
9. /etc/rc.conf
10
11LOCALRC=/usr/etc/rc.local
12INETCONF=/etc/inet.conf
13RCNET=/etc/rc.net
14HOSTS=/etc/hosts
15HOSTNAME=/etc/hostname.file
16USRKBFILE=/.usrkb
17LSPCI=/tmp/lspci.$$
18DEVICES=/tmp/devices.$$
19
20step1=""
21step2=""
22step3=""
23v=1 # verbosity
24manual_opts=0
25prefix=""
26cd="no" # running from cd?
27
28eth=""
29driver=""
30driverargs=""
31
32config=""
33manual=""
34dhcp="no"
35
36hostname=""
37hostname_prev=""
38ip=""
39ip_prev=""
40netmask=""
41netmask_prev=""
42gateway=""
43dns1=""
44dns2=""
45
46# Provide some sane defaults
47hostname_default=`uname -n`
48test -z "$hostname_default" && hostname_default="Minix"
49ip_default="10.0.0.1"
50netmask_default="255.255.255.0"
51gateway_default=""
52
53usage()
54{
55    cat >&2 <<'EOF'
56Usage:
57
58  netconf [-q] [-p <prefix>] [-e <num>] [-a]
59  netconf [-H <name> -i <ip> -n <mask> -g <gw> -d <prim dns> [-s <sec dns>]]
60
61  flags:
62     -q Limit generated output
63     -p Path prefix for configuration files (e.g., during install -p mnt is used as files are mounted on /mnt).
64     -e Ethernet card
65     -a Use DHCP (-H, -i, -n, -g, -d, and -s flags are discarded)
66     -H Hostname
67     -i IP address
68     -n Netmask
69     -g Default gateway
70     -d Primary DNS
71     -s Secondary DNS
72     -h Shows this help file
73     -c Shows a list of ethernet cards supported
74
75  By default netconf starts in Interactive mode. By providing parameters on the
76  command line, some questions can be omitted.
77EOF
78    exit 1
79}
80
81card()
82{
83	card_number=$1
84	card_name=$2
85	card_avail=0
86	shift 2
87	while [ $# -gt 0 ]
88	do
89		cat $LSPCI | grep > /dev/null "^$1" && card_avail=1
90		shift
91	done
92	if [ $card_avail -gt 0 ]
93	then
94		card_mark="*"
95		eth_default=$card_number
96	else
97		card_mark=" "
98	fi
99	printf "%2d. %s %s\n" "$card_number" "$card_mark" "$card_name"
100}
101
102first_pcicard=5
103
104cards()
105{
106    # Run lspci once to a temp file for use in 'card' function
107    lspci >$LSPCI 2>/dev/null || true
108
109    card 0 "No Ethernet card (no networking)"
110    card 1 "3Com 501 or 3Com 509 based card"
111    card 2 "Realtek 8029 based card (also emulated by Qemu)" "10EC:8029"
112    card 3 "NE2000, 3com 503 or WD based card (also emulated by Qemu, Bochs)"
113    card 4 "lan8710a (on BeagleBone, BeagleBone Black)"
114    n=$first_pcicard
115    for pcicard in $pci_list
116    do	var=\$pci_descr$pcicard; descr="`eval echo $var`"
117    	var=\$pci_pcilist$pcicard; pcilist="`eval echo $var`"
118    	card $n "$descr" $pcilist
119	n="`expr $n + 1`"
120    done
121
122    card $first_after_pci "Different Ethernet card (no networking)"
123
124    # Remove temporary lspci output
125    rm -f $LSPCI
126}
127
128warn()
129{
130    echo -e "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ! $1"
131}
132
133config_pci_cards() {
134	rm -f $DEVICES
135
136	n=0
137
138	# Collect configs from network devices
139	for dir in $SYSTEM_CONF_DIRS
140	do	for f in $dir/$SYSTEM_CONF_SUBDIR/*
141		do	if [ -f $f ]
142			then	printconfig $f | grep 'type net.*pci device'
143			fi
144		done
145	done >$DEVICES
146	while read devline
147	do	pcilist="`echo $devline | sed 's/.*pci device //' | sed 's/,.*//'`"
148		descr="`echo $devline | sed 's/.*,descr //' | sed 's/,.*//'`"
149		label="`echo $devline | sed 's/.*service //' | sed 's/,.*//'`"
150		pci_list="$pci_list $n"
151		eval "pci_pcilist$n=\"$pcilist\""
152		eval "pci_descr$n=\"$descr\""
153		eval "pci_label$n=\"$label\""
154		n="`expr $n + 1`"
155	done <$DEVICES
156
157	first_after_pci=`expr $first_pcicard + $n`
158
159	rm -f $DEVICES
160}
161
162do_step1()
163{
164    eth_default=0
165
166    # Ask user about networking
167    echo "MINIX 3 currently supports the following Ethernet cards. PCI cards detected"
168    echo "by MINIX are marked with *. Please choose: "
169    echo ""
170    cards
171    echo ""
172
173    while [ "$step1" != ok ]; do
174      echo -n "Ethernet card? [$eth_default] "; read eth
175      test -z $eth && eth=$eth_default
176
177      drv_params $eth
178      test -n "$driver" && step1="ok"
179    done
180}
181
182drv_params()
183{
184	# If this is a known pci device, we only have to set
185	# the driver.
186	if [ $1 -ge $first_pcicard -a $1 -lt $first_after_pci ]
187	then	pcicard="`expr $1 - $first_pcicard`"
188    		var=\$pci_label$pcicard; driver="`eval echo $var`"
189		return
190	fi
191
192      # Other possibilities.
193      case "$1" in
194        0) driver=psip0;    ;;
195	1) driver=dpeth;    driverargs="#dpeth_arg='DPETH0=port:irq:memory'";
196	   test "$v" = 1 && echo ""
197           test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
198		;;
199	2) driver=dp8390;   driverargs="dp8390_arg='DPETH0=pci'";	;;
200	3) driver=dp8390;   driverargs="dp8390_arg='DPETH0=300:9'";
201	   test "$v" = 1 && echo ""
202           test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
203           test "$v" = 1 && echo "You may then also have to edit /etc/system.conf ."
204           test "$v" = 1 && echo "For now, the defaults for emulation by Bochs/Qemu have been set."
205		;;
206	4) driver=lan8710a;	;;
207        $first_after_pci) driver="psip0"; ;;
208        *) warn "choose a number"
209      esac
210}
211
212do_step2()
213{
214    echo ""
215    echo "Configure network using DHCP or manually?"
216    echo ""
217    echo "1. Automatically using DHCP"
218    echo "2. Manually"
219    echo ""
220
221    while [ "$step2" != ok ]
222      do
223        echo -n "Configure method? [1] "; read config
224	test -z $config && config=1
225
226	case "$config" in
227	    1) step2="ok"; dhcp="yes" ; ;;
228            2) step2="ok"; manual="do"; ;;
229	    *) warn "choose a number"
230	esac
231    done
232
233    # Use manual parameters?
234    if [ -n "$manual" ]; then
235        # Query user for settings
236        # Hostname
237	if [ -z $hostname_prev ]; then
238	    hostname_prev=$hostname_default
239	fi
240	echo -n "Hostname [$hostname_prev]: "
241	read hostname
242	if [ ! -z $hostname ]; then
243	    hostname_prev=$hostname
244	else
245	    hostname=$hostname_prev
246	fi
247
248        # IP address
249	if [ -z $ip_prev ]; then
250	    ip_prev=$ip_default
251	fi
252	echo -n "IP address [$ip_prev]: "
253	read ip
254	if [ ! -z $ip ]; then
255	    ip_prev=$ip
256	else
257	    ip=$ip_prev
258	fi
259
260        # Netmask
261	if [ -z $netmask_prev ]; then
262	    netmask_prev=$netmask_default
263	fi
264	echo -n "Netmask [$netmask_prev]: "
265	read netmask
266	if [ ! -z $netmask ]; then
267	    netmask_prev=$netmask
268	else
269	    netmask=$netmask_prev
270	fi
271
272        # Gateway (no gateway is fine for local networking)
273	echo -n "Gateway: "
274	read gateway
275
276        # DNS Servers
277	echo -n "Primary DNS Server [$dns1_prev]: "
278	read dns1
279	test -z "$dns1" && test -n "$dns1_prev" && dns1=$dns1_prev
280	if [ ! -z "$dns1" ]; then
281	    dns1_prev=$dns1
282
283	    echo -n "Secondary DNS Server [$dns2_prev]: "
284	    read dns2
285	    if [ ! -z $dns2 ]; then
286		dns2_prev=$dns2
287	    fi
288	else
289	    # If no primary DNS, then also no secondary DNS
290	    dns2=""
291	fi
292    fi
293}
294
295# Find pci cards we know about
296config_pci_cards
297
298# Parse options
299while getopts ":qe:p:aH:i:n:g:d:s:hc" arg; do
300    case "$arg" in
301	q) v=0; ;;
302	e) ethernet=$OPTARG;
303	   test "$ethernet" -ge 0 -a "$ethernet" -le 7 2>/dev/null || usage
304	   drv_params $ethernet
305	   ;;
306	p) prefix=$OPTARG; ;;
307	a) dhcp="yes"; ;;
308	H) hostname=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
309	i) ip=$OPTARG;       manual_opts=`expr $manual_opts '+' 1`;;
310	n) netmask=$OPTARG;  manual_opts=`expr $manual_opts '+' 1`;;
311	g) gateway=$OPTARG;  manual_opts=`expr $manual_opts '+' 1`;;
312	d) dns1=$OPTARG;     ;;
313	s) dns2=$OPTARG;     ;;
314	h) usage ;;
315	c) echo -e "The following cards are supported by Minix:\n";
316	   cards; exit 0
317	   ;;
318	\?) echo "Unknown option -$OPTARG"; usage ;;
319	:) echo "Missing required argument for -$OPTARG"; usage ;;
320	*)  usage ;;
321    esac
322done
323
324# Verify parameter count
325if [ "$dhcp" != "yes" ] ; then
326    if [ $manual_opts -gt 0 ] ; then
327        test $manual_opts -eq 4 -a -n "$dns1" || usage
328        manual="do"
329    fi
330fi
331
332if [ -n "$prefix" ] ; then
333    LOCALRC=$prefix$LOCALRC
334    INETCONF=$prefix$INETCONF
335    RCNET=$prefix$RCNET
336    HOSTS=$prefix$HOSTS
337    HOSTNAME=$prefix$HOSTNAME
338    if [ ! -f  $INETCONF ]; then
339    	echo -e "It seems the supplied prefix (\`$prefix') is invalid."
340    	exit 1
341    fi
342fi
343
344if [ `whoami` != root ] ; then
345    test "$v" = 1 && echo "Please run netconf as root."
346    exit 1
347fi
348
349# Are we running from CD?
350if [ -f "$USRKBFILE" ] ; then
351    cd="yes" # We are running from CD
352fi
353
354# Do we know what ethernet card to use?
355test -z "$ethernet" && do_step1
356
357# If no parameters are supplied and we're not using DHCP, query for settings
358test $manual_opts -eq 0 -a "$dhcp" = "no" && do_step2
359
360# Store settings.
361# Do not make backups if we're running from CD
362test "$cd" != "yes" && test -f $INETCONF && mv $INETCONF "$INETCONF~" &&
363                     test "$v" = 1 && echo "Backed up $INETCONF to $INETCONF~"
364test "$cd" != "yes" && test -f $LOCALRC && mv $LOCALRC "$LOCALRC~" &&
365                     test "$v" = 1 && echo "Backed up $LOCALRC to $LOCALRC~"
366
367if [ "$driver" = "psip0" ]; then
368    echo "psip0 { default; } ;" > $INETCONF
369else
370    echo "eth0 $driver 0 { default; } ;" > $INETCONF
371fi
372echo "$driverargs" > $LOCALRC
373
374if [ -n "$manual" ]
375    then
376    # Backup config file if it exists and we're not running from CD
377    test "$cd" != "yes" && test -f $RCNET && mv $RCNET "$RCNET~" &&
378                      test "$v" = 1 && echo "Backed up $RCNET to $RCNET~"
379    test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
380                      test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
381
382    # Store manual config
383    echo "ifconfig -I /dev/ip0 -n $netmask -h $ip" > $RCNET
384    test ! -z $gateway && echo "add_route -g $gateway" >> $RCNET
385    echo "daemonize nonamed -L" >> $RCNET
386    test ! -z $dns1 && echo -e "$ip\t%nameserver\t#$hostname" > $HOSTS
387    test ! -z $dns1 && echo -e "$dns1\t%nameserver\t#DNS 1" >> $HOSTS
388    test ! -z $dns2 && echo -e "$dns2\t%nameserver\t#DNS 2" >> $HOSTS
389    echo -e "\n$ip\t$hostname" >> $HOSTS
390    echo $hostname > $HOSTNAME
391else
392    test "$cd" != "yes" && test -f "$RCNET" && mv "$RCNET" "$RCNET~" &&
393        test "$v" = 1 && echo "Moved $RCNET to $RCNET~ to use default settings"
394    test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
395        test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
396    test -f "$HOSTS~" && grep -v "%nameserver" "$HOSTS~" > $HOSTS
397fi
398
399test "$cd" != "yes" && test "$v" = 1 && echo "
400You might have to reboot for the changes to take effect."
401
402exit 0
403