diff options
Diffstat (limited to 'cloudinit/distros/freebsd.py')
-rw-r--r-- | cloudinit/distros/freebsd.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 4268abe6..706d0743 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -6,7 +6,9 @@ import os import re +from functools import lru_cache from io import StringIO +from typing import Optional import cloudinit.distros.bsd from cloudinit import log as logging @@ -192,5 +194,40 @@ class Distro(cloudinit.distros.bsd.BSD): freq=PER_INSTANCE, ) + @lru_cache() + def is_container(self) -> bool: + """return whether we're running in a container. + Cached, because it's unlikely to change.""" + jailed, _ = subp.subp(["sysctl", "-n", "security.jail.jailed"]) + if jailed.strip() == "0": + return False + return True + + @lru_cache() + def virtual(self) -> str: + """return the kind of virtualisation system we're running under. + Cached, because it's unlikely to change.""" + if self.is_container(): + return "jail" + # map FreeBSD's kern.vm_guest to systemd-detect-virt, just like we do + # in ds-identify + VM_GUEST_TO_SYSTEMD = { + "hv": "microsoft", + "vbox": "oracle", + "generic": "vm-other", + } + vm, _ = subp.subp(["sysctl", "-n", "kern.vm_guest"]) + vm = vm.strip() + if vm in VM_GUEST_TO_SYSTEMD: + return VM_GUEST_TO_SYSTEMD[vm] + return vm + + @property + def is_virtual(self) -> Optional[bool]: + """Detect if running on a virtual machine or bare metal. -# vi: ts=4 expandtab + This can fail on some platforms, so the signature is Optional[bool] + """ + if self.virtual() == "none": + return False + return True |