VirtualBox

Ticket #758: routines.2.sh

File routines.2.sh, 16.3 KB (added by John Gnew, 15 years ago)

Corrected for Linux From Scratch install

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy