SkipIfEqual

generic_qemu_loader.sh

Aug 4th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.05 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #       generic_qemu_loader.sh
  4. #       Last changed 2011/08/05
  5. #
  6. #       Starts QEMU with specified Openembedded kernel and image
  7. #       Originally idea and basic network code from http://en.wikibooks.org/wiki/QEMU/Networking
  8. #
  9. #       Copyright 2011 Samuel Stirtzel <[email protected]>
  10. #   Dual licensed BSD / GPL (you can use this script either with BSD or GPL license)
  11. #      
  12. #   BSD
  13. #
  14. #       Redistribution and use in source and binary forms, with or without
  15. #       modification, are permitted provided that the following conditions are
  16. #       met:
  17. #      
  18. #       * Redistributions of source code must retain the above copyright
  19. #         notice, this list of conditions and the following disclaimer.
  20. #       * Redistributions in binary form must reproduce the above
  21. #         copyright notice, this list of conditions and the following disclaimer
  22. #         in the documentation and/or other materials provided with the
  23. #         distribution.
  24. #       * Neither the name of the authors nor the names of its
  25. #         contributors may be used to endorse or promote products derived from
  26. #         this software without specific prior written permission.
  27. #      
  28. #       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. #       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. #       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. #       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. #       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. #       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. #       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. #       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. #       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. #       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. #       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39. #
  40. #
  41. #   GPL
  42. #
  43. #       This program is free software; you can redistribute it and/or modify
  44. #       it under the terms of the GNU General Public License as published by
  45. #       the Free Software Foundation; either version 2 of the License, or
  46. #       (at your option) any later version.
  47. #      
  48. #       This program is distributed in the hope that it will be useful,
  49. #       but WITHOUT ANY WARRANTY; without even the implied warranty of
  50. #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  51. #       GNU General Public License for more details.
  52. #      
  53. #       You should have received a copy of the GNU General Public License
  54. #       along with this program; if not, write to the Free Software
  55. #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  56. #       MA 02110-1301, USA.
  57. #
  58.  
  59. function print_usage() {
  60.     echo "Usage for the shell script: $0 [-bkmor]
  61. ___________________
  62. Brief description: \\_______________________________________________________________________________
  63. Starts QEMU with standard networking or network over a bridge/tap interface.
  64. This script needs no sudo or root user rights if executed without parameter -b.
  65. ____________
  66. Parameters: \\______________________________________________________________________________________
  67. -h  Shows this information.
  68. -b  [optional] Will create a local bridge and tap interface (needs root user rights),
  69.     the interface will be deleted after QEMU stops.
  70. -k *    Uses the file after -k as kernel image.
  71.     Usage: ... -k /mydir/zImage-qemuarm.bin (or similar images)
  72. -m  [optional] Sets the main memory, default is 128M.
  73.     Usage: ... -m 256M
  74. -o  [optional] Additional parameters passed to QEMU, use quotes or -o multiple times
  75. -r *    Uses the file after -r as root filesystem.
  76.     Usage: ... -r /mydir/console-image-qemuarm.ext2 (or similar filesystems)
  77. _________
  78. Example: \\_________________________________________________________________________________________
  79. The script can be used like this.
  80. user@machine:/somedir$ sh $0 -k /mydir/zImage-qemuarm.bin \\
  81. -r /mydir/console-image-qemuarm.ext2 -b -m 256M
  82.  
  83. or with chmod +x instead of sh
  84.  
  85. This will set up the bridge/tap, start QEMU with the kernel (-k) / root filesystem (-r)
  86. and close the bridge/tap after QEMU exited."
  87. }
  88.  
  89. # pre start
  90. SCRIPT=$(readlink -f $0)
  91. scriptPath=$(dirname $SCRIPT)
  92.  
  93. # set standard variables
  94. memoryMB="128M"
  95. qemuOptions=
  96. kernelPath=0
  97. rootFSPath=0
  98.  
  99. # check options from parameters
  100. while getopts "hbk:m:o:r:" Option
  101. do
  102.     case $Option in
  103.         h ) print_usage
  104.             exit 0
  105.             ;;
  106.         b ) bridge=1
  107.             ;;
  108.         k ) kernelPath=$OPTARG
  109.             ;;
  110.         m ) memoryMB=$OPTARG
  111.             ;;
  112.     o ) qemuOptions+=" "+$OPTARG
  113.             ;;
  114.         r ) rootFSPath=$OPTARG
  115.             ;;
  116.         ? ) print_usage
  117.             exit 0
  118.             ;;
  119.     esac
  120. done
  121.  
  122. shift $(($OPTIND - 1))
  123.  
  124. if [ $kernelPath = 0 ]
  125.     then
  126.         echo "No kernel image set."
  127.         print_usage
  128.         exit 0
  129. fi
  130.  
  131. if [ $rootFSPath = 0 ]
  132.     then
  133.         echo "No root filesystem set."
  134.         print_usage
  135.         exit 0
  136. fi
  137. # pre stop
  138.  
  139. # actual script start
  140. if [ $bridge = 1 ]
  141. then
  142. ## setup the network connection for QEMU (this will abort your other connections)
  143.     sudo sh ${scriptPath}/start_qemu_networking.sh tap0 `id -un`
  144. else
  145.     echo "0. Standard network interface will be used"
  146. fi
  147. ## Start QEMU
  148. echo "Starting QEMU"
  149. if [ $bridge = 1 ]
  150. then
  151. ### start QEMU with network bridge and tap support
  152.     sudo qemu-system-arm -M versatilepb -m 128M -kernel ${kernelPath} -hda ${rootFSPath} -append 'root=/dev/sda rw console=tty0 raid=noautodetect' -net nic -net tap,ifname=tap0,script=no $qemuOption
  153. else
  154. ### start QEMU with standard network
  155.     qemu-system-arm -M versatilepb -m 128M -kernel ${kernelPath} -hda ${rootFSPath} -append 'root=/dev/sda rw console=tty0 raid=noautodetect' $qemuOption
  156. fi
  157. echo "QEMU stopped with exit code $?"
  158.  
  159.  
  160. if [ $bridge = 1 ]
  161. then
  162. ## restart the original network connection and refresh the dhcp
  163.     sudo sh ${scriptPath}/stop_qemu_networking.sh tap0
  164. fi
  165. # actual script stop
Advertisement
Add Comment
Please, Sign In to add comment