Philippe Mathieu-Daudé | 7c47752 | 2020-01-30 17:32:30 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 2 | # |
| 3 | # Test qcow2 and file image creation |
| 4 | # |
| 5 | # Copyright (C) 2018 Red Hat, Inc. |
| 6 | # |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 7 | # Creator/Owner: Kevin Wolf <kwolf@redhat.com> |
| 8 | # |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License as published by |
| 11 | # the Free Software Foundation; either version 2 of the License, or |
| 12 | # (at your option) any later version. |
| 13 | # |
| 14 | # This program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | # |
| 22 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 23 | import iotests |
| 24 | from iotests import imgfmt |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 25 | |
Max Reitz | eda7a9c | 2020-10-27 20:05:53 +0100 | [diff] [blame] | 26 | iotests.script_initialize(supported_fmts=['qcow2'], |
| 27 | supported_protocols=['file']) |
Max Reitz | d2a839e | 2020-06-25 14:55:35 +0200 | [diff] [blame] | 28 | iotests.verify_working_luks() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 29 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 30 | with iotests.FilePath('t.qcow2') as disk_path, \ |
| 31 | iotests.FilePath('t.qcow2.base') as backing_path, \ |
| 32 | iotests.VM() as vm: |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 33 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 34 | vm.add_object('secret,id=keysec0,data=foo') |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 35 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 36 | # |
| 37 | # Successful image creation (defaults) |
| 38 | # |
| 39 | iotests.log("=== Successful image creation (defaults) ===") |
| 40 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 41 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 42 | size = 128 * 1024 * 1024 |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 43 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 44 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 45 | vm.blockdev_create({ 'driver': 'file', |
| 46 | 'filename': disk_path, |
| 47 | 'size': 0 }) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 48 | |
John Snow | f8ca860 | 2018-12-21 04:35:26 -0500 | [diff] [blame] | 49 | vm.qmp_log('blockdev-add', |
John Snow | 08fcd61 | 2018-12-21 04:35:27 -0500 | [diff] [blame] | 50 | filters=[iotests.filter_qmp_testfiles], |
John Snow | f8ca860 | 2018-12-21 04:35:26 -0500 | [diff] [blame] | 51 | driver='file', filename=disk_path, |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 52 | node_name='imgfile') |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 53 | |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 54 | vm.blockdev_create({ 'driver': imgfmt, |
| 55 | 'file': 'imgfile', |
| 56 | 'size': size }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 57 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 58 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 59 | iotests.img_info_log(disk_path) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 60 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 61 | # |
| 62 | # Successful image creation (inline blockdev-add, explicit defaults) |
| 63 | # |
| 64 | iotests.log("=== Successful image creation (inline blockdev-add, explicit defaults) ===") |
| 65 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 66 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 67 | # Choose a different size to show that we got a new image |
| 68 | size = 64 * 1024 * 1024 |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 69 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 70 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 71 | vm.blockdev_create({ 'driver': 'file', |
| 72 | 'filename': disk_path, |
| 73 | 'size': 0, |
| 74 | 'preallocation': 'off', |
| 75 | 'nocow': False }) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 76 | |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 77 | vm.blockdev_create({ 'driver': imgfmt, |
| 78 | 'file': { |
| 79 | 'driver': 'file', |
| 80 | 'filename': disk_path, |
| 81 | }, |
| 82 | 'size': size, |
| 83 | 'version': 'v3', |
| 84 | 'cluster-size': 65536, |
| 85 | 'preallocation': 'off', |
| 86 | 'lazy-refcounts': False, |
| 87 | 'refcount-bits': 16 }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 88 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 89 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 90 | iotests.img_info_log(disk_path) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 91 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 92 | # |
| 93 | # Successful image creation (v3 non-default options) |
| 94 | # |
| 95 | iotests.log("=== Successful image creation (v3 non-default options) ===") |
| 96 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 97 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 98 | # Choose a different size to show that we got a new image |
| 99 | size = 32 * 1024 * 1024 |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 100 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 101 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 102 | vm.blockdev_create({ 'driver': 'file', |
| 103 | 'filename': disk_path, |
| 104 | 'size': 0, |
| 105 | 'preallocation': 'falloc', |
| 106 | 'nocow': True }) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 107 | |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 108 | vm.blockdev_create({ 'driver': imgfmt, |
| 109 | 'file': { |
| 110 | 'driver': 'file', |
| 111 | 'filename': disk_path, |
| 112 | }, |
| 113 | 'size': size, |
| 114 | 'version': 'v3', |
| 115 | 'cluster-size': 2097152, |
| 116 | 'preallocation': 'metadata', |
| 117 | 'lazy-refcounts': True, |
| 118 | 'refcount-bits': 1 }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 119 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 120 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 121 | iotests.img_info_log(disk_path) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 122 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 123 | # |
| 124 | # Successful image creation (v2 non-default options) |
| 125 | # |
| 126 | iotests.log("=== Successful image creation (v2 non-default options) ===") |
| 127 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 128 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 129 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 130 | vm.blockdev_create({ 'driver': 'file', |
| 131 | 'filename': disk_path, |
| 132 | 'size': 0 }) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 133 | |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 134 | vm.blockdev_create({ 'driver': imgfmt, |
| 135 | 'file': { |
| 136 | 'driver': 'file', |
| 137 | 'filename': disk_path, |
| 138 | }, |
| 139 | 'size': size, |
| 140 | 'backing-file': backing_path, |
| 141 | 'backing-fmt': 'qcow2', |
| 142 | 'version': 'v2', |
| 143 | 'cluster-size': 512 }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 144 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 145 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 146 | iotests.img_info_log(disk_path) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 147 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 148 | # |
| 149 | # Successful image creation (encrypted) |
| 150 | # |
| 151 | iotests.log("=== Successful image creation (encrypted) ===") |
| 152 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 153 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 154 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 155 | vm.blockdev_create({ 'driver': imgfmt, |
| 156 | 'file': { |
| 157 | 'driver': 'file', |
| 158 | 'filename': disk_path, |
| 159 | }, |
| 160 | 'size': size, |
| 161 | 'encrypt': { |
| 162 | 'format': 'luks', |
| 163 | 'key-secret': 'keysec0', |
| 164 | 'cipher-alg': 'twofish-128', |
| 165 | 'cipher-mode': 'ctr', |
| 166 | 'ivgen-alg': 'plain64', |
| 167 | 'ivgen-hash-alg': 'md5', |
| 168 | 'hash-alg': 'sha1', |
| 169 | 'iter-time': 10, |
| 170 | }}) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 171 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 172 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 173 | iotests.img_info_log(disk_path) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 174 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 175 | # |
| 176 | # Invalid BlockdevRef |
| 177 | # |
| 178 | iotests.log("=== Invalid BlockdevRef ===") |
| 179 | iotests.log("") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 180 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 181 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 182 | vm.blockdev_create({ 'driver': imgfmt, |
| 183 | 'file': "this doesn't exist", |
| 184 | 'size': size }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 185 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 186 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 187 | # |
| 188 | # Invalid sizes |
| 189 | # |
| 190 | iotests.log("=== Invalid sizes ===") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 191 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 192 | # TODO Negative image sizes aren't handled correctly, but this is a problem |
| 193 | # with QAPI's implementation of the 'size' type and affects other commands |
| 194 | # as well. Once this is fixed, we may want to add a test case here. |
| 195 | # |
| 196 | # 1. Misaligned image size |
| 197 | # 2. 2^64 - 512 |
| 198 | # 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this) |
| 199 | # 4. 2^63 - 512 (generally valid, but qcow2 can't handle images this size) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 200 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 201 | vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path)) |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 202 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 203 | vm.launch() |
| 204 | for size in [ 1234, 18446744073709551104, 9223372036854775808, |
Vladimir Sementsov-Ogievskiy | 8b11700 | 2020-12-04 01:27:13 +0300 | [diff] [blame] | 205 | 9223372036854775296, 9223372035781033984 ]: |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 206 | vm.blockdev_create({ 'driver': imgfmt, |
| 207 | 'file': 'node0', |
| 208 | 'size': size }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 209 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 210 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 211 | # |
| 212 | # Invalid version |
| 213 | # |
| 214 | iotests.log("=== Invalid version ===") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 215 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 216 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 217 | vm.blockdev_create({ 'driver': imgfmt, |
| 218 | 'file': 'node0', |
| 219 | 'size': 67108864, |
| 220 | 'version': 'v1' }) |
| 221 | vm.blockdev_create({ 'driver': imgfmt, |
| 222 | 'file': 'node0', |
| 223 | 'size': 67108864, |
| 224 | 'version': 'v2', |
| 225 | 'lazy-refcounts': True }) |
| 226 | vm.blockdev_create({ 'driver': imgfmt, |
| 227 | 'file': 'node0', |
| 228 | 'size': 67108864, |
| 229 | 'version': 'v2', |
| 230 | 'refcount-bits': 8 }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 231 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 232 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 233 | # |
| 234 | # Invalid backing file options |
| 235 | # |
| 236 | iotests.log("=== Invalid backing file options ===") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 237 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 238 | vm.launch() |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 239 | vm.blockdev_create({ 'driver': imgfmt, |
| 240 | 'file': 'node0', |
| 241 | 'size': 67108864, |
| 242 | 'backing-file': '/dev/null', |
| 243 | 'preallocation': 'full' }) |
| 244 | vm.blockdev_create({ 'driver': imgfmt, |
| 245 | 'file': 'node0', |
| 246 | 'size': 67108864, |
| 247 | 'backing-fmt': imgfmt }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 248 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 249 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 250 | # |
| 251 | # Invalid cluster size |
| 252 | # |
| 253 | iotests.log("=== Invalid cluster size ===") |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 254 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 255 | vm.launch() |
| 256 | for csize in [ 1234, 128, 4194304, 0 ]: |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 257 | vm.blockdev_create({ 'driver': imgfmt, |
| 258 | 'file': 'node0', |
| 259 | 'size': 67108864, |
| 260 | 'cluster-size': csize }) |
| 261 | vm.blockdev_create({ 'driver': imgfmt, |
| 262 | 'file': 'node0', |
| 263 | 'size': 281474976710656, |
| 264 | 'cluster-size': 512 }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 265 | vm.shutdown() |
Kevin Wolf | 39218a7 | 2018-02-07 16:42:44 +0100 | [diff] [blame] | 266 | |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 267 | # |
| 268 | # Invalid refcount width |
| 269 | # |
| 270 | iotests.log("=== Invalid refcount width ===") |
| 271 | |
| 272 | vm.launch() |
| 273 | for refcount_bits in [ 128, 0, 7 ]: |
Kevin Wolf | f6da173 | 2019-12-16 17:57:19 +0100 | [diff] [blame] | 274 | vm.blockdev_create({ 'driver': imgfmt, |
| 275 | 'file': 'node0', |
| 276 | 'size': 67108864, |
| 277 | 'refcount-bits': refcount_bits }) |
Kevin Wolf | 4de110f | 2018-05-23 18:19:00 +0200 | [diff] [blame] | 278 | vm.shutdown() |