forked from checkpoint-restore/p.haul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.py
176 lines (137 loc) · 3.75 KB
/
images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#
# images driver for migration (without FS transfer)
#
import os
import tempfile
import tarfile
import time
import shutil
import time
import threading
import util
import criu_api
def_path = "/var/local/p.haul-fs/"
class opendir:
def __init__(self, path):
self._dirname = path
self._dirfd = os.open(path, os.O_DIRECTORY)
util.set_cloexec(self)
def close(self):
os.close(self._dirfd)
os._dirname = None
os._dirfd = -1
def name(self):
return self._dirname
def fileno(self):
return self._dirfd
class untar_thread(threading.Thread):
def __init__(self, sk, tdir):
threading.Thread.__init__(self)
self.__sk = sk
self.__dir = tdir
def run(self):
tf = tarfile.open(mode = "r|", fileobj = self.__sk.makefile())
tf.extractall(self.__dir)
tf.close()
class img_tar:
def __init__(self, sk, dirname):
self.__tf = tarfile.open(mode = "w|", fileobj = sk.makefile())
self.__dir = dirname
def add(self, img, path = None):
if not path:
path = os.path.join(self.__dir, img)
self.__tf.add(path, img)
def close(self):
self.__tf.close()
class phaul_images:
WDIR = 1
IMGDIR = 2
def __init__(self, typ):
self.current_iter = 0
self.sync_time = 0.0
self._typ = typ
self._keep_on_close = False
self._wdir = None
self._current_dir = None
def save_images(self):
print "Keeping images"
self._keep_on_close = True
def set_options(self, opts):
self._keep_on_close = opts["keep_images"]
suf = time.strftime("-%y.%m.%d-%H.%M", time.localtime())
util.makedirs(opts["img_path"])
wdir = tempfile.mkdtemp(suf, "%s-" % self._typ, opts["img_path"])
self._wdir = opendir(wdir)
self._img_path = os.path.join(self._wdir.name(), "img")
os.mkdir(self._img_path)
def close(self):
if not self._wdir:
return
self._wdir.close()
if self._current_dir:
self._current_dir.close()
if not self._keep_on_close:
print "Removing images"
shutil.rmtree(self._wdir.name())
else:
print "Images are kept in %s" % self._wdir.name()
pass
def img_sync_time(self):
return self.sync_time
def new_image_dir(self):
if self._current_dir:
self._current_dir.close()
self.current_iter += 1
img_dir = "%s/%d" % (self._img_path, self.current_iter)
print "\tMaking directory %s" % img_dir
os.mkdir(img_dir)
self._current_dir = opendir(img_dir)
def image_dir_fd(self):
return self._current_dir.fileno()
def work_dir_fd(self):
return self._wdir.fileno()
def image_dir(self):
return self._current_dir.name()
def work_dir(self):
return self._wdir.name()
def prev_image_dir(self):
if self.current_iter == 1:
return None
else:
return "../%d" % (self.current_iter - 1)
# Images transfer
# Are there better ways for doing this?
def sync_imgs_to_target(self, th, htype, sock):
# Pre-dump doesn't generate any images (yet?)
# so copy only those from the top dir
print "Sending images to target"
start = time.time()
cdir = self.image_dir()
th.start_accept_images(phaul_images.IMGDIR)
tf = img_tar(sock, cdir)
print "\tPack"
for img in filter(lambda x: x.endswith(".img"), os.listdir(cdir)):
tf.add(img)
print "\tAdd htype images"
for himg in htype.get_meta_images(cdir):
tf.add(himg[1], himg[0])
tf.close()
th.stop_accept_images()
self.sync_time = time.time() - start
def send_cpuinfo(self, th, sock):
th.start_accept_images(phaul_images.WDIR)
tf = img_tar(sock, self.work_dir())
tf.add(criu_api.cpuinfo_img_name)
tf.close()
th.stop_accept_images()
def start_accept_images(self, dir_id, sk):
if dir_id == phaul_images.WDIR:
dirname = self.work_dir()
else:
dirname = self.image_dir()
self.__acc_tar = untar_thread(sk, dirname)
self.__acc_tar.start()
print "Started images server"
def stop_accept_images(self):
print "Waiting for images to unpack"
self.__acc_tar.join()