dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone
Blob Blame History Raw
From a0404f547d5b693dd86a2773f1fd64ca207ad2bf Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sun, 14 Apr 2013 19:24:05 +0200
Subject: [PATCH 311/482] 	Add option to compress files on install/image
 creation.

---
 ChangeLog                 |  4 ++++
 util/grub-install.in      |  8 +++----
 util/grub-install_header  | 61 ++++++++++++++++++++++++++++++++++++++++-------
 util/grub-mkimage.c       |  2 +-
 util/grub-mknetdir.in     |  2 +-
 util/grub-mkrescue.in     |  6 ++---
 util/grub-mkstandalone.in |  4 ++--
 7 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c01ae94..8cbf241 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-04-14  Szymon Janc <szymon@janc.net.pl>
+
+	Add option to compress files on install/image creation.
+
 2013-04-14  Vladimir Serbinenko  <phcoder@gmail.com>
 
 	* docs/grub-dev.texi: Rearrange menu to match the section order.
diff --git a/util/grub-install.in b/util/grub-install.in
index 016b161..32a3be3 100644
--- a/util/grub-install.in
+++ b/util/grub-install.in
@@ -684,9 +684,9 @@ case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
 esac
 
 if [ x"$config_opt_file" = x ]; then
-    "$grub_mkimage" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
+    "$grub_mkimage" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $grub_decompression_module $modules || exit 1
 else
-    "$grub_mkimage" -c "${config_opt_file}" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $modules || exit 1
+    "$grub_mkimage" -c "${config_opt_file}" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/core.${imgext}" --prefix="${prefix_drive}${relative_grubdir}" $grub_decompression_module $modules || exit 1
 fi
 
 # Backward-compatibility kludges
@@ -697,9 +697,9 @@ elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-ieee1275" ]
 elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "i386-efi" ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = "x86_64-efi" ]; then
 
     if [ x"$config_opt_file" = x ]; then
-	"$grub_mkimage" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $modules || exit 1
+	"$grub_mkimage" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $grub_decompression_module $modules || exit 1
     else
-	"$grub_mkimage" -c "${config_opt_file}" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $modules || exit 1
+	"$grub_mkimage" -c "${config_opt_file}" -d "${source_dir}" -O "${mkimage_target}" --output="${grubdir}/${grub_modinfo_target_cpu}-$grub_modinfo_platform/grub.efi" --prefix="" $grub_decompression_module $modules || exit 1
     fi
 fi
 
diff --git a/util/grub-install_header b/util/grub-install_header
index 69aac46..805fc4f 100644
--- a/util/grub-install_header
+++ b/util/grub-install_header
@@ -19,6 +19,14 @@ set -e
 pkglib_DATA="moddep.lst command.lst fs.lst partmap.lst parttool.lst \
 handler.lst video.lst crypto.lst terminal.lst"
 
