195fc339cSHanna Reitz#!/usr/bin/env python3 295fc339cSHanna Reitz# group: migration 395fc339cSHanna Reitz# 495fc339cSHanna Reitz# Copyright (C) 2021 Red Hat, Inc. 595fc339cSHanna Reitz# 695fc339cSHanna Reitz# This program is free software; you can redistribute it and/or modify 795fc339cSHanna Reitz# it under the terms of the GNU General Public License as published by 895fc339cSHanna Reitz# the Free Software Foundation; either version 2 of the License, or 995fc339cSHanna Reitz# (at your option) any later version. 1095fc339cSHanna Reitz# 1195fc339cSHanna Reitz# This program is distributed in the hope that it will be useful, 1295fc339cSHanna Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 1395fc339cSHanna Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1495fc339cSHanna Reitz# GNU General Public License for more details. 1595fc339cSHanna Reitz# 1695fc339cSHanna Reitz# You should have received a copy of the GNU General Public License 1795fc339cSHanna Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 1895fc339cSHanna Reitz# 1995fc339cSHanna Reitz 2095fc339cSHanna Reitzimport os 21*7acb2ddfSJohn Snowfrom subprocess import CalledProcessError 22*7acb2ddfSJohn Snow 2395fc339cSHanna Reitzimport iotests 2495fc339cSHanna Reitzfrom iotests import imgfmt, qemu_img_create, qemu_io 2595fc339cSHanna Reitz 2695fc339cSHanna Reitz 2795fc339cSHanna Reitztest_img = os.path.join(iotests.test_dir, 'test.img') 2895fc339cSHanna Reitzmig_sock = os.path.join(iotests.sock_dir, 'mig.sock') 2995fc339cSHanna Reitz 3095fc339cSHanna Reitz 3195fc339cSHanna Reitzclass TestMigrationPermissions(iotests.QMPTestCase): 3295fc339cSHanna Reitz def setUp(self): 3395fc339cSHanna Reitz qemu_img_create('-f', imgfmt, test_img, '1M') 3495fc339cSHanna Reitz 3595fc339cSHanna Reitz # Set up two VMs (source and destination) accessing the same raw 3695fc339cSHanna Reitz # image file with a virtio-blk device; prepare the destination for 3795fc339cSHanna Reitz # migration with .add_incoming() and enable migration events 3895fc339cSHanna Reitz vms = [None, None] 3995fc339cSHanna Reitz for i in range(2): 4095fc339cSHanna Reitz vms[i] = iotests.VM(path_suffix=f'{i}') 4195fc339cSHanna Reitz vms[i].add_blockdev(f'file,node-name=prot,filename={test_img}') 4295fc339cSHanna Reitz vms[i].add_blockdev(f'{imgfmt},node-name=fmt,file=prot') 4395fc339cSHanna Reitz vms[i].add_device('virtio-blk,drive=fmt') 4495fc339cSHanna Reitz 4595fc339cSHanna Reitz if i == 1: 4695fc339cSHanna Reitz vms[i].add_incoming(f'unix:{mig_sock}') 4795fc339cSHanna Reitz 4895fc339cSHanna Reitz vms[i].launch() 4995fc339cSHanna Reitz 5095fc339cSHanna Reitz result = vms[i].qmp('migrate-set-capabilities', 5195fc339cSHanna Reitz capabilities=[ 5295fc339cSHanna Reitz {'capability': 'events', 'state': True} 5395fc339cSHanna Reitz ]) 5495fc339cSHanna Reitz self.assert_qmp(result, 'return', {}) 5595fc339cSHanna Reitz 5695fc339cSHanna Reitz self.vm_s = vms[0] 5795fc339cSHanna Reitz self.vm_d = vms[1] 5895fc339cSHanna Reitz 5995fc339cSHanna Reitz def tearDown(self): 6095fc339cSHanna Reitz self.vm_s.shutdown() 6195fc339cSHanna Reitz self.vm_d.shutdown() 6295fc339cSHanna Reitz try: 6395fc339cSHanna Reitz os.remove(mig_sock) 6495fc339cSHanna Reitz except FileNotFoundError: 6595fc339cSHanna Reitz pass 6695fc339cSHanna Reitz os.remove(test_img) 6795fc339cSHanna Reitz 6895fc339cSHanna Reitz # Migrate an image in use by a virtio-blk device to another VM and 6995fc339cSHanna Reitz # verify that the WRITE permission is unshared both before and after 7095fc339cSHanna Reitz # migration 7195fc339cSHanna Reitz def test_post_migration_permissions(self): 7295fc339cSHanna Reitz # Try to access the image R/W, which should fail because virtio-blk 7395fc339cSHanna Reitz # has not been configured with share-rw=on 74*7acb2ddfSJohn Snow emsg = ('ERROR (pre-migration): qemu-io should not be able to ' 7595fc339cSHanna Reitz 'access this image, but it reported no error') 76*7acb2ddfSJohn Snow with self.assertRaises(CalledProcessError, msg=emsg) as ctx: 77*7acb2ddfSJohn Snow qemu_io('-f', imgfmt, '-c', 'quit', test_img) 78*7acb2ddfSJohn Snow if 'Is another process using the image' not in ctx.exception.stdout: 79*7acb2ddfSJohn Snow raise ctx.exception 8095fc339cSHanna Reitz 8195fc339cSHanna Reitz # Now migrate the VM 8295fc339cSHanna Reitz self.vm_s.qmp('migrate', uri=f'unix:{mig_sock}') 8395fc339cSHanna Reitz assert self.vm_s.wait_migration(None) 8495fc339cSHanna Reitz assert self.vm_d.wait_migration(None) 8595fc339cSHanna Reitz 8695fc339cSHanna Reitz # Try the same qemu-io access again, verifying that the WRITE 8795fc339cSHanna Reitz # permission remains unshared 88*7acb2ddfSJohn Snow emsg = ('ERROR (post-migration): qemu-io should not be able to ' 8995fc339cSHanna Reitz 'access this image, but it reported no error') 90*7acb2ddfSJohn Snow with self.assertRaises(CalledProcessError, msg=emsg) as ctx: 91*7acb2ddfSJohn Snow qemu_io('-f', imgfmt, '-c', 'quit', test_img) 92*7acb2ddfSJohn Snow if 'Is another process using the image' not in ctx.exception.stdout: 93*7acb2ddfSJohn Snow raise ctx.exception 9495fc339cSHanna Reitz 9595fc339cSHanna Reitz 9695fc339cSHanna Reitzif __name__ == '__main__': 9795fc339cSHanna Reitz # Only works with raw images because we are testing the 9895fc339cSHanna Reitz # BlockBackend permissions; image format drivers may additionally 9995fc339cSHanna Reitz # unshare permissions and thus tamper with the result 10095fc339cSHanna Reitz iotests.main(supported_fmts=['raw'], 10195fc339cSHanna Reitz supported_protocols=['file']) 102