scomd_cgroupfs.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding : utf-8 -*-
  2. import os, sys
  3. def mountpoint(path):
  4. status = os.system("mountpoint -q %s" % path)
  5. if status == 0:
  6. return True
  7. else:
  8. return False
  9. class Controller:
  10. def __init__(self, subsysname, hierarchy, num_cgroups, enabled ):
  11. self.subsysname = subsysname
  12. self.hierarchy = hierarchy
  13. self.num_cgroups = num_cgroups
  14. self.enabled = enabled
  15. def mount(self):
  16. if self.enabled == 1:
  17. os.chdir("/sys/fs/cgroup")
  18. if mountpoint(self.subsysname) == False:
  19. s = self.subsysname
  20. status = os.system("mkdir -p %s; mount -n -t cgroup -o %s cgroup %s" % (s, s,s))
  21. if status == 0:
  22. return True
  23. else:
  24. return False
  25. class Cgroupfs:
  26. def __init__(self):
  27. self.controllers = {}
  28. if self.check_fstab == True:
  29. print("cgroupfs in fstab, exiting.")
  30. sys.exit(-1)
  31. if self.kernel_support() == False:
  32. print("No kernel support for cgroupfs, exiting.")
  33. sys.exit(-2)
  34. if self.check_sysfs() == False:
  35. print("/sys/fs/cgroups directory not found, exiting")
  36. sys.exit(-3)
  37. self.mount_cgroup()
  38. self.find_controllers()
  39. for cname, c in list(self.controllers.items()):
  40. c.mount()
  41. def check_fstab(self):
  42. found = False
  43. for line in open("/etc/fstab").readlines():
  44. if line[0] == "#":
  45. continue
  46. else:
  47. if line.find("cgroup"):
  48. found = True
  49. return found
  50. def kernel_support(self):
  51. return os.path.isfile("/proc/cgroups")
  52. def check_sysfs(self):
  53. return os.path.isdir("/sys/fs/cgroup")
  54. def mount_cgroup(self):
  55. if mountpoint("/sys/fs/cgroup") == False:
  56. cmd = " mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup"
  57. return os.system(cmd)
  58. def find_controllers(self):
  59. for line in open("/proc/cgroups").readlines():
  60. line = line.strip()
  61. if line[0] == "#":
  62. continue
  63. else:
  64. subsysname, hierarchy, num_cgroups, enabled = line.split()
  65. enb = int(enabled)
  66. hie = int(hierarchy)
  67. numc= int(num_cgroups)
  68. self.controllers[subsysname] = Controller(subsysname, hie, numc, enb)