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