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-2022 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 | GUEST_VERSION=@@VBOX_INSERT_GUEST_OS_VERSION@@
|
---|
31 | GUEST_MAJOR_VERSION=@@VBOX_INSERT_GUEST_OS_MAJOR_VERSION@@
|
---|
32 |
|
---|
33 | @@VBOX_COND_HAS_PROXY@@
|
---|
34 | PROXY="@@VBOX_INSERT_PROXY@@"
|
---|
35 | export http_proxy="${PROXY}"
|
---|
36 | export https_proxy="${PROXY}"
|
---|
37 | echo "HTTP proxy is ${http_proxy}" | tee -a "${MY_LOGFILE}"
|
---|
38 | echo "HTTPS proxy is ${https_proxy}" | tee -a "${MY_LOGFILE}"
|
---|
39 | @@VBOX_COND_END@@
|
---|
40 |
|
---|
41 | #
|
---|
42 | # Do we need to exec using target bash? If so, we must do that early
|
---|
43 | # or ash will bark 'bad substitution' and fail.
|
---|
44 | #
|
---|
45 | if [ "$1" = "--need-target-bash" ]; then
|
---|
46 | # Try figure out which directories we might need in the library path.
|
---|
47 | if [ -z "${LD_LIBRARY_PATH}" ]; then
|
---|
48 | LD_LIBRARY_PATH="${MY_TARGET}/lib"
|
---|
49 | fi
|
---|
50 | for x in \
|
---|
51 | ${MY_TARGET}/lib \
|
---|
52 | ${MY_TARGET}/usr/lib \
|
---|
53 | ${MY_TARGET}/lib/*linux-gnu/ \
|
---|
54 | ${MY_TARGET}/lib32/ \
|
---|
55 | ${MY_TARGET}/lib64/ \
|
---|
56 | ${MY_TARGET}/usr/lib/*linux-gnu/ \
|
---|
57 | ${MY_TARGET}/usr/lib32/ \
|
---|
58 | ${MY_TARGET}/usr/lib64/ \
|
---|
59 | ;
|
---|
60 | do
|
---|
61 | if [ -e "$x" ]; then LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${x}"; fi;
|
---|
62 | done
|
---|
63 | export LD_LIBRARY_PATH
|
---|
64 |
|
---|
65 | # Append target bin directories to the PATH as busybox may not have tee.
|
---|
66 | PATH="${PATH}:${MY_TARGET}/bin:${MY_TARGET}/usr/bin:${MY_TARGET}/sbin:${MY_TARGET}/usr/sbin"
|
---|
67 | export PATH
|
---|
68 |
|
---|
69 | # Drop the --need-target-bash argument and re-exec.
|
---|
70 | shift
|
---|
71 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
72 | echo "** Relaunching using ${MY_TARGET}/bin/bash $0 $*" >> "${MY_LOGFILE}"
|
---|
73 | echo "** LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" >> "${MY_LOGFILE}"
|
---|
74 | echo "** PATH=${PATH}" >> "${MY_LOGFILE}"
|
---|
75 | exec "${MY_TARGET}/bin/bash" "$0" "$@"
|
---|
76 | fi
|
---|
77 |
|
---|
78 |
|
---|
79 | #
|
---|
80 | # Commands.
|
---|
81 | #
|
---|
82 |
|
---|
83 | # Logs execution of a command.
|
---|
84 | log_command()
|
---|
85 | {
|
---|
86 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
87 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
88 | echo "** Executing: $*" >> "${MY_LOGFILE}"
|
---|
89 | "$@" 2>&1 | tee -a "${MY_LOGFILE}"
|
---|
90 | MY_TMP_EXITCODE="${PIPESTATUS[0]}" # bashism - whatever.
|
---|
91 | if [ "${MY_TMP_EXITCODE}" != "0" ]; then
|
---|
92 | if [ "${MY_TMP_EXITCODE}" != "${MY_IGNORE_EXITCODE}" ]; then
|
---|
93 | echo "** exit code: ${MY_TMP_EXITCODE}" | tee -a "${MY_LOGFILE}"
|
---|
94 | MY_EXITCODE=1;
|
---|
95 | else
|
---|
96 | echo "** exit code: ${MY_TMP_EXITCODE} (ignored)" | tee -a "${MY_LOGFILE}"
|
---|
97 | fi
|
---|
98 | fi
|
---|
99 | }
|
---|
100 |
|
---|
101 | # Logs execution of a command inside the target.
|
---|
102 | log_command_in_target()
|
---|
103 | {
|
---|
104 | log_command chroot "${MY_TARGET}" "$@"
|
---|
105 | }
|
---|
106 |
|
---|
107 | # Checks if $1 is a command on the PATH inside the target jail.
|
---|
108 | chroot_which()
|
---|
109 | {
|
---|
110 | for dir in /bin /usr/bin /sbin /usr/sbin;
|
---|
111 | do
|
---|
112 | if [ -x "${MY_TARGET}${dir}/$1" ]; then
|
---|
113 | return 0;
|
---|
114 | fi
|
---|
115 | done
|
---|
116 | return 1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | #
|
---|
120 | # Log header.
|
---|
121 | #
|
---|
122 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
123 | echo "** VirtualBox Unattended Guest Installation - Late installation actions" >> "${MY_LOGFILE}"
|
---|
124 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
125 | echo "** Started: $0 $*" >> "${MY_LOGFILE}"
|
---|
126 |
|
---|
127 |
|
---|
128 | #
|
---|
129 | # We want the ISO available inside the target jail.
|
---|
130 | #
|
---|
131 | if [ -d "${MY_TARGET}${MY_CHROOT_CDROM}" ]; then
|
---|
132 | MY_RMDIR_TARGET_CDROM=
|
---|
133 | else
|
---|
134 | MY_RMDIR_TARGET_CDROM="yes"
|
---|
135 | log_command mkdir -p ${MY_TARGET}${MY_CHROOT_CDROM}
|
---|
136 | fi
|
---|
137 |
|
---|
138 | if [ -f "${MY_TARGET}${MY_CHROOT_CDROM}/vboxpostinstall.sh" ]; then
|
---|
139 | MY_UNMOUNT_TARGET_CDROM=
|
---|
140 | echo "** binding cdrom into jail: already done" | tee -a "${MY_LOGFILE}"
|
---|
141 | else
|
---|
142 | MY_UNMOUNT_TARGET_CDROM="yes"
|
---|
143 | log_command mount -o bind "${MY_CDROM_NOCHROOT}" "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
144 | if [ -f "${MY_TARGET}${MY_CHROOT_CDROM}/vboxpostinstall.sh" ]; then
|
---|
145 | echo "** binding cdrom into jail: success" | tee -a "${MY_LOGFILE}"
|
---|
146 | else
|
---|
147 | echo "** binding cdrom into jail: failed" | tee -a "${MY_LOGFILE}"
|
---|
148 | fi
|
---|
149 | if [ "${MY_DEBUG}" = "yes" ]; then
|
---|
150 | log_command find "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
151 | fi
|
---|
152 | fi
|
---|
153 |
|
---|
154 |
|
---|
155 | #
|
---|
156 | # Debug
|
---|
157 | #
|
---|
158 | if [ "${MY_DEBUG}" = "yes" ]; then
|
---|
159 | log_command id
|
---|
160 | log_command ps
|
---|
161 | log_command ps auxwwwf
|
---|
162 | log_command env
|
---|
163 | log_command df
|
---|
164 | log_command mount
|
---|
165 | log_command_in_target df
|
---|
166 | log_command_in_target mount
|
---|
167 | #log_command find /
|
---|
168 | MY_EXITCODE=0
|
---|
169 | fi
|
---|
170 |
|
---|
171 |
|
---|
172 | #
|
---|
173 | # Add EPEL repository
|
---|
174 | #
|
---|
175 | EPEL_REPOSITORY="https://dl.fedoraproject.org/pub/epel/epel-release-latest-${GUEST_MAJOR_VERSION}.noarch.rpm"
|
---|
176 | log_command_in_target wget ${EPEL_REPOSITORY}
|
---|
177 | log_command_in_target yum localinstall -y "epel-release-latest-${GUEST_MAJOR_VERSION}.noarch.rpm"
|
---|
178 | log_command_in_target rpm -q "oraclelinux-release-el${GUEST_MAJOR_VERSION}"
|
---|
179 | log_command_in_target yum install -y yum-utils
|
---|
180 | log_command_in_target yum-config-manager --enable epel
|
---|
181 |
|
---|
182 |
|
---|
183 | #
|
---|
184 | # Packages needed for GAs.
|
---|
185 | #
|
---|
186 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
187 | echo '** Finding UEK kernel...' | tee -a "${MY_LOGFILE}"
|
---|
188 | UEK=$(find ${MY_TARGET}/boot/ -name "vmlinuz*"| grep uek | awk -F"-" 'BEGIN{OFS="-"} \
|
---|
189 | { kernel=$2; for (i = 3; i <= NF; i++) { kernel=sprintf("%s-%s",kernel,$i)}} END{print kernel}')
|
---|
190 | CURRENT_KERNEL=$(uname -r)
|
---|
191 |
|
---|
192 | if [ -n $UEK ]; then
|
---|
193 | echo "UEK kernel was found - ${UEK}" | tee -a "${MY_LOGFILE}"
|
---|
194 | log_command_in_target yum -y install "kernel-uek-devel-${UEK}"
|
---|
195 | fi
|
---|
196 | echo "Installation uses kernel ${CURRENT_KERNEL}" | tee -a "${MY_LOGFILE}"
|
---|
197 |
|
---|
198 | echo '** Installing packages for building kernel modules...' | tee -a "${MY_LOGFILE}"
|
---|
199 | log_command_in_target yum -y install "kernel-devel-$(uname -r)"
|
---|
200 | log_command_in_target yum -y install "kernel-headers-$(uname -r)"
|
---|
201 | log_command_in_target yum -y install gcc
|
---|
202 | log_command_in_target yum -y install binutils
|
---|
203 | log_command_in_target yum -y install make
|
---|
204 | @@VBOX_COND[${GUEST_OS_VERSION} vgt 8.0.0]@@
|
---|
205 | log_command_in_target yum -y install elfutils-libelf-devel
|
---|
206 | @@VBOX_COND_END@@
|
---|
207 | log_command_in_target yum -y install dkms
|
---|
208 | log_command_in_target yum -y install make
|
---|
209 | log_command_in_target yum -y install bzip2
|
---|
210 | log_command_in_target yum -y install perl
|
---|
211 |
|
---|
212 |
|
---|
213 | #
|
---|
214 | #Package cloud-init is needed for possible automation the initial setup of virtual machine
|
---|
215 | #
|
---|
216 | log_command_in_target yum -y install cloud-init
|
---|
217 | log_command_in_target systemctl enable cloud-init-local.service
|
---|
218 | log_command_in_target systemctl enable cloud-init.service
|
---|
219 | log_command_in_target systemctl enable cloud-config.service
|
---|
220 | log_command_in_target systemctl enable cloud-final.service
|
---|
221 |
|
---|
222 |
|
---|
223 | #
|
---|
224 | # GAs
|
---|
225 | #
|
---|
226 | @@VBOX_COND_IS_INSTALLING_ADDITIONS@@
|
---|
227 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
228 | echo '** Installing VirtualBox Guest Additions...' | tee -a "${MY_LOGFILE}"
|
---|
229 | MY_IGNORE_EXITCODE=2 # returned if modules already loaded and reboot required.
|
---|
230 | log_command_in_target /bin/bash "${MY_CHROOT_CDROM}/vboxadditions/VBoxLinuxAdditions.run" --nox11
|
---|
231 | log_command_in_target /bin/bash -c "udevadm control --reload-rules" # GAs doesn't yet do this.
|
---|
232 | log_command_in_target /bin/bash -c "udevadm trigger" # (ditto)
|
---|
233 | MY_IGNORE_EXITCODE=
|
---|
234 | log_command_in_target usermod -a -G vboxsf "@@VBOX_INSERT_USER_LOGIN@@"
|
---|
235 | @@VBOX_COND_END@@
|
---|
236 |
|
---|
237 |
|
---|
238 | #
|
---|
239 | # Test Execution Service.
|
---|
240 | #
|
---|
241 | @@VBOX_COND_IS_INSTALLING_TEST_EXEC_SERVICE@@
|
---|
242 | echo "--------------------------------------------------" >> "${MY_LOGFILE}"
|
---|
243 | echo '** Installing Test Execution Service...' | tee -a "${MY_LOGFILE}"
|
---|
244 | log_command_in_target test "${MY_CHROOT_CDROM}/vboxvalidationkit/linux/@@VBOX_INSERT_OS_ARCH@@/TestExecService"
|
---|
245 | log_command mkdir -p "${MY_TARGET}/opt/validationkit" "${MY_TARGET}/media/cdrom"
|
---|
246 | log_command cp -R ${MY_CDROM_NOCHROOT}/vboxvalidationkit/* "${MY_TARGET}/opt/validationkit/"
|
---|
247 | log_command chmod -R u+rw,a+xr "${MY_TARGET}/opt/validationkit/"
|
---|
248 | 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
|
---|
249 | MY_IGNORE_EXITCODE=1
|
---|
250 | log_command_in_target chcon -R -t usr_t "/opt/validationkit/"
|
---|
251 | MY_IGNORE_EXITCODE=
|
---|
252 | fi
|
---|
253 |
|
---|
254 | # systemd service config:
|
---|
255 | MY_UNIT_PATH="${MY_TARGET}/lib/systemd/system"
|
---|
256 | test -d "${MY_TARGET}/usr/lib/systemd/system" && MY_UNIT_PATH="${MY_TARGET}/usr/lib/systemd/system"
|
---|
257 | if [ -d "${MY_UNIT_PATH}" ]; then
|
---|
258 | log_command cp "${MY_TARGET}/opt/validationkit/linux/vboxtxs.service" "${MY_UNIT_PATH}/vboxtxs.service"
|
---|
259 | log_command chmod 644 "${MY_UNIT_PATH}/vboxtxs.service"
|
---|
260 | log_command_in_target systemctl -q enable vboxtxs
|
---|
261 |
|
---|
262 | # System V like:
|
---|
263 | elif [ -e "${MY_TARGET}/etc/init.d/" ]; then
|
---|
264 |
|
---|
265 | # Install the script. On rhel6 scripts are under /etc/rc.d/ with /etc/init.d and /etc/rc?.d being symlinks.
|
---|
266 | if [ -d "${MY_TARGET}/etc/rc.d/init.d/" ]; then
|
---|
267 | MY_INIT_D_PARENT_PATH="${MY_TARGET}/etc/rc.d"
|
---|
268 | log_command ln -s "../../../opt/validationkit/linux/vboxtxs" "${MY_INIT_D_PARENT_PATH}/init.d/"
|
---|
269 | else
|
---|
270 | MY_INIT_D_PARENT_PATH="${MY_TARGET}/etc"
|
---|
271 | log_command ln -s "../../opt/validationkit/linux/vboxtxs" "${MY_INIT_D_PARENT_PATH}/init.d/"
|
---|
272 | fi
|
---|
273 |
|
---|
274 | # Use runlevel management script if found.
|
---|
275 | if chroot_which chkconfig; then # Redhat based sysvinit systems
|
---|
276 | log_command_in_target chkconfig --add vboxtxs
|
---|
277 | elif chroot_which insserv; then # SUSE-based sysvinit systems
|
---|
278 | log_command_in_target insserv vboxtxs
|
---|
279 | elif chroot_which update-rc.d; then # Debian/Ubuntu-based systems
|
---|
280 | log_command_in_target update-rc.d vboxtxs defaults
|
---|
281 | elif chroot_which rc-update; then # Gentoo Linux
|
---|
282 | log_command_in_target rc-update add vboxtxs default
|
---|
283 | # Fall back on hardcoded symlinking.
|
---|
284 | else
|
---|
285 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc0.d/K65vboxtxs"
|
---|
286 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc1.d/K65vboxtxs"
|
---|
287 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc6.d/K65vboxtxs"
|
---|
288 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc2.d/S35vboxtxs"
|
---|
289 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc3.d/S35vboxtxs"
|
---|
290 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc4.d/S35vboxtxs"
|
---|
291 | log_command ln -s "../init.d/vboxtxs" "${MY_INIT_D_PARENT_PATH}/rc5.d/S35vboxtxs"
|
---|
292 | fi
|
---|
293 | else
|
---|
294 | echo "** error: Unknown init script system." | tee -a "${MY_LOGFILE}"
|
---|
295 | fi
|
---|
296 |
|
---|
297 | @@VBOX_COND_END@@
|
---|
298 |
|
---|
299 |
|
---|
300 | #
|
---|
301 | # Run user command.
|
---|
302 | #
|
---|
303 | @@VBOX_COND_HAS_POST_INSTALL_COMMAND@@
|
---|
304 | echo '** Running custom user command ...' | tee -a "${MY_LOGFILE}"
|
---|
305 | log_command @@VBOX_INSERT_POST_INSTALL_COMMAND@@
|
---|
306 | @@VBOX_COND_END@@
|
---|
307 |
|
---|
308 |
|
---|
309 | #
|
---|
310 | # Unmount the cdrom if we bound it and clean up the chroot if we set it up.
|
---|
311 | #
|
---|
312 | if [ -n "${MY_UNMOUNT_TARGET_CDROM}" ]; then
|
---|
313 | echo "** unbinding cdrom from jail..." | tee -a "${MY_LOGFILE}"
|
---|
314 | log_command umount "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
315 | fi
|
---|
316 |
|
---|
317 | if [ -n "${MY_RMDIR_TARGET_CDROM}" ]; then
|
---|
318 | log_command rmdir "${MY_TARGET}${MY_CHROOT_CDROM}"
|
---|
319 | fi
|
---|
320 |
|
---|
321 |
|
---|
322 | #
|
---|
323 | # Log footer.
|
---|
324 | #
|
---|
325 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
326 | echo "** Date: `date -R`" >> "${MY_LOGFILE}"
|
---|
327 | echo "** Final exit code: ${MY_EXITCODE}" >> "${MY_LOGFILE}"
|
---|
328 | echo "******************************************************************************" >> "${MY_LOGFILE}"
|
---|
329 |
|
---|
330 | exit ${MY_EXITCODE}
|
---|