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 = ["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])
31 check_call(cmd, shell=False, env=os.environ);
33 sys.stderr.write("vlmc creation failed\n")
37 def vlmc_snapshot(args):
45 def vlmc_remove(args):
47 cmd = ["vlmc-xseg", "%s" % SPEC, "remove", "--name", "%s" % name]
48 cmd.extend(["-mp", "%s" % MPORT])
49 cmd.extend(["-p", "%s" % VTOOL])
52 check_call(cmd, shell=False, env=os.environ);
54 sys.stderr.write("vlmc removal failed\n")
60 os.stat("/sys/bus/xsegbd")
61 except Exception, reason:
62 print >> sys.stderr, reason
66 def exclusive_args(args):
67 file = "/tmp/vlmc_map.lock"
70 fd = os.open(file, os.O_CREAT|os.O_EXCL|os.O_WRONLY)
72 except Exception, reason:
73 print >> sys.stderr, reason
89 result = [int(open(XSEGBD_SYSFS + "devices/" + f + "/srcport").read().strip()) for f in os.listdir(XSEGBD_SYSFS + "devices/")]
99 fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
100 print >> sys.stderr, "write to %s : %s %d:%d:%d" %( XSEGBD_SYSFS + "add", name, port, port + VPORT_START -4, REQS )
101 os.write(fd, "%s %d:%d:%d" % (name, port, port + VPORT_START-4, REQS))
103 except Exception, reason:
104 print >> sys.stderr, reason
108 def vlmc_unmap(args):
110 device = args.name[0]
112 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
113 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
114 name = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
115 if device == DEVICE_PREFIX + d_id:
116 fd = os.open(XSEGBD_SYSFS + "remove", os.O_WRONLY)
121 print >> sys.stderr, "Device %s doesn't exist" % device
123 except Exception, reason:
124 print >> sys.stderr, reason
128 def vlmc_showmapped(args):
130 print "id\tpool\timage\tsnap\tdevice"
132 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
133 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
134 target = open(XSEGBD_SYSFS + "devices/"+ f + "/target").read().strip()
136 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', target, '-', DEVICE_PREFIX +
138 except Exception, reason:
139 print >> sys.stderr, reason
143 def vlmc_resize(args):
151 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
152 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
153 d_name = open(XSEGBD_SYSFS + "devices/"+ f + "/name").read().strip()
155 fd = os.open(XSEGBD_SYSFS + "devices/" + d_id +"/refresh", os.O_WRONLY)
160 except Exception, reason:
161 print >> sys.stderr, reason
168 execfile(os.path.expanduser("/etc/default/archipelago"), globals())
170 execfile(rc, globals())
174 if __name__ == "__main__":
175 # parse arguments and discpatch to the correct func
176 parser = argparse.ArgumentParser(description='vlmc tool')
177 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
178 subparsers = parser.add_subparsers()
180 create_parser = subparsers.add_parser('create', help='Create volume')
181 #group = create_parser.add_mutually_exclusive_group(required=True)
182 create_parser.add_argument('-s', '--size', type=int, nargs='?', help='requested size in MB for create')
183 create_parser.add_argument('--snap', type=str, nargs='?', help='create from snapshot')
184 create_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
185 create_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
186 create_parser.set_defaults(func=vlmc_create)
188 remove_parser = subparsers.add_parser('remove', help='Delete volume')
189 remove_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
190 remove_parser.set_defaults(func=vlmc_remove)
191 remove_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
193 rm_parser = subparsers.add_parser('rm', help='Delete volume')
194 rm_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
195 rm_parser.set_defaults(func=vlmc_remove)
196 rm_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
198 map_parser = subparsers.add_parser('map', help='Map volume')
199 map_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
200 map_parser.set_defaults(func=vlmc_map)
201 map_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
203 unmap_parser = subparsers.add_parser('unmap', help='Unmap volume')
204 unmap_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
205 unmap_parser.set_defaults(func=vlmc_unmap)
206 unmap_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
208 showmapped_parser = subparsers.add_parser('showmapped', help='Show mapped volumes')
209 showmapped_parser.set_defaults(func=vlmc_showmapped)
210 showmapped_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
212 list_parser = subparsers.add_parser('list', help='List volumes')
213 list_parser.set_defaults(func=vlmc_list)
214 list_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
216 ls_parser = subparsers.add_parser('ls', help='List volumes')
217 ls_parser.set_defaults(func=vlmc_list)
218 ls_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
220 resize_parser = subparsers.add_parser('resize', help='Resize volume')
221 resize_parser.add_argument('-s', '--size', type=int, nargs=1, help='requested size in MB for resize')
222 resize_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
223 resize_parser.set_defaults(func=vlmc_resize)
224 resize_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
226 args = parser.parse_args()