kvm-transform.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. T=`mktemp -d /tmp/kvm-transform.sh.XXXXXXXXXX`
  24. trap 'rm -rf $T' 0 2
  25. image="$1"
  26. if test -z "$image"
  27. then
  28. echo Need kernel image file.
  29. exit 1
  30. fi
  31. consolelog="$2"
  32. if test -z "$consolelog"
  33. then
  34. echo "Need console log file name."
  35. exit 1
  36. fi
  37. jitter_dir="$3"
  38. if test -z "$jitter_dir" || ! test -d "$jitter_dir"
  39. then
  40. echo "Need valid jitter directory: '$jitter_dir'"
  41. exit 1
  42. fi
  43. seconds="$4"
  44. if test -n "$seconds" && echo $seconds | grep -q '[^0-9]'
  45. then
  46. echo "Invalid duration, should be numeric in seconds: '$seconds'"
  47. exit 1
  48. fi
  49. bootargs="$5"
  50. # Build awk program.
  51. echo "BEGIN {" > $T/bootarg.awk
  52. echo $bootargs | tr -s ' ' '\012' |
  53. awk -v dq='"' '/./ { print "\tbootarg[" NR "] = " dq $1 dq ";" }' >> $T/bootarg.awk
  54. echo $bootargs | tr -s ' ' '\012' | sed -e 's/=.*$//' |
  55. awk -v dq='"' '/./ { print "\tbootpar[" NR "] = " dq $1 dq ";" }' >> $T/bootarg.awk
  56. cat >> $T/bootarg.awk << '___EOF___'
  57. }
  58. /^# seconds=/ {
  59. if (seconds == "")
  60. print $0;
  61. else
  62. print "# seconds=" seconds;
  63. next;
  64. }
  65. /^# TORTURE_JITTER_START=/ {
  66. print "# TORTURE_JITTER_START=\". jitterstart.sh " $4 " " jitter_dir " " $6 " " $7;
  67. next;
  68. }
  69. /^# TORTURE_JITTER_STOP=/ {
  70. print "# TORTURE_JITTER_STOP=\". jitterstop.sh " " " jitter_dir " " $5;
  71. next;
  72. }
  73. /^#/ {
  74. print $0;
  75. next;
  76. }
  77. {
  78. line = "";
  79. for (i = 1; i <= NF; i++) {
  80. if (line == "") {
  81. line = $i;
  82. } else {
  83. line = line " " $i;
  84. }
  85. if ($i == "-serial") {
  86. i++;
  87. line = line " file:" consolelog;
  88. } else if ($i == "-kernel") {
  89. i++;
  90. line = line " " image;
  91. } else if ($i == "-append") {
  92. for (i++; i <= NF; i++) {
  93. arg = $i;
  94. lq = "";
  95. rq = "";
  96. if ("" seconds != "" && $i ~ /\.shutdown_secs=[0-9]*$/)
  97. sub(/[0-9]*$/, seconds, arg);
  98. if (arg ~ /^"/) {
  99. lq = substr(arg, 1, 1);
  100. arg = substr(arg, 2);
  101. }
  102. if (arg ~ /"$/) {
  103. rq = substr(arg, length($i), 1);
  104. arg = substr(arg, 1, length($i) - 1);
  105. }
  106. par = arg;
  107. gsub(/=.*$/, "", par);
  108. j = 1;
  109. while (bootpar[j] != "") {
  110. if (bootpar[j] == par) {
  111. arg = "";
  112. break;
  113. }
  114. j++;
  115. }
  116. if (line == "")
  117. line = lq arg;
  118. else
  119. line = line " " lq arg;
  120. }
  121. for (j in bootarg)
  122. line = line " " bootarg[j];
  123. line = line rq;
  124. }
  125. }
  126. print line;
  127. }
  128. ___EOF___
  129. awk -v image="$image" -v consolelog="$consolelog" -v jitter_dir="$jitter_dir" \
  130. -v seconds="$seconds" -f $T/bootarg.awk