xref: /qemu/tests/functional/test_ppc64_hv.py (revision 8c8dd70037952b3e6b700ecec7be829a5432a432)
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 qemu_test import QemuSystemTest, Asset
13from qemu_test import wait_for_console_pattern, exec_command
14from qemu_test import skipIfMissingCommands, skipBigDataTest
15from qemu_test import exec_command_and_wait_for_pattern
16import os
17import time
18import subprocess
19from datetime import datetime
20
21# Alpine is a light weight distro that supports QEMU. These tests boot
22# that on the machine then run a QEMU guest inside it in KVM mode,
23# that runs the same Alpine distro image.
24# QEMU packages are downloaded and installed on each test. That's not a
25# large download, but it may be more polite to create qcow2 image with
26# QEMU already installed and use that.
27# XXX: The order of these tests seems to matter, see git blame.
28@skipIfMissingCommands("xorriso")
29@skipBigDataTest()
30class HypervisorTest(QemuSystemTest):
31
32    timeout = 1000
33    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 console=hvc0 '
34    panic_message = 'Kernel panic - not syncing'
35    good_message = 'VFS: Cannot open root device'
36
37    ASSET_ISO = Asset(
38        ('https://dl-cdn.alpinelinux.org/alpine/v3.18/'
39         'releases/ppc64le/alpine-standard-3.18.4-ppc64le.iso'),
40        'c26b8d3e17c2f3f0fed02b4b1296589c2390e6d5548610099af75300edd7b3ff')
41
42    def extract_from_iso(self, iso, path):
43        """
44        Extracts a file from an iso file into the test workdir
45
46        :param iso: path to the iso file
47        :param path: path within the iso file of the file to be extracted
48        :returns: path of the extracted file
49        """
50        filename = self.scratch_file(os.path.basename(path))
51
52        cmd = "xorriso -osirrox on -indev %s -cpx %s %s" % (iso, path, filename)
53        subprocess.run(cmd.split(),
54                       stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
55
56        os.chmod(filename, 0o600)
57
58        return filename
59
60    def setUp(self):
61        super().setUp()
62
63        self.iso_path = self.ASSET_ISO.fetch()
64        self.vmlinuz = self.extract_from_iso(self.iso_path, '/boot/vmlinuz-lts')
65        self.initramfs = self.extract_from_iso(self.iso_path, '/boot/initramfs-lts')
66
67    def do_start_alpine(self):
68        self.vm.set_console()
69        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
70        self.vm.add_args("-kernel", self.vmlinuz)
71        self.vm.add_args("-initrd", self.initramfs)
72        self.vm.add_args("-smp", "4", "-m", "2g")
73        self.vm.add_args("-drive", f"file={self.iso_path},format=raw,if=none,"
74                                    "id=drive0,read-only=true")
75
76        self.vm.launch()
77        ps1='localhost:~#'
78        wait_for_console_pattern(self, 'localhost login:')
79        exec_command_and_wait_for_pattern(self, 'root', ps1)
80        # If the time is wrong, SSL certificates can fail.
81        exec_command_and_wait_for_pattern(self, 'date -s "' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S' + '"'), ps1)
82        ps1='alpine:~#'
83        exec_command_and_wait_for_pattern(self, 'setup-alpine -qe', ps1)
84
85    def do_stop_alpine(self):
86        exec_command(self, 'echo "TEST ME"')
87        wait_for_console_pattern(self, 'alpine:~#')
88        exec_command(self, 'poweroff')
89        wait_for_console_pattern(self, 'reboot: Power down')
90        self.vm.wait()
91
92    def do_setup_kvm(self):
93        ps1='alpine:~#'
94        exec_command_and_wait_for_pattern(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/main > /etc/apk/repositories', ps1)
95        exec_command_and_wait_for_pattern(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /etc/apk/repositories', ps1)
96        exec_command_and_wait_for_pattern(self, 'apk update', ps1)
97        exec_command_and_wait_for_pattern(self, 'apk add qemu-system-ppc64', ps1)
98        exec_command_and_wait_for_pattern(self, 'modprobe kvm-hv', ps1)
99
100    # This uses the host's block device as the source file for guest block
101    # device for install media. This is a bit hacky but allows reuse of the
102    # iso without having a passthrough filesystem configured.
103    def do_test_kvm(self, hpt=False):
104        if hpt:
105            append = 'disable_radix'
106        else:
107            append = ''
108        exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g '
109                           '-machine pseries,x-vof=on,accel=kvm '
110                           '-machine cap-cfpc=broken,cap-sbbc=broken,'
111                                    'cap-ibs=broken,cap-ccf-assist=off '
112                           '-drive file=/dev/nvme0n1,format=raw,readonly=on '
113                           '-initrd /media/nvme0n1/boot/initramfs-lts '
114                           '-kernel /media/nvme0n1/boot/vmlinuz-lts '
115                           '-append \'usbcore.nousb ' + append + '\'')
116        # Alpine 3.18 kernel seems to crash in XHCI USB driver.
117        ps1='localhost:~#'
118        wait_for_console_pattern(self, 'localhost login:')
119        exec_command_and_wait_for_pattern(self, 'root', ps1)
120        exec_command(self, 'poweroff')
121        wait_for_console_pattern(self, 'reboot: Power down')
122        # Now wait for the host's prompt to come back
123        wait_for_console_pattern(self, 'alpine:~#')
124
125    def test_hv_pseries(self):
126        self.require_accelerator("tcg")
127        self.set_machine('pseries')
128        self.vm.add_args("-accel", "tcg,thread=multi")
129        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
130        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on")
131        self.do_start_alpine()
132        self.do_setup_kvm()
133        self.do_test_kvm()
134        self.do_stop_alpine()
135
136    def test_hv_pseries_kvm(self):
137        self.require_accelerator("kvm")
138        self.set_machine('pseries')
139        self.vm.add_args("-accel", "kvm")
140        self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0')
141        self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off")
142        self.do_start_alpine()
143        self.do_setup_kvm()
144        self.do_test_kvm()
145        self.do_stop_alpine()
146
147    def test_hv_powernv(self):
148        self.require_accelerator("tcg")
149        self.set_machine('powernv')
150        self.vm.add_args("-accel", "tcg,thread=multi")
151        self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0',
152                         '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0',
153                         '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine')
154        self.do_start_alpine()
155        self.do_setup_kvm()
156        self.do_test_kvm()
157        self.do_test_kvm(True)
158        self.do_stop_alpine()
159
160if __name__ == '__main__':
161    QemuSystemTest.main()
162