Reuse timezone code from containers/common

Replaces: https://github.com/containers/podman/pull/21077

[NO NEW TESTS NEEDED] Existing tests should handle this.

Signed-off-by: Sohan Kunkerkar <sohank2602@gmail.com>
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2024-02-01 20:04:30 -05:00
parent 85904e01f3
commit fcae702205
2 changed files with 11 additions and 70 deletions

View File

@ -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

View File

@ -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)
}