2008-08-31 Colin D Bennett <colin@gibibit.com>
[grub2/phcoder.git] / util / i386 / pc / grub-mkrescue.in
blob3a74b06d90b2ea2e05ee36043214eecaee1cfedb
1 #! /bin/sh -e
3 # Make GRUB rescue image
4 # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
6 # GRUB is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # GRUB is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 # Initialize some variables.
20 transform="@program_transform_name@"
22 prefix=@prefix@
23 exec_prefix=@exec_prefix@
24 bindir=@bindir@
25 libdir=@libdir@
26 PACKAGE_NAME=@PACKAGE_NAME@
27 PACKAGE_TARNAME=@PACKAGE_TARNAME@
28 PACKAGE_VERSION=@PACKAGE_VERSION@
29 target_cpu=@target_cpu@
30 platform=@platform@
31 pkglibdir=${libdir}/`echo ${PACKAGE_TARNAME}/${target_cpu}-${platform} | sed ${transform}`
33 grub_mkimage=${bindir}/`echo grub-mkimage | sed ${transform}`
35 # Usage: usage
36 # Print the usage.
37 usage () {
38 cat <<EOF
39 Usage: grub-mkrescue [OPTION] output_image
40 Make GRUB rescue image.
42 -h, --help print this message and exit
43 -v, --version print the version information and exit
44 --modules=MODULES pre-load specified modules MODULES
45 --overlay=DIR overlay directory DIR in the memdisk image
46 (may be specified multiple times)
47 --pkglibdir=DIR use images from directory DIR instead of ${pkglibdir}
48 --grub-mkimage=FILE use FILE as grub-mkimage
49 --image-type=TYPE select floppy or cdrom (default)
50 --emulation=TYPE select El Torito boot emulation type floppy
51 or none (default) (cdrom only)
53 grub-mkimage generates a bootable rescue image of the specified type.
55 Report bugs to <bug-grub@gnu.org>.
56 EOF
59 image_type=cdrom
60 input_dir=${pkglibdir}
61 emulation=none
63 # Check the arguments.
64 for option in "$@"; do
65 case "$option" in
66 -h | --help)
67 usage
68 exit 0 ;;
69 -v | --version)
70 echo "grub-mkrescue (GNU GRUB ${PACKAGE_VERSION})"
71 exit 0 ;;
72 --modules=*)
73 modules=`echo "$option" | sed 's/--modules=//'` ;;
74 --overlay=*)
75 overlay=${overlay}${overlay:+ }`echo "$option" | sed 's/--overlay=//'` ;;
76 --pkglibdir=*)
77 input_dir=`echo "$option" | sed 's/--pkglibdir=//'` ;;
78 --grub-mkimage=*)
79 grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
80 --image-type=*)
81 image_type=`echo "$option" | sed 's/--image-type=//'`
82 case "$image_type" in
83 floppy|cdrom) ;;
85 echo "Unknown image type \`$image_type'" 1>&2
86 exit 1 ;;
87 esac ;;
88 --emulation=*)
89 emulation=`echo "$option" | sed 's/--emulation=//'`
90 case "$emulation" in
91 floppy|none) ;;
93 echo "Unknown emulation type \`$emulation'" 1>&2
94 exit 1 ;;
95 esac ;;
96 -*)
97 echo "Unrecognized option \`$option'" 1>&2
98 usage
99 exit 1
102 if test "x$output_image" != x; then
103 echo "Unrecognized option \`$option'" 1>&2
104 usage
105 exit 1
107 output_image="${option}" ;;
108 esac
109 done
111 if test "x$output_image" = x; then
112 usage
113 exit 1
116 aux_dir=`mktemp -d`
117 mkdir -p ${aux_dir}/boot/grub
119 cp ${input_dir}/*.mod \
120 ${input_dir}/command.lst ${input_dir}/moddep.lst ${input_dir}/fs.lst \
121 ${aux_dir}/boot/grub/
123 modules="biosdisk `cat ${input_dir}/partmap.lst` ${modules}"
124 for i in ${modules} ; do
125 echo "insmod $i"
126 done > ${aux_dir}/boot/grub/grub.cfg
128 for d in ${overlay}; do
129 echo "Overlaying $d"
130 cp -dpR "${d}"/* "${aux_dir}"/
131 done
133 if [ "x${image_type}" = xfloppy -o "x${emulation}" = xfloppy ] ; then
134 # build memdisk
135 memdisk_img=`mktemp`
136 tar -C ${aux_dir} -cf ${memdisk_img} boot
137 rm -rf ${aux_dir}
139 # build core.img
140 core_img=`mktemp`
141 ${grub_mkimage} -d ${input_dir}/ -m ${memdisk_img} -o ${core_img} memdisk cpio
142 rm -f ${memdisk_img}
144 # build floppy image
145 if [ "x${image_type}" = xcdrom ] ; then
146 floppy_dir=`mktemp -d`
147 floppy_img=${floppy_dir}/grub_floppy.img
148 else
149 floppy_img=${output_image}
151 cat ${input_dir}/boot.img ${core_img} /dev/zero | dd bs=1024 count=1440 > ${floppy_img}
152 rm -f ${core_img}
154 if [ "x${image_type}" = xcdrom ] ; then
155 # build iso image
156 genisoimage -b grub_floppy.img \
157 -o ${output_image} -r -J ${floppy_dir}
158 rm -rf ${floppy_dir}
160 else
161 # build core.img
162 core_img=`mktemp`
163 ${grub_mkimage} -d ${input_dir}/ -o ${core_img} biosdisk iso9660
165 # build grub_eltorito image
166 cat ${input_dir}/cdboot.img ${core_img} > ${aux_dir}/boot/grub/grub_eltorito
167 rm -f ${core_img}
169 # build iso image
170 genisoimage -b boot/grub/grub_eltorito \
171 -no-emul-boot -boot-load-size 4 -boot-info-table \
172 -o ${output_image} -r -J ${aux_dir}
173 rm -rf ${aux_dir}
176 exit 0