+grub_compress_file () {
+    if [ "$compressor" != "" ] ; then
+        "$compressor" $compressor_opts "$1" > "$2"
+    else
+        cp -f "$1" "$2"
+    fi
+}
+
 grub_install_files () {
     grub_install_files_source_directory="$1"
     grub_install_files_target_directory="$2"
@@ -42,7 +50,7 @@ grub_install_files () {
 
     if [ x"$install_modules" = xall ]; then
 	for file in "${grub_install_files_source_directory}/"*.mod; do
-	    cp -f "$file" "${grub_install_files_target_directory}"/"${grub_install_files_platform}"
+	    grub_compress_file "$file" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/$(basename "$file")"
 	done
     else
 	modules1=
@@ -56,13 +64,13 @@ grub_install_files () {
 	    modules2="$modules3"
 	done
 	for file in $(echo "$modules1" | sed 's, ,\n,g' |sort -u); do
-	    cp -f "${grub_install_files_source_directory}/$file.mod" "${grub_install_files_target_directory}"/"${grub_install_files_platform}"
+	    grub_compress_file "${grub_install_files_source_directory}/$file.mod" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/$file.mod"
 	done
     fi
     
     for file in ${pkglib_DATA} efiemu32.o efiemu64.o; do
 	if test -f "${grub_install_files_source_directory}/${file}"; then
-            cp -f "${grub_install_files_source_directory}/${file}" "${grub_install_files_target_directory}"/"${grub_install_files_platform}"
+            grub_compress_file "${grub_install_files_source_directory}/${file}" "${grub_install_files_target_directory}"/"${grub_install_files_platform}/${file}"
 	fi
     done
     
@@ -78,34 +86,36 @@ grub_install_files () {
     if [ x"$install_locales" = xall ]; then
 	for file in "${grub_install_files_source_directory}"/po/*.mo; do
 	    if test -f "$file"; then
-		cp -f "$file" "${grub_install_files_target_directory}"/locale/
+		grub_compress_file "$file" "${grub_install_files_target_directory}"/locale/"$(basename "$file")"
 	    fi
 	done
 	for dir in "${localedir}"/*; do
 	    if test -f "$dir/LC_MESSAGES/@PACKAGE@.mo" && ! test -f "${grub_install_files_target_directory}"/locale/"${dir##*/}.mo"; then
-		cp -f "$dir/LC_MESSAGES/@PACKAGE@.mo" "${grub_install_files_target_directory}"/locale/"${dir##*/}.mo"
+		grub_compress_file "$dir/LC_MESSAGES/@PACKAGE@.mo" "${grub_install_files_target_directory}"/locale/"${dir##*/}.mo"
 	    fi
 	done
     else
 	for locale in $install_locales; do
 	    if test -f "${grub_install_files_source_directory}"/po/$locale.mo; then
-		cp -f " "${grub_install_files_source_directory}"/po/$locale.mo" "${grub_install_files_target_directory}"/locale/$locale.mo
+		grub_compress_file "${grub_install_files_source_directory}"/po/locale.mo "${grub_install_files_target_directory}"/locale/$locale.mo
 	    elif test -f "${localedir}/$locale/LC_MESSAGES/@PACKAGE@.mo"; then
-		cp -f "${localedir}/$locale/LC_MESSAGES/@PACKAGE@.mo" "${grub_install_files_target_directory}"/locale/$locale.mo
+		grub_compress_file "${localedir}/$locale/LC_MESSAGES/@PACKAGE@.mo" "${grub_install_files_target_directory}"/locale/$locale.mo
 	    fi
 	done
     fi
     for theme in ${install_themes} ; do
 	if test -f "${pkgdatadir}"/themes/"${theme}"/theme.txt; then
 	    mkdir -p "${grub_install_files_target_directory}"/themes/"${theme}"
-	    cp "${pkgdatadir}"/themes/"${theme}"/* "${grub_install_files_target_directory}"/themes/"${theme}"
+	    for file in "${pkgdatadir}"/themes/"${theme}"/*; do
+		grub_compress_file "$file" "${grub_install_files_target_directory}"/themes/"${theme}"/"$(basename "$file")"
+	    done
 	fi
     done
 
     for font in ${install_fonts} ; do
 	if test -f "${pkgdatadir}"/"$font".pf2; then
 	    mkdir -p "${grub_install_files_target_directory}"/fonts
-	    cp "${pkgdatadir}"/"$font".pf2 "${grub_install_files_target_directory}"/fonts
+	    grub_compress_file "${pkgdatadir}"/"$font".pf2 "${grub_install_files_target_directory}"/fonts/"$font".pf2
 	fi
     done
 }
@@ -115,12 +125,17 @@ grub_print_install_files_help () {
     print_option_help "--themes=THEMES" "$(gettext_printf "install THEMES [default=%s]" "starfield")"
     print_option_help "--fonts=FONTS" "$(gettext_printf "install FONTS [default=%s]" "unicode")"
     print_option_help "--locales=LOCALES" "$(gettext_printf "install only LOCALES [default=all]")"
+    print_option_help "--compress[=no,xz,gz,lzo]" "$(gettext "compress GRUB files [optional]")"
 }
 
 install_modules=all
 install_themes=starfield
 install_fonts=unicode
 install_locales=all
+compress=no
+grub_decompression_module=""
+compressor=""
+compressor_opts=""
 
 argument () {
   opt=$1
@@ -133,6 +148,29 @@ argument () {
   echo $1
 }
 
+grub_parse_compress () {
+    compress="$1"
+    case x"$compress" in
+	xno) ;;
+	xgz)
+	    compressor=`which gzip || true`
+	    grub_decompression_module="gzio"
+	    compressor_opts="--best --stdout";;
+	xxz)
+	    compressor=`which xz || true`
+	    grub_decompression_module="xzio gcry_crc"
+	    compressor_opts="--lzma2=dict=128KiB --check=none --stdout";;
+	xlzo)
+	    compressor=`which lzop || true`
+	    grub_decompression_module="lzopio adler32 gcry_crc"
+	    compressor_opts="-9 -c";;
+	*)
+	    gettext_printf "Unrecognized compression \`%s'\n" "$compress" 1>&2
+	    usage
+	    exit 1
+    esac
+}
+
 grub_process_install_options () {
     option=$1
     shift
@@ -156,6 +194,11 @@ grub_process_install_options () {
 	    install_locales=`argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
 	--locales=*)
             install_locales=`echo "$option" | sed 's/--locales=//'`; grub_process_install_options_consumed=1; return ;;
+	--compress)
+	    grub_parse_compress `argument $option "$@"`; grub_process_install_options_consumed=2; return ;;
+        --compress=*)
+            grub_parse_compress `echo "${option}" | sed 's/--compress=//'`; grub_process_install_options_consumed=1; return ;;
     esac
 }
 
+export grub_decompression_module
diff --git a/util/grub-mkimage.c b/util/grub-mkimage.c
index 96279a4..0acc61e 100644
--- a/util/grub-mkimage.c
+++ b/util/grub-mkimage.c
@@ -1740,7 +1740,7 @@ static struct argp_option options[] = {
   {"note",   'n', 0, 0, N_("add NOTE segment for CHRP IEEE1275"), 0},
   {"output",  'o', N_("FILE"), 0, N_("output a generated image to FILE [default=stdout]"), 0},
   {"format",  'O', N_("FORMAT"), 0, 0, 0},
-  {"compression",  'C', "(xz|none|auto)", 0, N_("choose the compression to use"), 0},
+  {"compression",  'C', "(xz|none|auto)", 0, N_("choose the compression to use for core image"), 0},
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   { 0, 0, 0, 0, 0, 0 }
 };
diff --git a/util/grub-mknetdir.in b/util/grub-mknetdir.in
index 6df761a..d32de46 100644
--- a/util/grub-mknetdir.in
+++ b/util/grub-mknetdir.in
@@ -191,7 +191,7 @@ process_input_dir ()
 source ${subdir}/grub.cfg
 EOF
 
-    "$grub_mkimage" ${config_opt} -d "${input_dir}" -O ${mkimage_target} "--output=${grubdir}/core.$ext" "--prefix=$prefix" $modules $netmodules tftp || exit 1
+    "$grub_mkimage" ${config_opt} -d "${input_dir}" -O ${mkimage_target} "--output=${grubdir}/core.$ext" "--prefix=$prefix" $modules $grub_decompression_module $netmodules tftp || exit 1
     # TRANSLATORS: First %s is replaced by platform name. Second one by filename.
     gettext_printf "Netboot directory for %s created. Configure your DHCP server to point to %s\n" "${platform}" "${subdir}/${platform}/core.$ext"
 }
diff --git a/util/grub-mkrescue.in b/util/grub-mkrescue.in
index b97d674..7270d7f 100644
--- a/util/grub-mkrescue.in
+++ b/util/grub-mkrescue.in
@@ -248,7 +248,7 @@ EOF
     done ; ) > "${load_cfg}"
 
     "$grub_mkimage" -O ${platform} -d "${source_directory}" -c "${load_cfg}" -o "$3" \
-        search iso9660 $4
+        $grub_decompression_module search iso9660 $4
     rm -rf "${load_cfg}"
 }
 
@@ -263,7 +263,7 @@ make_image_fwdisk ()
     gettext_printf "Enabling %s support ...\n" "$2"
 
     "$grub_mkimage" -O ${platform} -d "${source_directory}" -p '()/boot/grub' -o "$3" \
-        iso9660 $4
+        $grub_decompression_module iso9660 $4
 }
 
 if [ "${override_dir}" = "" ] ; then
@@ -362,7 +362,7 @@ if test -e "${pc_dir}" ; then
     done ;) > "${load_cfg}"
 
     "$grub_mkimage" -O i386-pc -d "${pc_dir}/" -o "${core_img}" -c "$load_cfg" --prefix=/boot/grub \
-        iso9660 biosdisk
+        $grub_decompression_module iso9660 biosdisk
     cat "${pc_dir}/cdboot.img" "${core_img}" > "${iso9660_dir}/boot/grub/i386-pc/eltorito.img"
 
     grub_mkisofs_arguments="${grub_mkisofs_arguments} -b boot/grub/i386-pc/eltorito.img -no-emul-boot -boot-load-size 4 -boot-info-table"
diff --git a/util/grub-mkstandalone.in b/util/grub-mkstandalone.in
index a5434c4..927075b 100644
--- a/util/grub-mkstandalone.in
+++ b/util/grub-mkstandalone.in
@@ -60,7 +60,7 @@ usage () {
     print_option_help "-O, --format=$(gettext "FORMAT")" "$(gettext "generate an image in FORMAT")"; echo
     print_option_help "" "$(gettext "available formats:") $formats"
     echo
-    print_option_help "-C, --compression=(xz|none|auto)" "$(gettext "choose the compression to use")"
+    print_option_help "-C, --compression=(xz|none|auto)" "$(gettext "choose the compression to use for core image")"
     print_option_help "--modules=$(gettext "MODULES")" "$(gettext "pre-load specified modules MODULES")"
     grub_print_install_files_help
     print_option_help  "--grub-mkimage=$(gettext "FILE")" "$(gettext "use FILE as grub-mkimage")"
@@ -170,7 +170,7 @@ memdisk_img=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
 
 (cd "${memdisk_dir}"; tar -cf - * $source) > "${memdisk_img}"
 rm -rf "${memdisk_dir}"
-"$grub_mkimage" -O "${format}" -C "$compression" -d "${source_directory}" -m "${memdisk_img}" -o "$output_image" --prefix='(memdisk)/boot/grub' memdisk tar $modules
+"$grub_mkimage" -O "${format}" -C "$compression" -d "${source_directory}" -m "${memdisk_img}" -o "$output_image" --prefix='(memdisk)/boot/grub' memdisk tar $grub_decompression_module $modules
 rm -rf "${memdisk_img}"
 
 exit 0
-- 
1.8.2.1