Commit e1d00ea443 for qemu.org
commit e1d00ea443da6f15dc10aea8c3ab4ad7ffeb68cc
Author: Roman Bogorodskiy <bogorodskiy@gmail.com>
Date: Thu Oct 15 16:23:42 2026 +0200
qga: do not run hwclock(8) on non-Linux POSIX OSes
Currently, qmp_guest_set_time() sets system time using
the settimeofday(2) system call and then runs the hwclock(8)
util to set hardware time.
hwclock(8) is Linux only, so current implementation
always fails while trying to run it on non-Linux POSIX OSes,
such as BSDs (FreeBSD, OpenBSD, NetBSD) and illumos.
Update the code not to run it on non-Linux. The settimeofday(2)
call sets both system and hardware time on these systems.
For example, on FreeBSD settimeofday() calls settime()
which calls resettodr() [1] which updates RTC clock [2].
It is similar on OpenBSD and NetBSD, though implementations
differ a bit.
Additional detail: on FreeBSD RTC clock setting can be
disabled with the `machdep.disable_rtc_set` sysctl.
As we cannot do anything about it, do not handle that
case separately.
Non-Linux POSIX OSes do not provide a common utility
equivalent to hwclock -s, so report an unsupported operation
when no time is supplied.
1: https://cgit.freebsd.org/src/tree/sys/kern/kern_time.c
2: https://cgit.freebsd.org/src/tree/sys/kern/subr_rtc.c
Signed-off-by: Roman Bogorodskiy <bogorodskiy@gmail.com>
Reviewed-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>
Link: https://lore.kernel.org/qemu-devel/20261015142342.50436-1-bogorodskiy@gmail.com
Signed-off-by: Kostiantyn Kostiuk <kkostiuk@redhat.com>
diff --git a/qga/commands-posix.c b/qga/commands-posix.c
index dcc683f6c9..cfa5627a61 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -322,6 +322,20 @@ void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
}
}
+#if !defined(CONFIG_LINUX)
+ /*
+ * BSDs (FreeBSD, OpenBSD, NetBSD) and illumos do not have the
+ * hwclock(8) tool. These systems update hardware clock when
+ * settimeofday() sets system time, so no additional actions
+ * are needed. Setting system time from RTC is not supported.
+ */
+ if (!has_time) {
+ error_setg(errp, "Setting system time from RTC is not supported");
+ }
+
+ return;
+#endif
+
/* Now, if user has passed a time to set and the system time is set, we
* just need to synchronize the hardware clock. However, if no time was
* passed, user is requesting the opposite: set the system time from the