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 <bbaude@redhat.com>
This commit is contained in:
Brent Baude
2025-04-30 08:00:54 -05:00
parent dd8fbb7582
commit d7eaf42727
2 changed files with 17 additions and 8 deletions

View File

@ -151,7 +151,7 @@ func (ign *DynamicIgnition) GenerateIgnitionConfig() error {
if ign.TimeZone == "local" { if ign.TimeZone == "local" {
tz, err = getLocalTimeZone() tz, err = getLocalTimeZone()
if err != nil { if err != nil {
return err return fmt.Errorf("error getting local timezone: %q", err)
} }
} else { } else {
tz = ign.TimeZone tz = ign.TimeZone

View File

@ -1,20 +1,29 @@
package ignition package ignition
import ( import (
"errors"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"github.com/sirupsen/logrus"
) )
func getLocalTimeZone() (string, error) { 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() output, err := exec.Command("timedatectl", "show", "--property=Timezone").Output()
if errors.Is(err, exec.ErrNotFound) { if err == nil {
output, err = os.ReadFile("/etc/timezone") return trimTzFunc(string(output)), nil
} }
if err != nil { logrus.Debugf("Timedatectl show --property=Timezone failed: %s", err)
return "", err output, err = os.ReadFile("/etc/timezone")
if err == nil {
return trimTzFunc(string(output)), nil
} }
// Remove prepended field and the newline logrus.Debugf("unable to read /etc/timezone, falling back to empty timezone: %s", err)
return strings.TrimPrefix(strings.TrimSuffix(string(output), "\n"), "Timezone="), nil // if we cannot determine the tz, return empty string
return "", nil
} }