From 92ce4be9e56dd87e99ceeb9094cfdfb0ed6e29c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 25 Oct 2024 09:32:33 +0200 Subject: [PATCH] Avoid warning message when /dev/rtc does not exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For instance the Raspberry Pi does not have any real-time clock, so rely on network time synchronization instead (systemd-timesyncd). Signed-off-by: Anders F Björklund --- pkg/guestagent/guestagent_linux.go | 5 +++++ pkg/guestagent/timesync/timesync_linux.go | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/pkg/guestagent/guestagent_linux.go b/pkg/guestagent/guestagent_linux.go index 141d8d9f0f2..8fe3857a68c 100644 --- a/pkg/guestagent/guestagent_linux.go +++ b/pkg/guestagent/guestagent_linux.go @@ -324,6 +324,11 @@ const deltaLimit = 2 * time.Second func (a *agent) fixSystemTimeSkew() { for { + ok, err := timesync.HasRTC() + if !ok { + logrus.Warnf("fixSystemTimeSkew: error: %s", err.Error()) + break + } ticker := time.NewTicker(10 * time.Second) for now := range ticker.C { rtc, err := timesync.GetRTCTime() diff --git a/pkg/guestagent/timesync/timesync_linux.go b/pkg/guestagent/timesync/timesync_linux.go index 3ad02fb5f52..38f88fc8f5e 100644 --- a/pkg/guestagent/timesync/timesync_linux.go +++ b/pkg/guestagent/timesync/timesync_linux.go @@ -1,6 +1,7 @@ package timesync import ( + "errors" "os" "time" @@ -9,6 +10,11 @@ import ( const rtc = "/dev/rtc" +func HasRTC() (bool, error) { + _, err := os.Stat(rtc) + return !errors.Is(err, os.ErrNotExist), err +} + func GetRTCTime() (t time.Time, err error) { f, err := os.Open(rtc) if err != nil {