"Fossies" - the Fresh Open Source Software Archive

Member "libisoburn-1.5.6/frontend/grub-mkrescue-sed.sh" (6 Aug 2022, 13910 Bytes) of package /linux/misc/libisoburn-1.5.6.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 latest Fossies "Diffs" side-by-side code changes report for "grub-mkrescue-sed.sh": 1.5.4_vs_1.5.6.

    1 #!/bin/sh
    2 
    3 # Copyright (C) 2015 - 2022
    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" in MKRESCUE_SED_IN_EFI_NO_PT overwrites the MBR partition table area
  121 # in the EFI boot image by zeros. Some EFI implementations get stuck when
  122 # seeing in the EFI partition a 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 # "yes" in MKRESCUE_SED_UNPACK_EFI_TO_ISO causes the file tree from the FAT
  131 # image efi.img to be unpacked by mcopy to a directory in /tmp which then
  132 # gets mapped into the root directory of the emerging ISO 9660 filesystem.
  133 # This enables a way of installing the result image onto a USB stick with
  134 # FAT filesystem by simply copying its full file tree from ISO to FAT.
  135 # Whether the payload files of the ISO will be able to work from FAT depends
  136 # on their expectation towards file names and attributes.
  137 # WARNING: Make sure that the files in efi.img do not collide with your
  138 #          payload files or with files added to the ISO by GRUB. Before using
  139 #          MKRESCUE_SED_UNPACK_EFI_TO_ISO, make a vanilla grub-mkrescue ISO
  140 #          with your payload, mount it and then its /efi.img file, then use
  141 #          program "find" on the mount point of efi.img to see all its files.
  142 #          Compare this with the output of "find" on the ISO mount point.
  143 efi_unpack_to_iso=no
  144 if test -n "$MKRESCUE_SED_UNPACK_EFI_TO_ISO"
  145 then
  146   efi_unpack_to_iso="$MKRESCUE_SED_UNPACK_EFI_TO_ISO"
  147 fi
  148 
  149 #
  150 #                               Do the work 
  151 #
  152 
  153 # grub-mkrescue inquires features by running these arguments
  154 if test "$*" = "-as mkisofs -help"
  155 then
  156   "$xorriso" "$@"
  157   exit $?
  158 fi  
  159 
  160 echo "frontend/grub-mkrescue-sed.sh mode:  $mode" >&2
  161 echo >&2
  162 
  163 if test x"$debug" = xyes -o x"$debug" = xextra
  164 then
  165   # Show arguments
  166   echo "##### Begin of received arguments" >&2
  167   echo "$0" >&2
  168   for i in "$@"
  169   do
  170     echo "$i" >&2
  171   done
  172   echo "##### End of received arguments" >&2
  173   echo >&2
  174 fi
  175 
  176 # Check for option -iso_mbr_part_type which is new in 1.4.8
  177 iso_mbr_part_type=
  178 if "$xorriso" -as mkisofs -help 2>&1 | grep iso_mbr_part_type >/dev/null
  179 then
  180   iso_mbr_part_type="-iso_mbr_part_type 0x00"
  181 fi
  182 
  183 # Look for the name of the /tmp directory with the GRUB2 files.
  184 # It is the next argument after -r. But as default accept any /tmp/grub.*
  185 next_is_dir=0
  186 dir="."
  187 for i in "$@"
  188 do
  189   if test x"$i" = x"-r"
  190   then
  191     next_is_dir=1
  192   elif test $next_is_dir = 1
  193   then
  194     next_is_dir=0
  195     if echo "$i" | grep '^/tmp/grub.' >/dev/null 2>&1
  196     then
  197       test -d "$i" && dir="$i"
  198     fi
  199   elif test "$dir" = "."
  200   then
  201     if echo "$i" | grep '^/tmp/grub.' >/dev/null 2>&1 
  202     then
  203       test -d "$i" && dir="$i"
  204     fi
  205   fi
  206 done
  207 
  208 if test x"$debug" = xextra
  209 then
  210   # Show files on disk
  211   find "$dir" 2>&1
  212   echo 2>&1
  213 fi
  214 
  215 if test x"$efi_zero_inner_pt" = xyes -o x"$efi_zero_inner_pt" = xextra
  216 then
  217   did_dd=0
  218   if test -e "$dir"/efi.img
  219   then
  220     # Look for 0x55 0xAA in bytes 510 and 511
  221     magic=$(dd bs=1 skip=510 count=2 if="$dir"/efi.img 2>/dev/null | \
  222             od -c | head -1 | awk '{print $2 " " $3}')
  223     if test "$magic" = "U 252"
  224     then
  225       echo "Performing actions for MKRESCUE_SED_IN_EFI_NO_PT=$efi_zero_inner_pt" >&2
  226       dd if=/dev/zero bs=1 seek=446 count=64 conv=notrunc of="$dir"/efi.img
  227       did_dd=1
  228       if test "$efi_zero_inner_pt" = extra
  229       then
  230         dd if=/dev/zero bs=1 seek=510 count=2 conv=notrunc of="$dir"/efi.img
  231       fi
  232       echo >&2
  233     fi
  234   fi
  235   if test "$did_dd" = 0
  236   then
  237     echo >&2
  238     echo "$0 : NOTE : No EFI image found or no MBR signature in it." >&2
  239     echo "$0 : NOTE : Will not obey MKRESCUE_SED_IN_EFI_NO_PT=$efi_zero_inner_pt" >&2
  240     echo >&2
  241   fi
  242 fi
  243 
  244 efi_temp_tree=
  245 if test x"$efi_unpack_to_iso" = xyes
  246 then
  247   if test -e "$dir"/efi.img
  248   then
  249     temp_tree=/tmp/grub-mkrescue-sed-et."$$"
  250     # The mcopy command is the inverse of what grub-mkrescue does to pack it up
  251     if mcopy -s -i "$dir"/efi.img ::/ "$temp_tree"
  252     then
  253       efi_temp_tree="$temp_tree"
  254       if test x"$debug" = xyes -o x"$debug" = xextra
  255       then
  256         echo "Temporarily extracted $dir/efi.img to $temp_tree" >&2
  257         echo >&2
  258         if test x"$debug" = xextra
  259         then
  260           # Show extracted files
  261           find "$temp_tree" >&2
  262           echo >&2
  263         fi
  264       fi
  265     elif test -e "$temp_tree"
  266     then
  267       rm -r "$temp_tree"
  268     fi
  269     if test -z "$efi_temp_tree"
  270     then
  271       echo >&2
  272       echo "$0 : NOTE : Could not extract efi.img to $temp_tree" >&2
  273       echo "$0 : NOTE : Thus cannot obey MKRESCUE_SED_UNPACK_EFI_TO_ISO." >&2
  274       echo >&2
  275     fi
  276   fi
  277 fi
  278 
  279 efi_tmp_name=
  280 if test x"$mode" = xmjg \
  281      -o x"$mode" = xmbr_only \
  282      -o x"$mode" = xgpt_appended \
  283      -o x"$mode" = xmbr_hfs
  284 then
  285   # Move EFI partition image file out of the "$dir" tree, i.e. out of the ISO
  286   efi_tmp_name=grub-mkrescue-sed-ei.$$
  287   if test -e "$dir"/efi.img
  288   then
  289     mv "$dir"/efi.img /tmp/$efi_tmp_name
  290     if test x"$debug" = xyes -o x"$debug" = xextra
  291     then
  292       echo "Temporarily moved $dir/efi.img to /tmp/$efi_tmp_name" >&2
  293       echo >&2
  294     fi
  295   elif test -e /tmp/$efi_tmp_name
  296   then
  297     rm /tmp/$efi_tmp_name
  298   fi
  299 fi
  300 
  301 if test x"$mode" = xmjg
  302 then
  303   # Exchange arguments for the experimental GRUB2 mjg layout
  304   x=$(echo " $*" | sed \
  305     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition $partno 0xef \/tmp\/$efi_tmp_name/" \
  306     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_${partno}:all:: -no-emul-boot -isohybrid-gpt-basdat/" \
  307     -e "s/--protective-msdos-label/$protective -part_like_isohybrid/" \
  308      )
  309 
  310 elif test x"$mode" = xmjg_copy
  311 then
  312   # Exchange arguments for the experimental GRUB2 mjg layout
  313   x=$(echo " $*" | sed \
  314     -e "s/-efi-boot-part --efi-boot-image/-no-pad -append_partition $partno 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  315     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e efi.img -no-emul-boot -isohybrid-gpt-basdat/" \
  316     -e "s/--protective-msdos-label/$protective -part_like_isohybrid/" \
  317      )
  318 
  319 elif test x"$mode" = xmbr_only
  320 then
  321   # Exchange arguments for no-HFS MBR-only layout
  322   x=$(echo " $*" | sed \
  323     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$efi_tmp_name/" \
  324     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  325     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  326     -e "s/--protective-msdos-label/$protective/" \
  327      )
  328 
  329 elif test x"$mode" = xmbr_only_copy
  330 then
  331   # Exchange arguments for no-HFS MBR-only layout
  332   x=$(echo " $*" | sed \
  333     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  334     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  335     -e "s/--protective-msdos-label/$protective/" \
  336      )
  337 
  338 elif test x"$mode" = xmbr_hfs
  339 then
  340   # Exchange arguments for MBR and HFS+ layout
  341   x=$(echo " $*" | sed \
  342     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$efi_tmp_name/" \
  343     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  344     -e "s/--protective-msdos-label/$protective/" \
  345      )
  346 
  347 elif test x"$mode" = xmbr_hfs_copy
  348 then
  349   # Exchange arguments for MBR and HFS+ layout
  350   x=$(echo " $*" | sed \
  351     -e "s/-efi-boot-part --efi-boot-image/$iso_mbr_part_type -no-pad -append_partition 2 0xef \/tmp\/$(basename "$dir")\/efi.img/" \
  352     -e "s/--protective-msdos-label/$protective/" \
  353      )
  354 
  355 elif test x"$mode" = xgpt_appended
  356 then
  357   # Exchange arguments for no-HFS MBR-only layout
  358   x=$(echo " $*" | sed \
  359     -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/" \
  360     -e "s/--efi-boot efi\.img/-eltorito-alt-boot -e --interval:appended_partition_2:all:: -no-emul-boot/" \
  361     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  362      )
  363 
  364 elif test x"$mode" = xgpt_appended_copy
  365 then
  366   # Exchange arguments for no-HFS MBR-only layout
  367   x=$(echo " $*" | sed \
  368     -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/" \
  369     -e "s/-hfsplus .*CoreServices\/boot.efi//" \
  370      )
  371 
  372 elif test x"$mode" = xoriginal
  373 then
  374   # Pass arguments unchanged
  375   x=" $*"
  376 
  377 else
  378   echo >&2
  379   echo "$0 : FATAL : Unknown manipulation mode '$mode'." >&2
  380   echo >&2
  381   exit 1
  382 fi
  383 
  384 if echo "$efi_temp_tree" | grep '^/tmp/' >/dev/null
  385 then
  386   # Does the xorriso run end in native command mode ?
  387   separator_seen=0
  388   for i in "$@"
  389   do
  390     if test x"$i" = x--
  391     then
  392       separator_seen=1
  393     fi
  394   done
  395   if test "$separator_seen" = 1
  396   then
  397     # Native mode possible: Enable it for sure and then use -map
  398     x=" $x -- -map $efi_temp_tree /"
  399   else
  400     # Hopefully nobody finds a way to leave mkisofs emulation without "--" arg
  401     x=" $x -graft-points /=$efi_temp_tree"
  402   fi
  403 fi
  404 
  405 if test x"$debug" = xyes -o x"$debug" = xextra
  406 then
  407   echo "+ converted xorriso arguments:" >&2
  408   echo "  $x" >&2
  409   echo >&2
  410 fi
  411 
  412 # Run xorriso binary with the converted arguments
  413 use_gdb=no
  414 if test "$use_gdb" = yes
  415 then
  416   gdb_file=/tmp/grub-mkrescue-sed-gdb
  417   echo b assess_appended_gpt >$gdb_file
  418   echo run $MKRESCUE_SED_XORRISO_ARGS $x >>$gdb_file
  419   gdb -x $gdb_file "$xorriso"
  420   ret=0
  421 else
  422   "$xorriso" $MKRESCUE_SED_XORRISO_ARGS $x
  423   ret=$?
  424 fi
  425 
  426 # Move back the ESP if it was separated
  427 if test -n "$efi_tmp_name" -a -e /tmp/$efi_tmp_name
  428 then
  429    mv /tmp/$efi_tmp_name "$dir"/efi.img
  430 fi
  431 
  432 # Remove possible extracted EFI partition tree
  433 if echo "$efi_temp_tree" | grep '^/tmp/' >/dev/null
  434 then
  435   rm -r "$efi_temp_tree"
  436 fi
  437 
  438 exit $ret
  439