From 7bf4073025c6388e1a7c0d8dfa7906ea382c7033 Mon Sep 17 00:00:00 2001 From: casparvl casparvl Date: Wed, 18 Sep 2024 10:20:40 +0000 Subject: [PATCH 1/5] Make sure the run_cmd respects sysroot when picking the shell, i.e. use sysroot/bin/bash instead of /bin/bash if sysroot is set --- easybuild/tools/run.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index 30f413be55..eee95048b7 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -226,7 +226,11 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True if cmd_log: cmd_log.write("# output for command: %s\n\n" % cmd_msg) - exec_cmd = "/bin/bash" + sysroot = build_option('sysroot') + if sysroot: + exec_cmd = "%s/bin/bash" % sysroot + else: + exec_cmd = "/bin/bash" if not shell: if isinstance(cmd, list): From 73a53a8bae7ca6b3dd1b172c620801eb44aa7c54 Mon Sep 17 00:00:00 2001 From: casparvl casparvl Date: Wed, 18 Sep 2024 14:16:21 +0000 Subject: [PATCH 2/5] Make sure we only check for sysroot after set_up_configuration is called. Any command run before that with run_cmd should use the regular /bin/bash --- easybuild/tools/run.py | 14 +++++++++----- easybuild/tools/systemtools.py | 33 +++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index eee95048b7..0d34e44b3d 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -134,7 +134,7 @@ def get_output_from_process(proc, read_size=None, asynchronous=False): @run_cmd_cache def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True, log_output=False, path=None, force_in_dry_run=False, verbose=True, shell=None, trace=True, stream_output=None, asynchronous=False, - with_hooks=True): + with_hooks=True, with_sysroot=True): """ Run specified command (in a subshell) :param cmd: command to run @@ -152,6 +152,7 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True :param stream_output: enable streaming command output to stdout :param asynchronous: run command asynchronously (returns subprocess.Popen instance if set to True) :param with_hooks: trigger pre/post run_shell_cmd hooks (if defined) + :param with_sysroot: prepend sysroot to exec_cmd (if defined) """ cwd = os.getcwd() @@ -226,11 +227,14 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True if cmd_log: cmd_log.write("# output for command: %s\n\n" % cmd_msg) - sysroot = build_option('sysroot') - if sysroot: - exec_cmd = "%s/bin/bash" % sysroot + if with_sysroot: + sysroot = build_option('sysroot') + if sysroot: + exec_cmd = "%s/bin/bash" % sysroot + else: + exec_cmd = "/bin/bash" else: - exec_cmd = "/bin/bash" + exec_cmd = "/bin/bash" if not shell: if isinstance(cmd, list): diff --git a/easybuild/tools/systemtools.py b/easybuild/tools/systemtools.py index 24e910fb94..68c42fe416 100644 --- a/easybuild/tools/systemtools.py +++ b/easybuild/tools/systemtools.py @@ -275,7 +275,8 @@ def get_avail_core_count(): core_cnt = int(sum(sched_getaffinity())) else: # BSD-type systems - out, _ = run_cmd('sysctl -n hw.ncpu', force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, _ = run_cmd('sysctl -n hw.ncpu', force_in_dry_run=True, trace=False, stream_output=False, + with_hooks=False, with_sysroot=False) try: if int(out) > 0: core_cnt = int(out) @@ -312,7 +313,8 @@ def get_total_memory(): elif os_type == DARWIN: cmd = "sysctl -n hw.memsize" _log.debug("Trying to determine total memory size on Darwin via cmd '%s'", cmd) - out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False, + with_sysroot=False) if ec == 0: memtotal = int(out.strip()) // (1024**2) @@ -394,7 +396,8 @@ def get_cpu_vendor(): elif os_type == DARWIN: cmd = "sysctl -n machdep.cpu.vendor" - out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False, with_hooks=False) + out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False, + with_hooks=False, with_sysroot=False) out = out.strip() if ec == 0 and out in VENDOR_IDS: vendor = VENDOR_IDS[out] @@ -402,7 +405,7 @@ def get_cpu_vendor(): else: cmd = "sysctl -n machdep.cpu.brand_string" out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False, - with_hooks=False) + with_hooks=False, with_sysroot=False) out = out.strip().split(' ')[0] if ec == 0 and out in CPU_VENDORS: vendor = out @@ -505,7 +508,8 @@ def get_cpu_model(): elif os_type == DARWIN: cmd = "sysctl -n machdep.cpu.brand_string" - out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False, + with_sysroot=False) if ec == 0: model = out.strip() _log.debug("Determined CPU model on Darwin using cmd '%s': %s" % (cmd, model)) @@ -550,7 +554,8 @@ def get_cpu_speed(): elif os_type == DARWIN: cmd = "sysctl -n hw.cpufrequency_max" _log.debug("Trying to determine CPU frequency on Darwin via cmd '%s'" % cmd) - out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False, + with_sysroot=False) out = out.strip() cpu_freq = None if ec == 0 and out: @@ -599,7 +604,7 @@ def get_cpu_features(): cmd = "sysctl -n machdep.cpu.%s" % feature_set _log.debug("Trying to determine CPU features on Darwin via cmd '%s'", cmd) out, ec = run_cmd(cmd, force_in_dry_run=True, trace=False, stream_output=False, log_ok=False, - with_hooks=False) + with_hooks=False, with_sysroot=False) if ec == 0: cpu_feat.extend(out.strip().lower().split()) @@ -626,8 +631,8 @@ def get_gpu_info(): try: cmd = "nvidia-smi --query-gpu=gpu_name,driver_version --format=csv,noheader" _log.debug("Trying to determine NVIDIA GPU info on Linux via cmd '%s'", cmd) - out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, - force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True, + trace=False, stream_output=False, with_hooks=False, with_sysroot=False) if ec == 0: for line in out.strip().split('\n'): nvidia_gpu_info = gpu_info.setdefault('NVIDIA', {}) @@ -645,15 +650,15 @@ def get_gpu_info(): try: cmd = "rocm-smi --showdriverversion --csv" _log.debug("Trying to determine AMD GPU driver on Linux via cmd '%s'", cmd) - out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, - force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True, + trace=False, stream_output=False, with_hooks=False, with_sysroot=False) if ec == 0: amd_driver = out.strip().split('\n')[1].split(',')[1] cmd = "rocm-smi --showproductname --csv" _log.debug("Trying to determine AMD GPU info on Linux via cmd '%s'", cmd) - out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, - force_in_dry_run=True, trace=False, stream_output=False, with_hooks=False) + out, ec = run_cmd(cmd, simple=False, log_ok=False, log_all=False, force_in_dry_run=True, + trace=False, stream_output=False, with_hooks=False, with_sysroot=False) if ec == 0: for line in out.strip().split('\n')[1:]: amd_card_series = line.split(',')[1] @@ -900,7 +905,7 @@ def get_tool_version(tool, version_option='--version', ignore_ec=False): Output is returned as a single-line string (newlines are replaced by '; '). """ out, ec = run_cmd(' '.join([tool, version_option]), simple=False, log_ok=False, force_in_dry_run=True, - trace=False, stream_output=False, with_hooks=False) + trace=False, stream_output=False, with_hooks=False, with_sysroot=False) if not ignore_ec and ec: _log.warning("Failed to determine version of %s using '%s %s': %s" % (tool, tool, version_option, out)) return UNKNOWN From 347e64cb8a0a8643dcb0a6e7526c5d612cec07b6 Mon Sep 17 00:00:00 2001 From: casparvl casparvl Date: Wed, 18 Sep 2024 14:22:57 +0000 Subject: [PATCH 3/5] Also add with_sysroot=False here --- easybuild/tools/options.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easybuild/tools/options.py b/easybuild/tools/options.py index f48433e23c..fd22e384e5 100644 --- a/easybuild/tools/options.py +++ b/easybuild/tools/options.py @@ -1969,7 +1969,7 @@ def set_tmpdir(tmpdir=None, raise_error=False): os.close(fd) os.chmod(tmptest_file, 0o700) if not run_cmd(tmptest_file, simple=True, log_ok=False, regexp=False, force_in_dry_run=True, trace=False, - stream_output=False, with_hooks=False): + stream_output=False, with_hooks=False, with_sysroot=False): msg = "The temporary directory (%s) does not allow to execute files. " % tempfile.gettempdir() msg += "This can cause problems in the build process, consider using --tmpdir." if raise_error: From 165fe7910169ef8c7b0e8ff995d5d584e53d6a25 Mon Sep 17 00:00:00 2001 From: casparvl casparvl Date: Wed, 18 Sep 2024 14:29:35 +0000 Subject: [PATCH 4/5] Fix indent --- easybuild/tools/run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index 0d34e44b3d..5fb738f347 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -234,7 +234,7 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True else: exec_cmd = "/bin/bash" else: - exec_cmd = "/bin/bash" + exec_cmd = "/bin/bash" if not shell: if isinstance(cmd, list): From f7c3cb12abd73a6e769ff2be28d45535ccd9dd26 Mon Sep 17 00:00:00 2001 From: Kenneth Hoste Date: Wed, 18 Sep 2024 18:33:39 +0200 Subject: [PATCH 5/5] be a bit more careful with sysroot in run_cmd, add logging + add dedicated test for run_cmd using with_sysroot=True --- easybuild/tools/run.py | 15 ++++++++++----- test/framework/run.py | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/easybuild/tools/run.py b/easybuild/tools/run.py index 5fb738f347..f8d034f981 100644 --- a/easybuild/tools/run.py +++ b/easybuild/tools/run.py @@ -227,14 +227,17 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True if cmd_log: cmd_log.write("# output for command: %s\n\n" % cmd_msg) + exec_cmd = "/bin/bash" + + # if EasyBuild is configured to use an alternate sysroot, + # we should also run shell commands using the bash shell provided in there, + # since /bin/bash may not be compatible with the alternate sysroot if with_sysroot: sysroot = build_option('sysroot') if sysroot: - exec_cmd = "%s/bin/bash" % sysroot - else: - exec_cmd = "/bin/bash" - else: - exec_cmd = "/bin/bash" + sysroot_bin_bash = os.path.join(sysroot, 'bin', 'bash') + if os.path.exists(sysroot_bin_bash): + exec_cmd = sysroot_bin_bash if not shell: if isinstance(cmd, list): @@ -245,6 +248,8 @@ def run_cmd(cmd, log_ok=True, log_all=False, simple=False, inp=None, regexp=True else: raise EasyBuildError("Don't know how to prefix with /usr/bin/env for commands of type %s", type(cmd)) + _log.info("Using %s as shell for running cmd: %s", exec_cmd, cmd) + if with_hooks: hooks = load_hooks(build_option('hooks')) hook_res = run_hook(RUN_SHELL_CMD, hooks, pre_step_hook=True, args=[cmd], kwargs={'work_dir': os.getcwd()}) diff --git a/test/framework/run.py b/test/framework/run.py index bab4391cf6..bb2e5c0558 100644 --- a/test/framework/run.py +++ b/test/framework/run.py @@ -796,6 +796,31 @@ def post_run_shell_cmd_hook(cmd, *args, **kwargs): ]) self.assertEqual(stdout, expected_stdout) + def test_run_cmd_sysroot(self): + """Test with_sysroot option of run_cmd function.""" + + # put fake /bin/bash in place that will be picked up when using run_cmd with with_sysroot=True + bin_bash = os.path.join(self.test_prefix, 'bin', 'bash') + bin_bash_txt = '\n'.join([ + "#!/bin/bash", + "echo 'Hi there I am a fake /bin/bash in %s'" % self.test_prefix, + '/bin/bash "$@"', + ]) + write_file(bin_bash, bin_bash_txt) + adjust_permissions(bin_bash, stat.S_IXUSR) + + update_build_option('sysroot', self.test_prefix) + + (out, ec) = run_cmd("echo hello") + self.assertEqual(ec, 0) + self.assertTrue(out.startswith("Hi there I am a fake /bin/bash in")) + self.assertTrue(out.endswith("\nhello\n")) + + # picking up on alternate sysroot is enabled by default, but can be disabled via with_sysroot=False + (out, ec) = run_cmd("echo hello", with_sysroot=False) + self.assertEqual(ec, 0) + self.assertEqual(out, "hello\n") + def suite(): """ returns all the testcases in this module """