cciss fix and cleaner way to reach the partition.
[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(map(lambda x: x.replace("!", "/"),
33                          os.listdir("/sys/block/%s/slaves" % i)))
34     # Read the list of partitions
35     partitions = file("/proc/partitions", "r")
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("sliverauth: getslivers data lack's sliver information. IGNORING!")
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("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), 0755)
76                     except:
77                         pass
78                     os.mknod(path, st.st_mode, st.st_rdev)