1 #!/usr/bin/env python2.7
5 import os, sys, subprocess, shutil, re, argparse
7 XSEG_HOME="/root/archip/xseg/"
8 IMAGES="/srv/pithos/archip-data/images/"
9 XSEGBD_SYSFS="/sys/bus/xsegbd/"
10 DEVICE_PREFIX="/dev/xsegbd"
11 BLOCKD_LOGS="/root/logs/"
15 print "name\t\t\t\tsize"
17 for f in os.listdir(IMAGES):
18 print "%s\t\t\t\t%dM" % (f, os.stat(IMAGES + f).st_size / 1024 / 1024)
21 except Exception, reason:
22 print >> sys.stderr, reason
25 def vlmc_create(args):
43 shutil.copyfile(snap, name)
45 f = os.open(name, os.O_CREAT | os.O_WRONLY, 0755)
47 os.lseek(f, size - 1, os.SEEK_SET)
53 except Exception, reason:
54 print >> sys.stderr, reason
57 def vlmc_remove(args):
67 print "file doesn't exist"
71 os.unlink(IMAGES + '/' + name)
75 except Exception, reason:
76 print >> sys.stderr, reason
83 result = [int(open(XSEGBD_SYSFS + "devices/" + f + "/srcport").read().strip()) for f in os.listdir(XSEGBD_SYSFS + "devices/")]
93 fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
94 os.write(fd, "%s %d:%d:%d" % (name, port, FILED_PORT, REQS))
96 except Exception, reason:
97 print >> sys.stderr, reason
100 def vlmc_unmap(args):
101 device = args.name[0]
103 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
104 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
105 name = open(XSEGBD_SYSFS + "devices/"+ f + "/name").read().strip()
106 if device == DEVICE_PREFIX + d_id:
107 fd = os.open(XSEGBD_SYSFS + "remove", os.O_WRONLY)
112 except Exception, reason:
113 print >> sys.stderr, reason
116 def vlmc_showmapped(args):
117 print "id\tpool\timage\tsnap\tdevice"
119 for f in os.listdir(XSEGBD_SYSFS + "devices/"):
120 d_id = open(XSEGBD_SYSFS + "devices/" + f + "/id").read().strip()
121 name = open(XSEGBD_SYSFS + "devices/"+ f + "/name").read().strip()
123 print "%s\t%s\t%s\t%s\t%s" % (d_id, '-', name, '-', DEVICE_PREFIX +
125 except Exception, reason:
126 print >> sys.stderr, reason
129 if __name__ == "__main__":
130 # parse arguments and discpatch to the correct func
131 parser = argparse.ArgumentParser(description='vlmc tool')
132 parser.add_argument('-c', '--config', type=str, nargs='?', help='config file')
133 subparsers = parser.add_subparsers()
135 create_parser = subparsers.add_parser('create', help='Create volume')
136 group = create_parser.add_mutually_exclusive_group(required=True)
137 group.add_argument('-s', '--size', type=int, nargs='?', help='requested size in MB for create')
138 group.add_argument('--snap', type=str, nargs='?', help='create from snapshot')
139 create_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
140 create_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
141 create_parser.set_defaults(func=vlmc_create)
143 remove_parser = subparsers.add_parser('remove', help='Delete volume')
144 remove_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
145 remove_parser.set_defaults(func=vlmc_remove)
146 remove_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
148 rm_parser = subparsers.add_parser('rm', help='Delete volume')
149 rm_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
150 rm_parser.set_defaults(func=vlmc_remove)
151 rm_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
153 map_parser = subparsers.add_parser('map', help='Map volume')
154 map_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
155 map_parser.set_defaults(func=vlmc_map)
156 map_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
158 unmap_parser = subparsers.add_parser('unmap', help='Unmap volume')
159 unmap_parser.add_argument('name', type=str, nargs=1, help='volume/device name')
160 unmap_parser.set_defaults(func=vlmc_unmap)
161 unmap_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
163 showmapped_parser = subparsers.add_parser('showmapped', help='Show mapped volumes')
164 showmapped_parser.set_defaults(func=vlmc_showmapped)
165 showmapped_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
167 list_parser = subparsers.add_parser('list', help='List volumes')
168 list_parser.set_defaults(func=vlmc_list)
169 list_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
171 ls_parser = subparsers.add_parser('ls', help='List volumes')
172 ls_parser.set_defaults(func=vlmc_list)
173 ls_parser.add_argument('-p', '--pool', type=str, nargs='?', help='for backwards compatiblity with rbd')
175 args = parser.parse_args()
179 if args.config == None:
180 execfile(os.path.expanduser("~/.xsegrc"))
182 execfile(args.config)