blob: aaa8f59504370ae4357aea97f92a10345d4d2513 [file] [log] [blame]
John Snowdfdc48d2019-07-29 16:35:54 -04001#!/usr/bin/env python
2#
3# Test bitmap-sync backups (incremental, differential, and partials)
4#
5# Copyright (c) 2019 John Snow for Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20# owner=jsnow@redhat.com
21
John Snowdfdc48d2019-07-29 16:35:54 -040022import math
23import os
24
25import iotests
26from iotests import log, qemu_img
27
28SIZE = 64 * 1024 * 1024
29GRANULARITY = 64 * 1024
30
John Snowb0a32be2019-07-29 16:35:54 -040031
32class Pattern:
33 def __init__(self, byte, offset, size=GRANULARITY):
34 self.byte = byte
35 self.offset = offset
36 self.size = size
37
38 def bits(self, granularity):
39 lower = self.offset // granularity
40 upper = (self.offset + self.size - 1) // granularity
41 return set(range(lower, upper + 1))
42
John Snowdfdc48d2019-07-29 16:35:54 -040043
44class PatternGroup:
45 """Grouping of Pattern objects. Initialize with an iterable of Patterns."""
46 def __init__(self, patterns):
47 self.patterns = patterns
48
49 def bits(self, granularity):
50 """Calculate the unique bits dirtied by this pattern grouping"""
51 res = set()
52 for pattern in self.patterns:
John Snowb0a32be2019-07-29 16:35:54 -040053 res |= pattern.bits(granularity)
John Snowdfdc48d2019-07-29 16:35:54 -040054 return res
55
John Snowb0a32be2019-07-29 16:35:54 -040056
John Snowdfdc48d2019-07-29 16:35:54 -040057GROUPS = [
58 PatternGroup([
59 # Batch 0: 4 clusters
John Snowb0a32be2019-07-29 16:35:54 -040060 Pattern('0x49', 0x0000000),
61 Pattern('0x6c', 0x0100000), # 1M
62 Pattern('0x6f', 0x2000000), # 32M
63 Pattern('0x76', 0x3ff0000)]), # 64M - 64K
John Snowdfdc48d2019-07-29 16:35:54 -040064 PatternGroup([
65 # Batch 1: 6 clusters (3 new)
John Snowb0a32be2019-07-29 16:35:54 -040066 Pattern('0x65', 0x0000000), # Full overwrite
67 Pattern('0x77', 0x00f8000), # Partial-left (1M-32K)
68 Pattern('0x72', 0x2008000), # Partial-right (32M+32K)
69 Pattern('0x69', 0x3fe0000)]), # Adjacent-left (64M - 128K)
John Snowdfdc48d2019-07-29 16:35:54 -040070 PatternGroup([
71 # Batch 2: 7 clusters (3 new)
John Snowb0a32be2019-07-29 16:35:54 -040072 Pattern('0x74', 0x0010000), # Adjacent-right
73 Pattern('0x69', 0x00e8000), # Partial-left (1M-96K)
74 Pattern('0x6e', 0x2018000), # Partial-right (32M+96K)
75 Pattern('0x67', 0x3fe0000,
76 2*GRANULARITY)]), # Overwrite [(64M-128K)-64M)
John Snowdfdc48d2019-07-29 16:35:54 -040077 PatternGroup([
78 # Batch 3: 8 clusters (5 new)
79 # Carefully chosen such that nothing re-dirties the one cluster
80 # that copies out successfully before failure in Group #1.
John Snowb0a32be2019-07-29 16:35:54 -040081 Pattern('0xaa', 0x0010000,
82 3*GRANULARITY), # Overwrite and 2x Adjacent-right
83 Pattern('0xbb', 0x00d8000), # Partial-left (1M-160K)
84 Pattern('0xcc', 0x2028000), # Partial-right (32M+160K)
85 Pattern('0xdd', 0x3fc0000)]), # New; leaving a gap to the right
John Snowdfdc48d2019-07-29 16:35:54 -040086]
87
John Snow32afa5a2019-07-29 16:35:54 -040088
89class EmulatedBitmap:
90 def __init__(self, granularity=GRANULARITY):
91 self._bits = set()
92 self.granularity = granularity
93
94 def dirty_bits(self, bits):
95 self._bits |= set(bits)
96
97 def dirty_group(self, n):
98 self.dirty_bits(GROUPS[n].bits(self.granularity))
99
100 def clear(self):
101 self._bits = set()
102
103 def clear_bits(self, bits):
104 self._bits -= set(bits)
105
106 def clear_bit(self, bit):
107 self.clear_bits({bit})
108
109 def clear_group(self, n):
110 self.clear_bits(GROUPS[n].bits(self.granularity))
111
112 @property
113 def first_bit(self):
114 return sorted(self.bits)[0]
115
116 @property
117 def bits(self):
118 return self._bits
119
120 @property
121 def count(self):
122 return len(self.bits)
123
124 def compare(self, qmp_bitmap):
125 """
126 Print a nice human-readable message checking that a bitmap as reported
127 by the QMP interface has as many bits set as we expect it to.
128 """
129
130 name = qmp_bitmap.get('name', '(anonymous)')
131 log("= Checking Bitmap {:s} =".format(name))
132
133 want = self.count
134 have = qmp_bitmap['count'] // qmp_bitmap['granularity']
135
136 log("expecting {:d} dirty sectors; have {:d}. {:s}".format(
137 want, have, "OK!" if want == have else "ERROR!"))
138 log('')
139
140
John Snowdfdc48d2019-07-29 16:35:54 -0400141class Drive:
142 """Represents, vaguely, a drive attached to a VM.
143 Includes format, graph, and device information."""
144
145 def __init__(self, path, vm=None):
146 self.path = path
147 self.vm = vm
148 self.fmt = None
149 self.size = None
150 self.node = None
151 self.device = None
152
153 @property
154 def name(self):
155 return self.node or self.device
156
157 def img_create(self, fmt, size):
158 self.fmt = fmt
159 self.size = size
160 iotests.qemu_img_create('-f', self.fmt, self.path, str(self.size))
161
162 def create_target(self, name, fmt, size):
163 basename = os.path.basename(self.path)
164 file_node_name = "file_{}".format(basename)
165 vm = self.vm
166
167 log(vm.command('blockdev-create', job_id='bdc-file-job',
168 options={
169 'driver': 'file',
170 'filename': self.path,
171 'size': 0,
172 }))
173 vm.run_job('bdc-file-job')
174 log(vm.command('blockdev-add', driver='file',
175 node_name=file_node_name, filename=self.path))
176
177 log(vm.command('blockdev-create', job_id='bdc-fmt-job',
178 options={
179 'driver': fmt,
180 'file': file_node_name,
181 'size': size,
182 }))
183 vm.run_job('bdc-fmt-job')
184 log(vm.command('blockdev-add', driver=fmt,
185 node_name=name,
186 file=file_node_name))
187 self.fmt = fmt
188 self.size = size
189 self.node = name
190
191def query_bitmaps(vm):
192 res = vm.qmp("query-block")
193 return {"bitmaps": {device['device'] or device['qdev']:
194 device.get('dirty-bitmaps', []) for
195 device in res['return']}}
196
197def get_bitmap(bitmaps, drivename, name, recording=None):
198 """
199 get a specific bitmap from the object returned by query_bitmaps.
200 :param recording: If specified, filter results by the specified value.
201 """
202 for bitmap in bitmaps['bitmaps'][drivename]:
203 if bitmap.get('name', '') == name:
204 if recording is None:
205 return bitmap
206 elif bitmap.get('recording') == recording:
207 return bitmap
208 return None
209
John Snow0af2a092019-07-29 16:35:55 -0400210def blockdev_backup(vm, device, target, sync, **kwargs):
211 # Strip any arguments explicitly nulled by the caller:
212 kwargs = {key: val for key, val in kwargs.items() if val is not None}
213 result = vm.qmp_log('blockdev-backup',
214 device=device,
215 target=target,
216 sync=sync,
217 **kwargs)
218 return result
219
220def blockdev_backup_mktarget(drive, target_id, filepath, sync, **kwargs):
221 target_drive = Drive(filepath, vm=drive.vm)
222 target_drive.create_target(target_id, drive.fmt, drive.size)
223 blockdev_backup(drive.vm, drive.name, target_id, sync, **kwargs)
224
John Snowdfdc48d2019-07-29 16:35:54 -0400225def reference_backup(drive, n, filepath):
226 log("--- Reference Backup #{:d} ---\n".format(n))
227 target_id = "ref_target_{:d}".format(n)
228 job_id = "ref_backup_{:d}".format(n)
John Snow0af2a092019-07-29 16:35:55 -0400229 blockdev_backup_mktarget(drive, target_id, filepath, "full",
230 job_id=job_id)
John Snowdfdc48d2019-07-29 16:35:54 -0400231 drive.vm.run_job(job_id, auto_dismiss=True)
232 log('')
233
John Snow0af2a092019-07-29 16:35:55 -0400234def backup(drive, n, filepath, sync, **kwargs):
235 log("--- Test Backup #{:d} ---\n".format(n))
236 target_id = "backup_target_{:d}".format(n)
237 job_id = "backup_{:d}".format(n)
238 kwargs.setdefault('auto-finalize', False)
239 blockdev_backup_mktarget(drive, target_id, filepath, sync,
240 job_id=job_id, **kwargs)
John Snowdfdc48d2019-07-29 16:35:54 -0400241 return job_id
242
243def perform_writes(drive, n):
244 log("--- Write #{:d} ---\n".format(n))
245 for pattern in GROUPS[n].patterns:
246 cmd = "write -P{:s} 0x{:07x} 0x{:x}".format(
247 pattern.byte,
248 pattern.offset,
249 pattern.size)
250 log(cmd)
251 log(drive.vm.hmp_qemu_io(drive.name, cmd))
252 bitmaps = query_bitmaps(drive.vm)
253 log(bitmaps, indent=2)
254 log('')
255 return bitmaps
256
John Snowdfdc48d2019-07-29 16:35:54 -0400257
258def compare_images(image, reference, baseimg=None, expected_match=True):
259 """
260 Print a nice human-readable message comparing these images.
261 """
262 expected_ret = 0 if expected_match else 1
263 if baseimg:
264 assert qemu_img("rebase", "-u", "-b", baseimg, image) == 0
265 ret = qemu_img("compare", image, reference)
266 log('qemu_img compare "{:s}" "{:s}" ==> {:s}, {:s}'.format(
267 image, reference,
268 "Identical" if ret == 0 else "Mismatch",
269 "OK!" if ret == expected_ret else "ERROR!"),
270 filters=[iotests.filter_testfiles])
271
John Snow0af2a092019-07-29 16:35:55 -0400272def test_bitmap_sync(bsync_mode, msync_mode='bitmap', failure=None):
John Snowdfdc48d2019-07-29 16:35:54 -0400273 """
274 Test bitmap backup routines.
275
276 :param bsync_mode: Is the Bitmap Sync mode, and can be any of:
277 - on-success: This is the "incremental" style mode. Bitmaps are
278 synchronized to what was copied out only on success.
279 (Partial images must be discarded.)
280 - never: This is the "differential" style mode.
281 Bitmaps are never synchronized.
282 - always: This is a "best effort" style mode.
283 Bitmaps are always synchronized, regardless of failure.
284 (Partial images must be kept.)
285
286 :param failure: Is the (optional) failure mode, and can be any of:
287 - None: No failure. Test the normative path. Default.
288 - simulated: Cancel the job right before it completes.
289 This also tests writes "during" the job.
290 - intermediate: This tests a job that fails mid-process and produces
291 an incomplete backup. Testing limitations prevent
292 testing competing writes.
293 """
294 with iotests.FilePaths(['img', 'bsync1', 'bsync2',
295 'fbackup0', 'fbackup1', 'fbackup2']) as \
296 (img_path, bsync1, bsync2,
297 fbackup0, fbackup1, fbackup2), \
298 iotests.VM() as vm:
299
John Snow0af2a092019-07-29 16:35:55 -0400300 mode = "Mode {:s}; Bitmap Sync {:s}".format(msync_mode, bsync_mode)
John Snowdfdc48d2019-07-29 16:35:54 -0400301 preposition = "with" if failure else "without"
302 cond = "{:s} {:s}".format(preposition,
303 "{:s} failure".format(failure) if failure
304 else "failure")
305 log("\n=== {:s} {:s} ===\n".format(mode, cond))
306
307 log('--- Preparing image & VM ---\n')
308 drive0 = Drive(img_path, vm=vm)
309 drive0.img_create(iotests.imgfmt, SIZE)
310 vm.add_device("{},id=scsi0".format(iotests.get_virtio_scsi_device()))
311 vm.launch()
312
313 file_config = {
314 'driver': 'file',
315 'filename': drive0.path
316 }
317
318 if failure == 'intermediate':
319 file_config = {
320 'driver': 'blkdebug',
321 'image': file_config,
322 'set-state': [{
323 'event': 'flush_to_disk',
324 'state': 1,
325 'new_state': 2
326 }, {
327 'event': 'read_aio',
328 'state': 2,
329 'new_state': 3
330 }],
331 'inject-error': [{
332 'event': 'read_aio',
333 'errno': 5,
334 'state': 3,
335 'immediately': False,
336 'once': True
337 }]
338 }
339
340 vm.qmp_log('blockdev-add',
341 filters=[iotests.filter_qmp_testfiles],
342 node_name="drive0",
343 driver=drive0.fmt,
344 file=file_config)
345 drive0.node = 'drive0'
346 drive0.device = 'device0'
347 # Use share-rw to allow writes directly to the node;
348 # The anonymous block-backend for this configuration prevents us
349 # from using HMP's qemu-io commands to address the device.
350 vm.qmp_log("device_add", id=drive0.device,
351 drive=drive0.name, driver="scsi-hd",
352 share_rw=True)
353 log('')
354
355 # 0 - Writes and Reference Backup
356 perform_writes(drive0, 0)
357 reference_backup(drive0, 0, fbackup0)
358 log('--- Add Bitmap ---\n')
359 vm.qmp_log("block-dirty-bitmap-add", node=drive0.name,
360 name="bitmap0", granularity=GRANULARITY)
361 log('')
John Snow32afa5a2019-07-29 16:35:54 -0400362 ebitmap = EmulatedBitmap()
John Snowdfdc48d2019-07-29 16:35:54 -0400363
364 # 1 - Writes and Reference Backup
365 bitmaps = perform_writes(drive0, 1)
John Snow32afa5a2019-07-29 16:35:54 -0400366 ebitmap.dirty_group(1)
John Snowdfdc48d2019-07-29 16:35:54 -0400367 bitmap = get_bitmap(bitmaps, drive0.device, 'bitmap0')
John Snow32afa5a2019-07-29 16:35:54 -0400368 ebitmap.compare(bitmap)
John Snowdfdc48d2019-07-29 16:35:54 -0400369 reference_backup(drive0, 1, fbackup1)
370
John Snow0af2a092019-07-29 16:35:55 -0400371 # 1 - Test Backup (w/ Optional induced failure)
John Snowdfdc48d2019-07-29 16:35:54 -0400372 if failure == 'intermediate':
373 # Activate blkdebug induced failure for second-to-next read
374 log(vm.hmp_qemu_io(drive0.name, 'flush'))
375 log('')
John Snow0af2a092019-07-29 16:35:55 -0400376 job = backup(drive0, 1, bsync1, msync_mode,
377 bitmap="bitmap0", bitmap_mode=bsync_mode)
John Snowdfdc48d2019-07-29 16:35:54 -0400378
379 def _callback():
380 """Issue writes while the job is open to test bitmap divergence."""
381 # Note: when `failure` is 'intermediate', this isn't called.
382 log('')
383 bitmaps = perform_writes(drive0, 2)
384 # Named bitmap (static, should be unchanged)
John Snow32afa5a2019-07-29 16:35:54 -0400385 ebitmap.compare(get_bitmap(bitmaps, drive0.device, 'bitmap0'))
John Snowdfdc48d2019-07-29 16:35:54 -0400386 # Anonymous bitmap (dynamic, shows new writes)
John Snow32afa5a2019-07-29 16:35:54 -0400387 anonymous = EmulatedBitmap()
388 anonymous.dirty_group(2)
389 anonymous.compare(get_bitmap(bitmaps, drive0.device, '',
390 recording=True))
391
392 # Simulate the order in which this will happen:
393 # group 1 gets cleared first, then group two gets written.
394 if ((bsync_mode == 'on-success' and not failure) or
395 (bsync_mode == 'always')):
396 ebitmap.clear_group(1)
397 ebitmap.dirty_group(2)
John Snowdfdc48d2019-07-29 16:35:54 -0400398
399 vm.run_job(job, auto_dismiss=True, auto_finalize=False,
400 pre_finalize=_callback,
401 cancel=(failure == 'simulated'))
402 bitmaps = query_bitmaps(vm)
John Snowdfdc48d2019-07-29 16:35:54 -0400403 log(bitmaps, indent=2)
404 log('')
405
John Snowdfdc48d2019-07-29 16:35:54 -0400406 if bsync_mode == 'always' and failure == 'intermediate':
407 # We manage to copy one sector (one bit) before the error.
John Snow32afa5a2019-07-29 16:35:54 -0400408 ebitmap.clear_bit(ebitmap.first_bit)
409 ebitmap.compare(get_bitmap(bitmaps, drive0.device, 'bitmap0'))
John Snowdfdc48d2019-07-29 16:35:54 -0400410
411 # 2 - Writes and Reference Backup
412 bitmaps = perform_writes(drive0, 3)
John Snow32afa5a2019-07-29 16:35:54 -0400413 ebitmap.dirty_group(3)
414 ebitmap.compare(get_bitmap(bitmaps, drive0.device, 'bitmap0'))
John Snowdfdc48d2019-07-29 16:35:54 -0400415 reference_backup(drive0, 2, fbackup2)
416
417 # 2 - Bitmap Backup (In failure modes, this is a recovery.)
John Snow0af2a092019-07-29 16:35:55 -0400418 job = backup(drive0, 2, bsync2, "bitmap",
419 bitmap="bitmap0", bitmap_mode=bsync_mode)
John Snowdfdc48d2019-07-29 16:35:54 -0400420 vm.run_job(job, auto_dismiss=True, auto_finalize=False)
421 bitmaps = query_bitmaps(vm)
John Snowdfdc48d2019-07-29 16:35:54 -0400422 log(bitmaps, indent=2)
423 log('')
John Snow32afa5a2019-07-29 16:35:54 -0400424 if bsync_mode != 'never':
425 ebitmap.clear()
426 ebitmap.compare(get_bitmap(bitmaps, drive0.device, 'bitmap0'))
John Snowdfdc48d2019-07-29 16:35:54 -0400427
428 log('--- Cleanup ---\n')
429 vm.qmp_log("block-dirty-bitmap-remove",
430 node=drive0.name, name="bitmap0")
431 log(query_bitmaps(vm), indent=2)
432 vm.shutdown()
433 log('')
434
435 log('--- Verification ---\n')
436 # 'simulated' failures will actually all pass here because we canceled
437 # while "pending". This is actually undefined behavior,
438 # don't rely on this to be true!
439 compare_images(bsync1, fbackup1, baseimg=fbackup0,
440 expected_match=failure != 'intermediate')
441 if not failure or bsync_mode == 'always':
442 # Always keep the last backup on success or when using 'always'
443 base = bsync1
444 else:
445 base = fbackup0
446 compare_images(bsync2, fbackup2, baseimg=base)
447 compare_images(img_path, fbackup2)
448 log('')
449
450def main():
451 for bsync_mode in ("never", "on-success", "always"):
452 for failure in ("simulated", "intermediate", None):
John Snow0af2a092019-07-29 16:35:55 -0400453 test_bitmap_sync(bsync_mode, "bitmap", failure)
John Snowdfdc48d2019-07-29 16:35:54 -0400454
455if __name__ == '__main__':
456 iotests.script_main(main, supported_fmts=['qcow2'])