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