• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Shell functions for the rest of the scripts.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, you can access it online at
17# http://www.gnu.org/licenses/gpl-2.0.html.
18#
19# Copyright (C) IBM Corporation, 2013
20#
21# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
22
23# bootparam_hotplug_cpu bootparam-string
24#
25# Returns 1 if the specified boot-parameter string tells rcutorture to
26# test CPU-hotplug operations.
27bootparam_hotplug_cpu () {
28	echo "$1" | grep -q "rcutorture\.onoff_"
29}
30
31# checkarg --argname argtype $# arg mustmatch cannotmatch
32#
33# Checks the specified argument "arg" against the mustmatch and cannotmatch
34# patterns.
35checkarg () {
36	if test $3 -le 1
37	then
38		echo $1 needs argument $2 matching \"$5\"
39		usage
40	fi
41	if echo "$4" | grep -q -e "$5"
42	then
43		:
44	else
45		echo $1 $2 \"$4\" must match \"$5\"
46		usage
47	fi
48	if echo "$4" | grep -q -e "$6"
49	then
50		echo $1 $2 \"$4\" must not match \"$6\"
51		usage
52	fi
53}
54
55# configfrag_boot_params bootparam-string config-fragment-file
56#
57# Adds boot parameters from the .boot file, if any.
58configfrag_boot_params () {
59	if test -r "$2.boot"
60	then
61		echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
62	else
63		echo $1
64	fi
65}
66
67# configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
68#
69# Decreases number of CPUs based on any maxcpus= boot parameters specified.
70configfrag_boot_cpus () {
71	local bootargs="`configfrag_boot_params "$1" "$2"`"
72	local maxcpus
73	if echo "${bootargs}" | grep -q 'maxcpus=[0-9]'
74	then
75		maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
76		if test "$3" -gt "$maxcpus"
77		then
78			echo $maxcpus
79		else
80			echo $3
81		fi
82	else
83		echo $3
84	fi
85}
86
87# configfrag_hotplug_cpu config-fragment-file
88#
89# Returns 1 if the config fragment specifies hotplug CPU.
90configfrag_hotplug_cpu () {
91	if test ! -r "$1"
92	then
93		echo Unreadable config fragment "$1" 1>&2
94		exit -1
95	fi
96	grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
97}
98
99# identify_boot_image qemu-cmd
100#
101# Returns the relative path to the kernel build image.  This will be
102# arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the
103# architecture, unless overridden with the TORTURE_BOOT_IMAGE environment
104# variable.
105identify_boot_image () {
106	if test -n "$TORTURE_BOOT_IMAGE"
107	then
108		echo $TORTURE_BOOT_IMAGE
109	else
110		case "$1" in
111		qemu-system-x86_64|qemu-system-i386)
112			echo arch/x86/boot/bzImage
113			;;
114		*)
115			echo vmlinux
116			;;
117		esac
118	fi
119}
120
121# identify_qemu builddir
122#
123# Returns our best guess as to which qemu command is appropriate for
124# the kernel at hand.  Override with the TORTURE_QEMU_CMD environment variable.
125identify_qemu () {
126	local u="`file "$1"`"
127	if test -n "$TORTURE_QEMU_CMD"
128	then
129		echo $TORTURE_QEMU_CMD
130	elif echo $u | grep -q x86-64
131	then
132		echo qemu-system-x86_64
133	elif echo $u | grep -q "Intel 80386"
134	then
135		echo qemu-system-i386
136	elif uname -a | grep -q ppc64
137	then
138		echo qemu-system-ppc64
139	else
140		echo Cannot figure out what qemu command to use! 1>&2
141		echo file $1 output: $u
142		# Usually this will be one of /usr/bin/qemu-system-*
143		# Use TORTURE_QEMU_CMD environment variable or appropriate
144		# argument to top-level script.
145		exit 1
146	fi
147}
148
149# identify_qemu_append qemu-cmd
150#
151# Output arguments for the qemu "-append" string based on CPU type
152# and the TORTURE_QEMU_INTERACTIVE environment variable.
153identify_qemu_append () {
154	case "$1" in
155	qemu-system-x86_64|qemu-system-i386)
156		echo noapic selinux=0 initcall_debug debug
157		;;
158	esac
159	if test -n "$TORTURE_QEMU_INTERACTIVE"
160	then
161		echo root=/dev/sda
162	else
163		echo console=ttyS0
164	fi
165}
166
167# identify_qemu_args qemu-cmd serial-file
168#
169# Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
170# and TORTURE_QEMU_INTERACTIVE environment variables.
171identify_qemu_args () {
172	case "$1" in
173	qemu-system-x86_64|qemu-system-i386)
174		;;
175	qemu-system-ppc64)
176		echo -enable-kvm -M pseries -nodefaults
177		echo -device spapr-vscsi
178		if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
179		then
180			echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
181			echo -netdev bridge,br=br0,id=net0
182		elif test -n "$TORTURE_QEMU_INTERACTIVE"
183		then
184			echo -net nic -net user
185		fi
186		;;
187	esac
188	if test -n "$TORTURE_QEMU_INTERACTIVE"
189	then
190		echo -monitor stdio -serial pty -S
191	else
192		echo -serial file:$2
193	fi
194}
195
196# identify_qemu_vcpus
197#
198# Returns the number of virtual CPUs available to the aggregate of the
199# guest OSes.
200identify_qemu_vcpus () {
201	lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
202}
203
204# print_bug
205#
206# Prints "BUG: " in red followed by remaining arguments
207print_bug () {
208	printf '\033[031mBUG: \033[m'
209	echo $*
210}
211
212# print_warning
213#
214# Prints "WARNING: " in yellow followed by remaining arguments
215print_warning () {
216	printf '\033[033mWARNING: \033[m'
217	echo $*
218}
219
220# specify_qemu_cpus qemu-cmd qemu-args #cpus
221#
222# Appends a string containing "-smp XXX" to qemu-args, unless the incoming
223# qemu-args already contains "-smp".
224specify_qemu_cpus () {
225	local nt;
226
227	if echo $2 | grep -q -e -smp
228	then
229		echo $2
230	else
231		case "$1" in
232		qemu-system-x86_64|qemu-system-i386)
233			echo $2 -smp $3
234			;;
235		qemu-system-ppc64)
236			nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
237			echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
238			;;
239		esac
240	fi
241}
242