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