1#!/usr/bin/env python3 2# group: migration 3# 4# Copyright (C) 2021 Red Hat, Inc. 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18# 19 20import os 21import iotests 22from iotests import imgfmt, qemu_img_create, qemu_io 23 24 25test_img = os.path.join(iotests.test_dir, 'test.img') 26mig_sock = os.path.join(iotests.sock_dir, 'mig.sock') 27 28 29class TestMigrationPermissions(iotests.QMPTestCase): 30 def setUp(self): 31 qemu_img_create('-f', imgfmt, test_img, '1M') 32 33 # Set up two VMs (source and destination) accessing the same raw 34 # image file with a virtio-blk device; prepare the destination for 35 # migration with .add_incoming() and enable migration events 36 vms = [None, None] 37 for i in range(2): 38 vms[i] = iotests.VM(path_suffix=f'{i}') 39 vms[i].add_blockdev(f'file,node-name=prot,filename={test_img}') 40 vms[i].add_blockdev(f'{imgfmt},node-name=fmt,file=prot') 41 vms[i].add_device('virtio-blk,drive=fmt') 42 43 if i == 1: 44 vms[i].add_incoming(f'unix:{mig_sock}') 45 46 vms[i].launch() 47 48 result = vms[i].qmp('migrate-set-capabilities', 49 capabilities=[ 50 {'capability': 'events', 'state': True} 51 ]) 52 self.assert_qmp(result, 'return', {}) 53 54 self.vm_s = vms[0] 55 self.vm_d = vms[1] 56 57 def tearDown(self): 58 self.vm_s.shutdown() 59 self.vm_d.shutdown() 60 try: 61 os.remove(mig_sock) 62 except FileNotFoundError: 63 pass 64 os.remove(test_img) 65 66 # Migrate an image in use by a virtio-blk device to another VM and 67 # verify that the WRITE permission is unshared both before and after 68 # migration 69 def test_post_migration_permissions(self): 70 # Try to access the image R/W, which should fail because virtio-blk 71 # has not been configured with share-rw=on 72 log = qemu_io('-f', imgfmt, '-c', 'quit', test_img) 73 if not log.strip(): 74 print('ERROR (pre-migration): qemu-io should not be able to ' 75 'access this image, but it reported no error') 76 else: 77 # This is the expected output 78 assert 'Is another process using the image' in log 79 80 # Now migrate the VM 81 self.vm_s.qmp('migrate', uri=f'unix:{mig_sock}') 82 assert self.vm_s.wait_migration(None) 83 assert self.vm_d.wait_migration(None) 84 85 # Try the same qemu-io access again, verifying that the WRITE 86 # permission remains unshared 87 log = qemu_io('-f', imgfmt, '-c', 'quit', test_img) 88 if not log.strip(): 89 print('ERROR (post-migration): qemu-io should not be able to ' 90 'access this image, but it reported no error') 91 else: 92 # This is the expected output 93 assert 'Is another process using the image' in log 94 95 96if __name__ == '__main__': 97 # Only works with raw images because we are testing the 98 # BlockBackend permissions; image format drivers may additionally 99 # unshare permissions and thus tamper with the result 100 iotests.main(supported_fmts=['raw'], 101 supported_protocols=['file']) 102