]> git.proxmox.com Git - grub2.git/blob - util/grub-mkrescue.in
* grub-core/loader/i386/bsdXX.c (grub_openbsd_find_ramdisk): Use
[grub2.git] / util / grub-mkrescue.in
1 #! /bin/sh
2 set -e
3
4 # Make GRUB rescue image
5 # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
21
22 prefix="@prefix@"
23 exec_prefix="@exec_prefix@"
24 datarootdir="@datarootdir@"
25 bindir="@bindir@"
26 libdir="@libdir@"
27 PACKAGE_NAME=@PACKAGE_NAME@
28 PACKAGE_TARNAME=@PACKAGE_TARNAME@
29 PACKAGE_VERSION=@PACKAGE_VERSION@
30 datadir="@datadir@"
31 if [ "x$pkgdatadir" = x ]; then
32 pkgdatadir="${datadir}/@PACKAGE@"
33 fi
34 pkglib_DATA="moddep.lst command.lst fs.lst partmap.lst parttool.lst handler.lst video.lst crypto.lst terminal.lst"
35
36 self=`basename $0`
37
38 multiboot_dir="${libdir}/@PACKAGE@/i386-multiboot"
39 coreboot_dir="${libdir}/@PACKAGE@/i386-coreboot"
40 qemu_dir="${libdir}/@PACKAGE@/i386-qemu"
41 mipsel_qemu_dir="${libdir}/@PACKAGE@/mipsel-qemu_mips"
42 loongson_dir="${libdir}/@PACKAGE@/mipsel-loongson"
43 mips_qemu_dir="${libdir}/@PACKAGE@/mips-qemu_mips"
44 pc_dir="${libdir}/@PACKAGE@/i386-pc"
45 efi32_dir="${libdir}/@PACKAGE@/i386-efi"
46 efi64_dir="${libdir}/@PACKAGE@/x86_64-efi"
47 ia64_dir="${libdir}/@PACKAGE@/ia64-efi"
48 rom_directory=
49 override_dir=
50 grub_mkimage="${bindir}/@grub_mkimage@"
51
52 xorriso=xorriso
53
54 export TEXTDOMAIN=@PACKAGE@
55 export TEXTDOMAINDIR="@localedir@"
56
57 localedir="@datadir@/locale"
58
59 . "${pkgdatadir}/grub-mkconfig_lib"
60
61 # Usage: usage
62 # Print the usage.
63 usage () {
64 gettext_printf "Usage: %s [OPTION] SOURCE...\n" "$self"
65 # TRANSLATORS: it generates one single image which is bootable through any method.
66 gettext "Make GRUB CD-ROM, disk, pendrive and floppy bootable image."; echo
67 echo
68 filetrans="$(gettext FILE)"
69 print_option_help "-h, --help" "$(gettext "print this message and exit")"
70 print_option_help "-v, --version" "$(gettext "print the version information and exit")"
71 print_option_help "-o, --output=$filetrans" "$(gettext "save output in FILE [required]")"
72 print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
73 print_option_help "--rom-directory=$(gettext "DIR")" "$(gettext "save ROM images in DIR [optional]")"
74 # TRANSLATORS: xorriso is a program for creating ISOs and burning CDs
75 print_option_help "--xorriso=$filetrans" "$(gettext "use FILE as xorriso [optional]")"
76 print_option_help "--grub-mkimage=$filetrans" "$(gettext "use FILE as grub-mkimage")"
77 echo
78 gettext_printf "%s generates a bootable rescue image with specified source files, source directories, or mkisofs options listed by the output of \`%s'\n" "xorriso -as mkisofs -help" "$self" | grub_fmt
79 echo
80 gettext "Option -- switches to native xorriso command mode."; echo
81 echo
82 gettext "Report bugs to <bug-grub@gnu.org>."; echo
83 gettext "Mail xorriso support requests to <bug-xorriso@gnu.org>."; echo
84 }
85
86 argument () {
87 opt=$1
88 shift
89
90 if test $# -eq 0; then
91 gettext_printf "%s: option requires an argument -- \`%s'\n" "$0" "$opt" 1>&2
92 exit 1
93 fi
94 echo $1
95 }
96
97 # Check the arguments.
98 while test $# -gt 0
99 do
100 option=$1
101 shift
102
103 case "$option" in
104 -h | --help)
105 usage
106 exit 0 ;;
107 -v | --version)
108 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
109 exit 0 ;;
110
111 --modules)
112 modules=`argument $option "$@"`; shift ;;
113 --modules=*)
114 modules=`echo "$option" | sed 's/--modules=//'` ;;
115
116 -o | --output)
117 output_image=`argument $option "$@"`; shift ;;
118 --output=*)
119 output_image=`echo "$option" | sed 's/--output=//'` ;;
120
121 --rom-directory)
122 rom_directory=`argument $option "$@"`; shift ;;
123 --rom-directory=*)
124 rom_directory=`echo "$option" | sed 's/--rom-directory=//'` ;;
125
126 # Intentionally undocumented
127 --override-directory)
128 override_dir=`argument $option "$@"`
129 shift
130 PATH=${override_dir}:$PATH
131 export PATH
132 ;;
133 --override-directory=*)
134 override_dir=`echo "${option}/" | sed 's/--override-directory=//'`
135 PATH=${override_dir}:$PATH
136 export PATH
137 ;;
138
139 --grub-mkimage)
140 grub_mkimage=`argument $option "$@"`; shift ;;
141 --grub-mkimage=*)
142 grub_mkimage=`echo "$option" | sed 's/--grub-mkimage=//'` ;;
143
144 --xorriso)
145 xorriso=`argument $option "$@"`; shift ;;
146 --xorriso=*)
147 xorriso=`echo "${option}" | sed 's/--xorriso=//'` ;;
148
149 *)
150 source="${source} ${option} $@"; break ;;
151 esac
152 done
153
154 if [ "x${output_image}" = x ] ; then
155 gettext "output file must be specified" >&2
156 echo >&2
157 usage
158 exit 1
159 fi
160
161 set $grub_mkimage dummy
162 if test -f "$1"; then
163 :
164 else
165 gettext_printf "%s: Not found.\n" "$1" 1>&2
166 exit 1
167 fi
168
169 iso9660_dir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
170 mkdir -p ${iso9660_dir}/boot/grub
171
172 process_input_dir ()
173 {
174 input_dir="$1"
175 platform="$2"
176 mkdir -p "${iso9660_dir}/boot/grub/${platform}"
177 for file in "${input_dir}/"*.mod "${input_dir}/"efiemu32.o "${input_dir}/"efiemu64.o; do
178 if test -f "$file"; then
179 cp -f "$file" "${iso9660_dir}/boot/grub/${platform}/"
180 fi
181 done
182 for file in ${pkglib_DATA}; do
183 if test -f "${input_dir}/${file}"; then
184 cp -f "${input_dir}/${file}" "${iso9660_dir}/boot/grub/${platform}/"
185 fi
186 done
187
188 mkdir -p "${iso9660_dir}/boot/grub/locale"
189 for dir in "${localedir}"/*; do
190 if test -f "$dir/LC_MESSAGES/grub.mo"; then
191 cp -f "$dir/LC_MESSAGES/grub.mo" "${iso9660_dir}/boot/grub/locale/${dir##*/}.mo"
192 fi
193 done
194 }
195
196 make_image ()
197 {
198 source_directory="$1"
199 platform=$2
200 if ! test -e "${source_directory}"; then
201 return;
202 fi
203
204 gettext_printf "Enabling %s support ...\n" "$2"
205
206 memdisk_img="`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`" || exit 1
207 memdisk_dir="`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`" || exit 1
208 mkdir -p "${memdisk_dir}/boot/grub"
209
210 (cat << EOF
211 search --fs-uuid --set=root ${iso_uuid}
212 set prefix=(\${root})/boot/grub
213 EOF
214 for i in $(cat "${source_directory}/partmap.lst") ${modules} ; do
215 echo "insmod $i"
216 done ; \
217 echo "source \$prefix/grub.cfg") \
218 > "${memdisk_dir}/boot/grub/grub.cfg"
219
220 (cd "${memdisk_dir}"; tar -cf - boot) > "${memdisk_img}"
221 rm -rf "${memdisk_dir}"
222 "$grub_mkimage" -O ${platform} -d "${source_directory}" -m "${memdisk_img}" -o "$3" --prefix='(memdisk)/boot/grub' \
223 search iso9660 configfile normal memdisk tar $4
224 rm -rf "${memdisk_img}"
225 }
226
227 if [ "${override_dir}" = "" ] ; then
228 if test -e "${multiboot_dir}" ; then
229 process_input_dir "${multiboot_dir}" i386-multiboot
230 fi
231 if test -e "${coreboot_dir}" ; then
232 process_input_dir "${coreboot_dir}" i386-coreboot
233 fi
234 if test -e "${qemu_dir}" ; then
235 process_input_dir "${qemu_dir}" i386-qemu
236 fi
237 if test -e "${pc_dir}" ; then
238 process_input_dir "${pc_dir}" i386-pc
239 fi
240 if test -e "${efi32_dir}" ; then
241 process_input_dir "${efi32_dir}" i386-efi
242 fi
243 if test -e "${efi64_dir}" ; then
244 process_input_dir "${efi64_dir}" x86_64-efi
245 fi
246 if test -e "${ia64_dir}" ; then
247 process_input_dir "${ia64_dir}" ia64-efi
248 fi
249 if test -e "${mips_qemu_dir}" ; then
250 process_input_dir "${mips_qemu_dir}" mips-qemu_mips
251 fi
252 if test -e "${mipsel_qemu_dir}" ; then
253 process_input_dir "${mipsel_qemu_dir}" mipsel-qemu_mips
254 fi
255 if test -e "${loongson_dir}" ; then
256 process_input_dir "${loongson_dir}" mipsel-loongson
257 fi
258 else
259 . "${override_dir}"/modinfo.sh
260 process_input_dir "${override_dir}" ${grub_modinfo_target_cpu}-${grub_modinfo_platform}
261 multiboot_dir=
262 pc_dir=
263 efi32_dir=
264 efi64_dir=
265 ia64_dir=
266 coreboot_dir=
267 qemu_dir=
268 mipsel_qemu_dir=
269 mips_qemu_dir=
270 loongson_dir=
271 case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
272 i386-multiboot) multiboot_dir="${override_dir}" ;;
273 i386-coreboot) coreboot_dir="${override_dir}" ;;
274 i386-qemu) qemu_dir="${override_dir}" ;;
275 i386-pc) pc_dir="${override_dir}" ;;
276 i386-efi) efi32_dir="${override_dir}" ;;
277 x86_64-efi) efi64_dir="${override_dir}" ;;
278 ia64-efi) ia64_dir="${override_dir}" ;;
279 mipsel-qemu_mips) mipsel_qemu_dir="${override_dir}" ;;
280 mipsel-loongson) loongson_dir="${override_dir}" ;;
281 mips-qemu_mips) mips_qemu_dir="${override_dir}" ;;
282 esac
283 fi
284
285 # obtain date-based UUID
286 iso_uuid=$(date -u +%Y-%m-%d-%H-%M-%S-00)
287 grub_mkisofs_arguments="${grub_mkisofs_arguments} --modification-date=$(echo ${iso_uuid} | sed -e s/-//g)"
288
289 # build BIOS core.img
290 if test -e "${pc_dir}" ; then
291 gettext_printf "Enabling %s support ...\n" "BIOS"
292 load_cfg="`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`"
293 core_img="`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`" || exit 1
294
295 (for i in $(cat "${pc_dir}/partmap.lst") ${modules} ; do
296 echo "insmod $i"
297 done ;) > "${load_cfg}"
298
299 "$grub_mkimage" -O i386-pc -d "${pc_dir}/" -o "${core_img}" -c "$load_cfg" --prefix=/boot/grub \
300 iso9660 biosdisk
301 cat "${pc_dir}/cdboot.img" "${core_img}" > "${iso9660_dir}/boot/grub/i386-pc/eltorito.img"
302
303 embed_img="`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"`" || exit 1
304 cat "${pc_dir}/boot.img" "${core_img}" > "${embed_img}"
305
306 rm -f "${core_img}"
307
308 grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-info-table \
309 --embedded-boot ${embed_img}"
310 fi
311
312 # build multiboot core.img
313 make_image "${multiboot_dir}" i386-multiboot "${iso9660_dir}/boot/multiboot.img" "pata ahci at_keyboard"
314
315 if test -e "${efi64_dir}" || test -e "${efi32_dir}" || test -e "${ia64_dir}"; then
316 efi_dir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
317 mkdir -p "${efi_dir}/efi/boot"
318
319 # build bootia64.efi
320 make_image "${ia64_dir}" ia64-efi "${efi_dir}"/efi/boot/bootia64.efi ""
321 # build bootx64.efi
322 make_image "${efi64_dir}" x86_64-efi "${efi_dir}"/efi/boot/bootx64.efi ""
323 # build bootia32.efi
324 make_image "${efi32_dir}" i386-efi "${efi_dir}"/efi/boot/bootia32.efi ""
325 if [ -e "${efi_dir}"/efi/boot/bootia32.efi ]; then
326 # For old macs. Suggested by Peter Jones.
327 cp "${efi_dir}"/efi/boot/bootia32.efi "${efi_dir}"/efi/boot/boot.efi
328 fi
329
330 mformat -C -f 2880 -L 16 -i "${iso9660_dir}"/efi.img ::
331 mcopy -s -i "${iso9660_dir}"/efi.img ${efi_dir}/efi ::/
332 rm -rf ${efi_dir}
333 grub_mkisofs_arguments="${grub_mkisofs_arguments} --efi-boot efi.img"
334 fi
335
336 make_image "${mipsel_qemu_dir}" mipsel-qemu_mips-elf "${iso9660_dir}/boot/mipsel-qemu_mips.elf" "pata"
337 if [ -e "${iso9660_dir}/boot/mipsel-qemu_mips.elf" ] && [ -d "${rom_directory}" ]; then
338 cp "${iso9660_dir}/boot/mipsel-qemu_mips.elf" "${rom_directory}/mipsel-qemu_mips.elf"
339 fi
340
341 make_image "${loongson_dir}" mipsel-loongson-elf "${iso9660_dir}/boot/mipsel-loongson.elf" "pata -C xz"
342 if [ -e "${iso9660_dir}/boot/mipsel-loongson.elf" ] && [ -d "${rom_directory}" ]; then
343 cp "${iso9660_dir}/boot/mipsel-loongson.elf" "${rom_directory}/mipsel-loongson.elf"
344 fi
345 make_image "${loongson_dir}" mipsel-yeeloong-flash "${iso9660_dir}/boot/mipsel-yeeloong.bin" "pata -C xz"
346 if [ -e "${iso9660_dir}/boot/mipsel-yeeloong.bin" ] && [ -d "${rom_directory}" ]; then
347 cp "${iso9660_dir}/boot/mipsel-yeeloong.bin" "${rom_directory}/mipsel-yeeloong.bin"
348 fi
349
350 make_image "${loongson_dir}" mipsel-fuloong2f-flash "${iso9660_dir}/boot/mipsel-fuloong2f.bin" "pata -C xz"
351 if [ -e "${iso9660_dir}/boot/mipsel-fulong.bin" ] && [ -d "${rom_directory}" ]; then
352 cp "${iso9660_dir}/boot/mipsel-fulong.bin" "${rom_directory}/mipsel-fulong.bin"
353 fi
354
355 make_image "${mips_qemu_dir}" mips-qemu_mips-elf "${iso9660_dir}/boot/mips-qemu_mips.elf" "pata"
356 if [ -e "${iso9660_dir}/boot/mips-qemu_mips.elf" ] && [ -d "${rom_directory}" ]; then
357 cp "${iso9660_dir}/boot/mips-qemu_mips.elf" "${rom_directory}/mips-qemu_mips.elf"
358 fi
359 make_image "${qemu_dir}" i386-qemu "${iso9660_dir}/boot/qemu.img" "pata at_keyboard"
360 if [ -e "${iso9660_dir}/boot/qemu.img" ] && [ -d "${rom_directory}" ]; then
361 cp "${iso9660_dir}/boot/qemu.img" "${rom_directory}/qemu.img"
362 fi
363 make_image "${coreboot_dir}" i386-coreboot "${iso9660_dir}/boot/coreboot.elf" "pata ahci at_keyboard"
364 if [ -e "${iso9660_dir}/boot/coreboot.elf" ] && [ -d "${rom_directory}" ]; then
365 cp "${iso9660_dir}/boot/coreboot.elf" "${rom_directory}/coreboot.elf"
366 fi
367
368 # build iso image
369 "${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}
370 rm -rf "${iso9660_dir}"
371
372 rm -f "${embed_img}"
373
374 exit 0