1 | #!/bin/bash
|
---|
2 | ## @file
|
---|
3 | # Post installation script template for redhat- distros.
|
---|
4 | #
|
---|
5 | # Note! This script expects to be running chrooted (inside new sytem).
|
---|
6 | #
|
---|
7 |
|
---|
8 | #
|
---|
9 | # Copyright (C) 2017 Oracle Corporation
|
---|
10 | #
|
---|
11 | # This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | # available from http://www.virtualbox.org. This file is free software;
|
---|
13 | # you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | # General Public License (GPL) as published by the Free Software
|
---|
15 | # Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | # VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | # hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | #
|
---|
19 |
|
---|
20 |
|
---|
21 | #
|
---|
22 | # Globals.
|
---|
23 | #
|
---|
24 | MY_TARGET="/mnt/sysimage"
|
---|
25 | MY_LOGFILE="${MY_TARGET}/var/log/vboxpostinstall.log"
|
---|
26 | MY_CHROOT_CDROM="/cdrom"
|
---|
27 | MY_CDROM_NOCHROOT="/tmp/vboxcdrom"
|
---|
28 | MY_EXITCODE=0
|
---|
29 | MY_DEBUG="" # "yes"
|
---|
30 |
|
---|
31 |
|
---|
32 | #
|
---|
33 | # Do we need to exec using target bash? If so, we must do that early
|
---|
34 | # or ash will bark 'bad substitution' and fail.
|
---|
35 | #
|
---|
36 | if [ "$1" = "--need-target-bash" ]; then
|
---|
37 | # Try figure out which directories we might need in the library path.
|
---|
38 | if [ -z "${LD_LIBRARY_PATH}" ]; then
|
---|
39 | LD_LIBRARY_PATH="${MY_TARGET}/lib"
|
---|
40 | fi
|
---|
41 | for x in \
|
---|
42 | ${MY_TARGET}/lib \
|
---|
43 | ${MY_TARGET}/usr/lib \
|
---|
44 | ${MY_TARGET}/lib/*linux-gnu/ \
|
---|
45 | ${MY_TARGET}/lib32/ \
|
---|
46 | ${MY_TARGET}/lib64/ \
|
---|
47 | ${MY_TARGET}/usr/lib/*linux-gnu/ \
|
---|
48 | ${MY_TARGET}/usr/lib32/ \
|
---|
49 | ${MY_TARGET}/usr/lib64/ \
|
---|
50 | ;
|
---|
51 | do
|
---|
52 | if [ -e "$x" ]; then LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${x}"; fi;
|
---|
53 | done
|
---|
54 | export LD_LIBRARY_PATH
|
---|
55 |
|
---|
56 | # Append target bin directories to the PATH as busybox may not have tee.
|
---|
57 | PATH="${PATH}:${MY_TARGET}/bin:${MY_TARGET}/usr/bin:${MY_TARGET}/sbin:${MY_TARGET}/usr/sbin"
|
---|
58 | export PATH
|
---|
59 |
|
---|
60 | # Drop the --need-target-bash argument and re-exec.
|
---|
61 | shift
|
---|
62 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
63 | echo "** Relaunching using ${MY_TARGET}/bin/bash $0 $*" >> "${MY_LOGFILE}"
|
---|
64 | echo "** LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> "${MY_LOGFILE}"
|
---|
65 | echo "** PATH=${PATH}" >> "${MY_LOGFILE}"
|
---|
66 | exec "${MY_TARGET}/bin/bash" "$0" "$@"
|
---|
67 | fi
|
---|
68 |
|
---|
69 |
|
---|
70 | #
|
---|
71 | # Commands.
|
---|
72 | #
|
---|
73 |
|
---|
74 | # Logs execution of a command.
|
---|
75 | log_command()
|
---|
76 | {
|
---|
77 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
78 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
79 | echo "** Executing: $*" >> "${MY_LOGFILE}"
|
---|
80 | "$@" 2>&1 | tee -a "${MY_LOGFILE}"
|
---|
81 | MY_TMP_EXITCODE="${PIPESTATUS[0]}" # bashism - whatever.
|
---|
82 | if [ "${MY_TMP_EXITCODE}" != "0" ]; then
|
---|
83 | if [ "${MY_TMP_EXITCODE}" != "${MY_IGNORE_EXITCODE}" ]; then
|
---|
84 | echo "** exit code: ${MY_TMP_EXITCODE}" | tee -a "${MY_LOGFILE}"
|
---|
85 | MY_EXITCODE=1;
|
---|
86 | else
|
---|
87 | echo "** exit code: ${MY_TMP_EXITCODE} (ignored)" | tee -a "${MY_LOGFILE}"
|
---|
88 | fi
|
---|
89 | fi
|
---|
90 | }
|
---|
91 |
|
---|
92 | # Logs execution of a command inside the target.
|
---|
93 | log_command_in_target()
|
---|
94 | {
|
---|
95 | log_command chroot "${MY_TARGET}" "$@"
|
---|
96 | }
|
---|
97 |
|
---|
98 | # Checks if $1 is a command on the PATH inside the target jail.
|
---|
99 | chroot_which()
|
---|
100 | {
|
---|
101 | for dir in /bin /usr/bin /sbin /usr/sbin;
|
---|
102 | do
|
---|
103 | if [ -x "${MY_TARGET}${dir}/$1" ]; then
|
---|
104 | return 0;
|
---|
105 | fi
|
---|
106 | done
|
---|
107 | return 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #
|
---|
111 | # Log header.
|
---|
112 | #
|
---|
113 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
114 | echo "** VirtualBox Unattended Guest Installation - Late installation actions" >> "${MY_LOGFILE}"
|
---|
115 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
116 | echo "** Started: $0 $*" >> "${MY_LOGFILE}"
|
---|
117 |
|
---|
118 |
|
---|
119 | #
|
---|
120 | # We want the ISO available inside the target jail.
|
---|
121 | #
|
---|
122 | if [ -d "${MY_TARGET}${MY_CHROOT_CDROM}" ]; then
|
---|
123 | MY_RMDIR_TARGET_CDROM=
|
---|
124 | else
|
---|
125 | MY_RMDIR_TARGET_CDROM="yes"
|
---|
126 | log_command mkdir -p ${MY_TARGET}${MY_CHROOT_CDROM}
|
---|
127 | fi
|
---|
128 |
|
---|
129 | if [ -f "${MY_TARGET}${MY_CHROOT_CDROM}/vboxpostinstall.sh" ]; then
|
---|
130 | MY_UNMOUNT_TARGET_CDROM=
|
---|
131 | echo "** binding cdrom into jail: already done" | tee -a "${MY_LOGFILE}"
|
---|
132 | else
|
---|
133 | MY_UNMOUNT_TARGET_CDROM="yes"
|
---|
134 | log_command mount -o bind "${MY_CDROM_NOCHROOT}" "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
135 | if [ -f "${MY_TARGET}${MY_CHROOT_CDROM}/vboxpostinstall.sh" ]; then
|
---|
136 | echo "** binding cdrom into jail: success" | tee -a "${MY_LOGFILE}"
|
---|
137 | else
|
---|
138 | echo "** binding cdrom into jail: failed" | tee -a "${MY_LOGFILE}"
|
---|
139 | fi
|
---|
140 | if [ "${MY_DEBUG}" = "yes" ]; then
|
---|
141 | log_command find "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
142 | fi
|
---|
143 | fi
|
---|
144 |
|
---|
145 |
|
---|
146 | #
|
---|
147 | # Debug
|
---|
148 | #
|
---|
149 | if [ "${MY_DEBUG}" = "yes" ]; then
|
---|
150 | log_command id
|
---|
151 | log_command ps
|
---|
152 | log_command ps auxwwwf
|
---|
153 | log_command env
|
---|
154 | log_command df
|
---|
155 | log_command mount
|
---|
156 | log_command_in_target df
|
---|
157 | log_command_in_target mount
|
---|
158 | #log_command find /
|
---|
159 | MY_EXITCODE=0
|
---|
160 | fi
|
---|
161 |
|
---|
162 |
|
---|
163 | #
|
---|
164 | # Packages needed for GAs.
|
---|
165 | #
|
---|
166 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
167 | echo '** Installing packages for building kernel modules...' | tee -a "${MY_LOGFILE}"
|
---|
168 | log_command_in_target yum -y install "kernel-devel-$(uname -r)"
|
---|
169 | log_command_in_target yum -y install "kernel-headers-$(uname -r)"
|
---|
170 | log_command_in_target yum -y install gcc
|
---|
171 | log_command_in_target yum -y install binutils
|
---|
172 | log_command_in_target yum -y install make
|
---|
173 | log_command_in_target yum -y install dkms
|
---|
174 | log_command_in_target yum -y install make
|
---|
175 | log_command_in_target yum -y install bzip2
|
---|
176 | log_command_in_target yum -y install perl
|
---|
177 |
|
---|
178 |
|
---|
179 | #
|
---|
180 | # GAs
|
---|
181 | #
|
---|
182 | @@VBOX_COND_IS_INSTALLING_ADDITIONS@@
|
---|
183 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
184 | echo '** Installing VirtualBox Guest Additions...' | tee -a "${MY_LOGFILE}"
|
---|
185 | MY_IGNORE_EXITCODE=2 # returned if modules already loaded and reboot required.
|
---|
186 | log_command_in_target /bin/bash "${MY_CHROOT_CDROM}/vboxadditions/VBoxLinuxAdditions.run" --nox11
|
---|
187 | MY_IGNORE_EXITCODE=
|
---|
188 | log_command_in_target usermod -a -G vboxsf "@@VBOX_INSERT_USER_LOGIN@@"
|
---|
189 | @@VBOX_COND_END@@
|
---|
190 |
|
---|
191 |
|
---|
192 | #
|
---|
193 | # Test Execution Service.
|
---|
194 | #
|
---|
195 | @@VBOX_COND_IS_INSTALLING_TEST_EXEC_SERVICE@@
|
---|
196 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
197 | echo '** Installing Test Execution Service...' | tee -a "${MY_LOGFILE}"
|
---|
198 | log_command_in_target test "${MY_CHROOT_CDROM}/vboxvalidationkit/linux/@@VBOX_INSERT_OS_ARCH@@/TestExecService"
|
---|
199 | log_command mkdir -p "${MY_TARGET}/opt/validationkit" "${MY_TARGET}/media/cdrom"
|
---|
200 | log_command cp -R ${MY_CDROM_NOCHROOT}/vboxvalidationkit/* "${MY_TARGET}/opt/validationkit/"
|
---|
201 | log_command chmod -R u+rw,a+xr "${MY_TARGET}/opt/validationkit/"
|
---|
202 | if [ -e "${MY_TARGET}/usr/bin/chcon" -o -e "${MY_TARGET}/bin/chcon" -o -e "${MY_TARGET}/usr/sbin/chcon" -o -e "${MY_TARGET}/sbin/chcon" ]; then
|
---|
203 | MY_IGNORE_EXITCODE=1
|
---|
204 | log_command_in_target chcon -R -t usr_t "/opt/validationkit/"
|
---|
205 | MY_IGNORE_EXITCODE=
|
---|
206 | fi
|
---|
207 |
|
---|
208 | # systemd service config:
|
---|
209 | MY_UNIT_PATH="${MY_TARGET}/lib/systemd/system"
|
---|
210 | test -d "${MY_TARGET}/usr/lib/systemd/system" && MY_UNIT_PATH="${MY_TARGET}/usr/lib/systemd/system"
|
---|
211 | if [ -d "${MY_UNIT_PATH}" ]; then
|
---|
212 | log_command cp "${MY_TARGET}/opt/validationkit/linux/vboxtxs.service" "${MY_UNIT_PATH}/vboxtxs.service"
|
---|
213 | log_command chmod 644 "${MY_UNIT_PATH}/vboxtxs.service"
|
---|
214 | log_command_in_target systemctl -q enable vboxtxs
|
---|
215 |
|
---|
216 | # System V like:
|
---|
217 | elif [ -e "${MY_TARGET}/etc/init.d/" ]; then
|
---|
218 |
|
---|
219 | # Install the script. On rhel6 scripts are under /etc/rc.d/ with /etc/init.d and /etc/rc?.d being symlinks.
|
---|
220 | if [ -d "${MY_TARGET}/etc/rc.d/init.d/" ]; then
|
---|
221 | MY_INIT_D_PARENT_PATH="${MY_TARGET}/etc/rc.d"
|
---|
222 | log_command ln -s "../../../opt/validationkit/linux/vboxtxs" "${MY_INIT_D_PARENT_PATH}/init.d/"
|
---|
223 | else
|
---|
224 | MY_INIT_D_PARENT_PATH="${MY_TARGET}/etc"
|
---|
225 | log_command ln -s "../../opt/validationkit/linux/vboxtxs" "${MY_INIT_D_PARENT_PATH}/init.d/"
|
---|
226 | fi
|
---|
227 |
|
---|
228 | # Use runlevel management script if found.
|
---|
229 | if chroot_which chkconfig; then # Redhat based sysvinit systems
|
---|
230 | log_command_in_target chkconfig --add vboxtxs
|
---|
231 | elif chroot_which insserv; then # SUSE-based sysvinit systems
|
---|
232 | log_command_in_target insserv vboxtxs
|
---|
233 | elif chroot_which update-rc.d; then # Debian/Ubuntu-based systems
|
---|
234 | log_command_in_target update-rc.d vboxtxs defaults
|
---|
235 | elif chroot_which rc-update; then # Gentoo Linux
|
---|
236 | log_command_in_target rc-update add vboxtxs default
|
---|
237 | # Fall back on hardcoded symlinking.
|
---|
238 | else
|
---|
239 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc0.d/K65vboxtxs"
|
---|
240 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc1.d/K65vboxtxs"
|
---|
241 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc6.d/K65vboxtxs"
|
---|
242 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc2.d/S35vboxtxs"
|
---|
243 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc3.d/S35vboxtxs"
|
---|
244 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc4.d/S35vboxtxs"
|
---|
245 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc5.d/S35vboxtxs"
|
---|
246 | fi
|
---|
247 | else
|
---|
248 | echo "** error: Unknown init script system." | tee -a "${MY_LOGFILE}"
|
---|
249 | fi
|
---|
250 |
|
---|
251 | @@VBOX_COND_END@@
|
---|
252 |
|
---|
253 |
|
---|
254 | #
|
---|
255 | # Run user command.
|
---|
256 | #
|
---|
257 | @@VBOX_COND_HAS_POST_INSTALL_COMMAND@@
|
---|
258 | echo '** Running custom user command ...' | tee -a "${MY_LOGFILE}"
|
---|
259 | log_command @@VBOX_INSERT_POST_INSTALL_COMMAND@@
|
---|
260 | @@VBOX_COND_END@@
|
---|
261 |
|
---|
262 |
|
---|
263 | #
|
---|
264 | # Unmount the cdrom if we bound it and clean up the chroot if we set it up.
|
---|
265 | #
|
---|
266 | if [ -n "${MY_UNMOUNT_TARGET_CDROM}" ]; then
|
---|
267 | echo "** unbinding cdrom from jail..." | tee -a "${MY_LOGFILE}"
|
---|
268 | log_command umount "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
269 | fi
|
---|
270 |
|
---|
271 | if [ -n "${MY_RMDIR_TARGET_CDROM}" ]; then
|
---|
272 | log_command rmdir "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
273 | fi
|
---|
274 |
|
---|
275 |
|
---|
276 | #
|
---|
277 | # Log footer.
|
---|
278 | #
|
---|
279 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
280 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
281 | echo "** Final exit code: ${MY_EXITCODE}" >> "${MY_LOGFILE}"
|
---|
282 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
283 |
|
---|
284 | exit ${MY_EXITCODE}
|
---|
285 |
|
---|