setup: more reliably detect container environment (#66183)

* virtual facts: /proc/<pid>/environ is NULL-separated
* non-empty "container" env is a guest container
* This is consistent with `systemd-detect-virt --container`
* fact utils: line_sep option: specify delim for split
* allow using the same function to split null-delimited objects
This commit is contained in:
James Cassell 2020-01-09 01:21:45 -05:00 committed by Abhijeet Kasurde
parent fff613dd3e
commit 963b8249ec
3 changed files with 17 additions and 3 deletions

View file

@ -0,0 +1,4 @@
---
bugfixes:
- virtual facts - detect generic container environment based on non-empty
"container" env var

View file

@ -36,11 +36,17 @@ def get_file_content(path, default=None, strip=True):
return data
def get_file_lines(path, strip=True):
def get_file_lines(path, strip=True, line_sep=None):
'''get list of lines from file'''
data = get_file_content(path, strip=strip)
if data:
ret = data.splitlines()
if line_sep is None:
ret = data.splitlines()
else:
if len(line_sep) == 1:
ret = data.rstrip(line_sep).split(line_sep)
else:
ret = data.split(line_sep)
else:
ret = []
return ret

View file

@ -49,7 +49,7 @@ class LinuxVirtual(Virtual):
# lxc does not always appear in cgroups anymore but sets 'container=lxc' environment var, requires root privs
if os.path.exists('/proc/1/environ'):
for line in get_file_lines('/proc/1/environ'):
for line in get_file_lines('/proc/1/environ', line_sep='\x00'):
if re.search('container=lxc', line):
virtual_facts['virtualization_type'] = 'lxc'
virtual_facts['virtualization_role'] = 'guest'
@ -58,6 +58,10 @@ class LinuxVirtual(Virtual):
virtual_facts['virtualization_type'] = 'podman'
virtual_facts['virtualization_role'] = 'guest'
return virtual_facts
if re.search('^container=.', line):
virtual_facts['virtualization_type'] = 'container'
virtual_facts['virtualization_role'] = 'guest'
return virtual_facts
if os.path.exists('/proc/vz') and not os.path.exists('/proc/lve'):
virtual_facts['virtualization_type'] = 'openvz'