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