Remove building with NOCRYPTO option
[minix3.git] / minix / commands / netconf / netconf.sh
blob826603460743ac2da9318aa86ff8d7a52872b3b3
1 #!/bin/sh
3 # netconf 0.2 - Configure network
5 # Changes:
6 # v0.2: rewrite for NetBSD network infrastructure
7 # - the primary choice is now for an interface, not a network card;
8 # - manual driver configuration is now an exception;
9 # - the menu transition system is slightly more solid;
10 # - all non-interactive functionality has been removed.
13 # Get system config
14 . /etc/rc.conf
16 LOCALRC=/usr/etc/rc.local
17 IFCONF=/etc/ifconfig.
18 RESOLVCONF=/etc/resolv.conf
19 HOSTNAME=/etc/hostname.file
20 USRKBFILE=/.usrkb
22 prefix=""
23 cd="no" # running from cd?
24 changed="no" # have any ifconfig.if(5) files been changed?
26 usage()
28 cat >&2 <<'EOF'
29 Usage:
31 netconf [-lh] [-p <prefix>]
33 flags:
34 -l Print a list of configurable interfaces
35 -h Print this help file
36 -p Set a path prefix for all configuration files (e.g., /mnt)
37 EOF
38 exit 1
41 backup_file()
43 # Do not make backups if we're running from CD.
44 if [ "$cd" != "yes" -a -f "$1" ]; then
45 mv "$1" "$1~" || exit 1
46 echo
47 echo "Backed up $1 to $1~"
51 select_number()
53 while true; do
54 echo -n "$4 [$3] "
55 read input
56 case "$input" in
57 '')
58 return $3
60 *[!0-9]*)
63 [ $input -ge $1 -a $input -le $2 ] && return $input
65 esac
66 done
69 interfaces()
71 # Get a list of interfaces that are not virtual (i.e., cloners). There
72 # is always one virtual interface type ("lo", loopback).
73 cloners_regex='^('`ifconfig -C | sed 's/ /[0-9]|/g'`'[0-9])'
74 iflist=`ifconfig -l | tr ' ' '\n' | grep -vE "$cloners_regex"`
76 ifcount=0
77 ifunconf=0 # the first interface with no configuration file, or 0
78 if [ -z "$iflist" ]; then
79 echo " No network hardware interfaces detected!"
80 else
81 for if in $iflist; do
82 ifcount=$(($ifcount + 1))
83 if [ -r $IFCONF$if ]; then
84 info="($1)"
85 else
86 [ $ifunconf -eq 0 ] && ifunconf=$ifcount
87 info=""
89 printf "%2d. %-8s %s\n" $ifcount "$if" "$info"
90 done
94 do_step1()
96 echo "
97 The following network interfaces are available for configuration. These are
98 interfaces corresponding to network drivers that are currently running. If no
99 interface is listed for your network card here, then either MINIX 3 does not
100 support your card, or, if it is not a plug-and-play device, it may require
101 manual configuration first.
103 Please choose the interface you would like to configure, or another option.
105 interfaces "already configured"
106 echo
107 manual_choice=$(($ifcount + 1))
108 quit_choice=$(($ifcount + 2))
109 printf "%2d. Manually configure an ethernet driver\n" $manual_choice
110 printf "%2d. Quit\n\n" $quit_choice
112 default_choice=$ifunconf
113 [ $default_choice -eq 0 ] && default_choice=$quit_choice
115 select_number 1 $quit_choice $default_choice "Interface choice?"
116 choice=$?
118 case $choice in
119 $manual_choice)
120 step=do_stepM
122 $quit_choice)
125 ifchoice="$(echo $iflist | cut -d' ' -f$choice)"
126 step=do_step2
127 esac
130 do_stepM()
132 # TODO: it would be nice if this list changed on a per-platform basis..
133 echo "
134 MINIX 3 has drivers for a limited number of older cards that require manual
135 configuration. They are shown below. Please choose one of the listed options.
137 1. 3Com 501 or 3Com 509 based ISA card (i386)
138 2. NE2000, 3Com 503, or WD based ISA card (i386) (emulated by Bochs, Qemu)
140 3. Go back to interface selection
141 4. Quit
144 select_number 1 4 4 "Card choice?"
146 case $? in
148 driver=dpeth
149 driverargs="#dpeth_args='DPETH0=port:irq:memory'"
150 echo "
151 Note: After installing, edit $LOCALRC to the right configuration."
154 driver=dp8390
155 driverargs="dp8390_args='DPETH0=300:9'"
156 echo "
157 Note: After installing, edit $LOCALRC to the right configuration.
158 You may then also have to edit /etc/system.conf.d/dp8390 to match.
159 For now, the defaults for emulation by Bochs/Qemu have been set."
162 step=do_step1
163 return
166 return
168 esac
170 backup_file "$LOCALRC"
171 echo "# Generated by netconf(8). Edit as necessary." > $LOCALRC
172 echo "netdriver='"$driver"'" >> $LOCALRC
173 echo "$driverargs" >> $LOCALRC
175 # $LOCALRC typically expands to /mnt/usr/etc/rc.local, so leave room..
176 echo "
177 A template to start the driver has been written to $LOCALRC . As
178 noted above, you may have to edit it. Once you are done editing, reboot the
179 system, after which the driver will be started. Once the driver is running,
180 you can run 'netconf' to configure the corresponding network interface."
183 do_step2()
185 iffile="$IFCONF$ifchoice"
187 echo "
188 Configure interface $ifchoice using DHCP or manually?
190 For now, the choice here is primarily about IPv4. With DHCP it is possible to
191 enable IPv6 as well. Even if the local network has no IPv6 facilities, enabling
192 IPv6 should do no harm. For IPv6-only mode or any other configuration that is
193 not supported here, you will have to edit $iffile yourself.
195 1. Automatically using DHCP (IPv4 + IPv6)
196 2. Automatically using DHCP (IPv4 only)
197 3. Manually (IPv4 only)"
199 if [ -r "$iffile" ]; then
200 echo " 4. Remove current configuration"
201 remove_choice=4
202 goback_choice=5
203 quit_choice=6
204 else
205 remove_choice=X
206 goback_choice=4
207 quit_choice=5
210 echo
211 printf "%2d. Go back to interface selection\n" $goback_choice
212 printf "%2d. Quit\n\n" $quit_choice
214 select_number 1 $quit_choice 1 "Configuration choice?"
216 case $? in
218 backup_file "$iffile"
219 echo 'up' > $iffile
220 echo '!dhcpcd -qM $int' >> $iffile
222 echo
223 echo "Interface $ifchoice configured for DHCP (IPv4 + IPv6)."
224 step=do_step3
227 backup_file "$iffile"
228 echo 'up' > $iffile
229 echo '!dhcpcd -qM -4 $int' >> $iffile
231 echo
232 echo "Interface $ifchoice configured for DHCP (IPv4 only)."
233 step=do_step3
236 # Query user for settings
238 # Some of these settings (hostname, nameservers) do not apply
239 # to just the selected interface. Still, they are what one has
240 # to specify for a complete manual configuration. In order to
241 # make manual configuration of multiple interfaces less
242 # frustrating in this regard, offer defaults that match what
243 # may just have been set already.
245 echo
247 # Hostname
248 if [ -r $HOSTNAME ]; then
249 hostname_default=$(cat $HOSTNAME)
250 else
251 hostname_default="minix"
253 echo -n "Hostname [$hostname_default]: "
254 read hostname
255 if [ -z "$hostname" ]; then
256 hostname="$hostname_default"
259 # IP address
260 ip=""
261 while [ -z "$ip" ]; do
262 echo -n "IP address []: "
263 read ip
264 done
266 # Netmask
267 echo -n "Netmask (optional) []: "
268 read netmask
269 [ -n "$netmask" ] && netmask=" netmask $netmask"
271 # Gateway (no gateway is fine for local networking)
272 echo -n "Gateway (optional) []: "
273 read gateway
275 # DNS Servers
276 dns1_default="$(grep '^nameserver' $RESOLVCONF 2>/dev/null | \
277 sed '1q' | awk '{print $2}')"
278 dns2_default="$(grep '^nameserver' $RESOLVCONF 2>/dev/null | \
279 sed '2q;d' | awk '{print $2}')"
281 echo -n "Primary DNS Server [$dns1_default]: "
282 read dns1
283 [ -z "$dns1" ] && dns1="$dns1_default"
285 if [ -n "$dns1" ]; then
286 echo -n "Secondary DNS Server (optional) [$dns2_default]: "
287 read dns2
288 [ -z "$dns2" ] && dns2="$dns2_default"
289 else
290 dns2=""
293 backup_file "$HOSTNAME"
294 echo "$hostname" > $HOSTNAME
295 hostname "$hostname"
297 backup_file "$iffile"
298 echo 'up' > $iffile
299 echo "inet $ip$netmask" >> $iffile
300 if [ -n "$gateway" ]; then
301 echo "!route -q add default $gateway" >> $iffile
304 if [ -n "$dns1" ]; then
305 backup_file "$RESOLVCONF"
306 echo "nameserver $dns1" > $RESOLVCONF
307 if [ -n "$dns2" ]; then
308 echo "nameserver $dns2" >> $RESOLVCONF
312 echo
313 echo "Interface $ifchoice configured manually."
314 step=do_step3
316 $remove_choice)
317 backup_file "$iffile"
318 rm -f "$iffile"
319 echo
320 echo "Removed configuration for interface $ifchoice."
321 step=do_step3
323 $goback_choice)
324 step=do_step1
326 esac
329 do_step3()
332 # We get here only if one of the ifconfig.if(5) files have changed.
333 changed="yes"
335 echo "
336 Do you want to configure additional interfaces?
338 You can also invoke the 'netconf' command as root at any later time.
340 1. Go back to interface selection
341 2. Quit
344 # Note that "quit" is deliberately the default choice: most people will
345 # want to configure at most one interface, and keep pressing Enter in
346 # order to make it through the setup procedure as easily as possible.
347 select_number 1 2 2 "Menu choice?"
349 [ $? -eq 1 ] && step=do_step1
352 # Parse options
353 while getopts "p:hl" arg; do
354 case "$arg" in
355 p) prefix=$OPTARG; ;;
356 h) usage ;;
357 l) echo "The following network hardware interfaces are detected:"
358 echo
359 interfaces "configured"
360 exit 0
362 \?) echo "Unknown option -$OPTARG"; usage ;;
363 :) echo "Missing required argument for -$OPTARG"; usage ;;
364 *) usage ;;
365 esac
366 done
368 if [ -n "$prefix" ] ; then
369 if [ ! -d $prefix ]; then
370 echo -e "It seems the supplied prefix (\`$prefix') is invalid."
371 exit 1
373 LOCALRC=$prefix$LOCALRC
374 IFCONF=$prefix$IFCONF
375 RESOLVCONF=$prefix$RESOLVCONF
376 HOSTNAME=$prefix$HOSTNAME
379 if [ `whoami` != root ] ; then
380 echo "Please run netconf as root."
381 exit 1
384 if ! ifconfig -l >/dev/null 2>&1; then
385 echo "Unable to obtain a list of interfaces. Is the LWIP service running?"
386 exit 1
389 # Are we running from CD?
390 if [ -f "$USRKBFILE" ] ; then
391 cd="yes" # We are running from CD
394 # The interactive program.
395 step=do_step1
396 while [ $step != exit ]; do
397 proc=$step
398 step=exit
399 $proc
400 done
402 # Skip printing this last bit of information if it will not actually work. The
403 # fact that it will not work on the CD (i.e. from the setup program) at least
404 # yet, is also the reason why we do not simply issue the command ourselves
405 # right now. We might reconsider this later.
406 if [ "$changed" = "yes" -a -z "$prefix" ]; then
407 echo
408 echo "One or more of the interface configuration files have been changed."
409 echo "You can use the command 'service network restart' to reload them now."
412 # Aesthetics.
413 echo
415 exit 0