grub-mkrescue.in revision 2
2N/A#! /bin/sh
2N/Aset -e
2N/A
2N/A# Make GRUB rescue image
2N/A# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A# GRUB is free software: you can redistribute it and/or modify
2N/A# it under the terms of the GNU General Public License as published by
2N/A# the Free Software Foundation, either version 3 of the License, or
2N/A# (at your option) any later version.
2N/A#
2N/A# GRUB is distributed in the hope that it will be useful,
2N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A# GNU General Public License for more details.
2N/A#
2N/A# You should have received a copy of the GNU General Public License
2N/A# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A
2N/A# Initialize some variables.
2N/Atransform="@program_transform_name@"
2N/A
2N/Aprefix=@prefix@
2N/Aexec_prefix=@exec_prefix@
2N/Abindir=@bindir@
2N/Alibdir=@libdir@
2N/APACKAGE_NAME=@PACKAGE_NAME@
2N/APACKAGE_TARNAME=@PACKAGE_TARNAME@
2N/APACKAGE_VERSION=@PACKAGE_VERSION@
2N/Apkglib_DATA="moddep.lst command.lst fs.lst partmap.lst parttool.lst handler.lst video.lst crypto.lst terminal.lst"
2N/A
2N/Aself=`basename $0`
2N/A
2N/Amultiboot_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-multiboot"
2N/Acoreboot_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-coreboot"
2N/Aqemu_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-qemu"
2N/Apc_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-pc"
2N/Aefi32_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi"
2N/Aefi64_dir="${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi"
2N/Arom_directory=
2N/Aoverride_dir=
2N/Agrub_mkimage="${bindir}/`echo grub-mkimage | sed ${transform}`"
2N/A
2N/Axorriso=xorriso
2N/A
2N/A# Usage: usage
2N/A# Print the usage.
2N/Ausage () {
2N/A cat <<EOF
2N/AUsage: $self [OPTION] SOURCE...
2N/AMake GRUB rescue image.
2N/A
2N/A -h, --help print this message and exit
2N/A -v, --version print the version information and exit
2N/A -o, --output=FILE save output in FILE [required]
2N/A --modules=MODULES pre-load specified modules MODULES
2N/A --rom-directory=DIR save rom images in DIR [optional]
2N/A --xorriso=FILE use FILE as xorriso [optional]
2N/A --grub-mkimage=FILE use FILE as grub-mkimage
2N/A
2N/A$self generates a bootable rescue image with specified source files, source
2N/Adirectories, or mkisofs options listed by: xorriso -as mkisofs -help
2N/A
2N/AOption -- switches to native xorriso command mode.
2N/A
2N/AReport bugs to <bug-grub@gnu.org>.
2N/AMail xorriso support requests to <bug-xorriso@gnu.org>.
2N/AEOF
2N/A}
2N/A
2N/Aargument () {
2N/A opt=$1
2N/A shift
2N/A
2N/A if test $# -eq 0; then
2N/A echo "$0: option requires an argument -- '$opt'" 1>&2
2N/A exit 1
2N/A fi
2N/A echo $1
2N/A}
2N/A
2N/A# Check the arguments.
2N/Awhile test $# -gt 0
2N/Ado
2N/A option=$1
2N/A shift
2N/A
2N/A case "$option" in
2N/A -h | --help)
2N/A usage
2N/A exit 0 ;;
2N/A -v | --version)
2N/A echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
2N/A exit 0 ;;
2N/A
2N/A --modules)
2N/A modules=`argument $option "$@"`; shift ;;
2N/A --modules=*)
2N/A modules=`echo "$option" | sed 's/--modules=//'` ;;
2N/A
2N/A -o | --output)
2N/A output_image=`argument $option "$@"`; shift ;;
2N/A --output=*)
2N/A output_image=`echo "$option" | sed 's/--output=//'` ;;
2N/A
2N/A --rom-directory)
2N/A rom_directory=`argument $option "$@"`; shift ;;
2N/A --rom-directory=*)
2N/A rom_directory=`echo "$option" | sed 's/--rom-directory=//'` ;;
2N/A
2N/A # Intentionally undocumented
2N/A --override-directory)
2N/A override_dir=`argument $option "$@"`
2N/A shift
2N/A PATH=${override_dir}:$PATH
2N/A export PATH
2N/A ;;
2N/A --override-directory=*)
2N/A override_dir=`echo "${option}/" | sed 's/--override-directory=//'`
2N/A PATH=${override_dir}:$PATH
2N/A export PATH
2N/A ;;
2N/A
2N/A --grub-mkimage)
2N/A grub_mkimage=`argument $option "$@"`; shift ;;
2N/A --grub-mkimage=*)
2N/A grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
2N/A
2N/A --xorriso)
2N/A xorriso=`argument $option "$@"`; shift ;;
2N/A --xorriso=*)
2N/A xorriso=`echo "${option}" | sed 's/--xorriso=//'` ;;
2N/A
2N/A *)
2N/A source="${source} ${option} $@"; break ;;
2N/A esac
2N/Adone
2N/A
2N/Aif [ "x${output_image}" = x ] ; then
2N/A echo "output file must be given" >&2
2N/A usage
2N/A exit 1
2N/Afi
2N/A
2N/Aset $grub_mkimage dummy
2N/Aif test -f "$1"; then
2N/A :
2N/Aelse
2N/A echo "$1: Not found." 1>&2
2N/A exit 1
2N/Afi
2N/A
2N/Aiso9660_dir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/Amkdir -p ${iso9660_dir}/boot/grub
2N/A
2N/Aprocess_input_dir ()
2N/A{
2N/A input_dir="$1"
2N/A platform="$2"
2N/A mkdir -p ${iso9660_dir}/boot/grub/${platform}
2N/A for file in "${input_dir}/"*.mod "${input_dir}/"efiemu32.o "${input_dir}/"efiemu64.o; do
2N/A if test -f "$file"; then
2N/A cp -f "$file" ${iso9660_dir}/boot/grub/${platform}/
2N/A fi
2N/A done
2N/A for file in ${pkglib_DATA}; do
2N/A if test -f "${input_dir}/${file}"; then
2N/A cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${platform}/
2N/A fi
2N/A done
2N/A
2N/A mkdir -p ${iso9660_dir}/boot/grub/locale
2N/A for file in ${input_dir}/po/*.mo; do
2N/A if test -f "$file"; then
2N/A cp -f "$file" ${iso9660_dir}/boot/grub/locale/
2N/A fi
2N/A done
2N/A}
2N/A
2N/Amake_image ()
2N/A{
2N/A source_directory="$1"
2N/A platform=$2
2N/A if ! test -e "${source_directory}"; then
2N/A return;
2N/A fi
2N/A
2N/A echo "Enabling $2 support ..."
2N/A
2N/A memdisk_img=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/A memdisk_dir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/A mkdir -p ${memdisk_dir}/boot/grub
2N/A
2N/A cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg
2N/Asearch --fs-uuid --set=root ${iso_uuid}
2N/Aset prefix=(\${root})/boot/grub/${platform}
2N/Asource \$prefix/grub.cfg
2N/AEOF
2N/A (for i in $(cat ${source_directory}/partmap.lst) ${modules} ; do
2N/A echo "insmod $i"
2N/A done ; \
2N/A echo "source /boot/grub/grub.cfg") \
2N/A > ${iso9660_dir}/boot/grub/${platform}/grub.cfg
2N/A
2N/A (cd "${memdisk_dir}"; tar -cf - boot) > "${memdisk_img}"
2N/A rm -rf ${memdisk_dir}
2N/A $grub_mkimage -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \
2N/A search iso9660 configfile normal memdisk tar $4
2N/A rm -rf ${memdisk_img}
2N/A}
2N/A
2N/Aif [ "${override_dir}" = "" ] ; then
2N/A if test -e "${multiboot_dir}" ; then
2N/A process_input_dir ${multiboot_dir} i386-multiboot
2N/A fi
2N/A if test -e "${coreboot_dir}" ; then
2N/A process_input_dir ${coreboot_dir} i386-coreboot
2N/A fi
2N/A if test -e "${qemu_dir}" ; then
2N/A process_input_dir ${qemu_dir} i386-qemu
2N/A fi
2N/A if test -e "${pc_dir}" ; then
2N/A process_input_dir ${pc_dir} i386-pc
2N/A fi
2N/A if test -e "${efi32_dir}" ; then
2N/A process_input_dir ${efi32_dir} i386-efi
2N/A fi
2N/A if test -e "${efi64_dir}" ; then
2N/A process_input_dir ${efi64_dir} x86_64-efi
2N/A fi
2N/Aelse
2N/A . "${override_dir}"/modinfo.sh
2N/A process_input_dir "${override_dir}" ${grub_modinfo_target_cpu}-${grub_modinfo_platform}
2N/A multiboot_dir=
2N/A pc_dir=
2N/A efi32_dir=
2N/A efi64_dir=
2N/A coreboot_dir=
2N/A qemu_dir=
2N/A case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
2N/A i386-multiboot) multiboot_dir=${override_dir} ;;
2N/A i386-coreboot) coreboot_dir=${override_dir} ;;
2N/A i386-qemu) qemu_dir=${override_dir} ;;
2N/A i386-pc) pc_dir=${override_dir} ;;
2N/A i386-efi) efi32_dir=${override_dir} ;;
2N/A x86_64-efi) efi64_dir=${override_dir} ;;
2N/A esac
2N/Afi
2N/A
2N/A# obtain date-based UUID
2N/Aiso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00)
2N/Agrub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)"
2N/A
2N/A# build BIOS core.img
2N/Aif test -e "${pc_dir}" ; then
2N/A echo "Enabling BIOS support ..."
2N/A core_img=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/A $grub_mkimage -O i386-pc -d ${pc_dir}/ -o ${core_img} --prefix=/boot/grub/i386-pc \
2N/A iso9660 biosdisk
2N/A cat ${pc_dir}/cdboot.img ${core_img} > ${iso9660_dir}/boot/grub/i386-pc/eltorito.img
2N/A
2N/A embed_img=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/A cat ${pc_dir}/boot.img ${core_img} > ${embed_img}
2N/A
2N/A rm -f ${core_img}
2N/A
2N/A (for i in $(cat ${pc_dir}/partmap.lst) ${modules} ; do
2N/A echo "insmod $i"
2N/A done ; \
2N/A echo "source /boot/grub/grub.cfg") \
2N/A > ${iso9660_dir}/boot/grub/i386-pc/grub.cfg
2N/A
2N/A grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \
2N/A --embedded-boot ${embed_img}"
2N/Afi
2N/A
2N/A# build multiboot core.img
2N/Amake_image "${multiboot_dir}" i386-multiboot "${iso9660_dir}/boot/multiboot.img" "pata ahci at_keyboard"
2N/A
2N/Aif test -e "${efi64_dir}" || test -e "${efi32_dir}"; then
2N/A efi_dir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
2N/A mkdir -p "${efi_dir}/efi/boot"
2N/A
2N/A # build bootx64.efi
2N/A make_image "${efi64_dir}" x86_64-efi "${efi_dir}"/efi/boot/bootx64.efi ""
2N/A # build bootia32.efi
2N/A make_image "${efi32_dir}" i386-efi "${efi_dir}"/efi/boot/bootia32.efi ""
2N/A if [ -e "${efi_dir}"/efi/boot/bootia32.efi ]; then
2N/A # For old macs. Suggested by Peter Jones.
2N/A cp "${efi_dir}"/efi/boot/bootia32.efi "${efi_dir}"/efi/boot/boot.efi
2N/A fi
2N/A
2N/A if $(which mformat > /dev/null) && $(which mcopy > /dev/null); then
2N/A mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img ::
2N/A mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/
2N/A elif test -x /usr/sbin/lofiadm -a -x /usr/sbin/mkfile; then
2N/A if [ ! $(whoami) = root ]; then
2N/A echo This command must be run as root on Solaris
2N/A exit 1
2N/A fi
2N/A mkfile 2880k "${iso9660_dir}"/efi.img
2N/A lofidev=$(lofiadm -a "${iso9660_dir}"/efi.img)
2N/A if test ! -z $lofidev; then
2N/A rlofidev=$(echo $lofidev | sed s.lofi.rlofi.)
2N/A if mkfs -F pcfs -o nofdisk,fat=16,size=5760 $rlofidev < /dev/null; then
2N/A mkdir /tmp/$$.mnt
2N/A if mount -F pcfs $lofidev /tmp/$$.mnt; then
2N/A cp -r ${efi_dir}/efi /tmp/$$.mnt
2N/A umount /tmp/$$.mnt
2N/A else
2N/A echo Error mounting $lofidev
2N/A exit 1
2N/A fi
2N/A rmdir /tmp/$$.mnt
2N/A else
2N/A echo Error creating pcfs filesystem on $rlofidev
2N/A exit 1
2N/A fi
2N/A else
2N/A echo Error running lofiadm for "${iso9660_dir}"/efi.img
2N/A exit 1
2N/A fi
2N/A fi
2N/A rm -rf ${efi_dir}
2N/A grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img"
2N/Afi
2N/A
2N/Amake_image "${qemu_dir}" i386-qemu "${iso9660_dir}/boot/qemu.img" "pata at_keyboard"
2N/Aif [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then
2N/A cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img"
2N/Afi
2N/Amake_image "${coreboot_dir}" i386-coreboot "${iso9660_dir}/boot/coreboot.elf" "pata ahci at_keyboard"
2N/Aif [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then
2N/A cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf"
2N/Afi
2N/A
2N/A# build iso image
2N/A"${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}
2N/A
2N/Arm -rf ${iso9660_dir}
2N/A
2N/Arm -f ${embed_img}
2N/A
2N/Aexit 0