1 | # innotek VirtualBox
|
---|
2 | # VirtualBox installer shell routines
|
---|
3 | #
|
---|
4 |
|
---|
5 | # Copyright (C) 2007 innotek GmbH
|
---|
6 | #
|
---|
7 | # innotek GmbH confidential
|
---|
8 | # All rights reserved
|
---|
9 |
|
---|
10 | ro_LOG_FILE=""
|
---|
11 |
|
---|
12 | # Aborts the script and prints an error message to stderr.
|
---|
13 | #
|
---|
14 | # syntax: abort message
|
---|
15 |
|
---|
16 | abort() {
|
---|
17 | echo 1>&2 "$1"
|
---|
18 | exit 1
|
---|
19 | }
|
---|
20 |
|
---|
21 | # Creates an empty log file and remembers the name for future logging
|
---|
22 | # operations
|
---|
23 | #
|
---|
24 | # syntax: create_log logfile
|
---|
25 |
|
---|
26 | create_log() {
|
---|
27 | ro_LOG_FILE="$1"
|
---|
28 | if [ "$ro_LOG_FILE" = "" ]; then
|
---|
29 | abort "create_log called without an argument! Aborting..."
|
---|
30 | fi
|
---|
31 | # Create an empty file
|
---|
32 | echo > "$ro_LOG_FILE" 2> /dev/null
|
---|
33 | if [ ! -f "$ro_LOG_FILE" -o "`cat "$ro_LOG_FILE"`" != "" ]; then
|
---|
34 | abort "Error creating log file! Aborting..."
|
---|
35 | fi
|
---|
36 | }
|
---|
37 |
|
---|
38 | # Writes text to standard error
|
---|
39 | #
|
---|
40 | # Syntax: info text
|
---|
41 |
|
---|
42 | info() {
|
---|
43 | echo 1>&2 "$1"
|
---|
44 | }
|
---|
45 |
|
---|
46 | # Writes text to the log file
|
---|
47 | #
|
---|
48 | # Syntax: log text
|
---|
49 |
|
---|
50 | log() {
|
---|
51 | if [ "$ro_LOG_FILE" = "" ]; then
|
---|
52 | abort "Error! Logging has not been set up yet! Aborting..."
|
---|
53 | fi
|
---|
54 | echo "$1" >> $ro_LOG_FILE
|
---|
55 | }
|
---|
56 |
|
---|
57 | # Checks whether a module is loaded with a given string in its name.
|
---|
58 | #
|
---|
59 | # syntax: module_loaded string
|
---|
60 |
|
---|
61 | module_loaded() {
|
---|
62 | if [ "$1" = "" ]; then
|
---|
63 | log "module_loaded called without an argument. Aborting..."
|
---|
64 | abort "Error in installer. Aborting..."
|
---|
65 | fi
|
---|
66 | lsmod | grep -q $1
|
---|
67 | }
|
---|
68 |
|
---|
69 | # Abort if we are not running as root
|
---|
70 |
|
---|
71 | check_root() {
|
---|
72 | if [ `id -u` -ne 0 ]; then
|
---|
73 | abort "This program must be run with administrator privileges. Aborting"
|
---|
74 | fi
|
---|
75 | }
|
---|
76 |
|
---|
77 | # Do we have bzip2?
|
---|
78 |
|
---|
79 | check_bzip2() {
|
---|
80 | if ! ls /bin/bzip2 /usr/bin/bzip2 /usr/local/bin/bzip2 2> /dev/null | grep bzip2 > /dev/null; then
|
---|
81 | echo 1>&2 "Please install the bzip2 utility."
|
---|
82 | log "Please install bzip2."
|
---|
83 | return 1
|
---|
84 | fi
|
---|
85 | return 0
|
---|
86 | }
|
---|
87 |
|
---|
88 | # Do we have GNU make?
|
---|
89 |
|
---|
90 | check_gmake() {
|
---|
91 | make --version 2>&1 | grep GNU > /dev/null
|
---|
92 | if [ ! $? = 0 ]; then
|
---|
93 | echo 1>&2 "Please install GNU make."
|
---|
94 | log "Please install GNU make."
|
---|
95 | return 1
|
---|
96 | fi
|
---|
97 | return 0
|
---|
98 | }
|
---|
99 |
|
---|
100 | # Can we find the kernel source?
|
---|
101 |
|
---|
102 | check_ksource() {
|
---|
103 | ro_KBUILD_DIR=/lib/modules/`uname -r`/build
|
---|
104 | if [ ! -d $ro_KBUILD_DIR/include ]; then
|
---|
105 | ro_KBUILD_DIR=/usr/src/linux
|
---|
106 | if [ ! -d $ro_KBUILD_DIR/include ]; then
|
---|
107 | echo 1>&2 "Please install the build and header files for your current Linux kernel."
|
---|
108 | echo 1>&2 "The current kernel version is `uname -r`"
|
---|
109 | ro_KBUILD_DIR=""
|
---|
110 | log "Could not find the Linux kernel header files - the directories"
|
---|
111 | log " /lib/modules/`uname -r`/build/include and /usr/src/linux/include"
|
---|
112 | log " do not exist."
|
---|
113 | return 1
|
---|
114 | fi
|
---|
115 | fi
|
---|
116 | return 0
|
---|
117 | }
|
---|
118 |
|
---|
119 | # Is GCC installed?
|
---|
120 |
|
---|
121 | check_gcc() {
|
---|
122 | ro_gcc_version=`gcc --version 2> /dev/null | head -n 1`
|
---|
123 | if [ "$ro_gcc_version" = "" ]; then
|
---|
124 | echo 1>&2 "Please install the GNU compiler."
|
---|
125 | log "Please install the GNU compiler."
|
---|
126 | return 1
|
---|
127 | fi # GCC installed
|
---|
128 | return 0
|
---|
129 | }
|
---|
130 |
|
---|
131 | # Is bash installed? You never know...
|
---|
132 |
|
---|
133 | check_bash() {
|
---|
134 | if [ ! -x /bin/bash ]; then
|
---|
135 | echo 1>&2 "Please install GNU bash."
|
---|
136 | log "Please install GNU bash."
|
---|
137 | return 1
|
---|
138 | fi
|
---|
139 | return 0
|
---|
140 | }
|
---|
141 |
|
---|
142 | # Is perl installed?
|
---|
143 |
|
---|
144 | check_perl() {
|
---|
145 | if [ ! `perl -e 'print "test"' 2> /dev/null` = "test" ]; then
|
---|
146 | echo 1>&2 "Please install perl."
|
---|
147 | echo "Please install perl."
|
---|
148 | return 1
|
---|
149 | fi
|
---|
150 | return 0
|
---|
151 | }
|
---|
152 |
|
---|
153 | # What type of system are we running on?
|
---|
154 |
|
---|
155 | check_system_type() {
|
---|
156 | if [ ! "$ro_SYS_TYPE" = "" ]; then
|
---|
157 | return 0
|
---|
158 | elif [ -f /etc/lfs-release ]; then
|
---|
159 | ro_SYS_TYPE=lfs
|
---|
160 | ro_INIT_TYPE=lfs
|
---|
161 | ro_X11_INIT_DIR=/etc/X11/
|
---|
162 | elif [ -f /etc/redhat-release ]; then
|
---|
163 | ro_SYS_TYPE=redhat
|
---|
164 | ro_INIT_TYPE=sysv
|
---|
165 | ro_X11_INIT_DIR=/etc/X11/xinit/xinitrc.d
|
---|
166 | elif [ -f /etc/SuSE-release ]; then
|
---|
167 | ro_SYS_TYPE=suse
|
---|
168 | ro_INIT_TYPE=sysv
|
---|
169 | ro_X11_INIT_DIR=/etc/X11/xinit/xinitrc.d
|
---|
170 | elif [ -f /etc/debian_version ]; then
|
---|
171 | ro_SYS_TYPE=debian
|
---|
172 | ro_INIT_TYPE=sysv
|
---|
173 | ro_X11_INIT_DIR=/etc/X11/Xsession.d
|
---|
174 | elif [ -f /etc/gentoo-release ]; then
|
---|
175 | ro_SYS_TYPE=gentoo
|
---|
176 | ro_INIT_TYPE=sysv
|
---|
177 | ro_X11_INIT_DIR=
|
---|
178 | elif [ -f /etc/rc.d/rc.local ]; then
|
---|
179 | ro_SYS_TYPE=unknown
|
---|
180 | ro_INIT_TYPE=bsd
|
---|
181 | ro_X11_INIT_DIR=
|
---|
182 | elif [ -d /etc/init.d ]; then
|
---|
183 | ro_SYS_TYPE=unknown
|
---|
184 | ro_INIT_TYPE=sysv
|
---|
185 | ro_X11_INIT_DIR=
|
---|
186 | else # Perhaps we can determine what we need to know anyway though?
|
---|
187 | echo 1>&2 "Unable to determine your Linux distribution"
|
---|
188 | log "Unable to determine the Linux distribution"
|
---|
189 | return 1
|
---|
190 | fi
|
---|
191 | return 0
|
---|
192 | }
|
---|
193 |
|
---|
194 | # Installs a file containing a shell script as an init script
|
---|
195 | #
|
---|
196 | # syntax: install_init_script file name
|
---|
197 | #
|
---|
198 | # where file is the file to be installed and
|
---|
199 | # name is the name of the new init script
|
---|
200 |
|
---|
201 | install_init_script() {
|
---|
202 | check_system_type
|
---|
203 | test $? -ne 1 || return 1
|
---|
204 | test -r "$1" || return 1
|
---|
205 | test ! "$2" = "" || \
|
---|
206 | { log "install_init_script: invalid arguments" && return 1; }
|
---|
207 | if [ "$ro_INIT_TYPE" = "lfs" ]; then
|
---|
208 | cp "$1" "/etc/rc.d/init.d/$2" 2> /dev/null
|
---|
209 | chmod 755 "/etc/rc.d/init.d/$2" 2> /dev/null
|
---|
210 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
211 | cp "$1" "/etc/init.d/$2" 2> /dev/null
|
---|
212 | chmod 755 "/etc/init.d/$2" 2> /dev/null
|
---|
213 | elif [ "$ro_INIT_TYPE" = "bsd" ]; then
|
---|
214 | cp "$1" "/etc/rc.d/rc.$2" 2> /dev/null
|
---|
215 | chmod 755 "/etc/rc.d/rc.$2" 2> /dev/null
|
---|
216 | else
|
---|
217 | log "install_init_script: error: unknown init type"
|
---|
218 | return 1
|
---|
219 | fi
|
---|
220 | return 0
|
---|
221 | }
|
---|
222 |
|
---|
223 | # Remove the init script "name"
|
---|
224 | #
|
---|
225 | # syntax: remove_init_script name
|
---|
226 |
|
---|
227 | remove_init_script() {
|
---|
228 | check_system_type
|
---|
229 | test $? -ne 1 || return 1
|
---|
230 | test ! "$1" = "" || \
|
---|
231 | { log "remove_init_script: missing argument" && return 1; }
|
---|
232 | if [ "$ro_INIT_TYPE" = "lfs" ]; then
|
---|
233 | rm -f "/etc/rc.d/init.d/$1" > /dev/null 2>&1
|
---|
234 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
235 | rm -f "/etc/init.d/$1" > /dev/null 2>&1
|
---|
236 | elif [ "$ro_INIT_TYPE" = "bsd" ]; then
|
---|
237 | rm -f "/etc/rc.d/rc.$1" > /dev/null 2>&1
|
---|
238 | else
|
---|
239 | log "remove_init_script: error: unknown init type"
|
---|
240 | return 1
|
---|
241 | fi
|
---|
242 | return 0
|
---|
243 | }
|
---|
244 |
|
---|
245 | # Start the init script "name"
|
---|
246 | #
|
---|
247 | # syntax: start_init_script name
|
---|
248 |
|
---|
249 | start_init_script() {
|
---|
250 | check_system_type
|
---|
251 | test $? -ne 1 || return 1
|
---|
252 | test ! -z "$1" || \
|
---|
253 | { log "start_init_script: missing argument" && return 1; }
|
---|
254 | if [ "$ro_INIT_TYPE" = "lfs" ]; then
|
---|
255 | "/etc/rc.d/init.d/$1" start > /dev/null 2>&1
|
---|
256 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
257 | "/etc/init.d/$1" start > /dev/null 2>&1
|
---|
258 | elif [ "$ro_INIT_TYPE" = "bsd" ]; then
|
---|
259 | "/etc/rc.d/rc.$1" start > /dev/null 2>&1
|
---|
260 | else
|
---|
261 | log "start_init_script: error: unknown init type"
|
---|
262 | return 0
|
---|
263 | fi
|
---|
264 | }
|
---|
265 |
|
---|
266 | # Stop the init script "name"
|
---|
267 | #
|
---|
268 | # syntax: start_init_script name
|
---|
269 |
|
---|
270 | stop_init_script() {
|
---|
271 | check_system_type
|
---|
272 | test $? -ne 1 || return 1
|
---|
273 | test ! -z "$1" || \
|
---|
274 | { log "stop_init_script: missing argument" && return 1; }
|
---|
275 | if [ "$ro_INIT_TYPE" = "lfs" ]; then
|
---|
276 | "/etc/init.d/$1" stop > /dev/null 2>&1
|
---|
277 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
278 | "/etc/init.d/$1" stop > /dev/null 2>&1
|
---|
279 | elif [ "$ro_INIT_TYPE" = "bsd" ]; then
|
---|
280 | "/etc/rc.d/rc.$1" stop > /dev/null 2>&1
|
---|
281 | else
|
---|
282 | log "stop_init_script: error: unknown init type"
|
---|
283 | return 1
|
---|
284 | fi
|
---|
285 | return 0
|
---|
286 | }
|
---|
287 |
|
---|
288 | # Add a service to a runlevel
|
---|
289 | #
|
---|
290 | # syntax: addrunlevel name stop_order start_order
|
---|
291 |
|
---|
292 | addrunlevel() {
|
---|
293 | test ! -z "$1" || \
|
---|
294 | { log "addrunlevel: missing argument(s)" && return 1; }
|
---|
295 | check_system_type
|
---|
296 | # SUSE and Redhat based systems
|
---|
297 | if [ "$ro_SYS_TYPE" = "redhat" ] || [ "$ro_SYS_TYPE" = "suse" ]; then
|
---|
298 | test -x "/sbin/chkconfig" || \
|
---|
299 | { log "addrunlevel: /sbin/chkconfig not found" && return 1; }
|
---|
300 | /sbin/chkconfig --del $1 > /dev/null 2>&1
|
---|
301 |
|
---|
302 | if /sbin/chkconfig -v > /dev/null 2>&1; then
|
---|
303 | /sbin/chkconfig --level 35 $1 on || {
|
---|
304 | log "Cannot add $1 to run levels: 35" && return 1
|
---|
305 | }
|
---|
306 | else
|
---|
307 | /sbin/chkconfig $1 35 || {
|
---|
308 | log "Cannot add $1 to run levels: 35" && return 1
|
---|
309 | }
|
---|
310 | fi
|
---|
311 | # Debian/Ubuntu-based systems
|
---|
312 | elif [ "$ro_SYS_TYPE" = "debian" ]; then
|
---|
313 | test -x `which update-rc.d` || \
|
---|
314 | { log "addrunlevel: update-rc.d not found" && return 1; }
|
---|
315 | test ! -z "$2" || \
|
---|
316 | { log "addrunlevel: missing second argument" && return 1; }
|
---|
317 | test ! -z "$3" || \
|
---|
318 | { log "addrunlevel: missing third argument" && return 1; }
|
---|
319 | # Debian does not support dependencies currently -- use argument $2
|
---|
320 | # for start sequence number and argument $3 for stop sequence number
|
---|
321 | update-rc.d -f $1 remove > /dev/null 2>&1
|
---|
322 | update-rc.d $1 defaults $2 $3 > /dev/null 2>&1
|
---|
323 | # Gentoo Linux
|
---|
324 | elif [ "$ro_SYS_TYPE" = "gentoo" ]; then
|
---|
325 | test -x `which rc-update` || \
|
---|
326 | { log "addrunlevel: update-rc.d not found" && return 1; }
|
---|
327 | rc-update del $1 > /dev/null 2>&1
|
---|
328 | rc-update add $1 default > /dev/null 2>&1
|
---|
329 | # BSD-based systems require changing the rc.local file to start a new service.
|
---|
330 | # We do not want to do that sort of thing automatically, so we tell the user to do it.
|
---|
331 | elif [ "$ro_INIT_TYPE" = "bsd" ]; then
|
---|
332 | echo 1>&2 "Please add the following to /etc/rc.d/rc.local :"
|
---|
333 | echo 1>&2 ""
|
---|
334 | echo 1>&2 " # Start $1"
|
---|
335 | echo 1>&2 " if [ -x /etc/rc.d/rc.$1 ]; then"
|
---|
336 | echo 1>&2 " /etc/rc.d/rc.$1 start"
|
---|
337 | echo 1>&2 " fi"
|
---|
338 | echo 1>&2 ""
|
---|
339 | # Probably most unknown Linux systems will be sysv type ones. These can theoretically
|
---|
340 | # be handled automatically if people give us information about them.
|
---|
341 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
342 | echo 1>&2 "As our installer does not recognize your Linux distribution, we were unable to"
|
---|
343 | echo 1>&2 "set up the initialization script $1 correctly. The script has been copied"
|
---|
344 | echo 1>&2 "copied to the /etc/init.d directory. You should set up your system to start"
|
---|
345 | echo 1>&2 "it at system start, or start it manually before using VirtualBox."
|
---|
346 | echo 1>&2 ""
|
---|
347 | echo 1>&2 "If you would like to help us add support for your distribution, please open a"
|
---|
348 | echo 1>&2 "new ticket on http://www.virtualbox.org/wiki/Bugtracker."
|
---|
349 | fi
|
---|
350 | return 0
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | # Delete a service from a runlevel
|
---|
355 | #
|
---|
356 | # syntax: delrunlevel name stop_order start_order
|
---|
357 |
|
---|
358 | delrunlevel() {
|
---|
359 | test ! -z "$1" || \
|
---|
360 | { log "delrunlevel: missing argument" && return 1; }
|
---|
361 | check_system_type
|
---|
362 | # Redhat/SUSE-based systems
|
---|
363 | if [ "$ro_SYS_TYPE" = "redhat" ] || [ "$system" = "suse" ]; then
|
---|
364 | test -x "/sbin/chkconfig" || \
|
---|
365 | { log "addrunlevel: /sbin/chkconfig not found" && return 1; }
|
---|
366 | if /sbin/chkconfig --list $1 > /dev/null 2>&1; then
|
---|
367 | /sbin/chkconfig --del $1 > /dev/null 2>&1 || {
|
---|
368 | log "Cannot delete $1 from runlevels" && return 1
|
---|
369 | }
|
---|
370 | fi
|
---|
371 | # Debian/Ubuntu-based systems
|
---|
372 | elif [ "$ro_SYS_TYPE" = "debian" ]; then
|
---|
373 | test -x `which update-rc.d` || \
|
---|
374 | { log "addrunlevel: update-rc.d not found" && return 1; }
|
---|
375 | update-rc.d -f $1 remove > /dev/null 2>&1
|
---|
376 | # Gentoo Linux
|
---|
377 | elif [ "$ro_SYS_TYPE" = "gentoo" ]; then
|
---|
378 | test -x `which rc-update` || \
|
---|
379 | { log "addrunlevel: update-rc.d not found" && return 1; }
|
---|
380 | rc-update del "$1" > /dev/null 2>&1
|
---|
381 | # Unknown sysv-type system
|
---|
382 | elif [ "$ro_INIT_TYPE" = "sysv" ]; then
|
---|
383 | echo 1>&2 "Please remove remove references to the initialization script"
|
---|
384 | echo 1>&2 "/etc/init.d/$1 to complete the uninstallation."
|
---|
385 | fi
|
---|
386 | # BSD-type systems will just not start the script if it is not there.
|
---|
387 | # Assume that BSD users understand their system.
|
---|
388 | return 0
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | # Install an X11 desktop startup application. The application should be a shell script.
|
---|
393 | # Its name should be purely alphanumeric and should start with a two digit number (preferably
|
---|
394 | # 98 or thereabouts) to indicate its place in the Debian Xsession startup order.
|
---|
395 | #
|
---|
396 | # syntax: install_x11_startup_app file service_name
|
---|
397 |
|
---|
398 | install_x11_startup_app() {
|
---|
399 | test -r $1 || \
|
---|
400 | { log "install_x11_startup_app: no arguments given" && return 1; }
|
---|
401 | check_system_type
|
---|
402 | if [ "$ro_X11_INIT_DIR" = "" ]; then
|
---|
403 | echo 1>&2 "The $2 service was not set up, as we do not know of a way to create"
|
---|
404 | echo 1>&2 "X Window System startup services on your distribution. If you know"
|
---|
405 | echo 1>&2 "of a way to do this and wish to help us support your distribution, please"
|
---|
406 | echo 1>&2 "open a new ticket on http://www.virtualbox.org/wiki/Bugtracker."
|
---|
407 | echo 1>&2 ""
|
---|
408 | echo 1>&2 "To start the $2 service at log in for a given user, add the command"
|
---|
409 | echo 1>&2 "$1 to the file .xinitrc in their home directory."
|
---|
410 | fi
|
---|
411 | if [ "$ro_SYS_TYPE" = "debian" ]; then
|
---|
412 | dest=`basename $1`
|
---|
413 | log "install_x11_startup_app(): dest is $dest"
|
---|
414 | elif [ "$ro_SYS_TYPE" = "redhat" -o "$ro_SYS_TYPE" = "suse" ]; then
|
---|
415 | dest=`basename $1`.sh
|
---|
416 | log "install_x11_startup_app(): dest is $dest"
|
---|
417 | fi
|
---|
418 | if [ -d "$ro_X11_INIT_DIR" -a -w "$ro_X11_INIT_DIR" ]; then
|
---|
419 | log "install_x11_startup_app(): executing install -m 0755 $1 $ro_X11_INIT_DIR/$dest"
|
---|
420 | install -m 0755 $1 "$ro_X11_INIT_DIR/$dest"
|
---|
421 | return 0
|
---|
422 | else
|
---|
423 | echo 1>&2 "Could not find or did not have permissions for the X Window System service"
|
---|
424 | echo 1>&2 "directory $ro_X11_INIT_DIR to set up the $2 service."
|
---|
425 | echo 1>&2 "To start the $2 service at log-in for a given user, add the command"
|
---|
426 | echo 1>&2 "$1 to the file .xinitrc in their home directory."
|
---|
427 | return 1
|
---|
428 | fi
|
---|
429 | }
|
---|