pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / scripts / netconf.sh
blob70a03d1f2bd7c4a6c991122e21866245d7aaded4
1 #!/bin/sh
3 # netconf 0.1 - Configure network
5 # Changes:
6 #
8 LOCALRC=/usr/etc/rc.local
9 INETCONF=/etc/inet.conf
10 RCNET=/etc/rc.net
11 HOSTS=/etc/hosts
12 HOSTNAME=/etc/hostname.file
13 USRKBFILE=/.usrkb
15 step1=""
16 step2=""
17 step3=""
18 v=1 # verbosity
19 manual_opts=0
20 prefix=""
21 cd="no" # running from cd?
23 eth=""
24 driver=""
25 driverargs=""
27 config=""
28 manual=""
29 dhcp="no"
31 hostname=""
32 hostname_prev=""
33 ip=""
34 ip_prev=""
35 netmask=""
36 netmask_prev=""
37 gateway=""
38 dns1=""
39 dns2=""
41 # Provide some sane defaults
42 hostname_default=`uname -n`
43 test -z "$hostname_default" && hostname_default="Minix"
44 ip_default="10.0.0.1"
45 netmask_default="255.255.255.0"
46 gateway_default=""
48 usage()
50 cat >&2 <<'EOF'
51 Usage:
53 netconf [-q] [-p <prefix>] [-e <num>] [-a]
54 netconf [-H <name> -i <ip> -n <mask> -g <gw> -d <prim dns> [-s <sec dns>]]
56 flags:
57 -q Limit generated output
58 -p Path prefix for configuration files (e.g., during install -p mnt is used as files are mounted on /mnt).
59 -e Ethernet card
60 -a Use DHCP (-H, -i, -n, -g, -d, and -s flags are discarded)
61 -H Hostname
62 -i IP address
63 -n Netmask
64 -g Default gateway
65 -d Primary DNS
66 -s Secondary DNS
67 -h Shows this help file
68 -c Shows a list of ethernet cards supported
70 By default netconf starts in Interactive mode. By providing parameters on the
71 command line, some questions can be omitted.
72 EOF
73 exit 1
76 card()
78 card_number=$1
79 card_name=$2
80 card_avail=0
81 shift 2
82 while [ $# -gt 0 ]
83 do
84 lspci | grep > /dev/null "^$1" && card_avail=1
85 shift
86 done
87 if [ $card_avail -gt 0 ]
88 then
89 card_mark="*"
90 eth_default=$card_number
91 else
92 card_mark=" "
94 printf "%2d. %s %s\n" "$card_number" "$card_mark" "$card_name"
97 cards()
99 card 0 "No Ethernet card (no networking)"
100 card 1 "Intel Pro/100" "8086:103D" "8086:1064" "8086:1229" "8086:2449"
101 card 2 "3Com 501 or 3Com 509 based card"
102 card 3 "Realtek 8139 based card (also emulated by KVM)" \
103 "10EC:8139" "02AC:1012" "1065:8139" "1113:1211" "1186:1300" \
104 "1186:1340" "11DB:1234" "1259:A117" "1259:A11E" "126C:1211" \
105 "13D1:AB06" "1432:9130" "14EA:AB06" "14EA:AB07" "1500:1360" \
106 "1743:8139" "4033:1360"
107 card 4 "Realtek 8169 based card" \
108 "10EC:8129" "10EC:8167" "10EC:8169" "1186:4300" "1259:C107" \
109 "1385:8169" "16EC:0116" "1737:1032"
110 card 5 "Realtek 8029 based card (also emulated by Qemu)" "10EC:8029"
111 card 6 "NE2000, 3com 503 or WD based card (also emulated by Bochs)"
112 card 7 "AMD LANCE (also emulated by VMWare and VirtualBox)" "1022:2000"
113 card 8 "Intel PRO/1000 Gigabit" "8086:100E" "8086:107C" "8086:10CD"
114 card 9 "Attansic/Atheros L2 FastEthernet" "1969:2048"
115 card 10 "DEC Tulip 21140A in VirtualPC" "1011:0009"
116 card 11 "Different Ethernet card (no networking)"
119 warn()
121 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"
124 do_step1()
126 eth_default=0
128 # Ask user about networking
129 echo "MINIX 3 currently supports the following Ethernet cards. PCI cards detected"
130 echo "by MINIX are marked with *. Please choose: "
131 echo ""
132 cards
133 echo ""
135 while [ "$step1" != ok ]; do
136 echo -n "Ethernet card? [$eth_default] "; read eth
137 test -z $eth && eth=$eth_default
139 drv_params $eth
140 test -n "$driver" && step1="ok"
141 done
144 drv_params()
146 case "$1" in
147 0) driver=psip0; ;;
148 1) driver=fxp; ;;
149 2) driver=dpeth; driverargs="#dpeth_arg='DPETH0=port:irq:memory'";
150 test "$v" = 1 && echo ""
151 test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
153 4) driver=rtl8169; ;;
154 3) driver=rtl8139; ;;
155 5) driver=dp8390; driverargs="dp8390_arg='DPETH0=pci'"; ;;
156 6) driver=dp8390; driverargs="dp8390_arg='DPETH0=240:9'";
157 test "$v" = 1 && echo ""
158 test "$v" = 1 && echo "Note: After installing, edit $LOCALRC to the right configuration."
159 test "$v" = 1 && echo " chose option 4, the defaults for emulation by Bochs have been set."
161 7) driver="lance"; ;;
162 8) driver="e1000"; ;;
163 9) driver="atl2"; ;;
164 10) driver="dec21140A"; ;;
165 11) driver="psip0"; ;;
166 *) warn "choose a number"
167 esac
170 do_step2()
172 echo ""
173 echo "Configure network using DHCP or manually?"
174 echo ""
175 echo "1. Automatically using DHCP"
176 echo "2. Manually"
177 echo ""
179 while [ "$step2" != ok ]
181 echo -n "Configure method? [1] "; read config
182 test -z $config && config=1
184 case "$config" in
185 1) step2="ok"; dhcp="yes" ; ;;
186 2) step2="ok"; manual="do"; ;;
187 *) warn "choose a number"
188 esac
189 done
191 # Use manual parameters?
192 if [ -n "$manual" ]; then
193 # Query user for settings
194 # Hostname
195 if [ -z $hostname_prev ]; then
196 hostname_prev=$hostname_default
198 echo -n "Hostname [$hostname_prev]: "
199 read hostname
200 if [ ! -z $hostname ]; then
201 hostname_prev=$hostname
202 else
203 hostname=$hostname_prev
206 # IP address
207 if [ -z $ip_prev ]; then
208 ip_prev=$ip_default
210 echo -n "IP address [$ip_prev]: "
211 read ip
212 if [ ! -z $ip ]; then
213 ip_prev=$ip
214 else
215 ip=$ip_prev
218 # Netmask
219 if [ -z $netmask_prev ]; then
220 netmask_prev=$netmask_default
222 echo -n "Netmask [$netmask_prev]: "
223 read netmask
224 if [ ! -z $netmask ]; then
225 netmask_prev=$netmask
226 else
227 netmask=$netmask_prev
230 # Gateway (no gateway is fine for local networking)
231 echo -n "Gateway: "
232 read gateway
234 # DNS Servers
235 echo -n "Primary DNS Server [$dns1_prev]: "
236 read dns1
237 test -z "$dns1" && test -n "$dns1_prev" && dns1=$dns1_prev
238 if [ ! -z "$dns1" ]; then
239 dns1_prev=$dns1
241 echo -n "Secondary DNS Server [$dns2_prev]: "
242 read dns2
243 if [ ! -z $dns2 ]; then
244 dns2_prev=$dns2
246 else
247 # If no primary DNS, then also no secondary DNS
248 dns2=""
253 # Parse options
254 while getopts ":qe:p:aH:i:n:g:d:s:hc" arg; do
255 case "$arg" in
256 q) v=0; ;;
257 e) ethernet=$OPTARG;
258 test "$ethernet" -ge 0 -a "$ethernet" -le 7 2>/dev/null || usage
259 drv_params $ethernet
261 p) prefix=$OPTARG; ;;
262 a) dhcp="yes"; ;;
263 H) hostname=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
264 i) ip=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
265 n) netmask=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
266 g) gateway=$OPTARG; manual_opts=`expr $manual_opts '+' 1`;;
267 d) dns1=$OPTARG; ;;
268 s) dns2=$OPTARG; ;;
269 h) usage ;;
270 c) echo -e "The following cards are supported by Minix:\n";
271 cards; exit 0
273 \?) echo "Unknown option -$OPTARG"; usage ;;
274 :) echo "Missing required argument for -$OPTARG"; usage ;;
275 *) usage ;;
276 esac
277 done
279 # Verify parameter count
280 if [ "$dhcp" != "yes" ] ; then
281 if [ $manual_opts -gt 0 ] ; then
282 test $manual_opts -eq 4 -a -n "$dns1" || usage
283 manual="do"
287 if [ -n "$prefix" ] ; then
288 LOCALRC=$prefix$LOCALRC
289 INETCONF=$prefix$INETCONF
290 RCNET=$prefix$RCNET
291 HOSTS=$prefix$HOSTS
292 HOSTNAME=$prefix$HOSTNAME
293 if [ ! -f $INETCONF ]; then
294 echo -e "It seems the supplied prefix (\`$prefix') is invalid."
295 exit 1
299 if [ "$USER" != root ] ; then
300 test "$v" = 1 && echo "Please run netconf as root."
301 exit 1
304 # Are we running from CD?
305 if [ -f "$USRKBFILE" ] ; then
306 cd="yes" # We are running from CD
309 # Do we know what ethernet card to use?
310 test -z "$ethernet" && do_step1
312 # If no parameters are supplied and we're not using DHCP, query for settings
313 test $manual_opts -eq 0 -a "$dhcp" = "no" && do_step2
315 # Store settings.
316 # Do not make backups if we're running from CD
317 test "$cd" != "yes" && test -f $INETCONF && mv $INETCONF "$INETCONF~" &&
318 test "$v" = 1 && echo "Backed up $INETCONF to $INETCONF~"
319 test "$cd" != "yes" && test -f $LOCALRC && mv $LOCALRC "$LOCALRC~" &&
320 test "$v" = 1 && echo "Backed up $LOCALRC to $LOCALRC~"
322 echo "eth0 $driver 0 { default; } ;" > $INETCONF
323 echo "$driverargs" > $LOCALRC
325 if [ -n "$manual" ]
326 then
327 # Backup config file if it exists and we're not running from CD
328 test "$cd" != "yes" && test -f $RCNET && mv $RCNET "$RCNET~" &&
329 test "$v" = 1 && echo "Backed up $RCNET to $RCNET~"
330 test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
331 test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
333 # Store manual config
334 echo "ifconfig -I /dev/ip0 -n $netmask -h $ip" > $RCNET
335 test ! -z $gateway && echo "add_route -g $gateway" >> $RCNET
336 echo "daemonize nonamed -L" >> $RCNET
337 test ! -z $dns1 && echo -e "$ip\t%nameserver\t#$hostname" > $HOSTS
338 test ! -z $dns1 && echo -e "$dns1\t%nameserver\t#DNS 1" >> $HOSTS
339 test ! -z $dns2 && echo -e "$dns2\t%nameserver\t#DNS 2" >> $HOSTS
340 echo -e "\n$ip\t$hostname" >> $HOSTS
341 echo $hostname > $HOSTNAME
342 else
343 test "$cd" != "yes" && test -f "$RCNET" && mv "$RCNET" "$RCNET~" &&
344 test "$v" = 1 && echo "Moved $RCNET to $RCNET~ to use default settings"
345 test "$cd" != "yes" && test -f $HOSTS && mv $HOSTS "$HOSTS~" &&
346 test "$v" = 1 && echo "Backed up $HOSTS to $HOSTS~"
347 test -f "$HOSTS~" && grep -v "%nameserver" "$HOSTS~" > $HOSTS
350 test "$cd" != "yes" && test "$v" = 1 && echo "
351 You might have to reboot for the changes to take effect."
352 exit 0