]> git.proxmox.com Git - grub2.git/blame - tests/util/grub-shell.in
grub-shell: support --files also for net boot
[grub2.git] / tests / util / grub-shell.in
CommitLineData
5626056f
CW
1#! /bin/sh
2set -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.
48b391e9
VS
21prefix="@prefix@"
22exec_prefix="@exec_prefix@"
23datarootdir="@datarootdir@"
24builddir="@builddir@"
c104b6f9 25srcdir="@srcdir@"
6fc804ff
BC
26PACKAGE_NAME=@PACKAGE_NAME@
27PACKAGE_TARNAME=@PACKAGE_TARNAME@
28PACKAGE_VERSION=@PACKAGE_VERSION@
6fc804ff
BC
29
30# Force build directory components
48b391e9 31PATH="${builddir}:$PATH"
6fc804ff
BC
32export PATH
33
08a14e0b
VS
34trim=0
35
6fc804ff
BC
36# Usage: usage
37# Print the usage.
38usage () {
39 cat <<EOF
40Usage: $0 [OPTION] [SOURCE]
41Run GRUB script in a Qemu instance.
42
43 -h, --help print this message and exit
44 -v, --version print the version information and exit
f93e89ef 45 --boot=[fd|hd|cd|net] boot method for Qemu instance
6fc804ff 46 --modules=MODULES pre-load specified modules MODULES
ad8e99ec 47 --qemu=FILE Name of qemu binary
a6393224 48 --disk=FILE Attach FILE as a disk
6fc804ff 49 --qemu-opts=OPTIONS extra options to pass to Qemu instance
ad8e99ec 50 --files=FILES add files to the image
f93e89ef
VS
51 --mkrescue-arg=ARGS additional arguments to grub-mkrescue
52 --timeout=SECONDS set timeout
08a14e0b 53 --trim trim firmware output
6fc804ff
BC
54
55$0 runs input GRUB script or SOURCE file in a Qemu instance and prints
56its output.
57
58Report bugs to <bug-grub@gnu.org>.
59EOF
60}
61
37ba07eb 62. "${builddir}/grub-core/modinfo.sh"
60fde14d 63qemuopts="${GRUB_QEMU_OPTS}"
ecfc6207
VS
64serial_port=com0
65serial_null=
a3706162 66halt_cmd=halt
f93e89ef 67pseries=n
a6393224 68disk=hda
37ba07eb 69case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
a6393224
VS
70 *-emu)
71 device_map=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
72 boot=emu
73 console=console
74 disk=0
75 # To skip "Welcome to GRUB" and color setttings
76 trim=1
77 ;;
ecfc6207 78 powerpc-ieee1275)
665c575b 79 boot=hd
ecfc6207
VS
80 qemu=qemu-system-ppc
81 console=console
889ebe92 82 serial_port=escc-ch-b
ecfc6207 83 serial_null="-serial null"
f93e89ef 84 netbootext=elf
ecfc6207
VS
85 ;;
86
3f729741
VS
87 sparc64-ieee1275)
88 boot=cd
89 qemu=qemu-system-sparc64
90 console=
91 serial_port=ieee1275/ttya
92 trim=1
93 qemuopts="$qemuopts -no-reboot"
94 halt_cmd=reboot
95 ;;
96
37ba07eb
VS
97 mips-qemu_mips)
98 boot=mips_qemu
99 qemu=qemu-system-mips
60fde14d 100 qemuopts="$qemuopts -M mips"
37ba07eb
VS
101 console=vga_text
102 ;;
6aff0107
VS
103 mips-arc)
104 boot=cd
105 qemu=qemu-system-mips64
106 qemuopts="$qemuopts -M indy"
3f9634e5
VS
107 serial_port=arc/serial0/line0
108 console=
6aff0107
VS
109 trim=1
110 ;;
7f63a64f
VS
111 mipsel-arc)
112 boot=cd
113 qemu=qemu-system-mips64el
114 qemuopts="$qemuopts -M magnum -no-reboot"
115 serial_port=arc/multi0/serial0
116 console=console
117 halt_cmd=reboot
118 trim=1
119 ;;
37ba07eb
VS
120 mipsel-qemu_mips)
121 boot=mipsel_qemu
122 qemu=qemu-system-mipsel
60fde14d 123 qemuopts="$qemuopts -M mips"
37ba07eb
VS
124 console=vga_text
125 ;;
eac49cff
VS
126 mipsel-loongson)
127 boot=mipsel_fulong2e
128 qemu=qemu-system-mips64el
3f9634e5 129 qemuopts="$qemuopts -M fulong2e"
eac49cff 130 console=
eac49cff
VS
131 trim=1
132 ;;
37ba07eb
VS
133 i386-coreboot)
134 boot=coreboot
135 qemu=qemu-system-i386
136 console=vga_text
137 ;;
138 i386-multiboot)
139 boot=cd
140 qemu=qemu-system-i386
141 console=vga_text;;
142
143 i386-ieee1275)
dbc56d8f 144 boot=hd
37ba07eb 145 qemu=qemu-system-i386
dbc56d8f
VS
146 console=console
147 trim=1
a6393224 148 disk=hdb
dbc56d8f 149 ;;
37ba07eb
VS
150 i386-qemu)
151 boot=qemu
152 qemu=qemu-system-i386
153 console=vga_text;;
154
155 i386-pc)
156 boot=cd
157 qemu=qemu-system-i386
f93e89ef
VS
158 console=console
159 netbootext=0
160 ;;
37ba07eb
VS
161
162 i386-efi)
163 qemu=qemu-system-i386
164 boot=cd
08a14e0b
VS
165 console=console
166 trim=1
167 ;;
37ba07eb
VS
168 x86_64-efi)
169 qemu=qemu-system-x86_64
170 boot=cd
08a14e0b
VS
171 console=console
172 trim=1
173 ;;
37ba07eb
VS
174 *)
175 boot=hd
176 qemu=qemu-system-i386
177 console=console;;
178esac
ad8e99ec 179
f93e89ef 180timeout=60
e7b8fd08 181mkimage_extra_arg=
f93e89ef 182
6fc804ff
BC
183# Check the arguments.
184for option in "$@"; do
185 case "$option" in
186 -h | --help)
187 usage
188 exit 0 ;;
189 -v | --version)
190 echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
191 exit 0 ;;
08a14e0b
VS
192 --trim)
193 trim=1
194 ;;
1e6ad6a6
BC
195 --debug)
196 debug=1 ;;
6fc804ff
BC
197 --modules=*)
198 ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
199 modules="$modules $ms" ;;
ad8e99ec
VS
200 --files=*)
201 fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'`
202 files="$files $fls" ;;
f93e89ef
VS
203 --mkrescue-arg=*)
204 mkr=`echo "$option" | sed -e 's/--mkrescue-arg=//' -e 's/,/ /g'`
205 mkrescue_args="$mkrescue_args $mkr" ;;
ad8e99ec
VS
206 --qemu=*)
207 qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;;
f93e89ef
VS
208 --pseries)
209 qemu=qemu-system-ppc64
210 serial_port=ieee1275/hvterm
211 serial_null=
f25a2ba6 212 qemuopts="$qemuopts -M pseries -no-reboot"
f93e89ef 213 trim=1
35c2851c 214 pseries=y
f93e89ef 215 ;;
6fc804ff
BC
216 --qemu-opts=*)
217 qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
218 qemuopts="$qemuopts $qs" ;;
a6393224
VS
219 --disk=*)
220 dsk=`echo "$option" | sed -e 's/--disk=//'`
221 if [ ${grub_modinfo_platform} = emu ]; then
222 echo "(hd$disk) $dsk" >> "$device_map"
223 disk="$((disk+1))"
224 else
225 if [ $disk = error ]; then
226 echo "Too many disks" 1>&2
227 exit 1;
228 fi
229 qemuopts="$qemuopts -$disk $dsk"
230 if [ "$disk" = hda ]; then
231 disk=hdb;
232 elif [ "$disk" = hdb ]; then
233 # CDROM is hdc
234 disk=hdd
235 elif [ "$disk" = hdd ]; then
236 # CDROM is hdc
237 disk=error
238 fi
239 fi
240 ;;
f93e89ef
VS
241 --timeout=*)
242 timeout=`echo "$option" | sed -e 's/--timeout=//'`
243 ;;
e7b8fd08
VS
244
245 # Intentionally undocumented
246 --grub-mkimage-extra)
247 mkimage_extra_arg="$mkimage_extra_arg `argument $option "$@"`"; shift ;;
248 --grub-mkimage-extra=*)
249 mkimage_extra_arg="$mkimage_extra_arg `echo "$option" | sed 's/--grub-mkimage-extra=//'`" ;;
250
afafb37e
BC
251 --boot=*)
252 dev=`echo "$option" | sed -e 's/--boot=//'`
7fec686e
VS
253 if [ "$dev" = "fd" ] ; then boot=fd;
254 elif [ "$dev" = "hd" ] ; then boot=hd;
255 elif [ "$dev" = "cd" ] ; then boot=cd;
8395034b 256 elif [ "$dev" = "net" ] ; then boot=net;
aa8d7b26
VS
257 elif [ "$dev" = "qemu" ] ; then boot=qemu;
258 elif [ "$dev" = "coreboot" ] ; then boot=coreboot;
37ba07eb 259 elif [ "$dev" = "mips_qemu" ] ; then boot=mips_qemu;
eac49cff
VS
260 elif [ "$dev" = "mipsel_qemu" ] ; then boot=mipsel_qemu;
261 elif [ "$dev" = "mipsel_fulong2e" ] ; then boot=mipsel_fulong2e;
afafb37e
BC
262 else
263 echo "Unrecognized boot method \`$dev'" 1>&2
264 usage
265 exit 1
266 fi ;;
6fc804ff
BC
267 -*)
268 echo "Unrecognized option \`$option'" 1>&2
269 usage
afafb37e 270 exit 1 ;;
6fc804ff
BC
271 *)
272 if [ "x${source}" != x ] ; then
273 echo "too many parameters at the end" 1>&2
274 usage
275 exit 1
276 fi
277 source="${option}" ;;
278 esac
279done
280
281if [ "x${source}" = x ] ; then
b65ea155 282 tmpfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
becce1b1 283 while read REPLY; do
64550500 284 echo "$REPLY" >> ${tmpfile}
afafb37e
BC
285 done
286 source=${tmpfile}
287fi
288
b65ea155 289cfgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
6fc804ff
BC
290cat <<EOF >${cfgfile}
291grubshell=yes
a936af56 292EOF
a6393224
VS
293
294
295if [ "${grub_modinfo_platform}" != emu ]; then
296 echo insmod serial >>${cfgfile}
297fi
298
a936af56
VS
299if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ]; then
300 echo insmod escc >>${cfgfile}
301fi
a6393224
VS
302if [ "${grub_modinfo_platform}" != emu ]; then
303 echo "serial ${serial_port}" >>${cfgfile}
304 term="serial_${serial_port}"
305else
306 term=console
307fi
308
a936af56 309cat <<EOF >>${cfgfile}
efb8de49 310terminfo -g 1024x1024 ${term} dumb
a6393224
VS
311terminal_input ${term}
312terminal_output ${term}
6fc804ff
BC
313EOF
314
08a14e0b
VS
315trim_head=664cbea8-132f-4770-8aa4-1696d59ac35c
316
317if [ $trim = 1 ]; then
318 echo "echo $trim_head" >>${cfgfile}
319fi
320
b65ea155 321rom_directory=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
aa8d7b26 322
6fc804ff
BC
323for mod in ${modules}
324do
afafb37e 325 echo "insmod ${mod}" >> ${cfgfile}
6fc804ff
BC
326done
327
328cat <<EOF >>${cfgfile}
f7457fb2 329source "\$prefix/testcase.cfg"
ab66c69f 330# Stop serial output to suppress "ACPI shutdown failed" error.
6fc804ff 331EOF
eac49cff
VS
332if [ x$console != x ]; then
333 echo "terminal_output $console" >>${cfgfile}
334fi
a3706162 335echo "${halt_cmd}" >>${cfgfile}
6fc804ff 336
1e6ad6a6
BC
337test -z "$debug" || echo "GRUB script: ${cfgfile}" >&2
338test -z "$debug" || echo "GRUB testcase script: ${tmpfile}" >&2
339test -z "$debug" || echo "Boot device: ${boot}" >&2
340
b65ea155 341isofile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
1e6ad6a6
BC
342test -z "$debug" || echo "GRUB ISO file: ${isofile}" >&2
343test -z "$debug" || echo "GRUB ROM directory: ${rom_directory}" >&2
344
345if test -z "$debug"; then
346 qemuopts="${qemuopts} -nographic -monitor file:/dev/null"
347fi
348
a6393224 349if [ x$boot != xnet ] && [ x$boot != xemu ]; then
cd46aa6c 350 pkgdatadir="@builddir@" "@builddir@/grub-mkrescue" "--output=${isofile}" "--override-directory=${builddir}/grub-core" \
3a82f8bb
CW
351 --rom-directory="${rom_directory}" \
352 --themes-directory="@srcdir@/themes" \
353 $mkimage_extra_arg ${mkrescue_args} \
48b391e9 354 "/boot/grub/grub.cfg=${cfgfile}" "/boot/grub/testcase.cfg=${source}" \
8395034b
VS
355 ${files} >/dev/null 2>&1
356fi
7fec686e 357if [ x$boot = xhd ]; then
f93e89ef 358 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; then
256ee7ac 359 device="hdb "
f93e89ef 360 else
256ee7ac 361 device="hda "
f93e89ef 362 fi
aa8d7b26 363 bootdev="-boot c"
ad8e99ec 364fi
7fec686e 365if [ x$boot = xcd ]; then
35c2851c 366 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ] && [ x$pseries != xy ] ; then
256ee7ac
VS
367 device="-drive if=ide,media=cdrom,file="
368 else
369 device="cdrom "
370 fi
aa8d7b26 371 bootdev="-boot d"
7fec686e
VS
372fi
373if [ x$boot = xfd ]; then
256ee7ac 374 device="fda "
aa8d7b26
VS
375 bootdev="-boot a"
376fi
afafb37e 377
aa8d7b26
VS
378if [ x$boot = xqemu ]; then
379 bootdev="-bios ${rom_directory}/qemu.img"
256ee7ac 380 device="cdrom "
7fec686e 381fi
afafb37e 382
37ba07eb
VS
383if [ x$boot = xmipsel_qemu ]; then
384 bootdev="-kernel ${rom_directory}/mipsel-qemu_mips.elf"
256ee7ac 385 device="cdrom "
37ba07eb
VS
386fi
387
eac49cff
VS
388if [ x$boot = xmipsel_fulong2e ]; then
389 bootdev="-kernel ${rom_directory}/mipsel-loongson.elf -append machtype=lemote-fuloong-2e"
256ee7ac 390 device="cdrom "
eac49cff
VS
391fi
392
37ba07eb
VS
393if [ x$boot = xmips_qemu ]; then
394 bootdev="-kernel ${rom_directory}/mips-qemu_mips.elf"
256ee7ac 395 device="cdrom "
37ba07eb
VS
396fi
397
aa8d7b26 398if [ x$boot = xcoreboot ]; then
b65ea155 399 imgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
aa8d7b26
VS
400 cp "${GRUB_COREBOOT_ROM}" "${imgfile}"
401 "${GRUB_CBFSTOOL}" "${imgfile}" add-payload "${rom_directory}/coreboot.elf" fallback/payload
402 bootdev="-bios ${imgfile}"
256ee7ac 403 device="cdrom "
1e6ad6a6 404 test -z "$debug" || echo "Coreboot image: ${imgfile}" >&2
aa8d7b26 405fi
6fc804ff 406
f93e89ef
VS
407if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mipsel-arc ]; then
408 case "$boot" in
409 hd)
410 bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/disk" ;;
411 *)
412 bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/cdrom";;
413 esac
414fi
415
08a14e0b
VS
416do_trim ()
417{
418 if [ $trim = 1 ]; then
419 awk '{ if (have_head == 1) print $0; } /664cbea8-132f-4770-8aa4-1696d59ac35c/ { have_head=1; }'
420 else
421 cat
422 fi
423}
424
4b94e322
AB
425copy_extra_files() {
426 _destdir="$1"
427 shift
428
429 # FIXME support '=' in file names
430 for _file in "$@"; do
431 _target="${_file%=*}"
432 _source="${_file#*=}"
433 [ -n "$_source" ] || _source="$_target"
434 _target="$_destdir/$_target"
435 _targetdir="$(dirname "$_target")"
436 [ -d "$_targetdir" ] || mkdir -p "$_targetdir"
437 cp "$_source" "$_target"
438 done
439}
440
8395034b 441if [ x$boot = xnet ]; then
b65ea155 442 netdir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
cd46aa6c 443 pkgdatadir="@builddir@" "@builddir@/grub-mknetdir" "--grub-mkimage=${builddir}/grub-mkimage" "--directory=${builddir}/grub-core" "--net-directory=$netdir" ${mkrescue_args} > /dev/null
48b391e9
VS
444 cp "${cfgfile}" "$netdir/boot/grub/grub.cfg"
445 cp "${source}" "$netdir/boot/grub/testcase.cfg"
4b94e322 446 [ -z "$files" ] || copy_extra_files "$netdir" $files
1e6ad6a6 447 timeout -s KILL $timeout "${qemu}" ${qemuopts} ${serial_null} -serial file:/dev/stdout -boot n -net "user,tftp=$netdir,bootfile=/boot/grub/${grub_modinfo_target_cpu}-${grub_modinfo_platform}/core.$netbootext" -net nic | cat | tr -d "\r" | do_trim
a6393224
VS
448elif [ x$boot = xemu ]; then
449 grubdir="$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")"
50361660 450 mkdir -p "$grubdir/fonts"
c104b6f9 451 mkdir -p "$grubdir/themes"
b7c036bc 452 mkdir -p "$grubdir/locale"
50361660 453 cp "@builddir@/"unicode.pf2 "$grubdir/fonts/unicode.pf2"
c104b6f9 454 cp -R "@srcdir@/themes/starfield" "$grubdir/themes/starfield"
1e8e2e78
CW
455 for file in "@srcdir@/po/"*.gmo; do
456 if [ -f "$file" ]; then
457 cp "$file" "$grubdir/locale/"
458 fi
459 done
a6393224
VS
460 cp "${cfgfile}" "$grubdir/grub.cfg"
461 cp "${source}" "$grubdir/testcase.cfg"
462 @builddir@/grub-core/grub-emu -m "$device_map" -d "$grubdir" | tr -d "\r" | do_trim
1202c764 463 rm -rf "$grubdir"
8395034b 464else
256ee7ac 465 timeout -s KILL $timeout "${qemu}" ${qemuopts} ${serial_null} -serial file:/dev/stdout -${device}"${isofile}" ${bootdev} | cat | tr -d "\r" | do_trim
8395034b 466fi
aa8d7b26 467if [ x$boot = xcoreboot ]; then
1e6ad6a6 468 test -n "$debug" || rm -f "${imgfile}"
aa8d7b26 469fi
1e6ad6a6
BC
470test -n "$debug" || rm -f "${isofile}"
471test -n "$debug" || rm -rf "${rom_directory}"
472test -n "$debug" || rm -f "${tmpfile}" "${cfgfile}"
6fc804ff
BC
473exit 0
474
475