functions.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 or vmlinux if bzImage is not a target for the
  97. # architecture, unless overridden with the TORTURE_BOOT_IMAGE environment
  98. # variable.
  99. identify_boot_image () {
  100. if test -n "$TORTURE_BOOT_IMAGE"
  101. then
  102. echo $TORTURE_BOOT_IMAGE
  103. else
  104. case "$1" in
  105. qemu-system-x86_64|qemu-system-i386)
  106. echo arch/x86/boot/bzImage
  107. ;;
  108. *)
  109. echo vmlinux
  110. ;;
  111. esac
  112. fi
  113. }
  114. # identify_qemu builddir
  115. #
  116. # Returns our best guess as to which qemu command is appropriate for
  117. # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable.
  118. identify_qemu () {
  119. local u="`file "$1"`"
  120. if test -n "$TORTURE_QEMU_CMD"
  121. then
  122. echo $TORTURE_QEMU_CMD
  123. elif echo $u | grep -q x86-64
  124. then
  125. echo qemu-system-x86_64
  126. elif echo $u | grep -q "Intel 80386"
  127. then
  128. echo qemu-system-i386
  129. elif uname -a | grep -q ppc64
  130. then
  131. echo qemu-system-ppc64
  132. else
  133. echo Cannot figure out what qemu command to use! 1>&2
  134. echo file $1 output: $u
  135. # Usually this will be one of /usr/bin/qemu-system-*
  136. # Use TORTURE_QEMU_CMD environment variable or appropriate
  137. # argument to top-level script.
  138. exit 1
  139. fi
  140. }
  141. # identify_qemu_append qemu-cmd
  142. #
  143. # Output arguments for the qemu "-append" string based on CPU type
  144. # and the TORTURE_QEMU_INTERACTIVE environment variable.
  145. identify_qemu_append () {
  146. case "$1" in
  147. qemu-system-x86_64|qemu-system-i386)
  148. echo noapic selinux=0 initcall_debug debug
  149. ;;
  150. esac
  151. if test -n "$TORTURE_QEMU_INTERACTIVE"
  152. then
  153. echo root=/dev/sda
  154. else
  155. echo console=ttyS0
  156. fi
  157. }
  158. # identify_qemu_args qemu-cmd serial-file
  159. #
  160. # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
  161. # and TORTURE_QEMU_INTERACTIVE environment variables.
  162. identify_qemu_args () {
  163. case "$1" in
  164. qemu-system-x86_64|qemu-system-i386)
  165. ;;
  166. qemu-system-ppc64)
  167. echo -enable-kvm -M pseries -nodefaults
  168. echo -device spapr-vscsi
  169. if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
  170. then
  171. echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
  172. echo -netdev bridge,br=br0,id=net0
  173. elif test -n "$TORTURE_QEMU_INTERACTIVE"
  174. then
  175. echo -net nic -net user
  176. fi
  177. ;;
  178. esac
  179. if test -n "$TORTURE_QEMU_INTERACTIVE"
  180. then
  181. echo -monitor stdio -serial pty -S
  182. else
  183. echo -serial file:$2
  184. fi
  185. }
  186. # identify_qemu_vcpus
  187. #
  188. # Returns the number of virtual CPUs available to the aggregate of the
  189. # guest OSes.
  190. identify_qemu_vcpus () {
  191. lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
  192. }
  193. # print_bug
  194. #
  195. # Prints "BUG: " in red followed by remaining arguments
  196. print_bug () {
  197. printf '\033[031mBUG: \033[m'
  198. echo $*
  199. }
  200. # print_warning
  201. #
  202. # Prints "WARNING: " in yellow followed by remaining arguments
  203. print_warning () {
  204. printf '\033[033mWARNING: \033[m'
  205. echo $*
  206. }
  207. # specify_qemu_cpus qemu-cmd qemu-args #cpus
  208. #
  209. # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
  210. # qemu-args already contains "-smp".
  211. specify_qemu_cpus () {
  212. local nt;
  213. if echo $2 | grep -q -e -smp
  214. then
  215. echo $2
  216. else
  217. case "$1" in
  218. qemu-system-x86_64|qemu-system-i386)
  219. echo $2 -smp $3
  220. ;;
  221. qemu-system-ppc64)
  222. nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
  223. echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
  224. ;;
  225. esac
  226. fi
  227. }