Skip to content

Commit

Permalink
cmd: Jump to /run/host/$PWD when entering toolbox
Browse files Browse the repository at this point in the history
As a convenience to users, when entering a container
toolbx tries to maintain the working directory from
the host. This works great if the user is inside
their home directory, for instance, since the
home directory is shared between host and container.

It can be confusing in other cases, though. The issue
is that the directory in the container may have the
same path as the directory in the host, but have
completely different contents. The old contents may
actually be in /run/host/$PWD instead.

Switching to /run/host/$PWD unconditionally has its own
downsides. For one, it's ugly, and also, in common cases,
like subdirectories of the home directory, it's
unnecessary.

This commit tries to find the balance, by making toolbx
check first if the directory is shared between host and
container, and if not, only then falling back to trying
/run/host/$PWD.

Closes containers#988

Signed-off-by: Ray Strode <[email protected]>
  • Loading branch information
halfline committed Dec 19, 2022
1 parent 4cd1c79 commit 82c4b2e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func runCommandWithFallbacks(container string,
runFallbackCommandsIndex := 0
runFallbackWorkDirsIndex := 0
workDir := workingDirectory
runFallbackWorkDirs := append([]string{"/run/host" + workDir}, runFallbackWorkDirs...)

for {
execArgs := constructExecArgs(container,
Expand Down Expand Up @@ -585,7 +586,7 @@ func isPathPresent(container, path string) (bool, error) {
"exec",
"--user", currentUser.Username,
container,
"sh", "-c", "test -d \"$1\"", "sh", path,
"sh", "-c", "test -d \"$1\" -a \"$1\" -ef \"/run/host/$1\"", "sh", path,
}

if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions test/system/104-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ teardown() {
assert_output "other-container"
}

@test "run: Ensure that $HOME is used as a fallback working directory" {
@test "run: Ensure that /run/host is used as a fallback working directory" {
local default_container_name="$(get_system_id)-toolbox-$(get_system_version)"
create_default_container

Expand All @@ -172,11 +172,11 @@ teardown() {
popd

assert_success
assert_line --index 0 "$HOME"
assert_line --index 0 "/run/host/etc/kernel"
assert [ ${#lines[@]} -eq 1 ]
lines=("${stderr_lines[@]}")
assert_line --index $((${#stderr_lines[@]}-2)) "Error: directory /etc/kernel not found in container $default_container_name"
assert_line --index $((${#stderr_lines[@]}-1)) "Using $HOME instead."
assert_line --index $((${#stderr_lines[@]}-1)) "Using /run/host/etc/kernel instead."
assert [ ${#stderr_lines[@]} -gt 2 ]
}

Expand Down
2 changes: 1 addition & 1 deletion test/system/libs/bats-support

0 comments on commit 82c4b2e

Please sign in to comment.