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