reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / plugins / rawdisk.py
1 #!/usr/bin/python3 -tt
2 # vim:set ts=4 sw=4 expandtab:
3 #
4 # NodeManager plugin to support mapping unused raw disks into a slice
5 # that has the rawdisk sliver tag
6
7 """
8 Raw disk support for NodeManager.
9
10 Copies all unused devices into slices with the rawdisk attribute set.
11 """
12
13 import errno
14 import os
15 import time
16 import re
17
18 import logger
19 import tools
20
21 def start():
22     logger.log("rawdisk: plugin starting up...")
23
24 def get_unused_devices():
25     devices = []
26     if os.path.exists("/dev/mapper/planetlab-rawdisk"):
27         devices.append("/dev/mapper/planetlab-rawdisk")
28     # Figure out which partitions are part of the VG
29     in_vg = []
30     for i in os.listdir("/sys/block"):
31         if not i.startswith("dm-"):
32             continue
33         in_vg.extend([x.replace("!", "/") for x in os.listdir("/sys/block/%s/slaves" % i)])
34     # Read the list of partitions
35     with open("/proc/partitions") as partitions:
36         pat = re.compile("\s+")
37         while True:
38             buf = partitions.readline()
39             if buf == "":
40                 break
41             buf = buf.strip()
42             fields = re.split(pat, buf)
43             dev = fields[-1]
44             if (not dev.startswith("dm-") and dev not in in_vg and
45                 os.path.exists("/dev/%s" % dev) and
46                 (os.minor(os.stat("/dev/%s" % dev).st_rdev) % 2) != 0):
47                 devices.append("/dev/%s" % dev)
48     partitions.close()
49     return devices
50
51 def GetSlivers(data, config=None, plc=None):
52     if 'slivers' not in data:
53         logger.log_missing_data("rawdisk.GetSlivers", 'slivers')
54         return
55
56     devices = get_unused_devices()
57     for sliver in data['slivers']:
58         for attribute in sliver['attributes']:
59             name = attribute.get('tagname', attribute.get('name', ''))
60             if name == 'rawdisk':
61                 for i in devices:
62                     st = os.stat(i)
63                     path = "/vservers/%s%s" % (sliver['name'], i)
64                     if os.path.exists(path):
65                         # should check whether its the proper type of device
66                         continue
67
68                     logger.log("rawdisk: Copying %s to %s" % (i, path))
69                     try:
70                         if os.path.exists(path):
71                             os.unlink(path)
72                     except:
73                         pass
74                     try:
75                         os.makedirs(os.path.dirname(path), 0o755)
76                     except:
77                         pass
78                     os.mknod(path, st.st_mode, st.st_rdev)