From 71a586b7c0d40a6f3698d071eaf4d981205ea70f Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Mon, 19 Dec 2022 09:32:31 -0500 Subject: [PATCH] cmd: Jump to /run/host/$PWD when entering toolbox 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 https://github.com/containers/toolbox/issues/988 --- src/cmd/run.go | 3 ++- test/system/104-run.bats | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cmd/run.go b/src/cmd/run.go index 25a8f7305..a5722e9d8 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -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, @@ -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 { diff --git a/test/system/104-run.bats b/test/system/104-run.bats index a7080de56..f2e989565 100644 --- a/test/system/104-run.bats +++ b/test/system/104-run.bats @@ -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 @@ -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 ] }