VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/docs/testbox-maintenance.sh@ 64600

Last change on this file since 64600 was 64600, checked in by vboxsync, 8 years ago

testbox-maintenance.sh: Try mount missing stuff.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1#!/bin/bash
2# $Id: testbox-maintenance.sh 64600 2016-11-08 16:34:10Z vboxsync $
3## @file
4# VirtualBox Validation Kit - testbox mainenance service
5#
6
7#
8# Copyright (C) 2006-2016 Oracle Corporation
9#
10# This file is part of VirtualBox Open Source Edition (OSE), as
11# available from http://www.virtualbox.org. This file is free software;
12# you can redistribute it and/or modify it under the terms of the GNU
13# General Public License (GPL) as published by the Free Software
14# Foundation, in version 2 as it comes in the "COPYING" file of the
15# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17#
18# The contents of this file may alternatively be used under the terms
19# of the Common Development and Distribution License Version 1.0
20# (CDDL) only, as it comes in the "COPYING.CDDL" file of the
21# VirtualBox OSE distribution, in which case the provisions of the
22# CDDL are applicable instead of those of the GPL.
23#
24# You may elect to license modified versions of this file under the
25# terms and conditions of either the GPL or the CDDL or both.
26#
27
28
29#
30# Global Variables (config first).
31#
32MY_REBOOT_WHEN_DONE="yes"
33#MY_REBOOT_WHEN_DONE="" # enable this for debugging the script
34
35MY_TFTP_ROOT="/mnt/testbox-tftp"
36MY_BACKUP_ROOT="/mnt/testbox-backup"
37MY_BACKUP_MNT_TEST_FILE="/mnt/testbox-backup/testbox-backup"
38MY_GLOBAL_LOG_FILE="${MY_BACKUP_ROOT}/maintenance.log"
39
40MY_IP=""
41MY_BACKUP_DIR=""
42MY_LOG_FILE=""
43MY_PXELINUX_CFG_FILE=""
44
45
46##
47# Info message.
48#
49InfoMsg()
50{
51 echo $*;
52 if test -n "${MY_LOG_FILE}"; then
53 echo "`date -uIsec`: ${MY_IP}: info:" $* >> ${MY_LOG_FILE};
54 fi
55}
56
57
58##
59# Error message and reboot+exit. First argument is exit code.
60#
61ErrorMsgExit()
62{
63 MY_RET=$1
64 shift
65 echo "testbox-maintenance.sh: error:" $* >&2;
66 # Append to the testbox log.
67 if test -n "${MY_LOG_FILE}"; then
68 echo "`date -uIsec`: ${MY_IP}: error:" $* >> "${MY_LOG_FILE}";
69 fi
70 # Append to the global log.
71 if test -f "${MY_BACKUP_MNT_TEST_FILE}"; then
72 echo "`date -uIsec`: ${MY_IP}: error:" $* >> "${MY_GLOBAL_LOG_FILE}";
73 fi
74
75 #
76 # On error we normally wait 5min before rebooting to avoid repeating the
77 # same error too many time before the admin finds out. We choose NOT to
78 # remove the PXE config file here because (a) the admin might otherwise
79 # not notice something went wrong, (b) the system could easily be in a
80 # weird unbootable state, (c) the problem might be temporary.
81 #
82 # While debugging, we just exit here.
83 #
84 if test -n "${MY_REBOOT_WHEN_DONE}"; then
85 sleep 5m
86 echo "testbox-maintenance.sh: rebooting (after error)" >&2;
87 reboot
88 fi
89 exit ${MY_RET}
90}
91
92#
93# Try figure out the IP address of the box and the hostname from it again.
94#
95MY_IP=` hostname -I | cut -f1 -d' ' | head -1 `
96if test -z "${MY_IP}" -o `echo "${MY_IP}" | wc -w` -ne "1" -o "${MY_IP}" = "127.0.0.1"; then
97 ErrorMsgExit 10 "Failed to get a good IP! (MY_IP=${MY_IP})"
98fi
99MY_HOSTNAME=`getent hosts "${MY_IP}" | sed -s 's/[[:space:]][[:space:]]*/ /g' | cut -d' ' -f2 `
100if test -z "${MY_HOSTNAME}"; then
101 MY_HOSTNAME="unknown";
102fi
103
104# Derive the backup dir and log file name from it.
105if test ! -f "${MY_BACKUP_MNT_TEST_FILE}"; then
106 mount "${MY_BACKUP_ROOT}"
107 if test ! -f "${MY_BACKUP_MNT_TEST_FILE}"; then
108 echo "Retrying mounting '${MY_BACKUP_ROOT}' in 15 seconds..." >&2
109 sleep 15
110 mount "${MY_BACKUP_ROOT}"
111 fi
112 if test ! -f "${MY_BACKUP_MNT_TEST_FILE}"; then
113 ErrorMsgExit 11 "Backup directory is not mounted."
114 fi
115fi
116MY_BACKUP_DIR="${MY_BACKUP_ROOT}/${MY_IP}"
117MY_LOG_FILE="${MY_BACKUP_DIR}/maintenance.log"
118mkdir -p "${MY_BACKUP_DIR}"
119echo "================ `date -uIsec`: ${MY_IP}: ${MY_HOSTNAME} starts a new session ================" >> "${MY_LOG_FILE}"
120echo "`date -uIsec`: ${MY_IP}: ${MY_HOSTNAME} says hi." >> "${MY_GLOBAL_LOG_FILE}"
121InfoMsg "MY_IP=${MY_IP}<eol>"
122
123#
124# Redirect stderr+stdout thru tee and to a log file on the server.
125#
126MY_OUTPUT_LOG_FILE="${MY_BACKUP_DIR}/maintenance-output.log"
127echo "" >> "${MY_OUTPUT_LOG_FILE}"
128echo "================ `date -uIsec`: ${MY_IP}: ${MY_HOSTNAME} starts a new session ================" >> "${MY_OUTPUT_LOG_FILE}"
129exec &> >(tee -a "${MY_OUTPUT_LOG_FILE}")
130
131#
132# Convert the IP address to PXELINUX hex format, then check that we've got
133# a config file on the TFTP share that we later can remove. We consider it a
134# fatal failure if we don't because we've probably got the wrong IP and we'll
135# be stuck doing the same stuff over and over again.
136#
137MY_TMP=`echo "${MY_IP}" | sed -e 's/\./ /g' `
138MY_IP_HEX=`printf "%02X%02X%02X%02X" ${MY_TMP}`
139InfoMsg "MY_IP_HEX=${MY_IP_HEX}<eol>"
140
141if test ! -f "${MY_TFTP_ROOT}/pxelinux.0"; then
142 mount "${MY_TFTP_ROOT}"
143 if test ! -f "${MY_TFTP_ROOT}/pxelinux.0"; then
144 echo "Retrying mounting '${MY_TFTP_ROOT}' in 15 seconds..." >&2
145 sleep 15
146 mount "${MY_BACKUP_ROOT}"
147 fi
148 if test ! -f "${MY_TFTP_ROOT}/pxelinux.0"; then
149 ErrorMsgExit 12 "TFTP share mounted or mixxing pxelinux.0 in the root."
150 fi
151fi
152
153MY_PXELINUX_CFG_FILE="${MY_TFTP_ROOT}/pxelinux.cfg/${MY_IP_HEX}"
154if test ! -f "${MY_PXELINUX_CFG_FILE}"; then
155 ErrorMsgExit 13 "No pxelinux.cfg file found (${MY_PXELINUX_CFG_FILE}) - wrong IP?"
156fi
157
158#
159# Dig the action out of from the kernel command line.
160#
161if test -n "${MY_REBOOT_WHEN_DONE}"; then
162 InfoMsg "/proc/cmdline: `cat /proc/cmdline`"
163 set `cat /proc/cmdline`
164else
165 InfoMsg "Using script command line: $*"
166fi
167MY_ACTION=not-found
168while test $# -ge 1; do
169 case "$1" in
170 testbox-action-*)
171 MY_ACTION="$1"
172 ;;
173 esac
174 shift
175done
176if test "${MY_ACTION}" = "not-found"; then
177 ErrorMsgExit 14 "No action given. Expected testbox-action-backup, testbox-action-backup-again, testbox-action-restore," \
178 "testbox-action-refresh-info, or testbox-action-rescue on the kernel command line.";
179fi
180
181# Validate and shorten the action.
182case "${MY_ACTION}" in
183 testbox-action-backup)
184 MY_ACTION="backup";
185 ;;
186 testbox-action-backup-again)
187 MY_ACTION="backup-again";
188 ;;
189 testbox-action-restore)
190 MY_ACTION="restore";
191 ;;
192 testbox-action-refresh-info)
193 MY_ACTION="refresh-info";
194 ;;
195 testbox-action-rescue)
196 MY_ACTION="rescue";
197 ;;
198 *) ErrorMsgExit 15 "Invalid action '${MY_ACTION}'";
199 ;;
200esac
201
202# Log the action in both logs.
203echo "`date -uIsec`: ${MY_IP}: info: Executing '${MY_ACTION}'." >> "${MY_GLOBAL_LOG_FILE}";
204
205#
206# Generate missing info for this testbox if backing up.
207#
208MY_INFO_FILE="${MY_BACKUP_DIR}/testbox-info.txt"
209if test '!' -f "${MY_INFO_FILE}" \
210 -o "${MY_ACTION}" = "backup" \
211 -o "${MY_ACTION}" = "backup-again" \
212 -o "${MY_ACTION}" = "refresh-info" ;
213then
214 echo "IP: ${MY_IP}" > ${MY_INFO_FILE};
215 echo "HEX-IP: ${MY_IP_HEX}" >> ${MY_INFO_FILE};
216 echo "Hostname: ${MY_HOSTNAME}" >> ${MY_INFO_FILE};
217 echo "" >> ${MY_INFO_FILE};
218 echo "**** cat /proc/cpuinfo ****" >> ${MY_INFO_FILE};
219 echo "**** cat /proc/cpuinfo ****" >> ${MY_INFO_FILE};
220 echo "**** cat /proc/cpuinfo ****" >> ${MY_INFO_FILE};
221 cat /proc/cpuinfo >> ${MY_INFO_FILE};
222 echo "" >> ${MY_INFO_FILE};
223 echo "**** lspci -vvv ****" >> ${MY_INFO_FILE};
224 echo "**** lspci -vvv ****" >> ${MY_INFO_FILE};
225 echo "**** lspci -vvv ****" >> ${MY_INFO_FILE};
226 lspci -vvv >> ${MY_INFO_FILE} 2>&1;
227 echo "" >> ${MY_INFO_FILE};
228 echo "**** biosdecode ****" >> ${MY_INFO_FILE};
229 echo "**** biosdecode ****" >> ${MY_INFO_FILE};
230 echo "**** biosdecode ****" >> ${MY_INFO_FILE};
231 biosdecode >> ${MY_INFO_FILE} 2>&1;
232 echo "" >> ${MY_INFO_FILE};
233 echo "**** dmidecode ****" >> ${MY_INFO_FILE};
234 echo "**** dmidecode ****" >> ${MY_INFO_FILE};
235 echo "**** dmidecode ****" >> ${MY_INFO_FILE};
236 dmidecode >> ${MY_INFO_FILE} 2>&1;
237 echo "" >> ${MY_INFO_FILE};
238 echo "**** fdisk -l ****" >> ${MY_INFO_FILE};
239 echo "**** fdisk -l ****" >> ${MY_INFO_FILE};
240 echo "**** fdisk -l ****" >> ${MY_INFO_FILE};
241 fdisk -l >> ${MY_INFO_FILE} 2>&1;
242 echo "" >> ${MY_INFO_FILE};
243 echo "**** dmesg ****" >> ${MY_INFO_FILE};
244 echo "**** dmesg ****" >> ${MY_INFO_FILE};
245 echo "**** dmesg ****" >> ${MY_INFO_FILE};
246 dmesg >> ${MY_INFO_FILE} 2>&1;
247
248 #
249 # Get the raw ACPI tables and whatnot since we can. Use zip as tar will
250 # zero pad virtual files due to wrong misleading size returned by stat (4K).
251 #
252 # Note! /sys/firmware/dmi/entries/15-0/system_event_log/raw_event_log has been
253 # see causing fatal I/O errors, so skip all raw_event_log files.
254 #
255 zip -qr9 "${MY_BACKUP_DIR}/testbox-info.zip" \
256 /proc/cpuinfo \
257 /sys/firmware/ \
258 -x "*/raw_event_log"
259fi
260
261if test '!' -f "${MY_BACKUP_DIR}/${MY_HOSTNAME}" -a "${MY_HOSTNAME}" != "unknown"; then
262 echo "${MY_HOSTNAME}" > "${MY_BACKUP_DIR}/${MY_HOSTNAME}"
263fi
264
265if test '!' -f "${MY_BACKUP_DIR}/${MY_IP_HEX}"; then
266 echo "${MY_IP}" > "${MY_BACKUP_DIR}/${MY_IP_HEX}"
267fi
268
269#
270# Assemble a list of block devices using /sys/block/* and some filtering.
271#
272if test -f "${MY_BACKUP_DIR}/disk-devices.lst"; then
273 MY_BLOCK_DEVS=`cat ${MY_BACKUP_DIR}/disk-devices.lst \
274 | sed -e 's/[[:space:]][::space::]]*/ /g' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' `;
275 if test -z "${MY_BLOCK_DEVS}"; then
276 ErrorMsgExit 17 "No block devices found via sys/block."
277 fi
278 InfoMsg "disk-device.lst: MY_BLOCK_DEVS=${MY_BLOCK_DEVS}";
279else
280 MY_BLOCK_DEVS="";
281 for MY_DEV in `ls /sys/block`; do
282 case "${MY_DEV}" in
283 [sh]d*)
284 MY_BLOCK_DEVS="${MY_BLOCK_DEVS} ${MY_DEV}"
285 ;;
286 *) InfoMsg "Ignoring /sys/block/${MY_DEV}";
287 ;;
288 esac
289 done
290 if test -z "${MY_BLOCK_DEVS}"; then
291 ErrorMsgExit 17 "No block devices found via /sys/block."
292 fi
293 InfoMsg "/sys/block: MY_BLOCK_DEVS=${MY_BLOCK_DEVS}";
294fi
295
296#
297# Take action
298#
299case "${MY_ACTION}" in
300 #
301 # Create a backup. The 'backup' action refuses to overwrite an
302 # existing backup, but is otherwise identical to 'backup-again'.
303 #
304 backup|backup-again)
305 for MY_DEV in ${MY_BLOCK_DEVS}; do
306 MY_DST="${MY_BACKUP_DIR}/${MY_DEV}.gz"
307 if test -f "${MY_DST}"; then
308 if test "${MY_ACTION}" != 'backup-again'; then
309 ErrorMsgExit 18 "${MY_DST} already exists"
310 fi
311 InfoMsg "${MY_DST} already exists"
312 fi
313 done
314
315 # Do the backing up.
316 for MY_DEV in ${MY_BLOCK_DEVS}; do
317 MY_SRC="/dev/${MY_DEV}"
318 MY_DST="${MY_BACKUP_DIR}/${MY_DEV}.gz"
319 if test -f "${MY_DST}"; then
320 mv -f "${MY_DST}" "${MY_DST}.old";
321 fi
322 if test -b "${MY_SRC}"; then
323 InfoMsg "Backing up ${MY_SRC} to ${MY_DST}...";
324 dd if="${MY_SRC}" bs=2M | gzip -c > "${MY_DST}";
325 MY_RCS=("${PIPESTATUS[@]}");
326 if test "${MY_RCS[0]}" -eq 0 -a "${MY_RCS[1]}" -eq 0; then
327 InfoMsg "Successfully backed up ${MY_SRC} to ${MY_DST}";
328 else
329 rm -f "${MY_DST}";
330 ErrorMsgExit 19 "There was a problem backing up ${MY_SRC} to ${MY_DST}: dd => ${MY_RCS[0]}; gzip => ${MY_RCS[1]}";
331 fi
332 else
333 InfoMsg "Skipping ${MY_SRC} as it either doesn't exist or isn't a block device";
334 fi
335 done
336 ;;
337
338 #
339 # Restore existing.
340 #
341 restore)
342 for MY_DEV in ${MY_BLOCK_DEVS}; do
343 MY_SRC="${MY_BACKUP_DIR}/${MY_DEV}.gz"
344 MY_DST="/dev/${MY_DEV}"
345 if test -b "${MY_DST}"; then
346 if test -f "${MY_SRC}"; then
347 InfoMsg "Restoring ${MY_SRC} onto ${MY_DST}...";
348 gunzip -c "${MY_SRC}" | dd of="${MY_DST}" bs=64K;
349 MY_RCS=("${PIPESTATUS[@]}");
350 if test ${MY_RCS[0]} -eq 0 -a ${MY_RCS[1]} -eq 0; then
351 InfoMsg "Successfully restored ${MY_SRC} onto ${MY_DST}";
352 else
353 ErrorMsgExit 20 "There was a problem restoring ${MY_SRC} onto ${MY_DST}: dd => ${MY_RCS[1]}; gunzip => ${MY_RCS[0]}";
354 fi
355 else
356 InfoMsg "Skipping ${MY_DST} because ${MY_SRC} does not exist.";
357 fi
358 else
359 InfoMsg "Skipping ${MY_DST} as it either doesn't exist or isn't a block device.";
360 fi
361 done
362 ;;
363
364 #
365 # Nothing else to do for refresh-info.
366 #
367 refresh-info)
368 ;;
369
370 #
371 # For the rescue action, we just quit without removing the PXE config or
372 # rebooting the box. The admin will do that once the system has been rescued.
373 #
374 rescue)
375 InfoMsg "rescue: exiting. Admin must remove PXE config and reboot manually when done."
376 exit 0;
377 ;;
378
379 *) ErrorMsgExit 98 "Huh? MY_ACTION='${MY_ACTION}'"
380 ;;
381esac
382
383#
384# If we get here, remove the PXE config and reboot immediately.
385#
386InfoMsg "'${MY_ACTION}' - done";
387if test -n "${MY_REBOOT_WHEN_DONE}"; then
388 sync
389 if rm -f "${MY_PXELINUX_CFG_FILE}"; then
390 InfoMsg "removed ${MY_PXELINUX_CFG_FILE}";
391 else
392 ErrorMsgExit 99 "failed to remove ${MY_PXELINUX_CFG_FILE}";
393 fi
394 sync
395 InfoMsg "rebooting";
396 reboot
397fi
398exit 0
399
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette