]> git.proxmox.com Git - grub2.git/blame - util/grub-mkrescue.in
merge mainline into rescue-efi
[grub2.git] / util / grub-mkrescue.in
CommitLineData
fc2208b0
RM
1#! /bin/sh -e
2
3# Make GRUB rescue image
4# Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
5#
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.
10#
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.
15#
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/>.
18
19# Initialize some variables.
20transform="@program_transform_name@"
21
22prefix=@prefix@
23exec_prefix=@exec_prefix@
24bindir=@bindir@
25libdir=@libdir@
26PACKAGE_NAME=@PACKAGE_NAME@
27PACKAGE_TARNAME=@PACKAGE_TARNAME@
28PACKAGE_VERSION=@PACKAGE_VERSION@
29target_cpu=@target_cpu@
efda854e 30native_platform=@platform@
990f3548 31pkglib_DATA="@pkglib_DATA@"
fc2208b0
RM
32
33coreboot_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-coreboot
34pc_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/${target_cpu}-pc
ac0289ba
VS
35efi32_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/i386-efi
36efi64_dir=${libdir}/$(echo ${PACKAGE_TARNAME} | sed ${transform})/x86_64-efi
fc2208b0
RM
37
38# Usage: usage
39# Print the usage.
40usage () {
41 cat <<EOF
42Usage: $0 [OPTION] SOURCE...
43Make GRUB rescue image.
44
45 -h, --help print this message and exit
46 -v, --version print the version information and exit
47 --modules=MODULES pre-load specified modules MODULES
74ff1dd5 48 --output=FILE save output in FILE [required]
fc2208b0
RM
49
50$0 generates a bootable rescue image with specified source files or directories.
51
52Report bugs to <bug-grub@gnu.org>.
53EOF
54}
55
56# Check the arguments.
57for option in "$@"; do
58 case "$option" in
59 -h | --help)
60 usage
61 exit 0 ;;
62 -v | --version)
63 echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
64 exit 0 ;;
65 --modules=*)
66 modules=`echo "$option" | sed 's/--modules=//'` ;;
67 --output=*)
68 output_image=`echo "$option" | sed 's/--output=//'` ;;
efda854e
RM
69 # Intentionally undocumented
70 --override-directory=*)
71 override_dir=`echo "${option}/" | sed 's/--override-directory=//'`
72 PATH=${override_dir}:$PATH
73 export PATH
74 ;;
fc2208b0
RM
75 -*)
76 echo "Unrecognized option \`$option'" 1>&2
77 usage
78 exit 1
79 ;;
80 *)
81 source="${source} ${option}" ;;
82 esac
83done
84
74ff1dd5
FZ
85if [ "x${output_image}" = x ] ; then
86 echo "output file must be given" >&2
87 usage
88 exit 1
89fi
90
33e2e6f3
VS
91if test "x$TMP" != x; then
92 MKTEMP_TEMPLATE="$TMP/grub-mkrescue.XXXXXXXXXX"
93elif test "x$TEMP" != x; then
94 MKTEMP_TEMPLATE="$TEMP/grub-mkrescue.XXXXXXXXXX"
95else
96 MKTEMP_TEMPLATE="/tmp/grub-mkrescue.XXXXXXXXXX"
97fi
98
99iso9660_dir=`mktemp -d "$MKTEMP_TEMPLATE"`
fc2208b0
RM
100mkdir -p ${iso9660_dir}/boot/grub
101
efda854e
RM
102process_input_dir ()
103{
104 input_dir="$1"
105 platform="$2"
106 mkdir -p ${iso9660_dir}/boot/grub/${target_cpu}-${platform}
10a88797 107 for file in ${input_dir}/*.mod; do
efda854e
RM
108 if test -f "$file"; then
109 cp -f "$file" ${iso9660_dir}/boot/grub/${target_cpu}-${platform}/
110 fi
111 done
10a88797
FZ
112 for file in ${pkglib_DATA}; do
113 if test -f "${input_dir}/${file}"; then
114 cp -f "${input_dir}/${file}" ${iso9660_dir}/boot/grub/${target_cpu}-${platform}/
115 fi
116 done
57bbe3be
CPE
117
118 mkdir -p ${iso9660_dir}/boot/grub/locale
119 for file in ${input_dir}/po/*.mo; do
120 if test -f "$file"; then
121 cp -f "$file" ${iso9660_dir}/boot/grub/locale/
122 fi
123 done
efda854e
RM
124}
125
126if [ "${override_dir}" = "" ] ; then
127 if test -e "${coreboot_dir}" ; then
128 process_input_dir ${coreboot_dir} coreboot
fc2208b0 129 fi
efda854e
RM
130 if test -e "${pc_dir}" ; then
131 process_input_dir ${pc_dir} pc
132 fi
ac0289ba
VS
133 if test -e "${efi32_dir}" ; then
134 process_input_dir ${efi32_dir} efi32
135 fi
136 if test -e "${efi64_dir}" ; then
137 process_input_dir ${efi64_dir} efi64
138 fi
efda854e
RM
139else
140 process_input_dir ${override_dir} ${native_platform}
141 coreboot_dir=
142 pc_dir=
ac0289ba
VS
143 efi32_dir=
144 efi64_dir=
145 case "${target_cpu}-${native_platform}" in
146 i386-coreboot) coreboot_dir=${override_dir} ;;
147 i386-pc) pc_dir=${override_dir} ;;
148 i386-efi) efi32_dir=${override_dir} ;;
149 x86_64-efi) efi64_dir=${override_dir} ;;
efda854e
RM
150 esac
151fi
fc2208b0
RM
152
153# build coreboot core.img
efda854e 154if test -e "${coreboot_dir}" ; then
52cc3ce0 155 echo "Enabling coreboot support ..."
33e2e6f3
VS
156 memdisk_img=`mktemp "$MKTEMP_TEMPLATE"`
157 memdisk_dir=`mktemp -d "$MKTEMP_TEMPLATE"`
fc2208b0
RM
158 mkdir -p ${memdisk_dir}/boot/grub
159 # obtain date-based UUID
d68b491e 160 iso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00)
fc2208b0
RM
161
162 modules="$(cat ${coreboot_dir}/partmap.lst) ${modules}"
163 cat << EOF > ${memdisk_dir}/boot/grub/grub.cfg
164search --fs-uuid --set ${iso_uuid}
165set prefix=(\${root})/boot/grub/${target_cpu}-coreboot
166EOF
167 (for i in ${modules} ; do
168 echo "insmod $i"
169 done ; \
170 echo "source /boot/grub/grub.cfg") \
47779711 171 > ${iso9660_dir}/boot/grub/i386-coreboot/grub.cfg
fc2208b0
RM
172
173 tar -C ${memdisk_dir} -cf ${memdisk_img} boot
174 rm -rf ${memdisk_dir}
175 grub-mkelfimage -d ${coreboot_dir}/ -m ${memdisk_img} -o ${iso9660_dir}/boot/multiboot.img \
176 memdisk tar search iso9660 configfile sh \
177 ata at_keyboard
178 rm -f ${memdisk_img}
efda854e 179 grub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)"
fc2208b0
RM
180fi
181
52cc3ce0 182# build BIOS core.img
efda854e 183if test -e "${pc_dir}" ; then
52cc3ce0 184 echo "Enabling BIOS support ..."
33e2e6f3 185 core_img=`mktemp "$MKTEMP_TEMPLATE"`
fc2208b0 186 grub-mkimage -d ${pc_dir}/ -o ${core_img} --prefix=/boot/grub/i386-pc \
b15937b1 187 iso9660 biosdisk
fc2208b0 188 cat ${pc_dir}/cdboot.img ${core_img} > ${iso9660_dir}/boot/grub/i386-pc/eltorito.img
0ae56929 189
33e2e6f3 190 embed_img=`mktemp "$MKTEMP_TEMPLATE"`
e9309813 191 cat ${pc_dir}/boot.img ${core_img} > ${embed_img}
0ae56929 192
fc2208b0
RM
193 rm -f ${core_img}
194
195 modules="$(cat ${pc_dir}/partmap.lst) ${modules}"
196 (for i in ${modules} ; do
197 echo "insmod $i"
198 done ; \
199 echo "source /boot/grub/grub.cfg") \
200 > ${iso9660_dir}/boot/grub/i386-pc/grub.cfg
201
47779711 202 grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \
0ae56929 203 --embedded-boot ${embed_img}"
fc2208b0
RM
204fi
205
ac0289ba
VS
206# build bootx64.efi
207if test -e "${efi64_dir}" ; then
208 echo "Generates bootx64.efi"
209 mkdir -p ${iso9660_dir}/efi/boot
210 grub-mkimage -d ${efi64_dir}/ -o ${iso9660_dir}/efi/boot/bootx64.efi --prefix=/boot/grub/x86_64-efi \
211 search iso9660 configfile sh
212
213 modules="$(cat ${efi64_dir}/partmap.lst) ${modules}"
214 (for i in ${modules} ; do
215 if [ "x$i" != xkernel.mod ]; then
216 echo "insmod $i"
217 fi
218 done ; \
219 echo "source /boot/grub/grub.cfg") \
220 > ${iso9660_dir}/boot/grub/x86_64-efi/grub.cfg
221fi
222
fc2208b0 223# build iso image
9b214e3a 224grub-mkisofs ${grub_mkisofs_arguments} --protective-msdos-label -o ${output_image} -r ${iso9660_dir} ${source}
fc2208b0
RM
225rm -rf ${iso9660_dir}
226
0ae56929
RM
227rm -f ${embed_img}
228
fc2208b0 229exit 0