Skip to content

Commit 71a586b

Browse files
committed
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 #988
1 parent 4cd1c79 commit 71a586b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/cmd/run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ func runCommandWithFallbacks(container string,
333333
runFallbackCommandsIndex := 0
334334
runFallbackWorkDirsIndex := 0
335335
workDir := workingDirectory
336+
runFallbackWorkDirs := append([]string{"/run/host" + workDir}, runFallbackWorkDirs...)
336337

337338
for {
338339
execArgs := constructExecArgs(container,
@@ -585,7 +586,7 @@ func isPathPresent(container, path string) (bool, error) {
585586
"exec",
586587
"--user", currentUser.Username,
587588
container,
588-
"sh", "-c", "test -d \"$1\"", "sh", path,
589+
"sh", "-c", "test -d \"$1\" -a \"$1\" -ef \"/run/host/$1\"", "sh", path,
589590
}
590591

591592
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {

test/system/104-run.bats

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ teardown() {
163163
assert_output "other-container"
164164
}
165165

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

@@ -172,11 +172,11 @@ teardown() {
172172
popd
173173

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

0 commit comments

Comments
 (0)