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