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