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