616d2c605ea1e84d7f407b47717c154e6f025423
[nodemanager.git] / plugins / rawdisk.py
1 #!/usr/bin/python -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(map(lambda x: x.replace("!", "/"),
34                          os.listdir("/sys/block/%s/slaves" % i)))
35     # Read the list of partitions
36     with open("/proc/partitions") as partitions:
37         pat = re.compile("\s+")
38         while True:
39             buf = partitions.readline()
40             if buf == "":
41                 break
42             buf = buf.strip()
43             fields = re.split(pat, buf)
44             dev = fields[-1]
45             if (not dev.startswith("dm-") and dev not in in_vg and
46                 os.path.exists("/dev/%s" % dev) and
47                 (os.minor(os.stat("/dev/%s" % dev).st_rdev) % 2) != 0):
48                 devices.append("/dev/%s" % dev)
49     partitions.close()
50     return devices
51
52 def GetSlivers(data, config=None, plc=None):
53     if 'slivers' not in data:
54         logger.log_missing_data("rawdisk.GetSlivers",'slivers')
55         return
56
57     devices = get_unused_devices()
58     for sliver in data['slivers']:
59         for attribute in sliver['attributes']:
60             name = attribute.get('tagname',attribute.get('name',''))
61             if name == 'rawdisk':
62                 for i in devices:
63                     st = os.stat(i)
64                     path = "/vservers/%s%s" % (sliver['name'], i)
65                     if os.path.exists(path):
66                         # should check whether its the proper type of device
67                         continue
68
69                     logger.log("rawdisk: Copying %s to %s" % (i, path))
70                     try:
71                         if os.path.exists(path):
72                             os.unlink(path)
73                     except:
74                         pass
75                     try:
76                         os.makedirs(os.path.dirname(path), 0755)
77                     except:
78                         pass
79                     os.mknod(path, st.st_mode, st.st_rdev)