Skip to content

Commit

Permalink
another trial but not working
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <jasonyangshadow@gmail.com>
  • Loading branch information
JasonYangShadow committed Jan 9, 2024
1 parent f8ab155 commit 8e392d3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
13 changes: 0 additions & 13 deletions e2e/imgbuild/imgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -2265,25 +2265,12 @@ ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
e2e.ExpectExit(0),
)

// this image will print wrong value `1577880000`, correct value should be `1577847600`
c.env.RunApptainer(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("exec"),
e2e.WithEnv([]string{"TZ=Asia/Tokyo"}),
e2e.WithArgs(imagePath, "sh", "-c", `date --date "2020-01-01T12:00:00" +%s`),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ExactMatch, "1577880000"),
),
)

// correct one
c.env.RunApptainer(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("exec"),
e2e.WithEnv([]string{"TZ=Asia/Tokyo"}),
e2e.WithArgs("--disable-cache", "docker://centos:7", "sh", "-c", `date --date "2020-01-01T12:00:00" +%s`),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ExactMatch, "1577847600"),
),
Expand Down
3 changes: 0 additions & 3 deletions internal/pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,6 @@ func (b *Build) Full(ctx context.Context) error {
defer os.Remove(sessionHosts)
}

// patch /etc/localtime if this file inside container is a symlink
patchLocaltime(stage.b)

if stage.b.Recipe.BuildData.Post.Script != "" {
if err := stage.runPostScript(sessionResolv, sessionHosts); err != nil {
return fmt.Errorf("while running engine: %v", err)
Expand Down
23 changes: 0 additions & 23 deletions internal/pkg/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,3 @@ func currentEnvNoApptainer(permitted []string) []string {

return envs
}

func patchLocaltime(b *types.Bundle) {
const (
localtime = "/etc/localtime"
)
conFile := filepath.Join(b.RootfsPath, localtime)

// could not read container's /etc/localtime file, skip
if err := unix.Access(conFile, unix.R_OK); err != nil {
return
}

if st, err := os.Lstat(conFile); err == nil {
// container's /etc/localtime is a symlink file
if st.Mode()&os.ModeSymlink > 0 {
// unlink it
sylog.Verbosef("%s inside container is symlink, will unlink it", conFile)
if err := unix.Unlink(conFile); err != nil {
sylog.Warningf("while unlinking %s: %s", conFile, err)
}
}
}
}
33 changes: 33 additions & 0 deletions internal/pkg/runtime/engine/apptainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ func create(ctx context.Context, engine *EngineOperations, rpcOps *client.RPC, p
return err
}

if err := system.RunBeforeTag(mount.BindsTag, c.patchLocaltime); err != nil {
return err
}

if err := c.addRootfsMount(system); err != nil {
return err
}
Expand Down Expand Up @@ -3073,6 +3077,35 @@ func (c *container) gocryptfsMount(params *image.MountParams, system *mount.Syst
return c.mountImageDriver(params, system, mfunc)
}

func (c *container) patchLocaltime(system *mount.System) error {
const (
localtime = "/etc/localtime"
)

// first check whether container /etc/localtime is symlink
if fi, err := c.rpcOps.Lstat(filepath.Join(c.session.FinalPath(), localtime)); err == nil && fi.Mode()&os.ModeSymlink != 0 {
// then check whether host /etc/localtime is symlink and target exists inside container
if fi, err := os.Lstat(localtime); err == nil && fi.Mode()&os.ModeSymlink != 0 {
// get the link target of host /etc/localtime, it should be the format of '/etc/localtime -> ../usr/share/zoneinfo/UTC'
if p, err := os.Readlink(localtime); err == nil {
// check whether the link target exists inside container
containerTarget := p
// if the symlink target is not absolute path, needing to do a calculation
if !strings.HasPrefix(p, "/") {
containerTarget = filepath.Join(filepath.Dir(localtime), p)
}
if _, err := c.rpcOps.Stat(containerTarget); err == nil {
sylog.Verbosef("%s inside container is symlink, will patch it to: %s", localtime, p)
if err := c.session.AddSymlink(localtime, p); err != nil {
return fmt.Errorf("could not add symlink %s -> %s inside container, err: %v", localtime, p, err)
}
}
}
}
}
return nil
}

func (c *container) GetPwUID(uid uint32) (*user.User, error) {
if c.engine.EngineConfig.JSON.UserInfo.Username == "" {
return nil, osuser.UnknownUserIdError(uid)
Expand Down

0 comments on commit 8e392d3

Please sign in to comment.