diff --git a/run_linux.go b/run_linux.go index 4decac12934..d20d39423aa 100644 --- a/run_linux.go +++ b/run_linux.go @@ -216,16 +216,28 @@ func (b *Builder) Run(command []string, options RunOptions) error { } // Empty file, so no need to recreate if it exists if _, ok := bindFiles["/run/.containerenv"]; !ok { - // Empty string for now, but we may consider populating this later containerenvPath := filepath.Join(path, "/run/.containerenv") if err = os.MkdirAll(filepath.Dir(containerenvPath), 0755); err != nil { return err } - emptyFile, err := os.Create(containerenvPath) - if err != nil { + + rootless := 0 + if unshare.IsRootless() { + rootless = 1 + } + // Populate the .containerenv with container information + containerenv := fmt.Sprintf(`\ +engine="buildah-%s" +name=%q +id=%q +image=%q +imageid=%q +rootless=%d +`, Version, b.Container, b.ContainerID, b.FromImage, b.FromImageID, rootless) + + if err = ioutils.AtomicWriteFile(containerenvPath, []byte(containerenv), 0755); err != nil { return err } - emptyFile.Close() if err := label.Relabel(containerenvPath, b.MountLabel, false); err != nil { return err } diff --git a/tests/run.bats b/tests/run.bats index c22a671dcf1..352f74fefea 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -391,6 +391,23 @@ function configure_and_check_user() { # test a standard mount to /run/.containerenv run_buildah run $cid ls -1 /run/.containerenv expect_output --substring "/run/.containerenv" + + run_buildah run $cid sh -c '. /run/.containerenv; echo $engine' + expect_output --substring "buildah" + + run_buildah run $cid sh -c '. /run/.containerenv; echo $name' + expect_output "alpine-working-container" + + run_buildah run $cid sh -c '. /run/.containerenv; echo $image' + expect_output --substring "alpine:latest" + + rootless=0 + if ["$(id -u)" -ne 0 ]; then + rootless=1 + fi + + run_buildah run $cid sh -c '. /run/.containerenv; echo $rootless' + expect_output ${rootless} } @test "run-device" {