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