scripts/qemu-guest-agent/fsfreeze-hook: Avoid bash-isms

The fsfreeze-hook script starts with #!/bin/sh, but it uses
several bash-specific constructs, resulting in misbehaviour
on guest systems where /bin/sh is some other POSIX shell.

Fix the simple ones reported by shellcheck:

In scripts/qemu-guest-agent/fsfreeze-hook line 27:
touch "$LOGFILE" &>/dev/null || USE_SYSLOG=1
                 ^---------^ SC3020 (warning): In POSIX sh, &> is undefined.

In scripts/qemu-guest-agent/fsfreeze-hook line 31:
    local message="$1"
    ^-----------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

In scripts/qemu-guest-agent/fsfreeze-hook line 46:
    log_message "Executing $file $@"
                                 ^-- SC2145 (error): Argument mixes string and array. Use * or separate argument.

In scripts/qemu-guest-agent/fsfreeze-hook line 55:
    if [ $STATUS -ne 0 ]; then
         ^-----^ SC2086 (info): Double quote to prevent globbing and word splitting.

There is also a use of PIPESTATUS that is more complex to fix;
that will be dealt with in a separate commit.

Cc: qemu-stable@nongnu.org
Fixes: 85978dfb6b ("qemu-ga: Optimize freeze-hook script logic of logging error")
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20260317094806.1944053-2-peter.maydell@linaro.org
Signed-off-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>
This commit is contained in:
Peter Maydell
2026-03-17 09:48:04 +00:00
committed by Kostiantyn Kostiuk
parent 770f50c14f
commit b5abb655fa

View File

@@ -24,15 +24,14 @@ USE_SYSLOG=0
# if log file is not writable, fallback to syslog
[ ! -w "$LOGFILE" ] && USE_SYSLOG=1
# try to update log file and fallback to syslog if it fails
touch "$LOGFILE" &>/dev/null || USE_SYSLOG=1
touch "$LOGFILE" >/dev/null 2>&1 || USE_SYSLOG=1
# Ensure the log file is writable, fallback to syslog if not
log_message() {
local message="$1"
if [ "$USE_SYSLOG" -eq 0 ]; then
printf "%s: %s\n" "$(date)" "$message" >>"$LOGFILE"
printf "%s: %s\n" "$(date)" "$1" >>"$LOGFILE"
else
logger -t qemu-ga-freeze-hook "$message"
logger -t qemu-ga-freeze-hook "$1"
fi
}
@@ -43,7 +42,7 @@ for file in "$FSFREEZE_D"/* ; do
is_ignored_file "$file" && continue
[ -x "$file" ] || continue
log_message "Executing $file $@"
log_message "Executing $file $*"
if [ "$USE_SYSLOG" -eq 0 ]; then
"$file" "$@" >>"$LOGFILE" 2>&1
STATUS=$?
@@ -52,7 +51,7 @@ for file in "$FSFREEZE_D"/* ; do
STATUS=${PIPESTATUS[0]}
fi
if [ $STATUS -ne 0 ]; then
if [ "$STATUS" -ne 0 ]; then
log_message "Error: $file finished with status=$STATUS"
else
log_message "$file finished successfully"