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