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