1
0
mirror of https://github.com/rcornwell/sims.git synced 2026-02-03 07:11:02 +00:00
Files
rcornwell.sims/SEL32/tests/SetupNet
AZBevier 598749c579 SEL32: Add track/sector label support for UDP/HSDP disks.
SEL32: Add Linux network setup utility Setup.Net.
SEL32: Update SEL32 documentation.
2020-12-22 19:22:12 -07:00

380 lines
7.9 KiB
Bash
Executable File

#!/bin/sh
# Copyright (c) 2020, Geert Rolf
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# GEERT ROLF BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# uncomment next line if you need more than one tap = run multiple SIMHs... edit the saved params file
#OPSMODE="expert"
PATH="/bin:/sbin:/etc:/usr/sbin:/usr/bin"
export PATH
# check brctl to exist
L=`whereis brctl |
awk -F: '{ print $2 }' |
grep brctl |
wc -l`
if test $L -eq 0
then
echo 'No brctl program -- please install package bridge-utils'
fi
# check tunctl to exist
LL=`whereis tunctl |
awk -F: '{ print $2 }' |
grep tunctl |
wc -l`
if test $LL -eq 0
then
echo 'No tunctl program -- please install package uml-utilities'
fi
# quit when either not installed
if test $L -eq 0 -o $LL -eq 0
then
exit 1
fi
# make list of all interfaces ignoring lo
AVL_IF=`ip link show |
grep '^[0-9]' |
awk -F: '{ print $2 }' |
grep -v 'lo' |
sed '1,$s/^ //'`
# found br0 in active interfaces?
L=`echo $AVL_IF | grep br0 | wc -l`
if test $L -ne 0
then
echo "$0: br0 already configured."
echo "-- if you configured br0 statically don't use this script"
echo "-- otherwise if you do not want permanent settings"
echo "-- and want br0 configured differently reboot first"
exit 1
fi
# let user chose the interface he/she wants to usx
echo " Available networkinterfaces"
N=0
for I in $AVL_IF
do
N=`expr $N + 1`
L=`ip addr show $I |
grep 'inet' |
grep -v 'inet6' |
wc -l`
if test $L -eq 0
then
M="not configured"
else
M=`ip addr show $I |
grep 'inet' |
grep -v 'inet6' |
awk '{ print $2 }' `
fi
# did we see wlan? alas...
L=`echo $I | grep wlan | wc -l`
if test $L -gt 0
then
M="$M (wlan not supported)"
echo "-: $I $M"
N=`expr $N - 1`
else
if test -r SnetSaved.$I
then
echo "$N: $I $M (saved params available)"
else
echo "$N: $I $M"
fi
fi
done
# take wlan out
AVL_IF=`echo $AVL_IF | sed 's/wlan.//'`
if test $N -gt 1
then
ANSWER=0
while test $ANSWER -lt 1 -o $ANSWER -gt $N
do
echo -n "Which interface do you want to use? "
read ANSWER
done
else
ANSWER=1
fi
N=0
for I in $AVL_IF
do
N=`expr $N + 1`
if test $N -eq $ANSWER
then
ACT_IF=$I
fi
done
# see if there are saved params for this interface
if test -r SnetSaved.$ACT_IF
then
. ./SnetSaved.$ACT_IF
echo "Saved params loaded from SnetSaved.$ACT_IF"
fi
echo ""
echo "Interface to use for SIMH......... " $ACT_IF
if test "x$IPNR" = "x"
then
L=`ip addr show $ACT_IF |
grep inet |
grep -v inet6 |
wc -l`
if test $L -eq 0
then
IPNR="not configured"
else
IPNR=`ip addr show dev $ACT_IF |
grep inet |
grep -v inet6 |
awk '{ print $2 }' `
fi
fi
echo "IPnumber on this interface........ " $IPNR
if test "x$IPBRO" = "x"
then
IPBRO=`ip addr show dev $ACT_IF |
grep inet |
grep -v inet6 |
sed '1s/^.*brd //' |
sed '1s/[ ].*$//'`
fi
echo "IP Broadcast on this interface.... " $IPBRO
if test "x$DEFRT" = "x"
then
DEFRT=`netstat -rn |
grep $ACT_IF |
grep '^0\.' |
uniq |
awk '{ print $2 }'`
fi
if test "x$DEFRT" = "x"
then
echo "Default Route set to Gateway...... " none
else
echo "Default Route set to Gateway...... " $DEFRT
fi
if test "x$IPFWD" = "x"
then
IPFWD=`cat /proc/sys/net/ipv4/ip_forward`
fi
if test $IPFWD -eq 1
then
echo "IP forwarding is active........... " YES
else
echo "IP forwarding is active........... " NO
fi
# nr of taps the user wants or 1 for default
if test "x${NrTaps}" = "x"
then
NrTaps=1
fi
echo "Number of taps to create.......... " $NrTaps
# the user who needs access to the tap
if test "x$SimhUser" = "x"
then
if test "x${SUDO_USER}" != "x"
then
SimhUser=${SUDO_USER}
else
SimhUser=${USER}
fi
fi
echo "User who runs SIMH on the taps.... " $SimhUser
# only save params when in expert OPSMODE
if test "X$OPSMODE" = "Xexpert"
then
if test ! -r SnetSaved.$ACT_IF
then
cat - > SnetSaved.$ACT_IF <<EOF
#
# expert users: edit your params and rerun the SetupNet script
#
# ipnr including mask
IPNR=$IPNR
# do we forward between interfaces 0=no 1=yes
IPFWD=$IPFWD
# default gateway
DEFRT=$DEFRT
# nr of SIMH emulators you want in parallel that use network
NrTaps=$NrTaps
# username of the user running the SIMH emulators
SimhUser=$SimhUser
EOF
L=`ls -l SnetSaved.$ACT_IF | grep root | wc -l`
if test $L -eq 1
then
chown $SimhUser SnetSaved.$ACT_IF
fi
fi
fi
JOBFILE="/tmp/SNjob.$$"
TMPFILE="/tmp/SNtmp.$$"
cat - > $JOBFILE <<\EOF
#!/bin/sh
# This script should be run under root permission
PATH="/bin:/sbin:/etc:/usr/sbin:/usr/bin"
export PATH
EOF
cat - > $TMPFILE <<\EOF
echo ${CMD}
${CMD}
if test $? -ne 0
then
echo '*** FAIL:' ${CMD}
exit 1
fi
EOF
echo "CMD=\"ip link set ${ACT_IF} down\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
echo "CMD=\"brctl addbr br0\"" >> $JOBFILE
## echo "CMD=\"ip tuntap add mode tun name br0\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
N=0
while test $N -lt $NrTaps
do
echo "CMD=\"tunctl -t tap${N} -u ${SimhUser}\"" >> $JOBFILE
## echo "CMD=\"ip tuntap add mode tap name tap${N} user ${SimhUser} dev br0\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
N=`expr $N + 1`
done
echo "CMD=\"brctl addif br0 ${ACT_IF}\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
echo "CMD=\"brctl setfd br0 0\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
if ! test "$IPNR" = "not configured"
then
echo "CMD=\"ip addr del ${IPNR} dev ${ACT_IF}\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
fi
echo "CMD=\"ip link set ${ACT_IF} up\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
if ! test "$IPNR" = "not configured"
then
echo "CMD=\"ip addr add ${IPNR} dev br0\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
fi
echo "CMD=\"ip link set br0 up\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
if test ! "x$DEFRT" = "x"
then
## echo "CMD=\"ip route del default via ${DEFRT} dev ${ACT_IF}\"" >> $JOBFILE
## cat $TMPFILE >> $JOBFILE
echo "CMD=\"ip route add default via ${DEFRT} dev br0\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
fi
ipfwd=`cat /proc/sys/net/ipv4/ip_forward`
if test $IPFWD -ne $ipfwd
then
echo "CMD=\"echo ${IPFWD} > /proc/sys/net/ipv4/ip_forward\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
fi
N=0
while test $N -lt $NrTaps
do
echo "CMD=\"brctl addif br0 tap${N}\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
echo "CMD=\"ip link set tap${N} up\"" >> $JOBFILE
cat $TMPFILE >> $JOBFILE
N=`expr $N + 1`
done
cat - >> $JOBFILE <<EOF
echo ""
echo 'Network reconfigured for use by SIMH simulators'
echo "Bridge br0 with access tap0 wired into interface ${ACT_IF}"
echo 'Configure tap:tap0 for your 1st SIMH host'
echo ""
echo 'Use unique IPnrs on your SIMH hosts within the'
echo 'same network and set a default route if needed'
rm -f $JOBFILE
EOF
if test "X$OPSMODE" = "Xexpert"
then
echo ""
echo "-- To change the params above edit SnetSaved.$ACT_IF"
echo "-- and rerun this script before executing the job"
fi
L=`ls -l $JOBFILE | grep root | wc -l`
if test $L -gt 0
then
echo "Executing $JOBFILE ..."
echo -n "Proceed...? (y)/n "
read ANSWER
if test "x$ANSWER" = "xn"
then
echo "-- To execute: bash $JOBFILE"
exit 0
fi
bash ${JOBFILE}
else
echo ""
echo "-- Executing $JOBFILE requires root permission"
echo "-- Either use sudo or su to execute: bash $JOBFILE"
fi
rm -f $TMPFILE