Skip to content

Commit 5e081e4

Browse files
Merge pull request #21332 from rhatdan/timezone
Reuse timezone code from containers/common
2 parents 9ad07d1 + fcae702 commit 5e081e4

File tree

7 files changed

+141
-70
lines changed

7 files changed

+141
-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"
@@ -1722,45 +1722,18 @@ func (c *Container) mountStorage() (_ string, deferredErr error) {
17221722
}
17231723

17241724
tz := c.Timezone()
1725-
if tz != "" {
1726-
timezonePath := filepath.Join("/usr/share/zoneinfo", tz)
1727-
if tz == "local" {
1728-
timezonePath, err = filepath.EvalSymlinks("/etc/localtime")
1729-
if err != nil {
1730-
return "", fmt.Errorf("finding local timezone for container %s: %w", c.ID(), err)
1731-
}
1732-
}
1733-
// make sure to remove any existing localtime file in the container to not create invalid links
1734-
err = unix.Unlinkat(etcInTheContainerFd, "localtime", 0)
1735-
if err != nil && !errors.Is(err, fs.ErrNotExist) {
1736-
return "", fmt.Errorf("removing /etc/localtime: %w", err)
1737-
}
1738-
1739-
hostPath, err := securejoin.SecureJoin(mountPoint, timezonePath)
1740-
if err != nil {
1741-
return "", fmt.Errorf("resolve zoneinfo path in the container: %w", err)
1725+
localTimePath, err := timezone.ConfigureContainerTimeZone(tz, c.state.RunDir, mountPoint, etcInTheContainerPath, c.ID())
1726+
if err != nil {
1727+
return "", fmt.Errorf("configuring timezone for container %s: %w", c.ID(), err)
1728+
}
1729+
if localTimePath != "" {
1730+
if err := c.relabel(localTimePath, c.config.MountLabel, false); err != nil {
1731+
return "", err
17421732
}
1743-
1744-
_, err = os.Stat(hostPath)
1745-
if err != nil {
1746-
// file does not exists which means tzdata is not installed in the container, just create /etc/locatime which a copy from the host
1747-
logrus.Debugf("Timezone %s does not exist in the container, create our own copy from the host", timezonePath)
1748-
localtimePath, err := c.copyTimezoneFile(timezonePath)
1749-
if err != nil {
1750-
return "", fmt.Errorf("setting timezone for container %s: %w", c.ID(), err)
1751-
}
1752-
if c.state.BindMounts == nil {
1753-
c.state.BindMounts = make(map[string]string)
1754-
}
1755-
c.state.BindMounts["/etc/localtime"] = localtimePath
1756-
} else {
1757-
// file exists lets just symlink according to localtime(5)
1758-
logrus.Debugf("Create locatime symlink for %s", timezonePath)
1759-
err = unix.Symlinkat(".."+timezonePath, etcInTheContainerFd, "localtime")
1760-
if err != nil {
1761-
return "", fmt.Errorf("creating /etc/localtime symlink: %w", err)
1762-
}
1733+
if c.state.BindMounts == nil {
1734+
c.state.BindMounts = make(map[string]string)
17631735
}
1736+
c.state.BindMounts["/etc/localtime"] = localTimePath
17641737
}
17651738

17661739
// 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
}

vendor/github.com/containers/common/pkg/timezone/timezone.go

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containers/common/pkg/timezone/timezone_linux.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containers/common/pkg/timezone/timezone_unix.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containers/common/pkg/timezone/timezone_windows.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ github.com/containers/common/pkg/supplemented
229229
github.com/containers/common/pkg/sysinfo
230230
github.com/containers/common/pkg/systemd
231231
github.com/containers/common/pkg/timetype
232+
github.com/containers/common/pkg/timezone
232233
github.com/containers/common/pkg/umask
233234
github.com/containers/common/pkg/util
234235
github.com/containers/common/pkg/version

0 commit comments

Comments
 (0)