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