VirtualBox

source: vbox/trunk/src/VBox/Installer/linux/VBoxAddIF.sh@ 5999

Last change on this file since 5999 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

File size: 7.7 KB
Line 
1#!/bin/sh
2#
3# innotek VirtualBox
4# Permanent host interface creation script for Linux systems.
5
6#
7# Copyright (C) 2006-2007 innotek GmbH
8#
9# This file is part of VirtualBox Open Source Edition (OSE), as
10# available from http://www.virtualbox.org. This file is free software;
11# you can redistribute it and/or modify it under the terms of the GNU
12# General Public License (GPL) as published by the Free Software
13# Foundation, in version 2 as it comes in the "COPYING" file of the
14# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16#
17
18# This script creates a new permanent host interface on a Linux system. In
19# fact, it does two things: it checks to see if the interface is present in
20# the list of permanent interfaces (/etc/vbox/interfaces) and checks to see
21# whether it can be created using VBoxTunctl.
22
23appname=`basename $0`
24interface=$1
25user=$2
26bridge=$3
27
28appadd="VBoxAddIF"
29appdel="VBoxDeleteIF"
30
31echo "VirtualBox host networking interface creation utility, version _VERSION_"
32echo "(C) 2005-2007 innotek GmbH"
33echo "All rights reserved."
34
35# Print out the correct usage instructions for the utility
36usage() {
37 if [ "$appname" = "$appadd" ]
38 then
39 echo 1>&2 ""
40 echo 1>&2 "Usage: $appname <interface name> <user name> [<bridge name>]"
41 echo 1>&2 "Create and register the permanent interface <interface name> for use by user"
42 echo 1>&2 "<user name> on the host system. Optionally attach the interface to the network"
43 echo 1>&2 "bridge <bridge name>. <interface name> should take the form vbox<0-99>."
44 elif [ "$appname" = "$appdel" ]
45 then
46 echo 1>&2 ""
47 echo 1>&2 "Usage: $appname <interface name>"
48 echo 1>&2 "Delete the permanent interface <interface name> from the host system."
49 else
50 echo 1>&2 ""
51 echo 1>&2 "Your VirtualBox setup appears to be incorrect. This utility should be called"
52 echo 1>&2 "$appadd or $appdel."
53 fi
54}
55
56valid_ifname() {
57 if expr match "$1" "vbox[0-9][0-9]*$" > /dev/null 2>&1
58 then
59 return 0
60 else
61 return 1
62 fi
63}
64
65# Check which name we were called under, and exit if it was not recognised.
66if [ ! "$appname" = "$appadd" -a ! "$appname" = "$appdel" ]
67then
68 usage
69 exit 1
70fi
71
72# Check that we have the right number of command line arguments
73if [ "$appname" = "$appadd" ]
74then
75 if [ -z "$1" -o -z "$2" -o ! -z "$4" ]
76 then
77 usage
78 exit 1
79 fi
80elif [ "$appname" = "$appdel" ]
81then
82 if [ -z "$1" -o ! -z "$2" ]
83 then
84 usage
85 exit 1
86 fi
87fi
88# Check that the interface name is valid if we are adding it. If we are
89# deleting it then the user will get a better error message later if it is
90# invalid as the interface will not exist.
91if [ "$appname" = "$appadd" ]
92then
93 if ! valid_ifname "$interface"
94 then
95 usage
96 exit 1
97 fi
98fi
99
100
101# Make sure that we can create files in the configuration directory
102if [ ! -r /etc/vbox -o ! -w /etc/vbox -o ! -x /etc/vbox ]
103then
104 echo 1>&2 ""
105 echo 1>&2 "This utility must be able to access the folder /etc/vbox/. Please"
106 echo 1>&2 "make sure that you have enough permissions to do this."
107 exit 1
108fi
109
110# Make sure that the configuration file is accessible and that the interface
111# is not already registered.
112if [ -f /etc/vbox/interfaces ]
113then
114 # Make sure that the configuration file is read and writable
115 if [ ! -r /etc/vbox/interfaces -o ! -w /etc/vbox/interfaces ]
116 then
117 echo 1>&2 ""
118 echo 1>&2 "This utility must be able to read from and write to the file"
119 echo 1>&2 "/etc/vbox/interfaces. Please make sure that you have enough permissions to"
120 echo 1>&2 "do this."
121 exit 1
122 fi
123fi
124
125# Parse the configuration file and create a new, updated one.
126oldbridge=""
127foundif=""
128tempfile=/etc/vbox/interfaces.tmp
129rm -f "$tempfile"
130if [ -f /etc/vbox/interfaces ]
131then
132 while read line
133 do
134 set ""$line
135 # If the line is a comment then ignore it
136 if (expr match "$1" "#" > /dev/null || test -z "$1")
137 then
138 echo ""$line >> "$tempfile"
139 else
140 # Check that the line is correctly formed (an interface name plus one
141 # or two non-comment entries, possibly followed by a comment).
142 if ((expr match "$2" "#" > /dev/null) ||
143 (! test -z "$4" && ! expr match "$4" "#" > /dev/null) ||
144 (! valid_ifname "$1"))
145 then
146 echo 1>&2 ""
147 echo 1>&2 "Removing badly formed line $line in /etc/vbox/interfaces."
148 # If the interface to be created is already registered in the file, then
149 # remove it and remember the fact
150 elif [ "$1" = "$interface" ]
151 then
152 # Remember which bridge the interface was attached to, if any, and
153 # do not write the line to the new configuration file. Remember that
154 # we have found the interface in the file.
155 foundif=1
156 oldbridge="$3"
157 else
158 echo ""$line >> "$tempfile"
159 fi
160 fi # The line was not a comment
161 done < /etc/vbox/interfaces
162else
163 # Create the file /etc/vbox/interfaces and add some explanations as comments
164 echo "# This file is for registering VirtualBox permanent host networking interfaces" > "$tempfile"
165 echo "# and optionally adding them to network bridges on the host." >> "$tempfile"
166 echo "# Each line should be of the format <interface name> <user name> [<bridge>]." >> "$tempfile"
167 echo "" >> "$tempfile"
168fi
169mv -f "$tempfile" /etc/vbox/interfaces
170
171# Add the new interface line to the file if so requested
172if [ "$appname" = "$appadd" ]
173then
174 echo "$interface" "$user" "$bridge" >> /etc/vbox/interfaces
175 echo ""
176 echo "Creating the permanent host networking interface \"$interface\" for user $user."
177fi
178
179# Remove the old interface (if it exists) from any bridge it was a part of and
180# take the interface down
181if [ ! -z "$oldbridge" ]
182then
183 brctl delif "$oldbridge" "$interface" > /dev/null 2>&1
184fi
185ifconfig "$interface" down > /dev/null 2>&1
186
187# Delete the old interface if it exists
188if ! VBoxTunctl -d "$interface" > /dev/null 2>&1
189then
190 echo 1>&2 ""
191 echo 1>&2 "Failed to take down the old interface in order to replace it with the new one."
192 echo 1>&2 "The interface may still be in use, or you may not currently have sufficient"
193 echo 1>&2 "permissions to do this. You can replace the interface manually using the"
194 echo 1>&2 "VBoxTunctl command, or alternatively, the new interface will be created"
195 echo 1>&2 "automatically next time you restart the host system."
196 exit 1
197else
198 # Create the new interface and bring it up if we are adding it
199 if [ "$appname" = "$appadd" ]
200 then
201 if ! VBoxTunctl -t "$interface" -u "$user" > /dev/null 2>&1
202 then
203 echo 1>&2 ""
204 echo 1>&2 "Failed to create the interface \"$interface\" for user $user. Please check"
205 echo 1>&2 "that you currently have sufficient permissions to do this."
206 exit 1
207 fi
208 # On SUSE Linux Enterprise Server, the tunctl command does not take
209 # effect at once, so we loop until it does.
210 i=1
211 while [ $i -le 10 ]
212 do
213 ifconfig "$interface" up > /dev/null 2>&1
214 if ifconfig | grep "$interface" up > /dev/null 2>&1
215 then
216 i=11
217 else
218 i=`expr $i + 1`
219 sleep .1
220 fi
221 done
222 if [ ! -z "$bridge" ]
223 then
224 # And add it to a bridge if this was requested
225 if ! brctl addif "$bridge" "$interface" > /dev/null 2>&1
226 then
227 echo 1>&2 ""
228 echo 1>&2 "Failed to add the interface \"$interface\" to the bridge \"$bridge\"."
229 echo 1>&2 "Make sure that the bridge exists and that you currently have sufficient"
230 echo 1>&2 "permissions to do this."
231 exit 1
232 fi
233 fi
234 fi # $appname = $appadd
235fi # VBoxTunctl -d succeeded
236
237if [ "$appname" = "$appdel" -a -z "$foundif" ]
238then
239 echo 1>&2 ""
240 echo 1>&2 "Warning: the utility could not find the registered interface \"$interface\"."
241 exit 1
242fi
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette