Skip to content

Commit fcae702

Browse files
committed
Reuse timezone code from containers/common
Replaces: #21077 [NO NEW TESTS NEEDED] Existing tests should handle this. Signed-off-by: Sohan Kunkerkar <[email protected]> Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 85904e0 commit fcae702

File tree

2 files changed

+11
-70
lines changed

2 files changed

+11
-70
lines changed

libpod/container_internal.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"io/fs"
1211
"os"
1312
"path/filepath"
1413
"strconv"
@@ -25,6 +24,7 @@ import (
2524
"github.com/containers/common/pkg/config"
2625
"github.com/containers/common/pkg/hooks"
2726
"github.com/containers/common/pkg/hooks/exec"
27+
"github.com/containers/common/pkg/timezone"
2828
cutil "github.com/containers/common/pkg/util"
2929
"github.com/containers/podman/v4/libpod/define"
3030
"github.com/containers/podman/v4/libpod/events"
@@ -1706,45 +1706,18 @@ func (c *Container) mountStorage() (_ string, deferredErr error) {
17061706
}
17071707

17081708
tz := c.Timezone()
1709-
if tz != "" {
1710-
timezonePath := filepath.Join("/usr/share/zoneinfo", tz)
1711-
if tz == "local" {
1712-
timezonePath, err = filepath.EvalSymlinks("/etc/localtime")
1713-
if err != nil {
1714-
return "", fmt.Errorf("finding local timezone for container %s: %w", c.ID(), err)
1715-
}
1716-
}
1717-
// make sure to remove any existing localtime file in the container to not create invalid links
1718-
err = unix.Unlinkat(etcInTheContainerFd, "localtime", 0)
1719-
if err != nil && !errors.Is(err, fs.ErrNotExist) {
1720-
return "", fmt.Errorf("removing /etc/localtime: %w", err)
1721-
}
1722-
1723-
hostPath, err := securejoin.SecureJoin(mountPoint, timezonePath)
1724-
if err != nil {
1725-
return "", fmt.Errorf("resolve zoneinfo path in the container: %w", err)
1709+
localTimePath, err := timezone.ConfigureContainerTimeZone(tz, c.state.RunDir, mountPoint, etcInTheContainerPath, c.ID())
1710+
if err != nil {
1711+
return "", fmt.Errorf("configuring timezone for container %s: %w", c.ID(), err)
1712+
}
1713+
if localTimePath != "" {
1714+
if err := c.relabel(localTimePath, c.config.MountLabel, false); err != nil {
1715+
return "", err
17261716
}
1727-
1728-
_, err = os.Stat(hostPath)
1729-
if err != nil {
1730-
// file does not exists which means tzdata is not installed in the container, just create /etc/locatime which a copy from the host
1731-
logrus.Debugf("Timezone %s does not exist in the container, create our own copy from the host", timezonePath)
1732-
localtimePath, err := c.copyTimezoneFile(timezonePath)
1733-
if err != nil {
1734-
return "", fmt.Errorf("setting timezone for container %s: %w", c.ID(), err)
1735-
}
1736-
if c.state.BindMounts == nil {
1737-
c.state.BindMounts = make(map[string]string)
1738-
}
1739-
c.state.BindMounts["/etc/localtime"] = localtimePath
1740-
} else {
1741-
// file exists lets just symlink according to localtime(5)
1742-
logrus.Debugf("Create locatime symlink for %s", timezonePath)
1743-
err = unix.Symlinkat(".."+timezonePath, etcInTheContainerFd, "localtime")
1744-
if err != nil {
1745-
return "", fmt.Errorf("creating /etc/localtime symlink: %w", err)
1746-
}
1717+
if c.state.BindMounts == nil {
1718+
c.state.BindMounts = make(map[string]string)
17471719
}
1720+
c.state.BindMounts["/etc/localtime"] = localTimePath
17481721
}
17491722

17501723
// Request a mount of all named volumes

libpod/container_internal_common.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,38 +2782,6 @@ func (c *Container) generatePasswdAndGroup() (string, string, error) {
27822782
return passwdPath, groupPath, nil
27832783
}
27842784

2785-
func (c *Container) copyTimezoneFile(zonePath string) (string, error) {
2786-
localtimeCopy := filepath.Join(c.state.RunDir, "localtime")
2787-
file, err := os.Stat(zonePath)
2788-
if err != nil {
2789-
return "", err
2790-
}
2791-
if file.IsDir() {
2792-
return "", errors.New("invalid timezone: is a directory")
2793-
}
2794-
src, err := os.Open(zonePath)
2795-
if err != nil {
2796-
return "", err
2797-
}
2798-
defer src.Close()
2799-
dest, err := os.Create(localtimeCopy)
2800-
if err != nil {
2801-
return "", err
2802-
}
2803-
defer dest.Close()
2804-
_, err = io.Copy(dest, src)
2805-
if err != nil {
2806-
return "", err
2807-
}
2808-
if err := c.relabel(localtimeCopy, c.config.MountLabel, false); err != nil {
2809-
return "", err
2810-
}
2811-
if err := dest.Chown(c.RootUID(), c.RootGID()); err != nil {
2812-
return "", err
2813-
}
2814-
return localtimeCopy, err
2815-
}
2816-
28172785
func (c *Container) cleanupOverlayMounts() error {
28182786
return overlay.CleanupContent(c.config.StaticDir)
28192787
}

0 commit comments

Comments
 (0)