From d7eaf42727b57f5e1de1f8f912faa1d158d2024c Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Wed, 30 Apr 2025 08:00:54 -0500 Subject: [PATCH] Do not error on tz detection In cases where systemd was not available, podman machine was erroring out using timedatectl (it requires systemd). on other providers like windows, we don't do any timezone detection so it seems valid to return a "" for timezone. This fixes the first problem described #25950. Fixes: https://github.com/containers/podman/issues/25950 Signed-off-by: Brent Baude --- pkg/machine/ignition/ignition.go | 2 +- pkg/machine/ignition/ignition_linux.go | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pkg/machine/ignition/ignition.go b/pkg/machine/ignition/ignition.go index 3995ef714c..d6206ff658 100644 --- a/pkg/machine/ignition/ignition.go +++ b/pkg/machine/ignition/ignition.go @@ -151,7 +151,7 @@ func (ign *DynamicIgnition) GenerateIgnitionConfig() error { if ign.TimeZone == "local" { tz, err = getLocalTimeZone() if err != nil { - return err + return fmt.Errorf("error getting local timezone: %q", err) } } else { tz = ign.TimeZone diff --git a/pkg/machine/ignition/ignition_linux.go b/pkg/machine/ignition/ignition_linux.go index c6cce2a90c..8ca303cbdb 100644 --- a/pkg/machine/ignition/ignition_linux.go +++ b/pkg/machine/ignition/ignition_linux.go @@ -1,20 +1,29 @@ package ignition import ( - "errors" "os" "os/exec" "strings" + + "github.com/sirupsen/logrus" ) func getLocalTimeZone() (string, error) { + trimTzFunc := func(s string) string { + return strings.TrimPrefix(strings.TrimSuffix(s, "\n"), "Timezone=") + } + + // perform a variety of ways to see if we can determine the tz output, err := exec.Command("timedatectl", "show", "--property=Timezone").Output() - if errors.Is(err, exec.ErrNotFound) { - output, err = os.ReadFile("/etc/timezone") + if err == nil { + return trimTzFunc(string(output)), nil } - if err != nil { - return "", err + logrus.Debugf("Timedatectl show --property=Timezone failed: %s", err) + output, err = os.ReadFile("/etc/timezone") + if err == nil { + return trimTzFunc(string(output)), nil } - // Remove prepended field and the newline - return strings.TrimPrefix(strings.TrimSuffix(string(output), "\n"), "Timezone="), nil + logrus.Debugf("unable to read /etc/timezone, falling back to empty timezone: %s", err) + // if we cannot determine the tz, return empty string + return "", nil }