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