5 import os, sys, subprocess, argparse, time
6 from subprocess import call, check_call
13 if size == None and snap == None:
14 print >> sys.stderr, "At least one of the size/snap args must be provided"
22 cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "create", "--name", "%s" % name]
24 cmd.extend(["--snap", "%s" % snap])
26 cmd.extend(["--size", "%s" % size])
27 cmd.extend(["-mp", "%s" % MPORT])
28 cmd.extend(["-p", "%s" % VTOOL])
29 os.environ['LD_LIBRARY_PATH'] = XSEG_HOME + "/lib:" + XSEG_HOME + "/lib/user"
32 check_call(cmd, shell=False, env=os.environ);
34 sys.stderr.write("vlmc creation failed\n")
38 def vlmc_snapshot(args):
46 def vlmc_remove(args):
48 cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "remove", "--name", "%s" % name]
49 cmd.extend(["-mp", "%s" % MPORT])
50 cmd.extend(["-p", "%s" % VTOOL])
51 os.environ['LD_LIBRARY_PATH'] = XSEG_HOME + "/lib:" + XSEG_HOME + "/lib/user"
54 check_call(cmd, shell=False, env=os.environ);
56 sys.stderr.write("vlmc removal failed\n")
62 os.stat("/sys/bus/xsegbd")
63 except Exception, reason:
64 print >> sys.stderr, reason
68 def exclusive_args(args):
69 file = "/tmp/vlmc_map.lock"
72 fd = os.open(file, os.O_CREAT|os.O_EXCL|os.O_WRONLY)
74 except Exception, reason:
75 print >> sys.stderr, reason
91 result = [int(open(XSEGBD_SYSFS + "devices/" + f + "/srcport").read().strip()) for f in os.listdir(XSEGBD_SYSFS + "devices/")]
101 fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
102 print >> sys.stderr, "write to %s : %s %d:%d:%d" %( XSEGBD_SYSFS + "add", name, port, port + VPORT_START -4, REQS )
103 os.write(fd, "%s %d:%d:%d" % (name, port, port + VPORT_START-4, REQS))
105 except Exception, reason:
106 print >> sys.stderr, reason
110 def vlmc_unmap(args):
112 device = args.name[0]
114 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
115 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
116 name = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
117 if device == DEVICE_PREFIX + d_id:
118 fd = os.open(XSEGBD_SYSFS + "remove", os.O_WRONLY)
123 print >> sys.stderr, "Device %s doesn't exist" % device
125 except Exception, reason:
126 print >> sys.stderr, reason
130 def vlmc_showmapped(args):
132 print "id\tpool\timage\tsnap\tdevice"
134 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
135 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
136 target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
138 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
140 except Exception, reason:
141 print >> sys.stderr, reason
145 def vlmc_resize(args):
153 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
154 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
155 d_name = open(XSEGBD_SYSFS + "devices/"+ f + "/name").read().strip()
157 fd = os.open(XSEGBD_SYSFS + "devices/" + d_id +"/refresh", os.O_WRONLY)
162 except Exception, reason:
163 print >> sys.stderr, reason
170 execfile(os.path.expanduser("/etc/default/archipelago"), globals())
172 execfile(rc, globals())
176 if __name__ == "__main__":
177 # parse arguments and discpatch to the correct func
178 parser = argparse.ArgumentParser(description='vlmc tool')
179 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
180 subparsers = parser.add_subparsers()
182 create_parser = subparsers.add_parser('create', help='Create volume')
183 #group = create_parser.add_mutually_exclusive_group(required=True)
184 create_parser.add_argument('-s', '--size', type=int, nargs='?', help='requested size in MB for create')
185 create_parser.add_argument('--snap', type=str, nargs='?', help='create from snapshot')
186 create_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
187 create_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
188 create_parser.set_defaults(func=vlmc_create)
190 remove_parser = subparsers.add_parser('remove', help='Delete volume')
191 remove_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
192 remove_parser.set_defaults(func=vlmc_remove)
193 remove_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
195 rm_parser = subparsers.add_parser('rm', help='Delete volume')
196 rm_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
197 rm_parser.set_defaults(func=vlmc_remove)
198 rm_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
200 map_parser = subparsers.add_parser('map', help='Map volume')
201 map_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
202 map_parser.set_defaults(func=vlmc_map)
203 map_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
205 unmap_parser = subparsers.add_parser('unmap', help='Unmap volume')
206 unmap_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
207 unmap_parser.set_defaults(func=vlmc_unmap)
208 unmap_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
210 showmapped_parser = subparsers.add_parser('showmapped', help='Show mapped volumes')
211 showmapped_parser.set_defaults(func=vlmc_showmapped)
212 showmapped_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
214 list_parser = subparsers.add_parser('list', help='List volumes')
215 list_parser.set_defaults(func=vlmc_list)
216 list_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
218 ls_parser = subparsers.add_parser('ls', help='List volumes')
219 ls_parser.set_defaults(func=vlmc_list)
220 ls_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
222 resize_parser = subparsers.add_parser('resize', help='Resize volume')
223 resize_parser.add_argument('-s', '--size', type=int, nargs=1, help='requested size in MB for resize')
224 resize_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
225 resize_parser.set_defaults(func=vlmc_resize)
226 resize_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
228 args = parser.parse_args()