1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test persistent bitmap resizing.
5#
6# Copyright (c) 2019 John Snow for Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21# owner=jsnow@redhat.com
22
23import iotests
24from iotests import log
25
26iotests.script_initialize(supported_fmts=['qcow2'])
27size = 64 * 1024 * 1024 * 1024
28gran_small = 32 * 1024
29gran_large = 128 * 1024
30
31def query_bitmaps(vm):
32    res = vm.qmp("query-block")
33    return { "bitmaps": { device['device']: device.get('inserted', {})
34                          .get('dirty-bitmaps', []) for
35                          device in res['return'] } }
36
37with iotests.FilePath('img') as img_path, \
38     iotests.VM() as vm:
39
40    log('--- Preparing image & VM ---\n')
41    iotests.qemu_img_create('-f', iotests.imgfmt, img_path, str(size))
42    vm.add_drive(img_path)
43
44
45    log('--- 1st Boot (Establish Baseline Image) ---\n')
46    vm.launch()
47
48    log('\n--- Adding bitmaps Small, Medium, Large, and Transient ---\n')
49    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
50               name="Small", granularity=gran_small, persistent=True)
51    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
52               name="Medium", persistent=True)
53    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
54               name="Large", granularity=gran_large, persistent=True)
55    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
56               name="Transient", persistent=False)
57
58    log('--- Forcing flush of bitmaps to disk ---\n')
59    log(query_bitmaps(vm), indent=2)
60    vm.shutdown()
61
62
63    log('--- 2nd Boot (Grow Image) ---\n')
64    vm.launch()
65    log(query_bitmaps(vm), indent=2)
66
67    log('--- Adding new bitmap, growing image, and adding 2nd new bitmap ---')
68    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
69               name="New", persistent=True)
70    vm.qmp_log("human-monitor-command",
71               command_line="block_resize drive0 70G")
72    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
73               name="Newtwo", persistent=True)
74    log(query_bitmaps(vm), indent=2)
75
76    log('--- Forcing flush of bitmaps to disk ---\n')
77    vm.shutdown()
78
79
80    log('--- 3rd Boot (Shrink Image) ---\n')
81    vm.launch()
82    log(query_bitmaps(vm), indent=2)
83
84    log('--- Adding "NewB" bitmap, removing "New" bitmap ---')
85    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
86               name="NewB", persistent=True)
87    vm.qmp_log("block-dirty-bitmap-remove", node="drive0",
88               name="New")
89
90    log('--- Truncating image ---\n')
91    vm.qmp_log("human-monitor-command",
92               command_line="block_resize drive0 50G")
93
94    log('--- Adding "NewC" bitmap, removing "NewTwo" bitmap ---')
95    vm.qmp_log("block-dirty-bitmap-add", node="drive0",
96               name="NewC", persistent=True)
97    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="Newtwo")
98
99    log('--- Forcing flush of bitmaps to disk ---\n')
100    vm.shutdown()
101
102
103    log('--- 4th Boot (Verification and Cleanup) ---\n')
104    vm.launch()
105    log(query_bitmaps(vm), indent=2)
106
107    log('--- Removing all Bitmaps ---\n')
108    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="Small")
109    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="Medium")
110    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="Large")
111    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="NewB")
112    vm.qmp_log("block-dirty-bitmap-remove", node="drive0", name="NewC")
113    log(query_bitmaps(vm), indent=2)
114
115    log('\n--- Done ---')
116    vm.shutdown()
117