Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
PR #1737: ch-run: set default HOME value
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaudill authored Oct 19, 2023
1 parent 7b6544f commit 88a47f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bin/ch-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,12 @@ void fix_environment(struct args *args)
char *old_value, *new_value;

// $HOME: If --home, set to “/home/$USER”.
if (args->c.host_home)
if (args->c.host_home) {
Z_ (setenv("HOME", cat("/home/", username), 1));
} else if (path_exists("/root", NULL, true)) {
Z_ (setenv("HOME", "/root", 1));
} else
Z_ (setenv("HOME", "/", 1));

// $PATH: Append /bin if not already present.
old_value = getenv("PATH");
Expand Down
2 changes: 2 additions & 0 deletions doc/ch-run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ Environment variables
:code:`ch-run` leaves environment variables unchanged, i.e. the host
environment is passed through unaltered, except:

* by default (:code:`--home` not specified), :code:`HOME` is set to
:code:`/root`, if it exists, and :code:`/` otherwise.
* limited tweaks to avoid significant guest breakage;
* user-set variables via :code:`--set-env`;
* user-unset variables via :code:`--unset-env`; and
Expand Down
2 changes: 2 additions & 0 deletions examples/chtest/Build
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ chmod 0777 img/maxperms_file
mkdir img/maxperms_dir
chmod 1777 img/maxperms_dir

# Get rid of “/root” directory, used for “HOME” test in “ch-run_misc.bats”.
rmdir "$img"/root

## Tar it up.

Expand Down
12 changes: 11 additions & 1 deletion test/run/ch-run_misc.bats
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,27 @@ EOF
}

@test "\$HOME" {
[[ $CH_TEST_BUILDER != 'none' ]] || skip 'image builder required'
LC_ALL=C

scope quick
echo "host: $HOME"
[[ $HOME ]]
[[ $USER ]]

# default: no change
# shellcheck disable=SC2016
run ch-run "${ch_imgdir}"/quick -- /bin/sh -c 'echo $HOME'
echo "$output"
[[ $status -eq 0 ]]
[[ $output = "/root" ]]

# default: no “/root”
# shellcheck disable=SC2016
run ch-run "$ch_timg" -- /bin/sh -c 'echo $HOME'
echo "$output"
[[ $status -eq 0 ]]
[[ $output = "$HOME" ]]
[[ $output = "/" ]]

# set $HOME if --home
# shellcheck disable=SC2016
Expand Down

0 comments on commit 88a47f8

Please sign in to comment.