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