xref: /qemu/scripts/qemu-guest-agent/fsfreeze-hook (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1#!/bin/sh
2
3# This script is executed when a guest agent receives fsfreeze-freeze and
4# fsfreeze-thaw command, if it is specified in --fsfreeze-hook (-F)
5# option of qemu-ga or placed in default path (/etc/qemu/fsfreeze-hook).
6# When the agent receives fsfreeze-freeze request, this script is issued with
7# "freeze" argument before the filesystem is frozen. And for fsfreeze-thaw
8# request, it is issued with "thaw" argument after filesystem is thawed.
9
10LOGFILE=/var/log/qga-fsfreeze-hook.log
11FSFREEZE_D=$(dirname -- "$0")/fsfreeze-hook.d
12
13# Check whether file $1 is a backup or rpm-generated file and should be ignored
14is_ignored_file() {
15    case "$1" in
16        *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave | *.sample | *.dpkg-old | *.dpkg-new | *.dpkg-tmp | *.dpkg-dist | *.dpkg-bak | *.dpkg-backup | *.dpkg-remove)
17            return 0 ;;
18    esac
19    return 1
20}
21
22USE_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
26touch "$LOGFILE" &>/dev/null || USE_SYSLOG=1
27
28# Ensure the log file is writable, fallback to syslog if not
29log_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
38# Iterate executables in directory "fsfreeze-hook.d" with the specified args
39[ ! -d "$FSFREEZE_D" ] && exit 0
40
41for 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        "$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
59done
60
61exit 0
62