5 import os, sys, subprocess, argparse, time, psutil, signal
6 from subprocess import call, check_call
12 PIDFILE_PATH="/var/run/archipelagos"
13 CHARDEV_NAME="/dev/segdev"
18 execfile(os.path.expanduser("/etc/default/archipelagos"), globals())
23 peer_blockerb = [BLOCKER, ["-p" , str(BPORT), "-g", str(SPEC), "-n", str(NR_OPS),
24 str(PITHOS), str(IMAGES), "-d", "--pidfile", os.path.join(PIDFILE_PATH, "blockerd.pid")], "blockerb"]
25 peer_blockerm = [BLOCKER, ["-p" , str(MBPORT), "-g", str(SPEC), "-n", str(NR_OPS),
26 str(PITHOSMAPS), str(MAPS), "-d", "--pidfile", os.path.join(PIDFILE_PATH, "blockerm.pid")], "blockerm" ]
27 peer_vlmcd = [VLMC, ["-t" , "1", "-sp", str(VPORT_START), "-ep", str(VPORT_END),
28 "-g", str(SPEC), "-n", str(NR_OPS), "-bp", str(BPORT), "-mp", str(MPORT),
29 "-d", "--pidfile", os.path.join(PIDFILE_PATH, "vlmcd.pid")], "vlmcd"]
30 peer_mapperd = [MAPPER, ["-t" , "1", "-p", str(MPORT), "-mbp", str(MBPORT),
31 "-g", str(SPEC), "-n", str(NR_OPS), "-bp", str(BPORT), "-d", "--pidfile",
32 os.path.join(PIDFILE_PATH, "mapper.pid")], "mapper"]
34 #peers = [peer_blockerb, peer_blockerm, peer_vlmcd, peer_mapperd]
35 peers = [peer_vlmcd, peer_mapperd]
36 modules = ["xseg", "segdev", "xseg_posix", "xseg_pthread", "xseg_segdev"]
40 def exclusive_args(args):
41 file = "/tmp/vlmc_map.lock"
44 fd = os.open(file, os.O_CREAT|os.O_EXCL|os.O_WRONLY)
46 except Exception, reason:
47 print >> sys.stderr, reason
58 def vlmc_showmapped(args):
59 print "id\tpool\timage\tsnap\tdevice"
61 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
62 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
63 target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
65 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
67 except Exception, reason:
68 print >> sys.stderr, reason
73 #check config file sanity
76 execfile(os.path.expanduser("/etc/default/archipelagos"), globals())
78 execfile(rc, globals())
83 lines = open("/proc/modules").read().split("\n")
84 modules = [f.split(" ")[0] for f in lines]
87 def load_module(name):
88 modules = loaded_modules()
91 cmd = ["modprobe -v %s" % name]
94 check_call(cmd, shell=True);
96 sys.stderr.write("Module %s failed to load. \n" % name)
100 def unload_module(name):
101 modules = loaded_modules()
102 if name not in modules:
104 cmd = ["modprobe -rv %s" % name]
107 check_call(cmd, shell=True);
109 sys.stderr.write("Module %s failed to unload. \n" % name)
113 def create_segment():
115 cmd = ["xseg", str(SPEC), "create"]
117 check_call(cmd, shell=False);
119 sys.stderr.write("Cannot create segment. \n")
123 def destroy_segment():
125 cmd = ["xseg", str(SPEC), "destroy"]
127 check_call(cmd, shell=False);
129 sys.stderr.write("Cannot destroy segment. \n")
133 def check_running(name, pid = -1):
134 for p in psutil.process_iter():
143 def check_pidfile(name):
144 pidfile = os.path.join(PIDFILE_PATH, name + ".pid")
147 pf = open(pidfile, "r")
157 def start_peer(peer):
158 cmd = [peer[0]] + peer[1]
160 check_call(cmd, shell=False);
162 sys.stderr.write("Peer %s start failed.\n" % peer[0])
167 pid = check_pidfile(peer[2])
169 print " process not running"
172 os.kill(pid, signal.SIGTERM)
174 while check_running(peer[0], pid) > 0:
178 print "process did not die in 10 secs"
184 os.stat(str(CHARDEV_NAME))
188 cmd = ["mknod", str(CHARDEV_NAME), "c", str(CHARDEV_MAJOR), str(CHARDEV_MINOR)]
191 check_call(cmd, shell=False);
193 sys.stderr.write("Segdev %s device creation failed. \n")
199 os.stat(str(CHARDEV_NAME))
203 os.unlink(str(CHARDEV_NAME))
205 sys.stderr.write("Segdev %s device removal failed. \n")
212 #if sth running or loaded
216 if load_module(m) < 0:
221 if make_segdev() < 0:
227 if create_segment() < 0:
234 if start_peer(p) < 0:
239 if load_module(xsegbd) < 0:
246 if unload_module(xsegbd):
249 for p in reversed(peers):
253 #destroy segment with timeout
254 # if destroy_segment() < 0:
259 for m in reversed(modules):
263 if __name__ == "__main__":
264 # parse arguments and discpatch to the correct func
265 parser = argparse.ArgumentParser(description='vlmc tool')
266 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
267 subparsers = parser.add_subparsers()
269 start_parser = subparsers.add_parser('start', help='Resize volume')
270 start_parser.set_defaults(func=start)
272 start_parser = subparsers.add_parser('stop', help='Resize volume')
273 start_parser.set_defaults(func=stop)
275 args = parser.parse_args()