[CRIU] [PATCH 1/4] crit: Introduce 'info' action

Pavel Emelyanov xemul at parallels.com
Wed Sep 16 06:16:24 PDT 2015


This should print some information about image file itself, e.g.
statistical information. Right now print magic and the number of
object written in the file.

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 crit                    | 11 ++++++++++
 pycriu/images/images.py | 57 ++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/crit b/crit
index ba586fa..e0118ff 100755
--- a/crit
+++ b/crit
@@ -41,6 +41,11 @@ def encode(opts):
 	img = json.load(inf(opts))
 	pycriu.images.dump(img, outf(opts))
 
+def info(opts):
+	infs = pycriu.images.info(inf(opts))
+	json.dump(infs, sys.stdout, indent = 4)
+	print
+
 def main():
 	desc = 'CRiu Image Tool'
 	parser = argparse.ArgumentParser(description=desc,
@@ -73,6 +78,12 @@ def main():
 			help = 'where to put criu image in binary format (stdout by default)')
 	encode_parser.set_defaults(func=encode)
 
+	# Info
+	info_parser = subparsers.add_parser('info',
+			help = 'show info about image')
+	info_parser.add_argument("in")
+	info_parser.set_defaults(func=info)
+
 	# Show
 	show_parser = subparsers.add_parser('show',
 			help = "convert criu image from binary to human-readable json")
diff --git a/pycriu/images/images.py b/pycriu/images/images.py
index 5efd60b..47b0aef 100644
--- a/pycriu/images/images.py
+++ b/pycriu/images/images.py
@@ -133,6 +133,22 @@ class entry_handler:
 		self.dump(entries, f)
 		return f.read()
 
+	def count(self, f):
+		"""
+		Counts the number of top-level object in the image file
+		"""
+		entries = 0
+
+		while True:
+			buf = f.read(4)
+			if buf == '':
+				break
+			size, = struct.unpack('i', buf)
+			f.seek(size, 1)
+			entries += 1
+
+		return entries
+
 # Special handler for pagemap.img
 class pagemap_handler:
 	"""
@@ -176,6 +192,9 @@ class pagemap_handler:
 		self.dump(entries, f)
 		return f.read()
 
+	def count(self, f):
+		return entry_handler(None).count(f) - 1
+
 
 # In following extra handlers we use base64 encoding
 # to store binary data. Even though, the nature
@@ -287,17 +306,9 @@ handlers = {
 	'NETNS'			: entry_handler(netns_entry)
 	}
 
-def load(f, pretty = False):
-	"""
-	Convert criu image from binary format to dict(json).
-	Takes a file-like object to read criu image from.
-	Returns criu image in dict(json) format.
-	"""
-	image = {}
-
+def __rhandler(f):
 	# Images v1.1 NOTE: First read "first" magic.
 	img_magic, = struct.unpack('i', f.read(4))
-
 	if img_magic in (magic.by_name['IMG_COMMON'], magic.by_name['IMG_SERVICE']):
 		img_magic, = struct.unpack('i', f.read(4))
 
@@ -309,13 +320,35 @@ def load(f, pretty = False):
 	try:
 		handler = handlers[m]
 	except:
-		raise Exception("No handler found for image with such magic "+m)
+		raise Exception("No handler found for image with magic " + m)
 
-	image['magic']		= m
-	image['entries']	= handler.load(f, pretty)
+	return m, handler
+
+def load(f, pretty = False):
+	"""
+	Convert criu image from binary format to dict(json).
+	Takes a file-like object to read criu image from.
+	Returns criu image in dict(json) format.
+	"""
+	image = {}
+
+	m, handler = __rhandler(f)
+
+	image['magic'] = m
+	image['entries'] = handler.load(f, pretty)
 
 	return image
 
+def info(f):
+	res = {}
+
+	m, handler = __rhandler(f)
+
+	res['magic'] = m
+	res['count'] = handler.count(f)
+
+	return res
+
 def loads(s, pretty = False):
 	"""
 	Same as load(), but takes a string.
-- 
1.9.3




More information about the CRIU mailing list