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