1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0+ 3# 4# Shell functions for the rest of the scripts. 5# 6# Copyright (C) IBM Corporation, 2013 7# 8# Authors: Paul E. McKenney <paulmck@linux.ibm.com> 9 10# bootparam_hotplug_cpu bootparam-string 11# 12# Returns 1 if the specified boot-parameter string tells rcutorture to 13# test CPU-hotplug operations. 14bootparam_hotplug_cpu () { 15 echo "$1" | grep -q "torture\.onoff_" 16} 17 18# checkarg --argname argtype $# arg mustmatch cannotmatch 19# 20# Checks the specified argument "arg" against the mustmatch and cannotmatch 21# patterns. 22checkarg () { 23 if test $3 -le 1 24 then 25 echo $1 needs argument $2 matching \"$5\" 26 usage 27 fi 28 if echo "$4" | grep -q -e "$5" 29 then 30 : 31 else 32 echo $1 $2 \"$4\" must match \"$5\" 33 usage 34 fi 35 if echo "$4" | grep -q -e "$6" 36 then 37 echo $1 $2 \"$4\" must not match \"$6\" 38 usage 39 fi 40} 41 42# configfrag_boot_params bootparam-string config-fragment-file 43# 44# Adds boot parameters from the .boot file, if any. 45configfrag_boot_params () { 46 if test -r "$2.boot" 47 then 48 echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '` 49 else 50 echo $1 51 fi 52} 53 54# configfrag_boot_cpus bootparam-string config-fragment-file config-cpus 55# 56# Decreases number of CPUs based on any nr_cpus= boot parameters specified. 57configfrag_boot_cpus () { 58 local bootargs="`configfrag_boot_params "$1" "$2"`" 59 local nr_cpus 60 if echo "${bootargs}" | grep -q 'nr_cpus=[0-9]' 61 then 62 nr_cpus="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`" 63 if test "$3" -gt "$nr_cpus" 64 then 65 echo $nr_cpus 66 else 67 echo $3 68 fi 69 else 70 echo $3 71 fi 72} 73 74# configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus 75# 76# Decreases number of CPUs based on any maxcpus= boot parameters specified. 77# This allows tests where additional CPUs come online later during the 78# test run. However, the torture parameters will be set based on the 79# number of CPUs initially present, so the scripting should schedule 80# test runs based on the maxcpus= boot parameter controlling the initial 81# number of CPUs instead of on the ultimate number of CPUs. 82configfrag_boot_maxcpus () { 83 local bootargs="`configfrag_boot_params "$1" "$2"`" 84 local maxcpus 85 if echo "${bootargs}" | grep -q 'maxcpus=[0-9]' 86 then 87 maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`" 88 if test "$3" -gt "$maxcpus" 89 then 90 echo $maxcpus 91 else 92 echo $3 93 fi 94 else 95 echo $3 96 fi 97} 98 99# configfrag_hotplug_cpu config-fragment-file 100# 101# Returns 1 if the config fragment specifies hotplug CPU. 102configfrag_hotplug_cpu () { 103 if test ! -r "$1" 104 then 105 echo Unreadable config fragment "$1" 1>&2 106 exit -1 107 fi 108 grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1" 109} 110 111# identify_boot_image qemu-cmd 112# 113# Returns the relative path to the kernel build image. This will be 114# arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the 115# architecture, unless overridden with the TORTURE_BOOT_IMAGE environment 116# variable. 117identify_boot_image () { 118 if test -n "$TORTURE_BOOT_IMAGE" 119 then 120 echo $TORTURE_BOOT_IMAGE 121 else 122 case "$1" in 123 qemu-system-x86_64|qemu-system-i386) 124 echo arch/x86/boot/bzImage 125 ;; 126 qemu-system-aarch64) 127 echo arch/arm64/boot/Image 128 ;; 129 *) 130 echo vmlinux 131 ;; 132 esac 133 fi 134} 135 136# identify_qemu builddir 137# 138# Returns our best guess as to which qemu command is appropriate for 139# the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable. 140identify_qemu () { 141 local u="`file "$1"`" 142 if test -n "$TORTURE_QEMU_CMD" 143 then 144 echo $TORTURE_QEMU_CMD 145 elif echo $u | grep -q x86-64 146 then 147 echo qemu-system-x86_64 148 elif echo $u | grep -q "Intel 80386" 149 then 150 echo qemu-system-i386 151 elif echo $u | grep -q aarch64 152 then 153 echo qemu-system-aarch64 154 elif uname -a | grep -q ppc64 155 then 156 echo qemu-system-ppc64 157 else 158 echo Cannot figure out what qemu command to use! 1>&2 159 echo file $1 output: $u 160 # Usually this will be one of /usr/bin/qemu-system-* 161 # Use TORTURE_QEMU_CMD environment variable or appropriate 162 # argument to top-level script. 163 exit 1 164 fi 165} 166 167# identify_qemu_append qemu-cmd 168# 169# Output arguments for the qemu "-append" string based on CPU type 170# and the TORTURE_QEMU_INTERACTIVE environment variable. 171identify_qemu_append () { 172 echo debug_boot_weak_hash 173 local console=ttyS0 174 case "$1" in 175 qemu-system-x86_64|qemu-system-i386) 176 echo selinux=0 initcall_debug debug 177 ;; 178 qemu-system-aarch64) 179 console=ttyAMA0 180 ;; 181 esac 182 if test -n "$TORTURE_QEMU_INTERACTIVE" 183 then 184 echo root=/dev/sda 185 else 186 echo console=$console 187 fi 188} 189 190# identify_qemu_args qemu-cmd serial-file 191# 192# Output arguments for qemu arguments based on the TORTURE_QEMU_MAC 193# and TORTURE_QEMU_INTERACTIVE environment variables. 194identify_qemu_args () { 195 local KVM_CPU="" 196 case "$1" in 197 qemu-system-x86_64) 198 KVM_CPU=kvm64 199 ;; 200 qemu-system-i386) 201 KVM_CPU=kvm32 202 ;; 203 esac 204 case "$1" in 205 qemu-system-x86_64|qemu-system-i386) 206 echo -machine q35,accel=kvm 207 echo -cpu ${KVM_CPU} 208 ;; 209 qemu-system-aarch64) 210 echo -machine virt,gic-version=host -cpu host 211 ;; 212 qemu-system-ppc64) 213 echo -enable-kvm -M pseries -nodefaults 214 echo -device spapr-vscsi 215 if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC" 216 then 217 echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC 218 echo -netdev bridge,br=br0,id=net0 219 fi 220 ;; 221 esac 222 if test -n "$TORTURE_QEMU_INTERACTIVE" 223 then 224 echo -monitor stdio -serial pty -S 225 else 226 echo -serial file:$2 227 fi 228} 229 230# identify_qemu_vcpus 231# 232# Returns the number of virtual CPUs available to the aggregate of the 233# guest OSes. 234identify_qemu_vcpus () { 235 lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://' -e 's/[ ]*//g' 236} 237 238# print_bug 239# 240# Prints "BUG: " in red followed by remaining arguments 241print_bug () { 242 printf '\033[031mBUG: \033[m' 243 echo $* 244} 245 246# print_warning 247# 248# Prints "WARNING: " in yellow followed by remaining arguments 249print_warning () { 250 printf '\033[033mWARNING: \033[m' 251 echo $* 252} 253 254# specify_qemu_cpus qemu-cmd qemu-args #cpus 255# 256# Appends a string containing "-smp XXX" to qemu-args, unless the incoming 257# qemu-args already contains "-smp". 258specify_qemu_cpus () { 259 local nt; 260 261 if echo $2 | grep -q -e -smp 262 then 263 echo $2 264 else 265 case "$1" in 266 qemu-system-x86_64|qemu-system-i386|qemu-system-aarch64) 267 echo $2 -smp $3 268 ;; 269 qemu-system-ppc64) 270 nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`" 271 echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt 272 ;; 273 esac 274 fi 275} 276 277# specify_qemu_net qemu-args 278# 279# Appends a string containing "-net none" to qemu-args, unless the incoming 280# qemu-args already contains "-smp" or unless the TORTURE_QEMU_INTERACTIVE 281# environment variable is set, in which case the string that is be added is 282# instead "-net nic -net user". 283specify_qemu_net () { 284 if echo $1 | grep -q -e -net 285 then 286 echo $1 287 elif test -n "$TORTURE_QEMU_INTERACTIVE" 288 then 289 echo $1 -net nic -net user 290 else 291 echo $1 -net none 292 fi 293} 294