diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 361a342f3f..97d3a869e3 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/fs" "os" "path/filepath" "strconv" @@ -25,6 +24,7 @@ import ( "github.com/containers/common/pkg/config" "github.com/containers/common/pkg/hooks" "github.com/containers/common/pkg/hooks/exec" + "github.com/containers/common/pkg/timezone" cutil "github.com/containers/common/pkg/util" "github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/events" @@ -1706,45 +1706,18 @@ func (c *Container) mountStorage() (_ string, deferredErr error) { } tz := c.Timezone() - if tz != "" { - timezonePath := filepath.Join("/usr/share/zoneinfo", tz) - if tz == "local" { - timezonePath, err = filepath.EvalSymlinks("/etc/localtime") - if err != nil { - return "", fmt.Errorf("finding local timezone for container %s: %w", c.ID(), err) - } + localTimePath, err := timezone.ConfigureContainerTimeZone(tz, c.state.RunDir, mountPoint, etcInTheContainerPath, c.ID()) + if err != nil { + return "", fmt.Errorf("configuring timezone for container %s: %w", c.ID(), err) + } + if localTimePath != "" { + if err := c.relabel(localTimePath, c.config.MountLabel, false); err != nil { + return "", err } - // make sure to remove any existing localtime file in the container to not create invalid links - err = unix.Unlinkat(etcInTheContainerFd, "localtime", 0) - if err != nil && !errors.Is(err, fs.ErrNotExist) { - return "", fmt.Errorf("removing /etc/localtime: %w", err) - } - - hostPath, err := securejoin.SecureJoin(mountPoint, timezonePath) - if err != nil { - return "", fmt.Errorf("resolve zoneinfo path in the container: %w", err) - } - - _, err = os.Stat(hostPath) - if err != nil { - // file does not exists which means tzdata is not installed in the container, just create /etc/locatime which a copy from the host - logrus.Debugf("Timezone %s does not exist in the container, create our own copy from the host", timezonePath) - localtimePath, err := c.copyTimezoneFile(timezonePath) - if err != nil { - return "", fmt.Errorf("setting timezone for container %s: %w", c.ID(), err) - } - if c.state.BindMounts == nil { - c.state.BindMounts = make(map[string]string) - } - c.state.BindMounts["/etc/localtime"] = localtimePath - } else { - // file exists lets just symlink according to localtime(5) - logrus.Debugf("Create locatime symlink for %s", timezonePath) - err = unix.Symlinkat(".."+timezonePath, etcInTheContainerFd, "localtime") - if err != nil { - return "", fmt.Errorf("creating /etc/localtime symlink: %w", err) - } + if c.state.BindMounts == nil { + c.state.BindMounts = make(map[string]string) } + c.state.BindMounts["/etc/localtime"] = localTimePath } // Request a mount of all named volumes diff --git a/libpod/container_internal_common.go b/libpod/container_internal_common.go index cea8ea8240..dd6d1deb45 100644 --- a/libpod/container_internal_common.go +++ b/libpod/container_internal_common.go @@ -2782,38 +2782,6 @@ func (c *Container) generatePasswdAndGroup() (string, string, error) { return passwdPath, groupPath, nil } -func (c *Container) copyTimezoneFile(zonePath string) (string, error) { - localtimeCopy := filepath.Join(c.state.RunDir, "localtime") - file, err := os.Stat(zonePath) - if err != nil { - return "", err - } - if file.IsDir() { - return "", errors.New("invalid timezone: is a directory") - } - src, err := os.Open(zonePath) - if err != nil { - return "", err - } - defer src.Close() - dest, err := os.Create(localtimeCopy) - if err != nil { - return "", err - } - defer dest.Close() - _, err = io.Copy(dest, src) - if err != nil { - return "", err - } - if err := c.relabel(localtimeCopy, c.config.MountLabel, false); err != nil { - return "", err - } - if err := dest.Chown(c.RootUID(), c.RootGID()); err != nil { - return "", err - } - return localtimeCopy, err -} - func (c *Container) cleanupOverlayMounts() error { return overlay.CleanupContent(c.config.StaticDir) }