1# Tests that specifically try to exercise hypervisor features of the 2# target machines. powernv supports the Power hypervisor ISA, and 3# pseries supports the nested-HV hypervisor spec. 4# 5# Copyright (c) 2023 IBM Corporation 6# 7# This work is licensed under the terms of the GNU GPL, version 2 or 8# later. See the COPYING file in the top-level directory. 9 10from avocado import skipIf, skipUnless 11from avocado.utils import archive 12from avocado_qemu import QemuSystemTest 13from avocado_qemu import wait_for_console_pattern, exec_command 14import os 15import time 16import subprocess 17 18deps = ["xorriso"] # dependent tools needed in the test setup/box. 19 20def which(tool): 21 """ looks up the full path for @tool, returns None if not found 22 or if @tool does not have executable permissions. 23 """ 24 paths=os.getenv('PATH') 25 for p in paths.split(os.path.pathsep): 26 p = os.path.join(p, tool) 27 if os.path.exists(p) and os.access(p, os.X_OK): 28 return p 29 return None 30 31def missing_deps(): 32 """ returns True if any of the test dependent tools are absent. 33 """ 34 for dep in deps: 35 if which(dep) is None: 36 return True 37 return False 38 39# Alpine is a light weight distro that supports QEMU. These tests boot 40# that on the machine then run a QEMU guest inside it in KVM mode, 41# that runs the same Alpine distro image. 42# QEMU packages are downloaded and installed on each test. That's not a 43# large download, but it may be more polite to create qcow2 image with 44# QEMU already installed and use that. 45# XXX: The order of these tests seems to matter, see git blame. 46@skipIf(missing_deps(), 'dependencies (%s) not installed' % ','.join(deps)) 47@skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test sometimes gets stuck due to console handling problem') 48@skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited') 49@skipUnless(os.getenv('SPEED') == 'slow', 'runtime 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 def extract_from_iso(self, iso, path): 58 """ 59 Extracts a file from an iso file into the test workdir 60 61 :param iso: path to the iso file 62 :param path: path within the iso file of the file to be extracted 63 :returns: path of the extracted file 64 """ 65 filename = os.path.basename(path) 66 67 cwd = os.getcwd() 68 os.chdir(self.workdir) 69 70 with open(filename, "w") as outfile: 71 cmd = "xorriso -osirrox on -indev %s -cpx %s %s" % (iso, path, filename) 72 subprocess.run(cmd.split(), 73 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 74 75 os.chdir(cwd) 76 77 # Return complete path to extracted file. Because callers to 78 # extract_from_iso() specify 'path' with a leading slash, it is 79 # necessary to use os.path.relpath() as otherwise os.path.join() 80 # interprets it as an absolute path and drops the self.workdir part. 81 return os.path.normpath(os.path.join(self.workdir, filename)) 82 83 def setUp(self): 84 super().setUp() 85 86 iso_url = ('https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/ppc64le/alpine-standard-3.18.4-ppc64le.iso') 87 88 # Alpine use sha256 so I recalculated this myself 89 iso_sha256 = 'c26b8d3e17c2f3f0fed02b4b1296589c2390e6d5548610099af75300edd7b3ff' 90 iso_path = self.fetch_asset(iso_url, asset_hash=iso_sha256, 91 algorithm = "sha256") 92 93 self.iso_path = iso_path 94 self.vmlinuz = self.extract_from_iso(iso_path, '/boot/vmlinuz-lts') 95 self.initramfs = self.extract_from_iso(iso_path, '/boot/initramfs-lts') 96 97 def do_start_alpine(self): 98 self.vm.set_console() 99 kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE 100 self.vm.add_args("-kernel", self.vmlinuz) 101 self.vm.add_args("-initrd", self.initramfs) 102 self.vm.add_args("-smp", "4", "-m", "2g") 103 self.vm.add_args("-drive", f"file={self.iso_path},format=raw,if=none,id=drive0") 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 exec_command(self, 'setup-alpine -qe') 111 wait_for_console_pattern(self, 'Updating repository indexes... done.') 112 113 def do_stop_alpine(self): 114 exec_command(self, 'poweroff') 115 wait_for_console_pattern(self, 'alpine:~#') 116 self.vm.wait() 117 118 def do_setup_kvm(self): 119 exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/main > /etc/apk/repositories') 120 wait_for_console_pattern(self, 'alpine:~#') 121 exec_command(self, 'echo http://dl-cdn.alpinelinux.org/alpine/v3.18/community >> /etc/apk/repositories') 122 wait_for_console_pattern(self, 'alpine:~#') 123 exec_command(self, 'apk update') 124 wait_for_console_pattern(self, 'alpine:~#') 125 exec_command(self, 'apk add qemu-system-ppc64') 126 wait_for_console_pattern(self, 'alpine:~#') 127 exec_command(self, 'modprobe kvm-hv') 128 wait_for_console_pattern(self, 'alpine:~#') 129 130 # This uses the host's block device as the source file for guest block 131 # device for install media. This is a bit hacky but allows reuse of the 132 # iso without having a passthrough filesystem configured. 133 def do_test_kvm(self, hpt=False): 134 if hpt: 135 append = 'disable_radix' 136 else: 137 append = '' 138 exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g ' 139 '-machine pseries,x-vof=on,accel=kvm ' 140 '-machine cap-cfpc=broken,cap-sbbc=broken,' 141 'cap-ibs=broken,cap-ccf-assist=off ' 142 '-drive file=/dev/nvme0n1,format=raw,readonly=on ' 143 '-initrd /media/nvme0n1/boot/initramfs-lts ' 144 '-kernel /media/nvme0n1/boot/vmlinuz-lts ' 145 '-append \'usbcore.nousb ' + append + '\'') 146 # Alpine 3.18 kernel seems to crash in XHCI USB driver. 147 wait_for_console_pattern(self, 'Welcome to Alpine Linux 3.18') 148 exec_command(self, 'root') 149 wait_for_console_pattern(self, 'localhost login:') 150 wait_for_console_pattern(self, 'You may change this message by editing /etc/motd.') 151 exec_command(self, 'poweroff >& /dev/null') 152 wait_for_console_pattern(self, 'localhost:~#') 153 wait_for_console_pattern(self, 'reboot: Power down') 154 time.sleep(1) 155 exec_command(self, '') 156 wait_for_console_pattern(self, 'alpine:~#') 157 158 def test_hv_pseries(self): 159 """ 160 :avocado: tags=arch:ppc64 161 :avocado: tags=machine:pseries 162 :avocado: tags=accel:tcg 163 """ 164 self.require_accelerator("tcg") 165 self.vm.add_args("-accel", "tcg,thread=multi") 166 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 167 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on") 168 self.do_start_alpine() 169 self.do_setup_kvm() 170 self.do_test_kvm() 171 self.do_stop_alpine() 172 173 def test_hv_pseries_kvm(self): 174 """ 175 :avocado: tags=arch:ppc64 176 :avocado: tags=machine:pseries 177 :avocado: tags=accel:kvm 178 """ 179 self.require_accelerator("kvm") 180 self.vm.add_args("-accel", "kvm") 181 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 182 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off") 183 self.do_start_alpine() 184 self.do_setup_kvm() 185 self.do_test_kvm() 186 self.do_stop_alpine() 187 188 def test_hv_powernv(self): 189 """ 190 :avocado: tags=arch:ppc64 191 :avocado: tags=machine:powernv 192 :avocado: tags=accel:tcg 193 """ 194 self.require_accelerator("tcg") 195 self.vm.add_args("-accel", "tcg,thread=multi") 196 self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0', 197 '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0', 198 '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine') 199 self.do_start_alpine() 200 self.do_setup_kvm() 201 self.do_test_kvm() 202 self.do_test_kvm(True) 203 self.do_stop_alpine() 204