xref: /linux/tools/testing/selftests/rcutorture/bin/kvm-transform.sh (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Transform a qemu-cmd file to allow reuse.
5#
6# Usage: kvm-transform.sh bzImage console.log jitter_dir seconds [ bootargs ] < qemu-cmd-in > qemu-cmd-out
7#
8#	bzImage: Kernel and initrd from the same prior kvm.sh run.
9#	console.log: File into which to place console output.
10#	jitter_dir: Jitter directory for TORTURE_JITTER_START and
11#		TORTURE_JITTER_STOP environment variables.
12#	seconds: Run duaration for *.shutdown_secs module parameter.
13#	bootargs: New kernel boot parameters.  Beware of Robert Tables.
14#
15# The original qemu-cmd file is provided on standard input.
16# The transformed qemu-cmd file is on standard output.
17# The transformation assumes that the qemu command is confined to a
18# single line.  It also assumes no whitespace in filenames.
19#
20# Copyright (C) 2020 Facebook, Inc.
21#
22# Authors: Paul E. McKenney <paulmck@kernel.org>
23
24T=`mktemp -d /tmp/kvm-transform.sh.XXXXXXXXXX`
25trap 'rm -rf $T' 0 2
26
27image="$1"
28if test -z "$image"
29then
30	echo Need kernel image file.
31	exit 1
32fi
33consolelog="$2"
34if test -z "$consolelog"
35then
36	echo "Need console log file name."
37	exit 1
38fi
39jitter_dir="$3"
40if test -z "$jitter_dir" || ! test -d "$jitter_dir"
41then
42	echo "Need valid jitter directory: '$jitter_dir'"
43	exit 1
44fi
45seconds="$4"
46if test -n "$seconds" && echo $seconds | grep -q '[^0-9]'
47then
48	echo "Invalid duration, should be numeric in seconds: '$seconds'"
49	exit 1
50fi
51bootargs="$5"
52
53# Build awk program.
54echo "BEGIN {" > $T/bootarg.awk
55echo $bootargs | tr -s ' ' '\012' |
56	awk -v dq='"' '/./ { print "\tbootarg[" NR "] = " dq $1 dq ";" }' >> $T/bootarg.awk
57echo $bootargs | tr -s ' ' '\012' | sed -e 's/=.*$//' |
58	awk -v dq='"' '/./ { print "\tbootpar[" NR "] = " dq $1 dq ";" }' >> $T/bootarg.awk
59cat >> $T/bootarg.awk << '___EOF___'
60}
61
62/^# seconds=/ {
63	if (seconds == "")
64		print $0;
65	else
66		print "# seconds=" seconds;
67	next;
68}
69
70/^# TORTURE_JITTER_START=/ {
71	print "# TORTURE_JITTER_START=\". jitterstart.sh " $4 " " jitter_dir " " $6 " " $7;
72	next;
73}
74
75/^# TORTURE_JITTER_STOP=/ {
76	print "# TORTURE_JITTER_STOP=\". jitterstop.sh " " " jitter_dir " " $5;
77	next;
78}
79
80/^#/ {
81	print $0;
82	next;
83}
84
85{
86	line = "";
87	for (i = 1; i <= NF; i++) {
88		if (line == "") {
89			line = $i;
90		} else {
91			line = line " " $i;
92		}
93		if ($i == "-serial") {
94			i++;
95			line = line " file:" consolelog;
96		} else if ($i == "-kernel") {
97			i++;
98			line = line " " image;
99		} else if ($i == "-append") {
100			for (i++; i <= NF; i++) {
101				arg = $i;
102				lq = "";
103				rq = "";
104				if ("" seconds != "" && $i ~ /\.shutdown_secs=[0-9]*$/)
105					sub(/[0-9]*$/, seconds, arg);
106				if (arg ~ /^"/) {
107					lq = substr(arg, 1, 1);
108					arg  = substr(arg, 2);
109				}
110				if (arg ~ /"$/) {
111					rq = substr(arg, length($i), 1);
112					arg = substr(arg, 1, length($i) - 1);
113				}
114				par = arg;
115				gsub(/=.*$/, "", par);
116				j = 1;
117				while (bootpar[j] != "") {
118					if (bootpar[j] == par) {
119						arg = "";
120						break;
121					}
122					j++;
123				}
124				if (line == "")
125					line = lq arg;
126				else
127					line = line " " lq arg;
128			}
129			for (j in bootarg)
130				line = line " " bootarg[j];
131			line = line rq;
132		}
133	}
134	print line;
135}
136___EOF___
137
138awk -v image="$image" -v consolelog="$consolelog" -v jitter_dir="$jitter_dir" \
139    -v seconds="$seconds" -f $T/bootarg.awk
140