grub-shell.in 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /bin/bash -e
  2. # Run GRUB script in a Qemu instance
  3. # Copyright (C) 2009,2010 Free Software Foundation, Inc.
  4. #
  5. # GRUB 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 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. # Initialize some variables.
  18. transform="@program_transform_name@"
  19. prefix=@prefix@
  20. exec_prefix=@exec_prefix@
  21. bindir=@bindir@
  22. libdir=@libdir@
  23. builddir=@builddir@
  24. PACKAGE_NAME=@PACKAGE_NAME@
  25. PACKAGE_TARNAME=@PACKAGE_TARNAME@
  26. PACKAGE_VERSION=@PACKAGE_VERSION@
  27. target_cpu=@target_cpu@
  28. # Force build directory components
  29. PATH=${builddir}:$PATH
  30. export PATH
  31. # Usage: usage
  32. # Print the usage.
  33. usage () {
  34. cat <<EOF
  35. Usage: $0 [OPTION] [SOURCE]
  36. Run GRUB script in a Qemu instance.
  37. -h, --help print this message and exit
  38. -v, --version print the version information and exit
  39. --boot=[fd|hd|cd] boot method for Qemu instance
  40. --modules=MODULES pre-load specified modules MODULES
  41. --qemu-opts=OPTIONS extra options to pass to Qemu instance
  42. $0 runs input GRUB script or SOURCE file in a Qemu instance and prints
  43. its output.
  44. Report bugs to <bug-grub@gnu.org>.
  45. EOF
  46. }
  47. # Check the arguments.
  48. for option in "$@"; do
  49. case "$option" in
  50. -h | --help)
  51. usage
  52. exit 0 ;;
  53. -v | --version)
  54. echo "$0 (${PACKAGE_NAME} ${PACKAGE_VERSION})"
  55. exit 0 ;;
  56. --modules=*)
  57. ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
  58. modules="$modules $ms" ;;
  59. --qemu-opts=*)
  60. qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
  61. qemuopts="$qemuopts $qs" ;;
  62. --boot=*)
  63. dev=`echo "$option" | sed -e 's/--boot=//'`
  64. if [ "$dev" = "fd" ] ; then bootdev=a;
  65. elif [ "$dev" = "hd" ] ; then bootdev=c;
  66. elif [ "$dev" = "cd" ] ; then bootdev=d;
  67. else
  68. echo "Unrecognized boot method \`$dev'" 1>&2
  69. usage
  70. exit 1
  71. fi ;;
  72. -*)
  73. echo "Unrecognized option \`$option'" 1>&2
  74. usage
  75. exit 1 ;;
  76. *)
  77. if [ "x${source}" != x ] ; then
  78. echo "too many parameters at the end" 1>&2
  79. usage
  80. exit 1
  81. fi
  82. source="${option}" ;;
  83. esac
  84. done
  85. if [ "x${source}" = x ] ; then
  86. tmpfile=`mktemp`
  87. while read; do
  88. echo $REPLY >> ${tmpfile}
  89. done
  90. source=${tmpfile}
  91. fi
  92. if [ "x${bootdev}" = x ] ; then
  93. bootdev=c # default is boot as disk image
  94. fi
  95. cfgfile=`mktemp`
  96. cat <<EOF >${cfgfile}
  97. grubshell=yes
  98. insmod serial
  99. serial
  100. terminal_input serial
  101. terminal_output serial
  102. EOF
  103. for mod in ${modules}
  104. do
  105. echo "insmod ${mod}" >> ${cfgfile}
  106. done
  107. cat <<EOF >>${cfgfile}
  108. source /boot/burg/testcase.cfg
  109. halt
  110. EOF
  111. isofile=`mktemp`
  112. burg-mkrescue --output=${isofile} --override-directory=${builddir} \
  113. /boot/burg/burg.cfg=${cfgfile} /boot/burg/testcase.cfg=${source} \
  114. >/dev/null 2>&1
  115. hdafile=`mktemp`
  116. cp ${isofile} ${hdafile}
  117. fdafile=`mktemp`
  118. cp ${isofile} ${fdafile}
  119. outfile=`mktemp`
  120. qemu-system-i386 ${qemuopts} -nographic -hda ${hdafile} -fda ${fdafile} -cdrom ${isofile} -boot ${bootdev} | tr -d "\r" >${outfile}
  121. cat $outfile
  122. rm -f ${tmpfile} ${outfile} ${cfgfile} ${isofile} ${hdafile} ${fdafile}
  123. exit 0