]>
Commit | Line | Data |
---|---|---|
5626056f CW |
1 | #! /bin/sh |
2 | set -e | |
6fc804ff BC |
3 | |
4 | # Run GRUB script in a Qemu instance | |
547e494f | 5 | # Copyright (C) 2009,2010 Free Software Foundation, Inc. |
6fc804ff BC |
6 | # |
7 | # GRUB is free software: you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
9 | # the Free Software Foundation, either version 3 of the License, or | |
10 | # (at your option) any later version. | |
11 | # | |
12 | # GRUB is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with GRUB. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
20 | # Initialize some variables. | |
21 | transform="@program_transform_name@" | |
22 | ||
48b391e9 VS |
23 | prefix="@prefix@" |
24 | exec_prefix="@exec_prefix@" | |
25 | datarootdir="@datarootdir@" | |
26 | builddir="@builddir@" | |
6fc804ff BC |
27 | PACKAGE_NAME=@PACKAGE_NAME@ |
28 | PACKAGE_TARNAME=@PACKAGE_TARNAME@ | |
29 | PACKAGE_VERSION=@PACKAGE_VERSION@ | |
6fc804ff BC |
30 | |
31 | # Force build directory components | |
48b391e9 | 32 | PATH="${builddir}:$PATH" |
6fc804ff BC |
33 | export PATH |
34 | ||
35 | # Usage: usage | |
36 | # Print the usage. | |
37 | usage () { | |
38 | cat <<EOF | |
39 | Usage: $0 [OPTION] [SOURCE] | |
40 | Run GRUB script in a Qemu instance. | |
41 | ||
42 | -h, --help print this message and exit | |
43 | -v, --version print the version information and exit | |
8395034b | 44 | --boot=[fd|hd|cd|net] boot method for Qemu instance |
6fc804ff | 45 | --modules=MODULES pre-load specified modules MODULES |
ad8e99ec | 46 | --qemu=FILE Name of qemu binary |
6fc804ff | 47 | --qemu-opts=OPTIONS extra options to pass to Qemu instance |
ad8e99ec | 48 | --files=FILES add files to the image |
6fc804ff BC |
49 | |
50 | $0 runs input GRUB script or SOURCE file in a Qemu instance and prints | |
51 | its output. | |
52 | ||
53 | Report bugs to <bug-grub@gnu.org>. | |
54 | EOF | |
55 | } | |
56 | ||
7fec686e | 57 | boot=hd |
ad8e99ec VS |
58 | qemu=qemu-system-i386 |
59 | ||
6fc804ff BC |
60 | # Check the arguments. |
61 | for option in "$@"; do | |
62 | case "$option" in | |
63 | -h | --help) | |
64 | usage | |
65 | exit 0 ;; | |
66 | -v | --version) | |
67 | echo "$0 (GNU GRUB ${PACKAGE_VERSION})" | |
68 | exit 0 ;; | |
69 | --modules=*) | |
70 | ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'` | |
71 | modules="$modules $ms" ;; | |
ad8e99ec VS |
72 | --files=*) |
73 | fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'` | |
74 | files="$files $fls" ;; | |
75 | --qemu=*) | |
76 | qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;; | |
6fc804ff BC |
77 | --qemu-opts=*) |
78 | qs=`echo "$option" | sed -e 's/--qemu-opts=//'` | |
79 | qemuopts="$qemuopts $qs" ;; | |
afafb37e BC |
80 | --boot=*) |
81 | dev=`echo "$option" | sed -e 's/--boot=//'` | |
7fec686e VS |
82 | if [ "$dev" = "fd" ] ; then boot=fd; |
83 | elif [ "$dev" = "hd" ] ; then boot=hd; | |
84 | elif [ "$dev" = "cd" ] ; then boot=cd; | |
8395034b | 85 | elif [ "$dev" = "net" ] ; then boot=net; |
aa8d7b26 VS |
86 | elif [ "$dev" = "qemu" ] ; then boot=qemu; |
87 | elif [ "$dev" = "coreboot" ] ; then boot=coreboot; | |
afafb37e BC |
88 | else |
89 | echo "Unrecognized boot method \`$dev'" 1>&2 | |
90 | usage | |
91 | exit 1 | |
92 | fi ;; | |
6fc804ff BC |
93 | -*) |
94 | echo "Unrecognized option \`$option'" 1>&2 | |
95 | usage | |
afafb37e | 96 | exit 1 ;; |
6fc804ff BC |
97 | *) |
98 | if [ "x${source}" != x ] ; then | |
99 | echo "too many parameters at the end" 1>&2 | |
100 | usage | |
101 | exit 1 | |
102 | fi | |
103 | source="${option}" ;; | |
104 | esac | |
105 | done | |
106 | ||
107 | if [ "x${source}" = x ] ; then | |
b65ea155 | 108 | tmpfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
becce1b1 | 109 | while read REPLY; do |
64550500 | 110 | echo "$REPLY" >> ${tmpfile} |
afafb37e BC |
111 | done |
112 | source=${tmpfile} | |
113 | fi | |
114 | ||
b65ea155 | 115 | cfgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
6fc804ff BC |
116 | cat <<EOF >${cfgfile} |
117 | grubshell=yes | |
118 | insmod serial | |
119 | serial | |
415502c2 | 120 | terminfo serial dumb |
6fc804ff BC |
121 | terminal_input serial |
122 | terminal_output serial | |
123 | EOF | |
124 | ||
b65ea155 | 125 | rom_directory=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
aa8d7b26 | 126 | |
6fc804ff BC |
127 | for mod in ${modules} |
128 | do | |
afafb37e | 129 | echo "insmod ${mod}" >> ${cfgfile} |
6fc804ff BC |
130 | done |
131 | ||
132 | cat <<EOF >>${cfgfile} | |
133 | source /boot/grub/testcase.cfg | |
ab66c69f JU |
134 | # Stop serial output to suppress "ACPI shutdown failed" error. |
135 | terminal_output console | |
6fc804ff BC |
136 | halt |
137 | EOF | |
138 | ||
b65ea155 | 139 | isofile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
8395034b | 140 | if [ x$boot != xnet ]; then |
16fc9293 | 141 | pkgdatadir="@builddir@" sh "@builddir@/grub-mkrescue" "--grub-mkimage=${builddir}/grub-mkimage" "--output=${isofile}" "--override-directory=${builddir}/grub-core" \ |
8395034b | 142 | --rom-directory="${rom_directory}" \ |
48b391e9 | 143 | "/boot/grub/grub.cfg=${cfgfile}" "/boot/grub/testcase.cfg=${source}" \ |
8395034b VS |
144 | ${files} >/dev/null 2>&1 |
145 | fi | |
7fec686e VS |
146 | if [ x$boot = xhd ]; then |
147 | device=hda | |
aa8d7b26 | 148 | bootdev="-boot c" |
ad8e99ec | 149 | fi |
7fec686e VS |
150 | if [ x$boot = xcd ]; then |
151 | device=cdrom | |
aa8d7b26 | 152 | bootdev="-boot d" |
7fec686e VS |
153 | fi |
154 | if [ x$boot = xfd ]; then | |
155 | device=fda | |
aa8d7b26 VS |
156 | bootdev="-boot a" |
157 | fi | |
afafb37e | 158 | |
aa8d7b26 VS |
159 | if [ x$boot = xqemu ]; then |
160 | bootdev="-bios ${rom_directory}/qemu.img" | |
161 | device=cdrom | |
7fec686e | 162 | fi |
afafb37e | 163 | |
aa8d7b26 | 164 | if [ x$boot = xcoreboot ]; then |
b65ea155 | 165 | imgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
aa8d7b26 VS |
166 | cp "${GRUB_COREBOOT_ROM}" "${imgfile}" |
167 | "${GRUB_CBFSTOOL}" "${imgfile}" add-payload "${rom_directory}/coreboot.elf" fallback/payload | |
168 | bootdev="-bios ${imgfile}" | |
169 | device=cdrom | |
170 | fi | |
6fc804ff | 171 | |
8395034b | 172 | if [ x$boot = xnet ]; then |
b65ea155 | 173 | netdir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1 |
16fc9293 | 174 | pkgdatadir="@builddir@" sh "@builddir@/grub-mknetdir" "--grub-mkimage=${builddir}/grub-mkimage" "--override-directory=${builddir}/grub-core" "--net-directory=$netdir" |
48b391e9 VS |
175 | cp "${cfgfile}" "$netdir/boot/grub/grub.cfg" |
176 | cp "${source}" "$netdir/boot/grub/testcase.cfg" | |
53f13848 VS |
177 | . "${builddir}/grub-core/modinfo.sh" |
178 | "${qemu}" ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -boot n -net "user,tftp=$netdir,bootfile=/boot/grub/${grub_modinfo_target_cpu}-${grub_modinfo_platform}/core.0" -net nic | cat | tr -d "\r" | |
8395034b | 179 | else |
48b391e9 | 180 | "${qemu}" ${qemuopts} -nographic -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} ${bootdev} | cat | tr -d "\r" |
8395034b | 181 | fi |
aa8d7b26 VS |
182 | rm -f "${isofile}" "${imgfile}" |
183 | rm -rf "${rom_directory}" | |
184 | if [ x$boot = xcoreboot ]; then | |
185 | rm -f "${imgfile}" | |
186 | fi | |
6fc804ff | 187 | |
aa8d7b26 | 188 | rm -f "${tmpfile}" "${cfgfile}" |
6fc804ff BC |
189 | exit 0 |
190 | ||
191 |