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 ] < 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#
11# The original qemu-cmd file is provided on standard input.
12# The transformed qemu-cmd file is on standard output.
13# The transformation assumes that the qemu command is confined to a
14# single line.  It also assumes no whitespace in filenames.
15#
16# Copyright (C) 2020 Facebook, Inc.
17#
18# Authors: Paul E. McKenney <paulmck@kernel.org>
19
20image="$1"
21if test -z "$image"
22then
23	echo Need kernel image file.
24	exit 1
25fi
26consolelog="$2"
27if test -z "$consolelog"
28then
29	echo "Need console log file name."
30	exit 1
31fi
32jitter_dir="$3"
33if test -z "$jitter_dir" || ! test -d "$jitter_dir"
34then
35	echo "Need valid jitter directory: '$jitter_dir'"
36	exit 1
37fi
38seconds="$4"
39if test -n "$seconds" && echo $seconds | grep -q '[^0-9]'
40then
41	echo "Invalid duration, should be numeric in seconds: '$seconds'"
42	exit 1
43fi
44
45awk -v image="$image" -v consolelog="$consolelog" -v jitter_dir="$jitter_dir" \
46    -v seconds="$seconds" '
47/^# seconds=/ {
48	if (seconds == "")
49		print $0;
50	else
51		print "# seconds=" seconds;
52	next;
53}
54
55/^# TORTURE_JITTER_START=/ {
56	print "# TORTURE_JITTER_START=\". jitterstart.sh " $4 " " jitter_dir " " $6 " " $7;
57	next;
58}
59
60/^# TORTURE_JITTER_STOP=/ {
61	print "# TORTURE_JITTER_STOP=\". jitterstop.sh " " " jitter_dir " " $5;
62	next;
63}
64
65/^#/ {
66	print $0;
67	next;
68}
69
70{
71	line = "";
72	for (i = 1; i <= NF; i++) {
73		if ("" seconds != "" && $i ~ /\.shutdown_secs=[0-9]*$/) {
74			sub(/[0-9]*$/, seconds, $i);
75			if (line == "")
76				line = $i;
77			else
78				line = line " " $i;
79		} else if (line == "") {
80			line = $i;
81		} else {
82			line = line " " $i;
83		}
84		if ($i == "-serial") {
85			i++;
86			line = line " file:" consolelog;
87		} else if ($i == "-kernel") {
88			i++;
89			line = line " " image;
90		}
91	}
92	print line;
93}'
94