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," 103 "id=drive0,read-only=true") 104 105 self.vm.launch() 106 wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18') 107 exec_command(self, 'root') 108 wait_for_console_pattern(self, 'localhost login:') 109 wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.') 110 # If the time is wrong, SSL certificates can fail. 111 exec_command(self, 'date -s "' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S' + '"')) 112 exec_command(self, 'setup-alpine -qe') 113 wait_for_console_pattern(self, 'Updating repository indexes... done.') 114 115 def do_stop_alpine(self): 116 exec_command(self, 'poweroff') 117 wait_for_console_pattern(self, 'alpine:~#') 118 self.vm.wait() 119 120 def do_setup_kvm(self): 121 exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/main > /etc/apk/repositories') 122 wait_for_console_pattern(self, 'alpine:~#') 123 exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /etc/apk/repositories') 124 wait_for_console_pattern(self, 'alpine:~#') 125 exec_command(self, 'apk update') 126 wait_for_console_pattern(self, 'alpine:~#') 127 exec_command(self, 'apk add qemu-system-ppc64') 128 wait_for_console_pattern(self, 'alpine:~#') 129 exec_command(self, 'modprobe kvm-hv') 130 wait_for_console_pattern(self, 'alpine:~#') 131 132 # This uses the host's block device as the source file for guest block 133 # device for install media. This is a bit hacky but allows reuse of the 134 # iso without having a passthrough filesystem configured. 135 def do_test_kvm(self, hpt=False): 136 if hpt: 137 append = 'disable_radix' 138 else: 139 append = '' 140 exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g ' 141 '-machine pseries,x-vof=on,accel=kvm ' 142 '-machine cap-cfpc=broken,cap-sbbc=broken,' 143 'cap-ibs=broken,cap-ccf-assist=off ' 144 '-drive file=/dev/nvme0n1,format=raw,readonly=on ' 145 '-initrd /media/nvme0n1/boot/initramfs-lts ' 146 '-kernel /media/nvme0n1/boot/vmlinuz-lts ' 147 '-append \'usbcore.nousb ' + append + '\'') 148 # Alpine 3.18 kernel seems to crash in XHCI USB driver. 149 wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18') 150 exec_command(self, 'root') 151 wait_for_console_pattern(self, 'localhost login:') 152 wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.') 153 exec_command(self, 'poweroff >& /dev/null') 154 wait_for_console_pattern(self, 'localhost:~#') 155 wait_for_console_pattern(self, 'reboot: Power down') 156 time.sleep(1) 157 exec_command(self, '') 158 wait_for_console_pattern(self, 'alpine:~#') 159 160 def test_hv_pseries(self): 161 self.require_accelerator("tcg") 162 self.set_machine('pseries') 163 self.vm.add_args("-accel", "tcg,thread=multi") 164 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 165 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on") 166 self.do_start_alpine() 167 self.do_setup_kvm() 168 self.do_test_kvm() 169 self.do_stop_alpine() 170 171 def test_hv_pseries_kvm(self): 172 self.require_accelerator("kvm") 173 self.set_machine('pseries') 174 self.vm.add_args("-accel", "kvm") 175 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 176 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off") 177 self.do_start_alpine() 178 self.do_setup_kvm() 179 self.do_test_kvm() 180 self.do_stop_alpine() 181 182 def test_hv_powernv(self): 183 self.require_accelerator("tcg") 184 self.set_machine('powernv') 185 self.vm.add_args("-accel", "tcg,thread=multi") 186 self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0', 187 '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0', 188 '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine') 189 self.do_start_alpine() 190 self.do_setup_kvm() 191 self.do_test_kvm() 192 self.do_test_kvm(True) 193 self.do_stop_alpine() 194 195if __name__ == '__main__': 196 QemuSystemTest.main() 197