summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorMina Galić <me+git@igalic.co>2020-11-06 19:49:05 +0100
committerGitHub <noreply@github.com>2020-11-06 13:49:05 -0500
commitd83c0bb4baca0b57166a74055f410fa4f75a08f5 (patch)
treeb64251be6f4b35f871983c08cc4ee0fb188de1fc /cloudinit/util.py
parentb542ce7a7c530fbfa874dd780294628b62fe092a (diff)
downloadvyos-cloud-init-d83c0bb4baca0b57166a74055f410fa4f75a08f5.tar.gz
vyos-cloud-init-d83c0bb4baca0b57166a74055f410fa4f75a08f5.zip
replace usage of dmidecode with kenv on FreeBSD (#621)
FreeBSD lets us read out kernel parameters with kenv(1), a user-space utility that's shipped in "base" We can use it in place of dmidecode(8), thus removing the dependency on sysutils/dmidecode, and the restrictions to i386 and x86_64 architectures that this utility imposes on FreeBSD. Co-authored-by: Scott Moser <smoser@brickies.net>
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py55
1 files changed, 41 insertions, 14 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index bdb3694d..769f3425 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -62,12 +62,6 @@ TRUE_STRINGS = ('true', '1', 'on', 'yes')
FALSE_STRINGS = ('off', '0', 'no', 'false')
-# Helper utils to see if running in a container
-CONTAINER_TESTS = (['systemd-detect-virt', '--quiet', '--container'],
- ['running-in-container'],
- ['lxc-is-container'])
-
-
def kernel_version():
return tuple(map(int, os.uname().release.split('.')[:2]))
@@ -1928,19 +1922,52 @@ def strip_prefix_suffix(line, prefix=None, suffix=None):
return line
+def _cmd_exits_zero(cmd):
+ if subp.which(cmd[0]) is None:
+ return False
+ try:
+ subp.subp(cmd)
+ except subp.ProcessExecutionError:
+ return False
+ return True
+
+
+def _is_container_systemd():
+ return _cmd_exits_zero(["systemd-detect-virt", "--quiet", "--container"])
+
+
+def _is_container_upstart():
+ return _cmd_exits_zero(["running-in-container"])
+
+
+def _is_container_old_lxc():
+ return _cmd_exits_zero(["lxc-is-container"])
+
+
+def _is_container_freebsd():
+ if not is_FreeBSD():
+ return False
+ cmd = ["sysctl", "-qn", "security.jail.jailed"]
+ if subp.which(cmd[0]) is None:
+ return False
+ out, _ = subp.subp(cmd)
+ return out.strip() == "1"
+
+
+@lru_cache()
def is_container():
"""
Checks to see if this code running in a container of some sort
"""
-
- for helper in CONTAINER_TESTS:
- try:
- # try to run a helper program. if it returns true/zero
- # then we're inside a container. otherwise, no
- subp.subp(helper)
+ checks = (
+ _is_container_systemd,
+ _is_container_freebsd,
+ _is_container_upstart,
+ _is_container_old_lxc)
+
+ for helper in checks:
+ if helper():
return True
- except (IOError, OSError):
- pass
# this code is largely from the logic in
# ubuntu's /etc/init/container-detect.conf