Coverage for drivers/cbtutil.py : 75%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Utility for CBT log file operations"""
2# Copyright (C) Citrix Systems Inc.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU Lesser General Public License as published
6# by the Free Software Foundation; version 2.1 only.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public License
14# along with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16#
17# Helper functions pertaining to VHD operations
18#
20import util
21import uuid
22from constants import CBT_UTIL
25def create_cbt_log(file_name, size):
26 """Create and initialise log file for tracking changed blocks"""
27 cmd = [CBT_UTIL, "create", "-n", file_name, "-s", str(size)]
28 _call_cbt_util(cmd)
31def set_cbt_parent(file_name, parent_uuid):
32 """Set parent field in log file"""
33 cmd = [CBT_UTIL, "set", "-n", file_name, "-p", str(parent_uuid)]
34 _call_cbt_util(cmd)
37def get_cbt_parent(file_name):
38 """Get parent field from log file"""
39 cmd = [CBT_UTIL, "get", "-n", file_name, "-p"]
40 ret = _call_cbt_util(cmd)
41 uret = uuid.UUID(ret.strip())
42 # TODO: Need to check for NULL UUID
43 # Ideally, we want to do
44 # if uuid.UUID(ret.strip()).int == 0
45 # return None
46 # Pylint doesn't like this for reason though
47 return str(uret)
50def set_cbt_child(file_name, child_uuid):
51 """Set child field in log file"""
52 cmd = [CBT_UTIL, "set", "-n", file_name, "-c", str(child_uuid)]
53 _call_cbt_util(cmd)
56def get_cbt_child(file_name):
57 """Get parent field from log file"""
58 cmd = [CBT_UTIL, "get", "-n", file_name, "-c"]
59 ret = _call_cbt_util(cmd)
60 uret = uuid.UUID(ret.strip())
61 # TODO: Need to check for NULL UUID
62 return str(uret)
65def set_cbt_consistency(file_name, consistent):
66 """Set consistency field in log file"""
67 if consistent:
68 flag = 1
69 else:
70 flag = 0
71 cmd = [CBT_UTIL, "set", "-n", file_name, "-f", str(flag)]
72 _call_cbt_util(cmd)
75def get_cbt_consistency(file_name):
76 """Get consistency field from log file"""
77 cmd = [CBT_UTIL, "get", "-n", file_name, "-f"]
78 ret = _call_cbt_util(cmd)
79 return bool(int(ret.strip()))
82def get_cbt_bitmap(file_name):
83 """Get bitmap field from log file"""
84 cmd = [CBT_UTIL, "get", "-n", file_name, "-b"]
85 ret = _call_cbt_util(cmd, text=False)
86 # Do not strip the return string. It's a byte string and stripping
87 # it sometimes leads to loss of information
88 return ret
91def set_cbt_size(filename, size):
92 """Set size field in log file"""
93 cmd = [CBT_UTIL, "set", "-n", filename, "-s", str(size)]
94 _call_cbt_util(cmd)
97def get_cbt_size(file_name):
98 """Get size field from log file"""
99 cmd = [CBT_UTIL, "get", "-n", file_name, "-s"]
100 ret = _call_cbt_util(cmd)
101 return int(ret.strip())
104def coalesce_bitmap(parent_path, child_path):
105 """Coalesce bitmap contents of parent onto child log file"""
106 cmd = [CBT_UTIL, "coalesce", "-p", parent_path, "-c", child_path]
107 _call_cbt_util(cmd)
110def _call_cbt_util(cmd, text=True):
111 return util.ioretry(lambda: util.pread2(cmd, text=text))