Skip to content

Commit 85978df

Browse files
6-dehankostyanf14
authored andcommitted
qemu-ga: Optimize freeze-hook script logic of logging error
Make sure the error log of fsfreeze hooks when freeze/thaw/snapshot could be logged to system logs if the default logfile of qga can't be written or other situations Signed-off-by: Dehan Meng <[email protected]> Reviewed-by: Yan Vugenfirer <[email protected]> Reviewed-by: Konstantin Kostiuk <[email protected]> Message-ID: <[email protected]> Signed-off-by: Konstantin Kostiuk <[email protected]>
1 parent 5288d9d commit 85978df

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

scripts/qemu-guest-agent/fsfreeze-hook

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,43 @@ is_ignored_file() {
1919
return 1
2020
}
2121

22+
USE_SYSLOG=0
23+
# if log file is not writable, fallback to syslog
24+
[ ! -w "$LOGFILE" ] && USE_SYSLOG=1
25+
# try to update log file and fallback to syslog if it fails
26+
touch "$LOGFILE" &>/dev/null || USE_SYSLOG=1
27+
28+
# Ensure the log file is writable, fallback to syslog if not
29+
log_message() {
30+
local message="$1"
31+
if [ "$USE_SYSLOG" -eq 0 ]; then
32+
printf "%s: %s\n" "$(date)" "$message" >>"$LOGFILE"
33+
else
34+
logger -t qemu-ga-freeze-hook "$message"
35+
fi
36+
}
37+
2238
# Iterate executables in directory "fsfreeze-hook.d" with the specified args
2339
[ ! -d "$FSFREEZE_D" ] && exit 0
40+
2441
for file in "$FSFREEZE_D"/* ; do
2542
is_ignored_file "$file" && continue
2643
[ -x "$file" ] || continue
27-
printf "$(date): execute $file $@\n" >>$LOGFILE
28-
"$file" "$@" >>$LOGFILE 2>&1
29-
STATUS=$?
30-
printf "$(date): $file finished with status=$STATUS\n" >>$LOGFILE
44+
45+
log_message "Executing $file $@"
46+
if [ "$USE_SYSLOG" -eq 0 ]; then
47+
"$file" "$@" >>"$LOGFILE" 2>&1
48+
STATUS=$?
49+
else
50+
"$file" "$@" 2>&1 | logger -t qemu-ga-freeze-hook
51+
STATUS=${PIPESTATUS[0]}
52+
fi
53+
54+
if [ $STATUS -ne 0 ]; then
55+
log_message "Error: $file finished with status=$STATUS"
56+
else
57+
log_message "$file finished successfully"
58+
fi
3159
done
3260

3361
exit 0

0 commit comments

Comments
 (0)