VirtualBox

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

Last change on this file since 3907 was 3907, checked in by vboxsync, 18 years ago

Fixed a problem with host networking on SLES 10.1 whereby the interfaces were not brought up properly

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