From a40c1ed8112c34bb3ff4ba7c199712a65ac7c40f Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 11 Apr 2019 11:24:08 +0200 Subject: [PATCH] setup: properly detect is_chroot on Btrfs (#55089) * setup: properly detect is_chroot on Btrfs Fixes #55006 * Use get_bin_path for stat * Remove comment --- .../fragments/55006-setup-is_chroot-btrfs.yaml | 2 ++ lib/ansible/module_utils/facts/system/chroot.py | 16 +++++++++++++--- lib/ansible/modules/system/systemd.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/55006-setup-is_chroot-btrfs.yaml diff --git a/changelogs/fragments/55006-setup-is_chroot-btrfs.yaml b/changelogs/fragments/55006-setup-is_chroot-btrfs.yaml new file mode 100644 index 00000000000..b47741bdc1c --- /dev/null +++ b/changelogs/fragments/55006-setup-is_chroot-btrfs.yaml @@ -0,0 +1,2 @@ +bugfixes: + - setup - properly detect is_chroot on Btrfs (https://github.com/ansible/ansible/issues/55006) diff --git a/lib/ansible/module_utils/facts/system/chroot.py b/lib/ansible/module_utils/facts/system/chroot.py index 5eb89d695aa..1d99d4568e5 100644 --- a/lib/ansible/module_utils/facts/system/chroot.py +++ b/lib/ansible/module_utils/facts/system/chroot.py @@ -8,7 +8,7 @@ import os from ansible.module_utils.facts.collector import BaseFactCollector -def is_chroot(): +def is_chroot(module=None): is_chroot = None @@ -22,7 +22,17 @@ def is_chroot(): is_chroot = my_root.st_ino != proc_root.st_ino or my_root.st_dev != proc_root.st_dev except Exception: # I'm not root or no proc, fallback to checking it is inode #2 - is_chroot = (my_root.st_ino != 2) + fs_root_ino = 2 + + if module is not None: + stat_path = module.get_bin_path('stat') + if stat_path: + cmd = [stat_path, '-f', '--format=%T', '/'] + rc, out, err = module.run_command(cmd) + if 'btrfs' in out: + fs_root_ino = 256 + + is_chroot = (my_root.st_ino != fs_root_ino) return is_chroot @@ -32,4 +42,4 @@ class ChrootFactCollector(BaseFactCollector): _fact_ids = set(['is_chroot']) def collect(self, module=None, collected_facts=None): - return {'is_chroot': is_chroot()} + return {'is_chroot': is_chroot(module)} diff --git a/lib/ansible/modules/system/systemd.py b/lib/ansible/modules/system/systemd.py index a2d2d7d2eae..10c9e42f253 100644 --- a/lib/ansible/modules/system/systemd.py +++ b/lib/ansible/modules/system/systemd.py @@ -508,7 +508,7 @@ def main(): if rc != 0: module.fail_json(msg="Unable to %s service %s: %s" % (action, unit, err)) # check for chroot - elif is_chroot(): + elif is_chroot(module): module.warn("Target is a chroot. This can lead to false positives or prevent the init system tools from working.") else: # this should not happen?