1#!/bin/bash
2#
3# Run a kvm-based test of the specified tree on the specified configs.
4# Fully automated run and error checking, no graphics console.
5#
6# Execute this in the source tree.  Do not run it as a background task
7# because qemu does not seem to like that much.
8#
9# Usage: sh kvm-test-1-run.sh config builddir resdir minutes qemu-args boot_args
10#
11# qemu-args defaults to "-nographic", along with arguments specifying the
12#			number of CPUs and other options generated from
13#			the underlying CPU architecture.
14# boot_args defaults to value returned by the per_version_boot_params
15#			shell function.
16#
17# Anything you specify for either qemu-args or boot_args is appended to
18# the default values.  The "-smp" value is deduced from the contents of
19# the config fragment.
20#
21# More sophisticated argument parsing is clearly needed.
22#
23# This program is free software; you can redistribute it and/or modify
24# it under the terms of the GNU General Public License as published by
25# the Free Software Foundation; either version 2 of the License, or
26# (at your option) any later version.
27#
28# This program is distributed in the hope that it will be useful,
29# but WITHOUT ANY WARRANTY; without even the implied warranty of
30# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31# GNU General Public License for more details.
32#
33# You should have received a copy of the GNU General Public License
34# along with this program; if not, you can access it online at
35# http://www.gnu.org/licenses/gpl-2.0.html.
36#
37# Copyright (C) IBM Corporation, 2011
38#
39# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
40
41grace=120
42
43T=/tmp/kvm-test-1-run.sh.$$
44trap 'rm -rf $T' 0
45
46. $KVM/bin/functions.sh
47. $KVPATH/ver_functions.sh
48
49config_template=${1}
50config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
51title=`echo $config_template | sed -e 's/^.*\///'`
52builddir=${2}
53if test -z "$builddir" -o ! -d "$builddir" -o ! -w "$builddir"
54then
55	echo "kvm-test-1-run.sh :$builddir: Not a writable directory, cannot build into it"
56	exit 1
57fi
58resdir=${3}
59if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
60then
61	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
62	exit 1
63fi
64cp $config_template $resdir/ConfigFragment
65echo ' ---' `date`: Starting build
66echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
67if test -r "$config_dir/CFcommon"
68then
69	cat < $config_dir/CFcommon >> $T
70fi
71# Optimizations below this point
72# CONFIG_USB=n
73# CONFIG_SECURITY=n
74# CONFIG_NFS_FS=n
75# CONFIG_SOUND=n
76# CONFIG_INPUT_JOYSTICK=n
77# CONFIG_INPUT_TABLET=n
78# CONFIG_INPUT_TOUCHSCREEN=n
79# CONFIG_INPUT_MISC=n
80# CONFIG_INPUT_MOUSE=n
81# # CONFIG_NET=n # disables console access, so accept the slower build.
82# CONFIG_SCSI=n
83# CONFIG_ATA=n
84# CONFIG_FAT_FS=n
85# CONFIG_MSDOS_FS=n
86# CONFIG_VFAT_FS=n
87# CONFIG_ISO9660_FS=n
88# CONFIG_QUOTA=n
89# CONFIG_HID=n
90# CONFIG_CRYPTO=n
91# CONFIG_PCCARD=n
92# CONFIG_PCMCIA=n
93# CONFIG_CARDBUS=n
94# CONFIG_YENTA=n
95if kvm-build.sh $config_template $builddir $T
96then
97	cp $builddir/Make*.out $resdir
98	cp $builddir/.config $resdir
99	cp $builddir/arch/x86/boot/bzImage $resdir
100	parse-build.sh $resdir/Make.out $title
101	if test -f $builddir.wait
102	then
103		mv $builddir.wait $builddir.ready
104	fi
105else
106	cp $builddir/Make*.out $resdir
107	echo Build failed, not running KVM, see $resdir.
108	if test -f $builddir.wait
109	then
110		mv $builddir.wait $builddir.ready
111	fi
112	exit 1
113fi
114while test -f $builddir.ready
115do
116	sleep 1
117done
118minutes=$4
119seconds=$(($minutes * 60))
120qemu_args=$5
121boot_args=$6
122
123cd $KVM
124kstarttime=`awk 'BEGIN { print systime() }' < /dev/null`
125echo ' ---' `date`: Starting kernel
126
127# Determine the appropriate flavor of qemu command.
128QEMU="`identify_qemu $builddir/vmlinux`"
129
130# Generate -smp qemu argument.
131qemu_args="-nographic $qemu_args"
132cpu_count=`configNR_CPUS.sh $config_template`
133vcpus=`identify_qemu_vcpus`
134if test $cpu_count -gt $vcpus
135then
136	echo CPU count limited from $cpu_count to $vcpus
137	touch $resdir/Warnings
138	echo CPU count limited from $cpu_count to $vcpus >> $resdir/Warnings
139	cpu_count=$vcpus
140fi
141qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
142
143# Generate architecture-specific and interaction-specific qemu arguments
144qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$builddir/console.log"`"
145
146# Generate qemu -append arguments
147qemu_append="`identify_qemu_append "$QEMU"`"
148
149# Pull in Kconfig-fragment boot parameters
150boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
151# Generate kernel-version-specific boot parameters
152boot_args="`per_version_boot_params "$boot_args" $builddir/.config $seconds`"
153
154echo $QEMU $qemu_args -m 512 -kernel $builddir/arch/x86/boot/bzImage -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
155if test -n "$RCU_BUILDONLY"
156then
157	echo Build-only run specified, boot/test omitted.
158	exit 0
159fi
160$QEMU $qemu_args -m 512 -kernel $builddir/arch/x86/boot/bzImage -append "$qemu_append $boot_args" &
161qemu_pid=$!
162commandcompleted=0
163echo Monitoring qemu job at pid $qemu_pid
164for ((i=0;i<$seconds;i++))
165do
166	if kill -0 $qemu_pid > /dev/null 2>&1
167	then
168		sleep 1
169	else
170		commandcompleted=1
171		kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
172		if test $kruntime -lt $seconds
173		then
174			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
175		else
176			echo ' ---' `date`: Kernel done
177		fi
178		break
179	fi
180done
181if test $commandcompleted -eq 0
182then
183	echo Grace period for qemu job at pid $qemu_pid
184	for ((i=0;i<=$grace;i++))
185	do
186		if kill -0 $qemu_pid > /dev/null 2>&1
187		then
188			sleep 1
189		else
190			break
191		fi
192		if test $i -eq $grace
193		then
194			kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }'`
195			echo "!!! Hang at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
196			kill -KILL $qemu_pid
197		fi
198	done
199fi
200
201cp $builddir/console.log $resdir
202parse-${TORTURE_SUITE}torture.sh $resdir/console.log $title
203parse-console.sh $resdir/console.log $title
204