VirtualBox

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

Last change on this file since 7692 was 7192, checked in by vboxsync, 17 years ago

shell scripts are eol-style LF

  • Property svn:eol-style set to LF
File size: 7.9 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 (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
23appname=`basename $0`
24interface=$1
25user=$2
26if [ "$user" = "-g" ]; then
27 shift;
28 group=$2
29 user=+$group
30fi
31bridge=$3
32
33appadd="VBoxAddIF"
34appdel="VBoxDeleteIF"
35
36echo "VirtualBox host networking interface creation utility, version _VERSION_"
37echo "(C) 2005-2007 innotek GmbH"
38echo "All rights reserved."
39
40# Print out the correct usage instructions for the utility
41usage() {
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.
64if [ ! "$appname" = "$appadd" -a ! "$appname" = "$appdel" ]
65then
66 usage
67 exit 1
68fi
69
70# Check that we have the right number of command line arguments
71if [ "$appname" = "$appadd" ]
72then
73 if [ -z "$1" -o -z "$2" -o ! -z "$4" ]
74 then
75 usage
76 exit 1
77 fi
78elif [ "$appname" = "$appdel" ]
79then
80 if [ -z "$1" -o ! -z "$2" ]
81 then
82 usage
83 exit 1
84 fi
85fi
86
87# Make sure that we can create files in the configuration directory
88if [ ! -r /etc/vbox -o ! -w /etc/vbox -o ! -x /etc/vbox ]
89then
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
94fi
95
96# Make sure that the configuration file is accessible and that the interface
97# is not already registered.
98if [ -f /etc/vbox/interfaces ]
99then
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
109fi
110
111# Parse the configuration file and create a new, updated one.
112oldbridge=""
113foundif=""
114tempfile=/etc/vbox/interfaces.tmp
115rm -f "$tempfile"
116if [ -f /etc/vbox/interfaces ]
117then
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
147else
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"
153fi
154mv -f "$tempfile" /etc/vbox/interfaces
155
156# Add the new interface line to the file if so requested
157if [ "$appname" = "$appadd" ]
158then
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
166fi
167
168# Remove the old interface (if it exists) from any bridge it was a part of and
169# take the interface down
170if [ ! -z "$oldbridge" ]
171then
172 brctl delif "$oldbridge" "$interface" > /dev/null 2>&1
173fi
174ifconfig "$interface" down > /dev/null 2>&1
175
176# Delete the old interface if it exists
177if ! VBoxTunctl -d "$interface" > /dev/null 2>&1
178then
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
186else
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
234fi # VBoxTunctl -d succeeded
235
236if [ "$appname" = "$appdel" -a -z "$foundif" ]
237then
238 echo 1>&2 ""
239 echo 1>&2 "Warning: the utility could not find the registered interface \"$interface\"."
240 exit 1
241fi
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