| 1 |
# Copyright (c) 2017 Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import typing as t |
| 7 |
|
| 8 |
from ansible.module_utils.facts.collector import BaseFactCollector |
| 9 |
|
| 10 |
|
| 11 |
def is_chroot(module=None): |
| 12 |
|
| 13 |
is_chroot = None |
| 14 |
|
| 15 |
if os.environ.get('debian_chroot', False): |
| 16 |
is_chroot = True |
| 17 |
else: |
| 18 |
my_root = os.stat('/') |
| 19 |
try: |
| 20 |
# check if my file system is the root one |
| 21 |
proc_root = os.stat('/proc/1/root/.') |
| 22 |
is_chroot = my_root.st_ino != proc_root.st_ino or my_root.st_dev != proc_root.st_dev |
| 23 |
except Exception: |
| 24 |
# I'm not root or no proc, fallback to checking it is inode #2 |
| 25 |
fs_root_ino = 2 |
| 26 |
|
| 27 |
if module is not None: |
| 28 |
stat_path = module.get_bin_path('stat') |
| 29 |
if stat_path: |
| 30 |
cmd = [stat_path, '-f', '--format=%T', '/'] |
| 31 |
rc, out, err = module.run_command(cmd) |
| 32 |
if 'btrfs' in out: |
| 33 |
fs_root_ino = 256 |
| 34 |
elif 'xfs' in out: |
| 35 |
fs_root_ino = 128 |
| 36 |
|
| 37 |
is_chroot = (my_root.st_ino != fs_root_ino) |
| 38 |
|
| 39 |
return is_chroot |
| 40 |
|
| 41 |
|
| 42 |
class ChrootFactCollector(BaseFactCollector): |
| 43 |
name = 'chroot' |
| 44 |
_fact_ids = set(['is_chroot']) # type: t.Set[str] |
| 45 |
|
| 46 |
def collect(self, module=None, collected_facts=None): |
| 47 |
return {'is_chroot': is_chroot(module)} |