You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

  1. from calculate.utils.files import grep_file
  2. import os
  3. class SystemType:
  4. """
  5. Тип контейнера текущей системы
  6. """
  7. Uml = "uml"
  8. VServer = "vserver"
  9. OpenVZ = "openvz"
  10. LXC = "lxc"
  11. Docker = "docker"
  12. Xen0 = "xen0"
  13. XenU = "xenU"
  14. Rkt = "rkt"
  15. SystemdNSpawn = "systemd-nspawn"
  16. NotDetected = ""
  17. @classmethod
  18. def detect_container(cls):
  19. if grep_file("/proc/cpuinfo", "UML"):
  20. return cls.Uml
  21. elif grep_file("/proc/self/status",
  22. r"(s_context|VxID):\s*[1-9]"):
  23. return cls.VServer
  24. elif (os.path.exists("/proc/vz/veinfo")
  25. and not os.path.exists("/proc/vz/version")):
  26. return cls.OpenVZ
  27. elif grep_file("/proc/self/status", r"envID:\s*[1-9]"):
  28. return cls.OpenVZ
  29. elif grep_file("/proc/1/environ", "container=lxc"):
  30. return cls.LXC
  31. elif grep_file("/proc/1/environ", "container=rkt"):
  32. return cls.Rkt
  33. elif grep_file("/proc/1/environ", "container=systemd-nspawn"):
  34. return cls.SystemdNSpawn
  35. elif os.path.exists("/.dockerenv"):
  36. return cls.Docker
  37. elif grep_file("/proc/1/environ", "container=docker"):
  38. return cls.Docker
  39. return cls.NotDetected
  40. @classmethod
  41. def detect_vm(cls):
  42. if os.path.exists("/proc/xen"):
  43. if grep_file("/proc/xen/capabilities", "control_d"):
  44. return cls.Xen0
  45. return cls.XenU
  46. return cls.NotDetected
  47. @classmethod
  48. def detect(cls):
  49. return cls.detect_container() or cls.detect_vm()