expects the 'interfaces' key in GetSlivers - review logs to always mention module
[nodemanager.git] / plugins / rawdisk.py
1 #!/usr/bin/python -tt
2 # vim:set ts=4 sw=4 expandtab:
3 #
4 # $Id$
5 # $URL$
6 #
7 # NodeManager plugin to support mapping unused raw disks into a slice
8 # that has the rawdisk sliver tag
9
10 """
11 Raw disk support for NodeManager.
12
13 Copies all unused devices into slices with the rawdisk attribute set.
14 """
15
16 import errno
17 import os
18 import time
19 import re
20
21 import logger
22 import tools
23
24 def start(options, config):
25     logger.log("rawdisk: plugin starting up...")
26
27 def get_unused_devices():
28     devices = []
29     if os.path.exists("/dev/mapper/planetlab-rawdisk"):
30         devices.append("/dev/mapper/planetlab-rawdisk")
31     # Figure out which partitions are part of the VG
32     in_vg = []
33     for i in os.listdir("/sys/block"):
34         if not i.startswith("dm-"):
35             continue
36         in_vg.extend(os.listdir("/sys/block/%s/slaves" % i))
37     # Read the list of partitions
38     partitions = file("/proc/partitions", "r")
39     pat = re.compile("\s+")
40     while True:
41         buf = partitions.readline()
42         if buf == "":
43             break
44         buf = buf.strip()
45         fields = re.split(pat, buf)
46         dev = fields[-1]
47         if not dev.startswith("dm-") and dev.endswith("1") and dev not in in_vg:
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                     os.mknod(path, st.st_mode, st.st_rdev)