Merge pull request #26029 from Luap99/machine-tz

pkg/machine: more timezone fixes
This commit is contained in:
openshift-merge-bot[bot]
2025-05-05 21:00:35 +00:00
committed by GitHub
3 changed files with 54 additions and 41 deletions

View File

@ -279,6 +279,12 @@ var _ = Describe("podman machine init", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(timezoneSession).To(Exit(0)) Expect(timezoneSession).To(Exit(0))
Expect(timezoneSession.outputToString()).To(ContainSubstring("HST")) Expect(timezoneSession.outputToString()).To(ContainSubstring("HST"))
sshTimezone = sshMachine{}
timezoneSession, err = mb.setName(name).setCmd(sshTimezone.withSSHCommand([]string{"timedatectl show --property Timezone"})).run()
Expect(err).ToNot(HaveOccurred())
Expect(timezoneSession).To(Exit(0))
Expect(timezoneSession.outputToString()).To(ContainSubstring("Pacific/Honolulu"))
}) })
It("machine init with swap", func() { It("machine init with swap", func() {

View File

@ -143,36 +143,43 @@ func (ign *DynamicIgnition) GenerateIgnitionConfig() error {
// Add or set the time zone for the machine // Add or set the time zone for the machine
if len(ign.TimeZone) > 0 { if len(ign.TimeZone) > 0 {
var ( var err error
err error tz := ign.TimeZone
tz string
)
// local means the same as the host // local means the same as the host
// look up where it is pointing to on the host // look up where it is pointing to on the host
if ign.TimeZone == "local" { if ign.TimeZone == "local" {
tz, err = getLocalTimeZone() if env, ok := os.LookupEnv("TZ"); ok {
if err != nil { tz = env
return fmt.Errorf("error getting local timezone: %q", err) } else {
tz, err = getLocalTimeZone()
if err != nil {
return fmt.Errorf("error getting local timezone: %q", err)
}
} }
}
// getLocalTimeZone() can return empty string, do not add broken symlink in that case
// coreos will default to UTC
if tz == "" {
logrus.Info("Unable to determine local timezone, machine will default to UTC")
} else { } else {
tz = ign.TimeZone tzLink := Link{
Node: Node{
Group: GetNodeGrp("root"),
Path: "/etc/localtime",
Overwrite: BoolToPtr(false),
User: GetNodeUsr("root"),
},
LinkEmbedded1: LinkEmbedded1{
Hard: BoolToPtr(false),
// We always want this value in unix form (../usr/share/zoneinfo) because this is being
// set in the machine OS (always Linux) and systemd needs the relative symlink. However,
// filepath.join on windows will use a "\\" separator so use path.Join() which always
// uses the slash.
Target: path.Join("../usr/share/zoneinfo", tz),
},
}
ignStorage.Links = append(ignStorage.Links, tzLink)
} }
tzLink := Link{
Node: Node{
Group: GetNodeGrp("root"),
Path: "/etc/localtime",
Overwrite: BoolToPtr(false),
User: GetNodeUsr("root"),
},
LinkEmbedded1: LinkEmbedded1{
Hard: BoolToPtr(false),
// We always want this value in unix form (/path/to/something) because this is being
// set in the machine OS (always Linux). However, filepath.join on windows will use a "\\"
// separator; therefore we use ToSlash to convert the path to unix style
Target: filepath.ToSlash(filepath.Join("/usr/share/zoneinfo", tz)),
},
}
ignStorage.Links = append(ignStorage.Links, tzLink)
} }
// This service gets environment variables that are provided // This service gets environment variables that are provided

View File

@ -1,29 +1,29 @@
package ignition package ignition
import ( import (
"errors"
"os" "os"
"os/exec" "path/filepath"
"strings" "strings"
"github.com/sirupsen/logrus"
) )
func getLocalTimeZone() (string, error) { func getLocalTimeZone() (string, error) {
trimTzFunc := func(s string) string { path, err := filepath.EvalSymlinks("/etc/localtime")
return strings.TrimPrefix(strings.TrimSuffix(s, "\n"), "Timezone=") if err != nil {
// of the path does not exist, ignore it as the code default to UTC then
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", err
} }
// perform a variety of ways to see if we can determine the tz // Allow using TZDIR per:
output, err := exec.Command("timedatectl", "show", "--property=Timezone").Output() // https://sourceware.org/git/?p=glibc.git;a=blob;f=time/tzfile.c;h=8a923d0cccc927a106dc3e3c641be310893bab4e;hb=HEAD#l149
if err == nil { zoneinfo := os.Getenv("TZDIR")
return trimTzFunc(string(output)), nil if zoneinfo == "" {
// default zoneinfo location
zoneinfo = "/usr/share/zoneinfo"
} }
logrus.Debugf("Timedatectl show --property=Timezone failed: %s", err) // Trim of the TZDIR part to extract the actual timezone name
output, err = os.ReadFile("/etc/timezone") return strings.TrimPrefix(path, filepath.Clean(zoneinfo)+"/"), nil
if err == nil {
return trimTzFunc(string(output)), 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
} }