4 # Make GRUB rescue image
5 # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 # Initialize some variables.
21 transform
="@program_transform_name@"
24 exec_prefix
=@exec_prefix@
27 PACKAGE_NAME
=@PACKAGE_NAME@
28 PACKAGE_TARNAME
=@PACKAGE_TARNAME@
29 PACKAGE_VERSION
=@PACKAGE_VERSION@
30 target_cpu
=@target_cpu@
31 native_platform
=@platform@
32 pkglib_DATA
="moddep.lst command.lst fs.lst partmap.lst parttool.lst handler.lst video.lst crypto.lst terminal.lst"
36 multiboot_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-multiboot
37 coreboot_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-coreboot
38 qemu_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-qemu
39 pc_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-pc
40 efi32_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi
41 efi64_dir
=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi
44 grub_mkimage
=${bindir}/`echo grub-mkimage | sed ${transform}`
52 Usage: $self [OPTION] SOURCE...
53 Make GRUB rescue image.
55 -h, --help print this message and exit
56 -v, --version print the version information and exit
57 -o, --output=FILE save output in FILE [required]
58 --modules=MODULES pre-load specified modules MODULES
59 --rom-directory=DIR save rom images in DIR [optional]
60 --xorriso=FILE use FILE as xorriso [optional]
61 --grub-mkimage=FILE use FILE as grub-mkimage
63 $self generates a bootable rescue image with specified source files, source
64 directories, or mkisofs options listed by: xorriso -as mkisofs -help
65 Option -- switches to native xorriso command mode. or directories.
67 Report bugs to <bug-grub@gnu.org>.
68 Mail xorriso support requests to <bug-xorriso@gnu.org>.
76 if test $# -eq 0; then
77 echo "$0: option requires an argument -- '$opt'" 1>&2
83 # Check the arguments.
94 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
98 modules
=`argument $option "$@"`; shift ;;
100 modules
=`echo "$option" | sed 's/--modules=//'` ;;
103 output_image
=`argument $option "$@"`; shift ;;
105 output_image
=`echo "$option" | sed 's/--output=//'` ;;
108 rom_directory
=`argument $option "$@"`; shift ;;
110 rom_directory
=`echo "$option" | sed 's/--rom-directory=//'` ;;
112 # Intentionally undocumented
113 --override-directory)
114 override_dir
=`argument $option "$@"`
116 PATH
=${override_dir}:$PATH
119 --override-directory=*)
120 override_dir
=`echo "${option}/" | sed 's/--override-directory=//'`
121 PATH
=${override_dir}:$PATH
126 grub_mkimage
=`argument $option "$@"`; shift ;;
128 grub_mkimage
=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
131 xorriso
=`argument $option "$@"`; shift ;;
133 xorriso
=`echo "${option}/" | sed 's/--xorriso=//'` ;;
136 source="${source} ${option} $@"; break ;;
140 if [ "x${output_image}" = x
] ; then
141 echo "output file must be given" >&2
146 set $grub_mkimage dummy
147 if test -f "$1"; then
150 echo "$1: Not found." 1>&2
154 if test "x$TMP" != x
; then
155 MKTEMP_TEMPLATE
="$TMP/grub-mkrescue.XXXXXXXXXX"
156 elif test "x$TEMP" != x
; then
157 MKTEMP_TEMPLATE
="$TEMP/grub-mkrescue.XXXXXXXXXX"
159 MKTEMP_TEMPLATE
="/tmp/grub-mkrescue.XXXXXXXXXX"
162 iso9660_dir
=`mktemp -d "$MKTEMP_TEMPLATE"`
163 mkdir
-p ${iso9660_dir}/boot
/grub
169 mkdir
-p ${iso9660_dir}/boot
/grub
/${platform}
170 for file in ${input_dir}/*.mod
; do
171 if test -f "$file"; then
172 cp -f "$file" ${iso9660_dir}/boot
/grub
/${platform}/
175 for file in ${pkglib_DATA}; do
176 if test -f "${input_dir}/${file}"; then
177 cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${platform}/
181 mkdir -p ${iso9660_dir}/boot/grub/locale
182 for file in ${input_dir}/po/*.mo; do
183 if test -f "$file"; then
184 cp -f "$file" ${iso9660_dir}/boot/grub/locale/
191 source_directory="$1"
193 if ! test -e "${source_directory}"; then
197 echo "Enabling
$2 support ...
"
199 memdisk_img=`mktemp "$MKTEMP_TEMPLATE"`
200 memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"`
201 mkdir -p ${memdisk_dir}/boot/grub
203 cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg
204 search --fs-uuid --set ${iso_uuid}
205 set prefix=(\${root})/boot/grub/${platform}
206 source \$prefix/grub.cfg
208 (for i in $(cat ${source_directory}/partmap.lst) ${modules} ; do
211 echo "source /boot
/grub
/grub.cfg
") \
212 > ${iso9660_dir}/boot/grub/${platform}/grub.cfg
214 (cd "${memdisk_dir}"; tar -cf - boot) > "${memdisk_img}"
215 rm -rf ${memdisk_dir}
216 $grub_mkimage -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \
217 search iso9660 configfile normal memdisk
tar $4
218 rm -rf ${memdisk_img}
221 if [ "${override_dir}" = "" ] ; then
222 if test -e "${multiboot_dir}" ; then
223 process_input_dir
${multiboot_dir} i386-multiboot
225 if test -e "${coreboot_dir}" ; then
226 process_input_dir
${coreboot_dir} i386-coreboot
228 if test -e "${qemu_dir}" ; then
229 process_input_dir
${qemu_dir} i386-qemu
231 if test -e "${pc_dir}" ; then
232 process_input_dir
${pc_dir} i386-pc
234 if test -e "${efi32_dir}" ; then
235 process_input_dir
${efi32_dir} i386-efi
237 if test -e "${efi64_dir}" ; then
238 process_input_dir
${efi64_dir} x86_64-efi
241 process_input_dir
${override_dir} ${target_cpu}-${native_platform}
248 case "${target_cpu}-${native_platform}" in
249 i386-multiboot
) multiboot_dir
=${override_dir} ;;
250 i386-coreboot
) coreboot_dir
=${override_dir} ;;
251 i386-qemu
) qemu_dir
=${override_dir} ;;
252 i386-pc
) pc_dir
=${override_dir} ;;
253 i386-efi
) efi32_dir
=${override_dir} ;;
254 x86_64-efi
) efi64_dir
=${override_dir} ;;
258 # obtain date-based UUID
259 iso_uuid
=$
(date -u +%Y-
%m-
%d-
%H-
%M-
%S-00
)
260 grub_mkisofs_arguments
="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)"
262 # build BIOS core.img
263 if test -e "${pc_dir}" ; then
264 echo "Enabling BIOS support ..."
265 core_img
=`mktemp "$MKTEMP_TEMPLATE"`
266 $grub_mkimage -O i386-pc
-d ${pc_dir}/ -o ${core_img} --prefix=/boot
/grub
/i386-pc \
268 cat ${pc_dir}/cdboot.img ${core_img} > ${iso9660_dir}/boot
/grub
/i386-pc
/eltorito.img
270 embed_img
=`mktemp "$MKTEMP_TEMPLATE"`
271 cat ${pc_dir}/boot.img ${core_img} > ${embed_img}
275 (for i
in $
(cat ${pc_dir}/partmap.lst
) ${modules} ; do
278 echo "source /boot/grub/grub.cfg") \
279 > ${iso9660_dir}/boot
/grub
/i386-pc
/grub.cfg
281 grub_mkisofs_arguments
="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \
282 --embedded-boot ${embed_img}"
285 # build multiboot core.img
286 make_image
"${multiboot_dir}" i386-multiboot
"${iso9660_dir}/boot/multiboot.img" "ata at_keyboard"
288 if test -e "${efi64_dir}" ||
test -e "${efi32_dir}"; then
289 efi_dir
=`mktemp -d "$MKTEMP_TEMPLATE"`
290 mkdir
-p "${efi_dir}/efi/boot"
293 make_image
"${efi64_dir}" x86_64-efi
"${efi_dir}"/efi
/boot
/bootx64.efi
""
295 make_image
"${efi32_dir}" i386-efi
"${efi_dir}"/efi
/boot
/bootia32.efi
""
297 mformat
-C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img
::
298 mcopy
-s -i "${iso9660_dir}"/efi.img
${efi_dir}/efi
::/
300 grub_mkisofs_arguments
="${grub_mkisofs_arguments} --efi-boot efi.img"
303 make_image
"${qemu_dir}" i386-qemu
"${iso9660_dir}/boot/qemu.img" "ata at_keyboard"
304 if [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then
305 cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img"
307 make_image
"${coreboot_dir}" i386-coreboot
"${iso9660_dir}/boot/coreboot.elf" "ata at_keyboard"
308 if [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then
309 cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf"
313 "${xorriso}" -as mkisofs -graft-points ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} --sort-weight 0 / --sort-weight 1 /boot ${source}
314 rm -rf ${iso9660_dir}