Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container information to .containerenv #2821

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isRootless := fmt.Sprintf("%v", unshare.IsRootless())

rootless := 0
if unshare.IsRootless() {
rootless = 1
}
// Populate the .containerenv with container information
containerenv := fmt.Sprintf(`\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird. Why is the \ here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes the code easier to read, i figure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That backslash makes it into the file, though, which is what makes it look weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok perhaps we should remove it then.

engine="buildah-%s"
name=%q
id=%q
image=%q
imageid=%q
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
imageid=%q
imageid=%q
rootless=%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