functions.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/bin/bash
  2. #
  3. # Shell functions for the rest of the scripts.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, you can access it online at
  17. # http://www.gnu.org/licenses/gpl-2.0.html.
  18. #
  19. # Copyright (C) IBM Corporation, 2013
  20. #
  21. # Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  22. # bootparam_hotplug_cpu bootparam-string
  23. #
  24. # Returns 1 if the specified boot-parameter string tells rcutorture to
  25. # test CPU-hotplug operations.
  26. bootparam_hotplug_cpu () {
  27. echo "$1" | grep -q "rcutorture\.onoff_"
  28. }
  29. # checkarg --argname argtype $# arg mustmatch cannotmatch
  30. #
  31. # Checks the specified argument "arg" against the mustmatch and cannotmatch
  32. # patterns.
  33. checkarg () {
  34. if test $3 -le 1
  35. then
  36. echo $1 needs argument $2 matching \"$5\"
  37. usage
  38. fi
  39. if echo "$4" | grep -q -e "$5"
  40. then
  41. :
  42. else
  43. echo $1 $2 \"$4\" must match \"$5\"
  44. usage
  45. fi
  46. if echo "$4" | grep -q -e "$6"
  47. then
  48. echo $1 $2 \"$4\" must not match \"$6\"
  49. usage
  50. fi
  51. }
  52. # configfrag_boot_params bootparam-string config-fragment-file
  53. #
  54. # Adds boot parameters from the .boot file, if any.
  55. configfrag_boot_params () {
  56. if test -r "$2.boot"
  57. then
  58. echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
  59. else
  60. echo $1
  61. fi
  62. }
  63. # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
  64. #
  65. # Decreases number of CPUs based on any maxcpus= boot parameters specified.
  66. configfrag_boot_cpus () {
  67. local bootargs="`configfrag_boot_params "$1" "$2"`"
  68. local maxcpus
  69. if echo "${bootargs}" | grep -q 'maxcpus=[0-9]'
  70. then
  71. maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
  72. if test "$3" -gt "$maxcpus"
  73. then
  74. echo $maxcpus
  75. else
  76. echo $3
  77. fi
  78. else
  79. echo $3
  80. fi
  81. }
  82. # configfrag_hotplug_cpu config-fragment-file
  83. #
  84. # Returns 1 if the config fragment specifies hotplug CPU.
  85. configfrag_hotplug_cpu () {
  86. if test ! -r "$1"
  87. then
  88. echo Unreadable config fragment "$1" 1>&2
  89. exit -1
  90. fi
  91. grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
  92. }
  93. # identify_boot_image qemu-cmd
  94. #
  95. # Returns the relative path to the kernel build image. This will be
  96. # arch/<arch>/boot/bzImage unless overridden with the TORTURE_BOOT_IMAGE
  97. # environment variable.
  98. identify_boot_image () {
  99. if test -n "$TORTURE_BOOT_IMAGE"
  100. then
  101. echo $TORTURE_BOOT_IMAGE
  102. else
  103. case "$1" in
  104. qemu-system-x86_64|qemu-system-i386)
  105. echo arch/x86/boot/bzImage
  106. ;;
  107. qemu-system-ppc64)
  108. echo arch/powerpc/boot/bzImage
  109. ;;
  110. *)
  111. echo ""
  112. ;;
  113. esac
  114. fi
  115. }
  116. # identify_qemu builddir
  117. #
  118. # Returns our best guess as to which qemu command is appropriate for
  119. # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable.
  120. identify_qemu () {
  121. local u="`file "$1"`"
  122. if test -n "$TORTURE_QEMU_CMD"
  123. then
  124. echo $TORTURE_QEMU_CMD
  125. elif echo $u | grep -q x86-64
  126. then
  127. echo qemu-system-x86_64
  128. elif echo $u | grep -q "Intel 80386"
  129. then
  130. echo qemu-system-i386
  131. elif uname -a | grep -q ppc64
  132. then
  133. echo qemu-system-ppc64
  134. else
  135. echo Cannot figure out what qemu command to use! 1>&2
  136. echo file $1 output: $u
  137. # Usually this will be one of /usr/bin/qemu-system-*
  138. # Use TORTURE_QEMU_CMD environment variable or appropriate
  139. # argument to top-level script.
  140. exit 1
  141. fi
  142. }
  143. # identify_qemu_append qemu-cmd
  144. #
  145. # Output arguments for the qemu "-append" string based on CPU type
  146. # and the TORTURE_QEMU_INTERACTIVE environment variable.
  147. identify_qemu_append () {
  148. case "$1" in
  149. qemu-system-x86_64|qemu-system-i386)
  150. echo noapic selinux=0 initcall_debug debug
  151. ;;
  152. esac
  153. if test -n "$TORTURE_QEMU_INTERACTIVE"
  154. then
  155. echo root=/dev/sda
  156. else
  157. echo console=ttyS0
  158. fi
  159. }
  160. # identify_qemu_args qemu-cmd serial-file
  161. #
  162. # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
  163. # and TORTURE_QEMU_INTERACTIVE environment variables.
  164. identify_qemu_args () {
  165. case "$1" in
  166. qemu-system-x86_64|qemu-system-i386)
  167. ;;
  168. qemu-system-ppc64)
  169. echo -enable-kvm -M pseries -cpu POWER7 -nodefaults
  170. echo -device spapr-vscsi
  171. if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
  172. then
  173. echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
  174. echo -netdev bridge,br=br0,id=net0
  175. elif test -n "$TORTURE_QEMU_INTERACTIVE"
  176. then
  177. echo -net nic -net user
  178. fi
  179. ;;
  180. esac
  181. if test -n "$TORTURE_QEMU_INTERACTIVE"
  182. then
  183. echo -monitor stdio -serial pty -S
  184. else
  185. echo -serial file:$2
  186. fi
  187. }
  188. # identify_qemu_vcpus
  189. #
  190. # Returns the number of virtual CPUs available to the aggregate of the
  191. # guest OSes.
  192. identify_qemu_vcpus () {
  193. lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
  194. }
  195. # print_bug
  196. #
  197. # Prints "BUG: " in red followed by remaining arguments
  198. print_bug () {
  199. printf '\033[031mBUG: \033[m'
  200. echo $*
  201. }
  202. # print_warning
  203. #
  204. # Prints "WARNING: " in yellow followed by remaining arguments
  205. print_warning () {
  206. printf '\033[033mWARNING: \033[m'
  207. echo $*
  208. }
  209. # specify_qemu_cpus qemu-cmd qemu-args #cpus
  210. #
  211. # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
  212. # qemu-args already contains "-smp".
  213. specify_qemu_cpus () {
  214. local nt;
  215. if echo $2 | grep -q -e -smp
  216. then
  217. echo $2
  218. else
  219. case "$1" in
  220. qemu-system-x86_64|qemu-system-i386)
  221. echo $2 -smp $3
  222. ;;
  223. qemu-system-ppc64)
  224. nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
  225. echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
  226. ;;
  227. esac
  228. fi
  229. }