"Fossies" - the Fresh Open Source Software Archive

Member "libisoburn-1.5.4/frontend/grub-mkrescue-sed.sh" (8 Jul 2020, 11077 Bytes) of package /linux/misc/libisoburn-1.5.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "grub-mkrescue-sed.sh": 1.5.0_vs_1.5.2.

    1 #!/bin/sh
    2 
    3 # Copyright (C) 2015 - 2019
    4 # Thomas Schmitt <scdbackup@gmx.net>, libburnia-project.org
    5 # Provided under BSD license: Use, modify, and distribute as you like.
    6 
    7 echo >&2
    8 echo "frontend/grub-mkrescue-sed.sh manipulating xorriso arguments" >&2
    9 echo >&2
   10 
   11 # This script may be handed by its absolute path to grub-mkrescue
   12 # via option --xorriso= . E.g.
   13 #
   14 #   mkdir minimal
   15 #   touch minimal/empty-file.txt
   16 #   grub-mkrescue -o output.iso minimal \
   17 #         --xorriso=/home/thomas/xorriso-1.4.3./frontend/grub-mkrescue-sed.sh
   18 #
   19 # It will manipulate the xorriso arguments before they get executed by a
   20 # xorriso program. Default is the neighboring ../../xorriso/xorriso program or,
   21 # if that neighbor cannot be found, the system-wide installed xorriso.
   22 #
   23 # The mode "mjg" implements a layout which resembles Fedora LiveCD and Debian
   24 # ISOs which are bootable by ISOLINUX for BIOS and GRUB2 for EFI.
   25 # Its GPT is considered to be surplus, according to UEFI specs.
   26 #
   27 # The mode "mbr_only" implements an alternative layout according to UEFI 2.4,
   28 # section 2.5.1 and table 16. No GTP, HFS+, or APM.
   29 # This mode produces a mountable ISO 9660 partition 1 only if variable
   30 # MKRESCUE_SED_PROTECTIVE is empty or set to "no".
   31 #
   32 # The mode "mbr_hfs" is like "mbr_only" but with HFS+ mentioned in APM.
   33 # It is still compliant to UEFI with no potentially deceiving GPT.
   34 # If you add xorrisofs option -part_like_isohybrid then no gap fillig APM
   35 # partition will emerge.
   36 #
   37 # Mode "gpt_appended" represents the same layout as "mbr_only" by GPT rather
   38 # than by MBR partition table. It differs from "original" by the fact that
   39 # option -partition_offset 16 is implied and that the first partition may
   40 # be used to mount the ISO 9660 filesystem. MKRESCUE_SED_PROTECTIVE is ignored,
   41 # because neat GPT is indicated by the existence of a Protective MBR.
   42 #
   43 # These modes avoid duplicate storing of the EFI system partition "efi.img"
   44 # by xorrisofs option  -e "--interval:appended_partition_${partno}:all::"
   45 # which is new to xorriso-1.4.4.
   46 # If "_copy" is appended to the mode name, then the file /efi.img will
   47 # appear in the ISO 9660 filesystem and traditional -e "/efi.img" is used.
   48 #
   49 # "mbr_only_copy" is supposed to work with unmodified xorriso >= 1.3.2
   50 
   51 #
   52 #                              Variation settings
   53 #
   54 # The environment variables MKRESCUE_SED_* override the following
   55 # default settings:
   56 
   57 # Manipulation mode:
   58 #  "mjg"         =  ESP in MBR+GPT+APM, with HFS+
   59 #  "mbr_only"    =  ESP in MBR, without HFS+
   60 #  "mbr_hfs"     =  ESP in MBR, HFS+ in APM
   61 #  "gpt_appended" = ESP in GPT, without HFS+
   62 #  $mode"_copy"  =  one of above modes, ESP in ISO and as appended partition
   63 #  "original"    =  pass arguments unchanged
   64 mode="mbr_only"
   65 if test -n "$MKRESCUE_SED_MODE"
   66 then
   67   mode="$MKRESCUE_SED_MODE"
   68 fi
   69 
   70 # First argument of -append_partition with mode "mjg". Values: 1 or 2.
   71 partno=1
   72 if test -n "$MKRESCUE_SED_PARTNO"
   73 then
   74   partno="$MKRESCUE_SED_PARTNO"
   75 fi
   76 
   77 # Replacement for option --protective-msdos-label. Either itself or empty text.
   78 # If the environment variable contains the word "no", this means empty.
   79 protective=""
   80 if test -n "$MKRESCUE_SED_PROTECTIVE"
   81 then
   82   if test x"$MKRESCUE_SED_PROTECTIVE" = xno
   83   then
   84     protective=""
   85   elif test x"$MKRESCUE_SED_PROTECTIVE" = xyes
   86   then
   87     protective="--protective-msdos-label"
   88   else
   89     protective="$MKRESCUE_SED_PROTECTIVE"
   90   fi
   91 fi
   92 
   93 # "yes" shows xorriso arguments, "extra" additionally shows all input files.
   94 debug=no
   95 if test -n "$MKRESCUE_SED_DEBUG"
   96 then
   97   debug="$MKRESCUE_SED_DEBUG"
   98 fi
   99 
  100 # The path to the program that will be executed with the converted arguments.
  101 if test -n "$MKRESCUE_SED_XORRISO"
  102 then
  103   xorriso="$MKRESCUE_SED_XORRISO"
  104 else
  105   # Prefer neighboring xorriso binary over system-wide installed one.
  106   self_dir="$(dirname $(dirname "$0") )"
  107   if test -x "$self_dir"/xorriso/xorriso
  108   then
  109     xorriso="$self_dir"/xorriso/xorriso
  110   else
  111     xorriso="xorriso"
  112   fi
  113 fi 
  114 
  115 # MKRESCUE_SED_XORRISO_ARGS will be used as first arguments of the xorriso run.
  116 # (Trailing xorriso arguments may be simply added to the grub-mkrescue
  117 #  command line.)
  118 # Each argument must be a single word. No whitespace. No quotation marks.
  119 
  120 # "yes" overwrites the MBR partition table area in the EFI boot image by zeros.
  121 # Some EFI implementations get stuck when seeing in the EFI partition a
  122 # partition table entry which begins at LBA 0.
  123 # "extra" not only zeros the partition table but also the MBR signature.
  124 efi_zero_inner_pt=no
  125 if test -n "$MKRESCUE_SED_IN_EFI_NO_PT"
  126 then
  127   efi_zero_inner_pt="$MKRESCUE_SED_IN_EFI_NO_PT"
  128 fi
  129 
  130 
  131 #
  132 #                               Do the work 
  133 #
  134 
  135 # grub-mkrescue inquires features by running these arguments
  136 if test "$*" = "-as mkisofs -help"
  137 then
  138   "$xorriso" "$@"
  139   exit $?
  140 fi  
  141 
  142 echo "frontend/grub-mkrescue-sed.sh mode:  $mode" >&2
  143 echo >&2
  144 
  145 if test x"$debug" = xyes -o x"$debug" = xextra
  146 then
  147   # Show arguments
  148   echo "##### Begin of received arguments" >&2
  149   echo "$0" >&2
  150   for i in "$@"
  151   do
  152     echo "$i" >&2
  153   done
  154   echo "##### End of received arguments" >&2
  155   echo >&2
  156 fi
  157 
  158 # Check for option -iso_mbr_part_type which is new in 1.4.8
  159 iso_mbr_part_type=
  160 if "$xorriso" -as mkisofs -help 2>&1 | grep iso_mbr_part_type >/dev/null
  161 then
  162   iso_mbr_part_type="-iso_mbr_part_type 0x00"
  163 fi
  164 
  165 # Look for the name of the /tmp directory with the GRUB2 files.
  166 # It is the next argument after -r. But as default accept any /tmp/grub.*
  167 next_is_dir=0
  168 dir="."
  169 for i in "$@"
  170 do
  171   if test x"$i" = x"-r"
  172   then
  173     next_is_dir=1
  174   elif test $next_is_dir = 1
  175   then
  176     next_is_dir=0
  177     if echo "$i" | grep '^/tmp/grub.' >/dev/null 2>&1
  178     then
  179       test -d "$i" && dir="$i"
  180     fi
  181   elif test "$dir" = "."
  182   then
  183     if echo "$i" | grep '^/tmp/grub.' >/dev/null 2>&1 
  184     then
  185       test -d "$i" && dir="$i"
  186     fi
  187   fi
  188 done
  189 
  190 if test x"$debug" = xextra
  191 then
  192   # Show files on disk
  193   find "$dir"
  194 fi
  195 
  196 if test "$efi_zero_inner_pt" = yes -o "$efi_zero_inner_pt" = extra
  197 then
  198   did_dd=0
  199   if test -e "$dir"/efi.img
  200   then
  201     # Look for 0x55 0xAA in bytes 510 and 511
  202     magic=$(dd bs=1 skip=510 count=2 if="$dir"/efi.img 2>/dev/null | \
  203             od -c | head -1 | awk '{print $2 " " $3}')
  204     if test "$magic" = "U 252"
  205     then
  206       echo "Performing actions for MKRESCUE_SED_IN_EFI_NO_PT=$efi_zero_inner_pt" >&2
  207       dd if=/dev/zero bs=1 seek=446 count=64 conv=notrunc of="$dir"/efi.img
  208       did_dd=1
  209       if test "$efi_zero_inner_pt" = extra
  210       then
  211         dd if=/dev/zero bs=1 seek=510 count=2 conv=notrunc of="$dir"/efi.img
  212       fi
  213       echo >&2
  214     fi
  215   fi
  216   if test "$did_dd" = 0
  217   then
  218     echo >&2
  219     echo "$0 : NOTE : No EFI image found or no MBR signature in it." >&2
  220     echo "$0 : NOTE : Will not obey MKRESCUE_SED_IN_EFI_NO_PT=$efi_zero_inner_pt" >&2
  221     echo >&2
  222   fi
  223 fi
  224 
  225 efi_tmp_name=
  226 if test x"$mode" = xmjg \
  227      -o x"$mode" = xmbr_only \
  228      -o x"$mode" = xgpt_appended \
  229      -o x"$mode" = xmbr_hfs
  230 then
  231   # Move EFI partition image file out of the "$dir" tree, i.e. out of the ISO
  232   efi_tmp_name=grub-mkrescue-sed-efi-img.$$
  233   if test -e "$dir"/efi.img
  234   then
  235     mv "$dir"/efi.img /tmp/$efi_tmp_name
  236   elif test -e /tmp/$efi_tmp_name
  237   then
  238     rm /tmp/$efi_tmp_name
  239   fi
  240 fi
  241 
  242 if test x"$mode" = xmjg
  243 then
  244   # Exchange arguments for the experimental GRUB2 mjg layout
  245   x=$(echo " $*" | sed \
  246     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition $partno 0xef \/tmp\/$efi_tmp_name/" \
  247     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_${partno}:all:: -no-emul-boot -isohybrid-gpt-basdat/" \
  248     -e "s/--protective-msdos-label/$protective -part_like_isohybrid/" \
  249      )
  250 
  251 elif test x"$mode" = xmjg_copy
  252 then
  253   # Exchange arguments for the experimental GRUB2 mjg layout
  254   x=$(echo " $*" | sed \
  255     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition $partno 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  256     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e efi.img -no-emul-boot -isohybrid-gpt-basdat/" \
  257     -e "s/--protective-msdos-label/$protective -part_like_isohybrid/" \
  258      )
  259 
  260 elif test x"$mode" = xmbr_only
  261 then
  262   # Exchange arguments for no-HFS MBR-only layout
  263   x=$(echo " $*" | sed \
  264     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$efi_tmp_name/" \
  265     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  266     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  267     -e "s/--protective-msdos-label/$protective/" \
  268      )
  269 
  270 elif test x"$mode" = xmbr_only_copy
  271 then
  272   # Exchange arguments for no-HFS MBR-only layout
  273   x=$(echo " $*" | sed \
  274     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  275     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  276     -e "s/--protective-msdos-label/$protective/" \
  277      )
  278 
  279 elif test x"$mode" = xmbr_hfs
  280 then
  281   # Exchange arguments for MBR and HFS+ layout
  282   x=$(echo " $*" | sed \
  283     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$efi_tmp_name/" \
  284     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  285     -e "s/--protective-msdos-label/$protective/" \
  286      )
  287 
  288 elif test x"$mode" = xmbr_hfs_copy
  289 then
  290   # Exchange arguments for MBR and HFS+ layout
  291   x=$(echo " $*" | sed \
  292     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  293     -e "s/--protective-msdos-label/$protective/" \
  294      )
  295 
  296 elif test x"$mode" = xgpt_appended
  297 then
  298   # Exchange arguments for no-HFS MBR-only layout
  299   x=$(echo " $*" | sed \
  300     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition 2 0xef \/tmp\/$efi_tmp_name -appended_part_as_gpt -partition_offset 16/" \
  301     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  302     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  303      )
  304 
  305 elif test x"$mode" = xgpt_appended_copy
  306 then
  307   # Exchange arguments for no-HFS MBR-only layout
  308   x=$(echo " $*" | sed \
  309     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition 2 0xef \/tmp\/$(basename "$dir")\/efi.img -appended_part_as_gpt -partition_offset 16/" \
  310     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  311      )
  312 
  313 elif test x"$mode" = xoriginal
  314 then
  315   # Pass arguments unchanged
  316   x=" $*"
  317 
  318 else
  319   echo >&2
  320   echo "$0 : FATAL : Unknown manipulation mode '$mode'." >&2
  321   echo >&2
  322   exit 1
  323 fi
  324 
  325 if test x"$debug" = xyes -o x"$debug" = xextra
  326 then
  327   echo "+ converted xorriso arguments:" >&2
  328   echo "  $x" >&2
  329   echo >&2
  330 fi
  331 
  332 # Run xorriso binary with the converted arguments
  333 use_gdb=no
  334 if test "$use_gdb" = yes
  335 then
  336   gdb_file=/tmp/grub-mkrescue-sed-gdb
  337   echo b assess_appended_gpt >$gdb_file
  338   echo run $MKRESCUE_SED_XORRISO_ARGS $x >>$gdb_file
  339   gdb -x $gdb_file "$xorriso"
  340   ret=0
  341 else
  342   "$xorriso" $MKRESCUE_SED_XORRISO_ARGS $x
  343   ret=$?
  344 fi
  345 
  346 # Move back the ESP if it was separated
  347 if test -n "$efi_tmp_name" -a -e /tmp/$efi_tmp_name
  348 then
  349    mv /tmp/$efi_tmp_name "$dir"/efi.img
  350 fi
  351 
  352 exit $ret
  353