5 import os, sys, subprocess, argparse
6 from subprocess import call, check_call
10 size = args.size << 20
13 if size == None and snap == None:
14 print >> sys.stderr, "At least one of the size/snap args must be provided"
17 cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "create", "--name", "%s" % name]
19 cmd.extend(["--snap", "%s" % snap])
21 cmd.extend(["--size", "%s" % size])
22 cmd.extend(["-mp", "%s" % MPORT])
23 cmd.extend(["-p", "%s" % VTOOL])
24 os.environ['LD_LIBRARY_PATH'] = XSEG_HOME + "/lib:" + XSEG_HOME + "/lib/user"
27 check_call(cmd, shell=False, env=os.environ);
29 sys.stderr.write("vlmc creation failed\n")
33 def vlmc_snapshot(args):
41 def vlmc_remove(args):
43 cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "remove", "--name", "%s" % name]
44 cmd.extend(["-mp", "%s" % MPORT])
45 cmd.extend(["-p", "%s" % VTOOL])
46 os.environ['LD_LIBRARY_PATH'] = XSEG_HOME + "/lib:" + XSEG_HOME + "/lib/user"
49 check_call(cmd, shell=False, env=os.environ);
51 sys.stderr.write("vlmc removal failed\n")
57 os.stat("/sys/bus/xsegbd")
58 except Exception, reason:
59 print >> sys.stderr, reason
67 result = [int(open(XSEGBD_SYSFS + "devices/" + f + "/srcport").read().strip()) for f in os.listdir(XSEGBD_SYSFS + "devices/")]
77 fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
78 os.write(fd, "%s %d:%d:%d" % (name, port, port + VPORT_START-4, REQS))
80 except Exception, reason:
81 print >> sys.stderr, reason
88 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
89 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
90 name = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
91 if device == DEVICE_PREFIX + d_id:
92 fd = os.open(XSEGBD_SYSFS + "remove", os.O_WRONLY)
97 print >> sys.stderr, "Device %s doesn't exist" % device
99 except Exception, reason:
100 print >> sys.stderr, reason
103 def vlmc_showmapped(args):
105 print "id\tpool\timage\tsnap\tdevice"
107 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
108 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
109 target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
111 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
113 except Exception, reason:
114 print >> sys.stderr, reason
118 def vlmc_resize(args):
126 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
127 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
128 d_name = open(XSEGBD_SYSFS + "devices/"+ f + "/name").read().strip()
130 fd = os.open(XSEGBD_SYSFS + "devices/" + d_id +"/refresh", os.O_WRONLY)
135 except Exception, reason:
136 print >> sys.stderr, reason
143 execfile(os.path.expanduser("~/.xsegrc"), globals())
145 execfile(rc, globals())
149 if __name__ == "__main__":
150 # parse arguments and discpatch to the correct func
151 parser = argparse.ArgumentParser(description='vlmc tool')
152 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
153 subparsers = parser.add_subparsers()
155 create_parser = subparsers.add_parser('create', help='Create volume')
156 #group = create_parser.add_mutually_exclusive_group(required=True)
157 create_parser.add_argument('-s', '--size', type=int, nargs='?', help='requested size in MB for create')
158 create_parser.add_argument('--snap', type=str, nargs='?', help='create from snapshot')
159 create_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
160 create_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
161 create_parser.set_defaults(func=vlmc_create)
163 remove_parser = subparsers.add_parser('remove', help='Delete volume')
164 remove_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
165 remove_parser.set_defaults(func=vlmc_remove)
166 remove_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
168 rm_parser = subparsers.add_parser('rm', help='Delete volume')
169 rm_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
170 rm_parser.set_defaults(func=vlmc_remove)
171 rm_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
173 map_parser = subparsers.add_parser('map', help='Map volume')
174 map_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
175 map_parser.set_defaults(func=vlmc_map)
176 map_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
178 unmap_parser = subparsers.add_parser('unmap', help='Unmap volume')
179 unmap_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
180 unmap_parser.set_defaults(func=vlmc_unmap)
181 unmap_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
183 showmapped_parser = subparsers.add_parser('showmapped', help='Show mapped volumes')
184 showmapped_parser.set_defaults(func=vlmc_showmapped)
185 showmapped_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
187 list_parser = subparsers.add_parser('list', help='List volumes')
188 list_parser.set_defaults(func=vlmc_list)
189 list_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
191 ls_parser = subparsers.add_parser('ls', help='List volumes')
192 ls_parser.set_defaults(func=vlmc_list)
193 ls_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
195 resize_parser = subparsers.add_parser('resize', help='Resize volume')
196 resize_parser.add_argument('-s', '--size', type=int, nargs=1, help='requested size in MB for resize')
197 resize_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
198 resize_parser.set_defaults(func=vlmc_resize)
199 resize_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
201 args = parser.parse_args()