xref: /qemu/tests/avocado/ppc_hv_tests.py (revision 9c707525)
1# Tests that specifically try to exercise hypervisor features of the
2# target machines. powernv supports the Power hypervisor ISA, and
3# pseries supports the nested-HV hypervisor spec.
4#
5# Copyright (c) 2023 IBM Corporation
6#
7# This work is licensed under the terms of the GNU GPL, version 2 or
8# later.  See the COPYING file in the top-level directory.
9
10from avocado import skipIf, skipUnless
11from avocado.utils import archive
12from avocado_qemu import QemuSystemTest
13from avocado_qemu import wait_for_console_pattern, exec_command
14import os
15import time
16import subprocess
17
18deps = ["xorriso"] # dependent tools needed in the test setup/box.
19
20def which(tool):
21    """ looks up the full path for @tool, returns None if not found
22        or if @tool does not have executable permissions.
23    """
24    paths=os.getenv('PATH')
25    for p in paths.split(os.path.pathsep):
26        p = os.path.join(p, tool)
27        if os.path.exists(p) and os.access(p, os.X_OK):
28            return p
29    return None
30
31def missing_deps():
32    """ returns True if any of the test dependent tools are absent.
33    """
34    for dep in deps:
35        if which(dep) is None:
36            return True
37    return False
38
39# Alpine is a light weight distro that supports QEMU. These tests boot
40# that on the machine then run a QEMU guest inside it in KVM mode,
41# that runs the same Alpine distro image.
42# QEMU packages are downloaded and installed on each test. That's not a
43# large download, but it may be more polite to create qcow2 image with
44# QEMU already installed and use that.
45@skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test sometimes gets stuck due to console handling problem')
46@skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
47@skipUnless(os.getenv('SPEED') == 'slow', 'runtime limited')
48@skipIf(missing_deps(), 'dependencies (%s) not installed' % ','.join(deps))
49class HypervisorTest(QemuSystemTest):
50
51    timeout = 1000
52    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 console=hvc0 '
53    panic_message = 'Kernel panic - not syncing'
54    good_message = 'VFS: Cannot open root device'
55
56    def extract_from_iso(self, iso, path):
57        """
58        Extracts a file from an iso file into the test workdir
59
60        :param iso: path to the iso file
61        :param path: path within the iso file of the file to be extracted
62        :returns: path of the extracted file
63        """
64        filename = os.path.basename(path)
65
66        cwd = os.getcwd()
67        os.chdir(self.workdir)
68
69        with open(filename, "w") as outfile:
70            cmd = "xorriso -osirrox on -indev %s -cpx %s %s" % (iso, path, filename)
71            subprocess.run(cmd.split(),
72                           stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
73
74        os.chdir(cwd)
75
76        # Return complete path to extracted file.  Because callers to
77        # extract_from_iso() specify 'path' with a leading slash, it is
78        # necessary to use os.path.relpath() as otherwise os.path.join()
79        # interprets it as an absolute path and drops the self.workdir part.
80        return os.path.normpath(os.path.join(self.workdir, filename))
81
82    def setUp(self):
83        super().setUp()
84
85        iso_url = ('https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/ppc64le/alpine-standard-3.18.4-ppc64le.iso')
86
87        # Alpine use sha256 so I recalculated this myself
88        iso_sha256 = 'c26b8d3e17c2f3f0fed02b4b1296589c2390e6d5548610099af75300edd7b3ff'
89        iso_path = self.fetch_asset(iso_url, asset_hash=iso_sha256,
90                                    algorithm = "sha256")
91
92        self.iso_path = iso_path
93        self.vmlinuz = self.extract_from_iso(iso_path, '/boot/vmlinuz-lts')
94        self.initramfs = self.extract_from_iso(iso_path, '/boot/initramfs-lts')
95
96    def do_start_alpine(self):
97        self.vm.set_console()
98        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
99        self.vm.add_args("-kernel", self.vmlinuz)
100        self.vm.add_args("-initrd", self.initramfs)
101        self.vm.add_args("-smp", "4", "-m", "2g")
102        self.vm.add_args("-drive", f"file={self.iso_path},format=raw,if=none,id=drive0")
103
104        self.vm.launch()
105        wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18')
106        exec_command(self, 'root')
107        wait_for_console_pattern(self, 'localhost login:')
108        wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.')
109        exec_command(self, 'setup-alpine -qe')
110        wait_for_console_pattern(self, 'Updating repository indexes... done.')
111
112    def do_stop_alpine(self):
113        exec_command(self, 'poweroff')
114        wait_for_console_pattern(self, 'alpine:~#')
115        self.vm.wait()
116
117    def do_setup_kvm(self):
118        exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/main > /etc/apk/repositories')
119        wait_for_console_pattern(self, 'alpine:~#')
120        exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /etc/apk/repositories')
121        wait_for_console_pattern(self, 'alpine:~#')
122        exec_command(self, 'apk update')
123        wait_for_console_pattern(self, 'alpine:~#')
124        exec_command(self, 'apk add qemu-system-ppc64')
125        wait_for_console_pattern(self, 'alpine:~#')
126        exec_command(self, 'modprobe kvm-hv')
127        wait_for_console_pattern(self, 'alpine:~#')
128
129    # This uses the host's block device as the source file for guest block
130    # device for install media. This is a bit hacky but allows reuse of the
131    # iso without having a passthrough filesystem configured.
132    def do_test_kvm(self, hpt=False):
133        if hpt:
134            append = 'disable_radix'
135        else:
136            append = ''
137        exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g '
138                           '-machine pseries,x-vof=on,accel=kvm '
139                           '-machine cap-cfpc=broken,cap-sbbc=broken,'
140                                    'cap-ibs=broken,cap-ccf-assist=off '
141                           '-drive file=/dev/nvme0n1,format=raw,readonly=on '
142                           '-initrd /media/nvme0n1/boot/initramfs-lts '
143                           '-kernel /media/nvme0n1/boot/vmlinuz-lts '
144                           '-append \'usbcore.nousb ' + append + '\'')
145        # Alpine 3.18 kernel seems to crash in XHCI USB driver.
146        wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18')
147        exec_command(self, 'root')
148        wait_for_console_pattern(self, 'localhost login:')
149        wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.')
150        exec_command(self, 'poweroff >& /dev/null')
151        wait_for_console_pattern(self, 'localhost:~#')
152        wait_for_console_pattern(self, 'reboot: Power down')
153        time.sleep(1)
154        exec_command(self, '')
155        wait_for_console_pattern(self, 'alpine:~#')
156
157    def test_hv_pseries(self):
158        """
159        :avocado: tags=arch:ppc64
160        :avocado: tags=machine:pseries
161        :avocado: tags=accel:tcg
162        """
163        self.require_accelerator("tcg")
164        self.vm.add_args("-accel", "tcg,thread=multi")
165        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
166        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on")
167        self.do_start_alpine()
168        self.do_setup_kvm()
169        self.do_test_kvm()
170        self.do_stop_alpine()
171
172    def test_hv_pseries_kvm(self):
173        """
174        :avocado: tags=arch:ppc64
175        :avocado: tags=machine:pseries
176        :avocado: tags=accel:kvm
177        """
178        self.require_accelerator("kvm")
179        self.vm.add_args("-accel", "kvm")
180        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
181        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off")
182        self.do_start_alpine()
183        self.do_setup_kvm()
184        self.do_test_kvm()
185        self.do_stop_alpine()
186
187    def test_hv_powernv(self):
188        """
189        :avocado: tags=arch:ppc64
190        :avocado: tags=machine:powernv
191        :avocado: tags=accel:tcg
192        """
193        self.require_accelerator("tcg")
194        self.vm.add_args("-accel", "tcg,thread=multi")
195        self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0',
196                         '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0',
197                         '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine')
198        self.do_start_alpine()
199        self.do_setup_kvm()
200        self.do_test_kvm()
201        self.do_test_kvm(True)
202        self.do_stop_alpine()
203