Skip to content

Commit

Permalink
Add container information to .containerenv
Browse files Browse the repository at this point in the history
We have been asked to leak some container information
and image information into the container to be used
by certain tools. (Toolbox and others)

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Nov 24, 2020
1 parent af10f8c commit d9a7f13
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
20 changes: 16 additions & 4 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit d9a7f13

Please sign in to comment.