umount: getopt return value is int, not char
[minix.git] / commands / netconf / netconf.sh
blobc435379afb909a1280eb2aa085a21d0d535ba4bf
1 #!/bin/sh
3 # netconf 0.1 - Configure network
5 # Changes:
6 #
8 # Get system config
9 . /etc/rc.conf
11 LOCALRC=/usr/etc/rc.local
12 INETCONF=/etc/inet.conf
13 RCNET=/etc/rc.net
14 HOSTS=/etc/hosts
15 HOSTNAME=/etc/hostname.file
16 USRKBFILE=/.usrkb
17 LSPCI=/tmp/lspci.$$
18 DEVICES=/tmp/devices.$$
20 step1=""
21 step2=""
22 step3=""
23 v=1 # verbosity
24 manual_opts=0
25 prefix=""
26 cd="no" # running from cd?
28 eth=""
29 driver=""
30 driverargs=""
32 config=""
33 manual=""
34 dhcp="no"
36 hostname=""
37 hostname_prev=""
38 ip=""
39 ip_prev=""
40 netmask=""
41 netmask_prev=""
42 gateway=""
43 dns1=""
44 dns2=""
46 # Provide some sane defaults
47 hostname_default=`uname -n`
48 test -z "$hostname_default" && hostname_default="Minix"
49 ip_default="10.0.0.1"
50 netmask_default="255.255.255.0"
51 gateway_default=""
53 usage()
55 cat >&2 <<'EOF'
56 Usage:
58 netconf [-q] [-p <prefix>] [-e <num>] [-a]
59 netconf [-H <name> -i <ip> -n <mask> -g <gw> -d <prim dns> [-s <sec dns>]]
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
75 By default netconf starts in Interactive mode. By providing parameters on the
76 command line, some questions can be omitted.
77 EOF
78 exit 1
81 card()
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=" "
99 printf "%2d. %s %s\n" "$card_number" "$card_mark" "$card_name"
102 first_pcicard=4
104 cards()
106 # Run lspci once to a temp file for use in 'card' function
107 lspci >$LSPCI || exit
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 Bochs)"
113 n=$first_pcicard
114 for pcicard in $pci_list
115 do var=\$pci_descr$pcicard; descr="`eval echo $var`"
116 var=\$pci_pcilist$pcicard; pcilist="`eval echo $var`"
117 card $n "$descr" $pcilist
118 n="`expr $n + 1`"
119 done
121 card $first_after_pci "Different Ethernet card (no networking)"
123 # Remove temporary lspci output
124 rm -f $LSPCI
127 warn()
129 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"
132 config_pci_cards() {
133 rm -f $DEVICES
137 # Collect configs from network devices
138 for dir in $SYSTEM_CONF_DIRS
139 do for f in $dir/$SYSTEM_CONF_SUBDIR/*
140 do if [ -f $f ]
141 then printconfig $f | grep 'type net.*pci device'
143 done
144 done >$DEVICES
145 while read devline
146 do pcilist="`echo $devline | sed 's/.*pci device //' | sed 's/,.*//'`"
147 descr="`echo $devline | sed 's/.*,descr //' | sed 's/,.*//'`"
148 label="`echo $devline | sed 's/.*service //' | sed 's/,.*//'`"
149 pci_list="$pci_list $n"
150 eval "pci_pcilist$n=\"$pcilist\""
151 eval "pci_descr$n=\"$descr\""
152 eval "pci_label$n=\"$label\""
153 n="`expr $n + 1`"
154 done <$DEVICES
156 first_after_pci=`expr $first_pcicard + $n`
158 rm -f $DEVICES
161 do_step1()
163 eth_default=0
165 # Ask user about networking
166 echo "MINIX 3 currently supports the following Ethernet cards. PCI cards detected"
167 echo "by MINIX are marked with *. Please choose: "
168 echo ""
169 cards
170 echo ""
172 while [ "$step1" != ok ]; do
173 echo -n "Ethernet card? [$eth_default] "; read eth
174 test -z $eth && eth=$eth_default
176 drv_params $eth
177 test -n "$driver" && step1="ok"
178 done
181 drv_params()
183 # If this is a known pci device, we only have to set
184 # the driver.
185 if [ $1 -ge $first_pcicard -a $1 -lt $first_after_pci ]
186 then pcicard="`expr $1 - $first_pcicard`"
187 var=\$pci_label$pcicard; driver="`eval echo $var`"
188 return
191 # Other possibilities.
192 case "$1" in
193 0) driver=psip0; ;;
194 1) driver=dpeth; driverargs="#dpeth_arg='DPETH0=port:irq:memory'";
195 test "$v" = 1 && echo ""
196 test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
198 2) driver=dp8390; driverargs="dp8390_arg='DPETH0=pci'"; ;;
199 3) driver=dp8390; driverargs="dp8390_arg='DPETH0=240:9'";
200 test "$v" = 1 && echo ""
201 test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
202 test "$v" = 1 && echo " chose option 4, the defaults for emulation by Bochs have been set."
204 $first_after_pci) driver="psip0"; ;;
205 *) warn "choose a number"
206 esac
209 do_step2()
211 echo ""
212 echo "Configure network using DHCP or manually?"
213 echo ""
214 echo "1. Automatically using DHCP"
215 echo "2. Manually"
216 echo ""
218 while [ "$step2" != ok ]
220 echo -n "Configure method? [1] "; read config
221 test -z $config && config=1
223 case "$config" in
224 1) step2="ok"; dhcp="yes" ; ;;
225 2) step2="ok"; manual="do"; ;;
226 *) warn "choose a number"
227 esac
228 done
230 # Use manual parameters?
231 if [ -n "$manual" ]; then
232 # Query user for settings
233 # Hostname
234 if [ -z $hostname_prev ]; then
235 hostname_prev=$hostname_default
237 echo -n "Hostname [$hostname_prev]: "
238 read hostname
239 if [ ! -z $hostname ]; then
240 hostname_prev=$hostname
241 else
242 hostname=$hostname_prev
245 # IP address
246 if [ -z $ip_prev ]; then
247 ip_prev=$ip_default
249 echo -n "IP address [$ip_prev]: "
250 read ip
251 if [ ! -z $ip ]; then
252 ip_prev=$ip
253 else
254 ip=$ip_prev
257 # Netmask
258 if [ -z $netmask_prev ]; then
259 netmask_prev=$netmask_default
261 echo -n "Netmask [$netmask_prev]: "
262 read netmask
263 if [ ! -z $netmask ]; then
264 netmask_prev=$netmask
265 else
266 netmask=$netmask_prev
269 # Gateway (no gateway is fine for local networking)
270 echo -n "Gateway: "
271 read gateway
273 # DNS Servers
274 echo -n "Primary DNS Server [$dns1_prev]: "
275 read dns1
276 test -z "$dns1" && test -n "$dns1_prev" && dns1=$dns1_prev
277 if [ ! -z "$dns1" ]; then
278 dns1_prev=$dns1
280 echo -n "Secondary DNS Server [$dns2_prev]: "
281 read dns2
282 if [ ! -z $dns2 ]; then
283 dns2_prev=$dns2
285 else
286 # If no primary DNS, then also no secondary DNS
287 dns2=""
292 # Find pci cards we know about
293 config_pci_cards
295 # Parse options
296 while getopts ":qe:p:aH:i:n:g:d:s:hc" arg; do
297 case "$arg" in
298 q) v=0; ;;
299 e) ethernet=$OPTARG;
300 test "$ethernet" -ge 0 -a "$ethernet" -le 7 2>/dev/null || usage
301 drv_params $ethernet
303 p) prefix=$OPTARG; ;;
304 a) dhcp="yes"; ;;
305 H) hostname=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
306 i) ip=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
307 n) netmask=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
308 g) gateway=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
309 d) dns1=$OPTARG; ;;
310 s) dns2=$OPTARG; ;;
311 h) usage ;;
312 c) echo -e "The following cards are supported by Minix:\n";
313 cards; exit 0
315 \?) echo "Unknown option -$OPTARG"; usage ;;
316 :) echo "Missing required argument for -$OPTARG"; usage ;;
317 *) usage ;;
318 esac
319 done
321 # Verify parameter count
322 if [ "$dhcp" != "yes" ] ; then
323 if [ $manual_opts -gt 0 ] ; then
324 test $manual_opts -eq 4 -a -n "$dns1" || usage
325 manual="do"
329 if [ -n "$prefix" ] ; then
330 LOCALRC=$prefix$LOCALRC
331 INETCONF=$prefix$INETCONF
332 RCNET=$prefix$RCNET
333 HOSTS=$prefix$HOSTS
334 HOSTNAME=$prefix$HOSTNAME
335 if [ ! -f $INETCONF ]; then
336 echo -e "It seems the supplied prefix (\`$prefix') is invalid."
337 exit 1
341 if [ `whoami` != root ] ; then
342 test "$v" = 1 && echo "Please run netconf as root."
343 exit 1
346 # Are we running from CD?
347 if [ -f "$USRKBFILE" ] ; then
348 cd="yes" # We are running from CD
351 # Do we know what ethernet card to use?
352 test -z "$ethernet" && do_step1
354 # If no parameters are supplied and we're not using DHCP, query for settings
355 test $manual_opts -eq 0 -a "$dhcp" = "no" && do_step2
357 # Store settings.
358 # Do not make backups if we're running from CD
359 test "$cd" != "yes" && test -f $INETCONF && mv $INETCONF "$INETCONF~" &&
360 test "$v" = 1 && echo "Backed up $INETCONF to $INETCONF~"
361 test "$cd" != "yes" && test -f $LOCALRC && mv $LOCALRC "$LOCALRC~" &&
362 test "$v" = 1 && echo "Backed up $LOCALRC to $LOCALRC~"
364 if [ "$driver" = "psip0" ]; then
365 echo "psip0 { default; } ;" > $INETCONF
366 else
367 echo "eth0 $driver 0 { default; } ;" > $INETCONF
369 echo "$driverargs" > $LOCALRC
371 if [ -n "$manual" ]
372 then
373 # Backup config file if it exists and we're not running from CD
374 test "$cd" != "yes" && test -f $RCNET && mv $RCNET "$RCNET~" &&
375 test "$v" = 1 && echo "Backed up $RCNET to $RCNET~"
376 test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
377 test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
379 # Store manual config
380 echo "ifconfig -I /dev/ip0 -n $netmask -h $ip" > $RCNET
381 test ! -z $gateway && echo "add_route -g $gateway" >> $RCNET
382 echo "daemonize nonamed -L" >> $RCNET
383 test ! -z $dns1 && echo -e "$ip\t%nameserver\t#$hostname" > $HOSTS
384 test ! -z $dns1 && echo -e "$dns1\t%nameserver\t#DNS 1" >> $HOSTS
385 test ! -z $dns2 && echo -e "$dns2\t%nameserver\t#DNS 2" >> $HOSTS
386 echo -e "\n$ip\t$hostname" >> $HOSTS
387 echo $hostname > $HOSTNAME
388 else
389 test "$cd" != "yes" && test -f "$RCNET" && mv "$RCNET" "$RCNET~" &&
390 test "$v" = 1 && echo "Moved $RCNET to $RCNET~ to use default settings"
391 test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
392 test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
393 test -f "$HOSTS~" && grep -v "%nameserver" "$HOSTS~" > $HOSTS
396 test "$cd" != "yes" && test "$v" = 1 && echo "
397 You might have to reboot for the changes to take effect."
399 exit 0