From 82c4b2e81a76fc96c80072ebadf1019dbd5f1ce2 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 Signed-off-by: Ray Strode --- src/cmd/run.go | 3 ++- test/system/104-run.bats | 6 +++--- test/system/libs/bats-assert | 2 +- test/system/libs/bats-support | 2 +- 4 files changed, 7 insertions(+), 6 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 ] } diff --git a/test/system/libs/bats-assert b/test/system/libs/bats-assert index d750c5a1b..78fa631d1 160000 --- a/test/system/libs/bats-assert +++ b/test/system/libs/bats-assert @@ -1 +1 @@ -Subproject commit d750c5a1b44bf6fc96726aea76f4621db5fd602f +Subproject commit 78fa631d1370562d2cd4a1390989e706158e7bf0 diff --git a/test/system/libs/bats-support b/test/system/libs/bats-support index 24a72e143..3c8fadc50 160000 --- a/test/system/libs/bats-support +++ b/test/system/libs/bats-support @@ -1 +1 @@ -Subproject commit 24a72e14349690bcbf7c151b9d2d1cdd32d36eb1 +Subproject commit 3c8fadc5097c9acfc96d836dced2bb598e48b009