1#!/usr/bin/env python3
2#
3# Linux initrd integration test.
4#
5# Copyright (c) 2018 Red Hat, Inc.
6#
7# Author:
8#  Wainer dos Santos Moschetta <wainersm@redhat.com>
9#
10# This work is licensed under the terms of the GNU GPL, version 2 or
11# later.  See the COPYING file in the top-level directory.
12
13import os
14import logging
15import tempfile
16
17from qemu_test import QemuSystemTest, Asset
18from unittest import skipUnless
19
20
21class LinuxInitrd(QemuSystemTest):
22    """
23    Checks QEMU evaluates correctly the initrd file passed as -initrd option.
24    """
25
26    timeout = 300
27
28    ASSET_F18_KERNEL = Asset(
29        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
30         'releases/18/Fedora/x86_64/os/images/pxeboot/vmlinuz'),
31        '1a27cb42559ce29237ac186699d063556ad69c8349d732bb1bd8d614e5a8cc2e')
32
33    ASSET_F28_KERNEL = Asset(
34        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/'
35         'releases/28/Everything/x86_64/os/images/pxeboot/vmlinuz'),
36        'd05909c9d4a742a6fcc84dcc0361009e4611769619cc187a07107579a035f24e')
37
38    def test_with_2gib_file_should_exit_error_msg_with_linux_v3_6(self):
39        """
40        Pretends to boot QEMU with an initrd file with size of 2GiB
41        and expect it exits with error message.
42        Fedora-18 shipped with linux-3.6 which have not supported xloadflags
43        cannot support more than 2GiB initrd.
44        """
45        self.set_machine('pc')
46        kernel_path = self.ASSET_F18_KERNEL.fetch()
47        max_size = 2 * (1024 ** 3) - 1
48
49        with tempfile.NamedTemporaryFile() as initrd:
50            initrd.seek(max_size)
51            initrd.write(b'\0')
52            initrd.flush()
53            self.vm.add_args('-kernel', kernel_path, '-initrd', initrd.name,
54                             '-m', '4096')
55            self.vm.set_qmp_monitor(enabled=False)
56            self.vm.launch()
57            self.vm.wait()
58            self.assertEqual(self.vm.exitcode(), 1)
59            expected_msg = r'.*initrd is too large.*max: \d+, need %s.*' % (
60                max_size + 1)
61            self.assertRegex(self.vm.get_log(), expected_msg)
62
63    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
64    def test_with_2gib_file_should_work_with_linux_v4_16(self):
65        """
66        QEMU has supported up to 4 GiB initrd for recent kernel
67        Expect guest can reach 'Unpacking initramfs...'
68        """
69        self.set_machine('pc')
70        kernel_path = self.ASSET_F28_KERNEL.fetch()
71        max_size = 2 * (1024 ** 3) + 1
72
73        with tempfile.NamedTemporaryFile() as initrd:
74            initrd.seek(max_size)
75            initrd.write(b'\0')
76            initrd.flush()
77
78            self.vm.set_console()
79            kernel_command_line = 'console=ttyS0'
80            self.vm.add_args('-kernel', kernel_path,
81                             '-append', kernel_command_line,
82                             '-initrd', initrd.name,
83                             '-m', '5120')
84            self.vm.launch()
85            console = self.vm.console_socket.makefile()
86            console_logger = logging.getLogger('console')
87            while True:
88                msg = console.readline()
89                console_logger.debug(msg.strip())
90                if 'Unpacking initramfs...' in msg:
91                    break
92                if 'Kernel panic - not syncing' in msg:
93                    self.fail("Kernel panic reached")
94
95if __name__ == '__main__':
96    QemuSystemTest.main()
97