VBoxAddIF.sh revision c3cd0366348bcd10e42bd7be9ea05d194394f389
#
# innotek VirtualBox
# Permanent host interface creation script for Linux systems.
#
# Copyright (C) 2006-2007 innotek GmbH
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# General Public License as published by the Free Software Foundation,
# in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
# distribution. VirtualBox OSE is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY of any kind.
#
# If you received this file as part of a commercial VirtualBox
# distribution, then only the terms of your commercial VirtualBox
# license agreement apply instead of the previous paragraph.
#
# This script creates a new permanent host interface on a Linux system. In
# fact, it does two things: it checks to see if the interface is present in
# the list of permanent interfaces (/etc/vbox/interfaces) and checks to see
# whether it can be created using VBoxTunctl.
interface=$1
user=$2
bridge=$3
appadd="VBoxAddIF"
appdel="VBoxDeleteIF"
echo "VirtualBox host networking interface creation utility, version _VERSION_"
echo "(C) 2005-2007 innotek GmbH"
echo "All rights reserved."
# Print out the correct usage instructions for the utility
then
echo 1>&2 ""
echo 1>&2 "Usage: $appname <interface name> <user name> [<bridge name>]"
echo 1>&2 "Create and register the permanent interface <interface name> for use by user"
echo 1>&2 "<user name> on the host system. Optionally attach the interface to the network"
echo 1>&2 "bridge <bridge name>. <interface name> should take the form vbox<0-99>."
then
echo 1>&2 ""
echo 1>&2 "Usage: $appname <interface name>"
echo 1>&2 "Delete the permanent interface <interface name> from the host system."
else
echo 1>&2 ""
echo 1>&2 "Your VirtualBox setup appears to be incorrect. This utility should be called"
fi
}
then
return 0
else
return 1
fi
}
# Check which name we were called under, and exit if it was not recognised.
then
exit 1
fi
# Check that we have the right number of command line arguments
then
if [ -z "$1" -o -z "$2" -o ! -z "$4" ]
then
exit 1
fi
then
if [ -z "$1" -o ! -z "$2" ]
then
exit 1
fi
fi
# Check that the interface name is valid if we are adding it. If we are
# deleting it then the user will get a better error message later if it is
# invalid as the interface will not exist.
then
if ! valid_ifname "$interface"
then
exit 1
fi
fi
# Make sure that we can create files in the configuration directory
then
echo 1>&2 ""
echo 1>&2 "make sure that you have enough permissions to do this."
exit 1
fi
# Make sure that the configuration file is accessible and that the interface
# is not already registered.
then
# Make sure that the configuration file is read and writable
then
echo 1>&2 ""
echo 1>&2 "This utility must be able to read from and write to the file"
echo 1>&2 "do this."
exit 1
fi
fi
# Parse the configuration file and create a new, updated one.
oldbridge=""
foundif=""
then
while read line
do
set ""$line
# If the line is a comment then ignore it
then
else
# Check that the line is correctly formed (an interface name plus one
# or two non-comment entries, possibly followed by a comment).
then
echo 1>&2 ""
# If the interface to be created is already registered in the file, then
# remove it and remember the fact
elif [ "$1" = "$interface" ]
then
# Remember which bridge the interface was attached to, if any, and
# do not write the line to the new configuration file. Remember that
# we have found the interface in the file.
foundif=1
oldbridge="$3"
else
fi
fi # The line was not a comment
else
# Create the file /etc/vbox/interfaces and add some explanations as comments
echo "# This file is for registering VirtualBox permanent host networking interfaces" > "$tempfile"
echo "# and optionally adding them to network bridges on the host." >> "$tempfile"
echo "# Each line should be of the format <interface name> <user name> [<bridge>]." >> "$tempfile"
echo "" >> "$tempfile"
fi
# Add the new interface line to the file if so requested
then
echo ""
echo "Creating the permanent host networking interface \"$interface\" for user $user."
fi
# Remove the old interface (if it exists) from any bridge it was a part of and
# take the interface down
if [ ! -z "$oldbridge" ]
then
fi
# Delete the old interface if it exists
then
echo 1>&2 ""
echo 1>&2 "Failed to take down the old interface in order to replace it with the new one."
echo 1>&2 "The interface may still be in use, or you may not currently have sufficient"
echo 1>&2 "permissions to do this. You can replace the interface manually using the"
echo 1>&2 "VBoxTunctl command, or alternatively, the new interface will be created"
echo 1>&2 "automatically next time you restart the host system."
exit 1
else
# Create the new interface and bring it up if we are adding it
then
then
echo 1>&2 ""
echo 1>&2 "that you currently have sufficient permissions to do this."
exit 1
fi
# On SUSE Linux Enterprise Server, the tunctl command does not take
# effect at once, so we loop until it does.
i=1
while [ $i -le 10 ]
do
then
i=11
else
i=`expr $i + 1`
sleep .1
fi
done
if [ ! -z "$bridge" ]
then
# And add it to a bridge if this was requested
then
echo 1>&2 ""
echo 1>&2 "Make sure that the bridge exists and that you currently have sufficient"
echo 1>&2 "permissions to do this."
exit 1
fi
fi
fi # $appname = $appadd
fi # VBoxTunctl -d succeeded
then
echo 1>&2 ""
exit 1
fi