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 < 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
32
33awk -v image="$image" -v consolelog="$consolelog" '
34{
35	line = "";
36	for (i = 1; i <= NF; i++) {
37		if (line == "")
38			line = $i;
39		else
40			line = line " " $i;
41		if ($i == "-serial") {
42			i++;
43			line = line " file:" consolelog;
44		}
45		if ($i == "-kernel") {
46			i++;
47			line = line " " image;
48		}
49	}
50	print line;
51}'
52