Skip to content

Commit

Permalink
[component] Ensure consistency across rootful and rootless containers
Browse files Browse the repository at this point in the history
Currently, if 'sos report' is run inside a rootful Toolbx [1] container
with the HOST environment variable set, it creates the report inside the
host operating system's /var/tmp, which is at $HOST/var/tmp inside the
container:
  # toolbox enter
  ⬢# HOST=/run/host sos report
  ...
  Your sosreport has been generated and saved in:
          /run/host/var/tmp/sosreport-toolbox-2023-10-01-trpwqii.tar.xz
  ...

However, if it's run as 'sudo sos report' inside a rootless Toolbx
container with the HOST environment variable set, it creates the report
inside the container's /var/tmp:
  $ toolbox enter
  ⬢$ sudo su -
  ⬢# HOST=/run/host sos report
  ...
  Your sosreport has been generated and saved in:
          /var/tmp/sosreport-toolbox-2023-10-01-nwjqcff.tar.xz
  ...

Toolbx [1] containers are ultimately Podman containers that are designed
to be used as interactive command line environments for development and
troubleshooting the host operating system.  So, one can replicate the
above with a podman(1) invocation as well.

This happens because the 'container' environment variable isn't set
inside the sudo(8) session.  Instead of relying on environment
variables, which often go missing in unexpected ways, it will be better
to check for the /run/.containerenv and /.dockerenv stamp files that
identify Podman and Docker containers respectively.

[1] https://containertoolbx.org/
    https://github.com/containers/toolbox

Signed-off-by: Debarshi Ray <debarshir@gnome.org>
  • Loading branch information
debarshiray committed Oct 2, 2023
1 parent 270c3ca commit bee9778
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sos/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ def get_tmpdir_default(self):
else:
tmpdir = os.getenv('TMPDIR', None) or '/var/tmp'

if os.getenv('HOST', None) and os.getenv('container', None):
if os.getenv('HOST', None) \
and (os.path.isfile("/run/.containerenv")
or os.path.isfile("/.dockerenv")):
tmpdir = os.path.join(os.getenv('HOST'), tmpdir.lstrip('/'))

# no standard library method exists for this, so call out to stat to
Expand Down

0 comments on commit bee9778

Please sign in to comment.