From e0ec24beaf925bd054c8569d04f823d0940ce192 Mon Sep 17 00:00:00 2001 From: David Trudgian Date: Fri, 16 Jun 2023 11:50:38 +0100 Subject: [PATCH] fix: set correct $HOME in --oci mode when mount home = no When `mount home = no` is set in `singularity.conf`, we still need to set the correct value for `$HOME` in the container... we just don't want to mount it. Fixes sylabs/singularity#1783 Signed-off-by: Edita Kizinevic --- e2e/config/oci.go | 11 ++++++++ .../pkg/runtime/launcher/oci/mounts_linux.go | 25 ++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/e2e/config/oci.go b/e2e/config/oci.go index 0e5295c5d0..a1e4386255 100644 --- a/e2e/config/oci.go +++ b/e2e/config/oci.go @@ -215,6 +215,17 @@ func (c configTests) ociConfigGlobal(t *testing.T) { directiveValue: "no", exit: 1, }, + // Verify that though mount is skipped, $HOME is still set correctly + // https://github.com/sylabs/singularity/issues/1783 + { + name: "MountHomeNoCorrectDir", + argv: []string{archiveRef, "sh", "-c", "test $HOME == " + e2e.OCIUserProfile.ContainerUser(t).Dir}, + profile: e2e.OCIUserProfile, + cwd: "/", + directive: "mount home", + directiveValue: "no", + exit: 0, + }, { name: "MountHomeYes", argv: []string{archiveRef, "grep", e2e.OCIUserProfile.ContainerUser(t).Dir, "/proc/self/mountinfo"}, diff --git a/internal/pkg/runtime/launcher/oci/mounts_linux.go b/internal/pkg/runtime/launcher/oci/mounts_linux.go index 16d013e15e..b8691da58e 100644 --- a/internal/pkg/runtime/launcher/oci/mounts_linux.go +++ b/internal/pkg/runtime/launcher/oci/mounts_linux.go @@ -265,13 +265,17 @@ func (l *Launcher) addSysMount(mounts *[]specs.Mount) error { return nil } -// addHomeMount adds a user home directory as a tmpfs mount. We are currently -// emulating `--compat` / `--containall`, so the user must specifically bind in -// their home directory from the host for it to be available. +// addHomeMount adds a user home directory as a tmpfs mount, and sets the +// container home directory. We are currently emulating `--compat` / +// `--containall`, so the user must specifically bind in their home directory +// from the host for it to be available. func (l *Launcher) addHomeMount(mounts *[]specs.Mount) error { + // If the $HOME mount is skipped by config need to still handle setting the + // correct $HOME dir, but just skip adding the mount. + skipMount := false if !l.apptainerConf.MountHome { sylog.Debugf("Skipping mount of $HOME due to apptainer.conf") - return nil + skipMount = true } // Get the host user's data @@ -295,6 +299,10 @@ func (l *Launcher) addHomeMount(mounts *[]specs.Mount) error { homeDest := homeSlice[1] l.cfg.HomeDir = homeDest + if skipMount { + return nil + } + // Since the home dir is a bind-mount in this case, we don't have to mount a tmpfs directory for the in-container home dir, and we can just do the bind-mount & return. return addBindMount(mounts, bind.Path{ Source: homeSrc, @@ -305,6 +313,11 @@ func (l *Launcher) addHomeMount(mounts *[]specs.Mount) error { // If we're running in fake-root mode (and we haven't requested a custom home dir), we do need to create a tmpfs mount for the home dir, but it's a special case (because of its location & permissions), so we handle that here & return. if l.cfg.Fakeroot { l.cfg.HomeDir = "/root" + + if skipMount { + return nil + } + *mounts = append(*mounts, specs.Mount{ Destination: "/root", @@ -325,6 +338,10 @@ func (l *Launcher) addHomeMount(mounts *[]specs.Mount) error { l.cfg.HomeDir = pw.Dir } + if skipMount { + return nil + } + // If we've not hit a special case (bind-mounted custom home dir, or fakeroot), then create a tmpfs mount as a home dir in the requested location (whether it's custom or not; by this point, l.cfg.HomeDir will reflect the right value). *mounts = append(*mounts, specs.Mount{