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