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 qemu_test import QemuSystemTest, Asset 13from qemu_test import wait_for_console_pattern, exec_command 14from qemu_test import skipIfMissingCommands, skipBigDataTest 15from qemu_test import exec_command_and_wait_for_pattern 16import os 17import time 18import subprocess 19from datetime import datetime 20 21# Alpine is a light weight distro that supports QEMU. These tests boot 22# that on the machine then run a QEMU guest inside it in KVM mode, 23# that runs the same Alpine distro image. 24# QEMU packages are downloaded and installed on each test. That's not a 25# large download, but it may be more polite to create qcow2 image with 26# QEMU already installed and use that. 27# XXX: The order of these tests seems to matter, see git blame. 28@skipIfMissingCommands("xorriso") 29@skipBigDataTest() 30class HypervisorTest(QemuSystemTest): 31 32 timeout = 1000 33 KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 console=hvc0 ' 34 panic_message = 'Kernel panic - not syncing' 35 good_message = 'VFS: Cannot open root device' 36 37 ASSET_ISO = Asset( 38 ('https://dl-cdn.alpinelinux.org/alpine/v3.21/' 39 'releases/ppc64le/alpine-standard-3.21.0-ppc64le.iso'), 40 '7651ab4e3027604535c0b36e86c901b4695bf8fe97b908f5b48590f6baae8f30') 41 42 def extract_from_iso(self, iso, path): 43 """ 44 Extracts a file from an iso file into the test workdir 45 46 :param iso: path to the iso file 47 :param path: path within the iso file of the file to be extracted 48 :returns: path of the extracted file 49 """ 50 filename = self.scratch_file(os.path.basename(path)) 51 52 cmd = "xorriso -osirrox on -indev %s -cpx %s %s" % (iso, path, filename) 53 subprocess.run(cmd.split(), 54 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) 55 56 os.chmod(filename, 0o600) 57 58 return filename 59 60 def setUp(self): 61 super().setUp() 62 63 self.iso_path = self.ASSET_ISO.fetch() 64 self.vmlinuz = self.extract_from_iso(self.iso_path, '/boot/vmlinuz-lts') 65 self.initramfs = self.extract_from_iso(self.iso_path, '/boot/initramfs-lts') 66 67 def do_start_alpine(self): 68 self.vm.set_console() 69 kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE 70 self.vm.add_args("-kernel", self.vmlinuz) 71 self.vm.add_args("-initrd", self.initramfs) 72 self.vm.add_args("-smp", "4", "-m", "2g") 73 self.vm.add_args("-drive", f"file={self.iso_path},format=raw,if=none," 74 "id=drive0,read-only=true") 75 76 self.vm.launch() 77 ps1='localhost:~#' 78 wait_for_console_pattern(self, 'localhost login:') 79 exec_command_and_wait_for_pattern(self, 'root', ps1) 80 # If the time is wrong, SSL certificates can fail. 81 exec_command_and_wait_for_pattern(self, 'date -s "' + datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S' + '"'), ps1) 82 ps1='alpine:~#' 83 exec_command_and_wait_for_pattern(self, 'setup-alpine -qe', ps1) 84 exec_command_and_wait_for_pattern(self, 'setup-apkrepos -c1', ps1) 85 exec_command_and_wait_for_pattern(self, 'apk update', ps1) 86 # Could upgrade here but it usually should not be necessary 87 # exec_command_and_wait_for_pattern(self, 'apk upgrade --available', ps1) 88 89 def do_stop_alpine(self): 90 exec_command(self, 'echo "TEST ME"') 91 wait_for_console_pattern(self, 'alpine:~#') 92 exec_command(self, 'poweroff') 93 wait_for_console_pattern(self, 'reboot: Power down') 94 self.vm.wait() 95 96 def do_setup_kvm(self): 97 ps1='alpine:~#' 98 exec_command_and_wait_for_pattern(self, 'apk add qemu-system-ppc64', ps1) 99 exec_command_and_wait_for_pattern(self, 'modprobe kvm-hv', ps1) 100 101 # This uses the host's block device as the source file for guest block 102 # device for install media. This is a bit hacky but allows reuse of the 103 # iso without having a passthrough filesystem configured. 104 def do_test_kvm(self, hpt=False): 105 if hpt: 106 append = 'disable_radix' 107 else: 108 append = '' 109 exec_command(self, 'qemu-system-ppc64 -nographic -smp 2 -m 1g ' 110 '-machine pseries,x-vof=on,accel=kvm ' 111 '-machine cap-cfpc=broken,cap-sbbc=broken,' 112 'cap-ibs=broken,cap-ccf-assist=off ' 113 '-drive file=/dev/nvme0n1,format=raw,readonly=on ' 114 '-initrd /media/nvme0n1/boot/initramfs-lts ' 115 '-kernel /media/nvme0n1/boot/vmlinuz-lts ' 116 '-append \'usbcore.nousb ' + append + '\'') 117 # Alpine 3.21 kernel seems to crash in XHCI USB driver. 118 ps1='localhost:~#' 119 wait_for_console_pattern(self, 'localhost login:') 120 exec_command_and_wait_for_pattern(self, 'root', ps1) 121 exec_command(self, 'poweroff') 122 wait_for_console_pattern(self, 'reboot: Power down') 123 # Now wait for the host's prompt to come back 124 wait_for_console_pattern(self, 'alpine:~#') 125 126 def test_hv_pseries(self): 127 self.require_accelerator("tcg") 128 self.set_machine('pseries') 129 self.vm.add_args("-accel", "tcg,thread=multi") 130 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 131 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on") 132 self.do_start_alpine() 133 self.do_setup_kvm() 134 self.do_test_kvm() 135 self.do_stop_alpine() 136 137 def test_hv_pseries_kvm(self): 138 self.require_accelerator("kvm") 139 self.set_machine('pseries') 140 self.vm.add_args("-accel", "kvm") 141 self.vm.add_args('-device', 'nvme,serial=1234,drive=drive0') 142 self.vm.add_args("-machine", "x-vof=on,cap-nested-hv=on,cap-ccf-assist=off") 143 self.do_start_alpine() 144 self.do_setup_kvm() 145 self.do_test_kvm() 146 self.do_stop_alpine() 147 148 def test_hv_powernv(self): 149 self.require_accelerator("tcg") 150 self.set_machine('powernv') 151 self.vm.add_args("-accel", "tcg,thread=multi") 152 self.vm.add_args('-device', 'nvme,bus=pcie.2,addr=0x0,serial=1234,drive=drive0', 153 '-device', 'e1000e,netdev=net0,mac=C0:FF:EE:00:00:02,bus=pcie.0,addr=0x0', 154 '-netdev', 'user,id=net0,hostfwd=::20022-:22,hostname=alpine') 155 self.do_start_alpine() 156 self.do_setup_kvm() 157 self.do_test_kvm() 158 self.do_test_kvm(True) 159 self.do_stop_alpine() 160 161if __name__ == '__main__': 162 QemuSystemTest.main() 163