master
text 76 lines 2.76 KB
Raw
1 #!/bin/sh
2
3 # This script is executed when the guest agent receives fsfreeze-freeze and
4 # fsfreeze-thaw commands, provided that the --fsfreeze-hook (-F) option of
5 # qemu-ga is specified and the script is placed in /etc/qemu/fsfreeze-hook or in
6 # the path specified together with -F. When the agent receives fsfreeze-freeze
7 # requests, this script is called with "freeze" as its argument before the
8 # filesystem is frozen. And for fsfreeze-thaw requests, it is called with "thaw"
9 # as its argument after the filesystem is thawed.
10
11 LOGFILE=/var/log/qga-fsfreeze-hook.log
12 FSFREEZE_D=$(dirname -- "$0")/fsfreeze-hook.d
13
14 # Check whether file $1 is a backup or rpm-generated file and should be ignored
15 is_ignored_file() {
16 case "$1" in
17 *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave | *.sample | *.dpkg-old | *.dpkg-new | *.dpkg-tmp | *.dpkg-dist | *.dpkg-bak | *.dpkg-backup | *.dpkg-remove)
18 return 0 ;;
19 esac
20 return 1
21 }
22
23 USE_SYSLOG=0
24 # if log file exists but is not writable, fallback to syslog
25 [ -e "$LOGFILE" ] && [ ! -w "$LOGFILE" ] && USE_SYSLOG=1
26 # try to update log file and fallback to syslog if it fails
27 touch "$LOGFILE" >/dev/null 2>&1 || USE_SYSLOG=1
28
29 # Ensure the log file is writable, fallback to syslog if not
30 log_message() {
31 if [ "$USE_SYSLOG" -eq 0 ]; then
32 printf "%s: %s\n" "$(date)" "$1" >>"$LOGFILE"
33 else
34 logger -t qemu-ga-freeze-hook "$1"
35 fi
36 }
37
38 # Iterate executables in directory "fsfreeze-hook.d" with the specified args
39 [ ! -d "$FSFREEZE_D" ] && exit 0
40
41 for file in "$FSFREEZE_D"/* ; do
42 is_ignored_file "$file" && continue
43 [ -x "$file" ] || continue
44
45 log_message "Executing $file $*"
46 if [ "$USE_SYSLOG" -eq 0 ]; then
47 "$file" "$@" >>"$LOGFILE" 2>&1
48 STATUS=$?
49 else
50 # We want to pipe the output of $file through 'logger' and also
51 # capture its exit status. Since we are a POSIX script we can't
52 # use PIPESTATUS, so instead this is a trick borrowed from
53 # https://unix.stackexchange.com/questions/14270/get-exit-status-of-process-thats-piped-to-another/70675#70675
54 # which uses command-groups and redirection to get the exit status.
55 # This is equivalent to
56 # "$file" "$@" 2>&1 | logger -t qemu-ga-freeze-hook
57 # plus setting the exit status of the pipe to the exit
58 # status of the first command rather than the last one.
59 { { { {
60 "$file" "$@" 2>&1 3>&- 4>&-
61 echo $? >&3
62 } | logger -t qemu-ga-freeze-hook >&4
63 } 3>&1
64 } | { read -r xs ; exit "$xs"; }
65 } 4>&1
66 STATUS=$?
67 fi
68
69 if [ "$STATUS" -ne 0 ]; then
70 log_message "Error: $file finished with status=$STATUS"
71 else
72 log_message "$file finished successfully"
73 fi
74 done
75
76 exit 0