Sosd: Change req->datalen with req->size.
[archipelago] / xseg / tools / vlmc_shared.py
1 # Copyright 2013 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 # shared funcs for both blockd and filed
35
36 import os, sys, shutil, glob, argparse
37
38 XSEG_HOME="/root/archip/xseg/"
39 IMAGES="/srv/archip/"
40 XSEGBD_SYSFS="/sys/bus/xsegbd/"
41 DEVICE_PREFIX="/dev/xsegbd"
42 BLOCKD_LOGS="/root/archip_logs/"
43 FILED_PORT=0
44 NR_OPS=16
45 REQS=512
46
47 def vlmc_list(args):
48     print "name\t\t\t\tsize"
49     try:
50         for f in glob.glob(IMAGES + '*'):
51             print "%s\t\t\t\t%dM" % (os.path.basename(f), os.stat(f).st_size / 1024 / 1024)
52
53         sys.exit(0)
54     except Exception, reason:
55         print >> sys.stderr, reason
56         sys.exit(-1)
57
58 def vlmc_create(args):
59     name = args.name[0]
60     size = args.size
61     snap = args.snap
62
63     if size == None and snap == None:
64         print >> sys.stderr, "At least one of the size/snap args must be provided"
65         sys.exit(-1)
66
67     try:
68         old_dir = os.getcwd()
69         os.chdir(IMAGES)
70
71         try:
72             os.stat(name)
73             print "file exists"
74             os.chdir(old_dir)
75             sys.exit(-1)
76         except:
77             pass
78
79         if size != None:
80             size *= 1024*1024
81
82             if snap != None and size < os.stat(snap).st_size:
83                 print >> sys.stderr, "Given size smaller than snapshot volume size"
84                 sys.exit(-1)
85
86         if snap != None:
87             shutil.copy(snap, name)
88             if size != None:
89                 f = os.open(name, os.O_WRONLY)
90         else:
91             f = os.open(name, os.O_CREAT | os.O_WRONLY, 0755)
92
93         if size != None:
94             os.lseek(f, size - 1, os.SEEK_SET)
95             os.write(f, "1")
96             os.close(f)
97
98         os.chdir(old_dir)
99         sys.exit(0)
100     except Exception, reason:
101         print >> sys.stderr, reason
102         sys.exit(-1)
103
104 def vlmc_remove(args):
105     name = args.name[0]
106
107     try:
108         old_dir = os.getcwd()
109         os.chdir(IMAGES)
110
111         try:
112             os.stat(name)
113         except:
114             print "file doesn't exist"
115             os.chdir(old_dir)
116             sys.exit(-1)
117
118         os.unlink(IMAGES + '/' + name)
119
120         os.chdir(old_dir)
121         sys.exit(0)
122     except Exception, reason:
123         print >> sys.stderr, reason
124         sys.exit(-1)
125
126 def xsegbd_loaded():
127     try:
128         os.stat("/sys/bus/xsegbd")
129     except Exception, reason:
130         print >> sys.stderr, reason
131         sys.exit(-1)
132
133 def vlmc_resize(args):
134     name = args.name[0]
135     size = args.size[0]
136
137     try:
138         old_dir = os.getcwd()
139         os.chdir(IMAGES)
140
141         size *= 1024*1024
142
143         f = os.open(name, os.O_WRONLY, 0755)
144         if size >= os.stat(name).st_size:
145             os.lseek(f, size - 1, os.SEEK_SET)
146             os.write(f, "1")
147         else:
148             os.ftruncate(f, size)
149
150         os.close(f)
151         os.chdir(old_dir)
152
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()
156             if name == d_name:
157                 fd = os.open(XSEGBD_SYSFS + "devices/" +  d_id +"/refresh", os.O_WRONLY)
158                 os.write(fd, "1")
159                 os.close(fd)
160
161         sys.exit(0)
162     except Exception, reason:
163         print >> sys.stderr, reason
164         sys.exit(-1)
165
166 def loadrc(rc):
167     #FIXME
168     try:
169         if rc == None:
170             execfile(os.path.expanduser("~/.xsegrc"), globals())
171         else:
172             execfile(rc, globals())
173     except:
174         pass