| 1 |
#!/usr/bin/env bash |
| 2 |
# Filename: grml2iso |
| 3 |
# Purpose: create a multiboot grml ISO using grml2usb |
| 4 |
# Authors: Michael Prokop <[email protected]> |
| 5 |
# Bug-Reports: see http://grml.org/bugs/ |
| 6 |
# License: This file is licensed under the GPL v2 or any later version. |
| 7 |
################################################################################ |
| 8 |
|
| 9 |
set -e -o pipefail |
| 10 |
|
| 11 |
# make sure we have the sbin directories in our PATH to find grml2usb ootb |
| 12 |
PATH="${PATH}:/sbin:/usr/local/sbin:/usr/sbin" |
| 13 |
|
| 14 |
# adjust variables if necessary through environment {{{ |
| 15 |
# path to the grml2usb script you'd like to use |
| 16 |
[ -n "$GRML2USB" ] || GRML2USB='grml2usb' |
| 17 |
|
| 18 |
if ! command -v xorriso >/dev/null 2>&1 ; then |
| 19 |
echo "Error: xorriso not found - can not create ISO." >&2 |
| 20 |
exit 1 |
| 21 |
fi |
| 22 |
# }}} |
| 23 |
|
| 24 |
# helper stuff {{{ |
| 25 |
usage() { |
| 26 |
echo >&2 "Usage: $0 [OPTIONS] -o target.iso source1.iso [source2.iso ...]" |
| 27 |
echo >&2 " |
| 28 |
Options: |
| 29 |
-b Boot Params Additional boot parameters passed to grml2usb |
| 30 |
-c Directory Copy files from directory to generated ISO |
| 31 |
-f Force overwrite of existing target.iso |
| 32 |
-r BootParam Remove specified boot params. |
| 33 |
Can be specified multiple times. |
| 34 |
-p <grml2usb param> Add the specified parameter to the grml2usb |
| 35 |
commandline. For a list of valid parameters have |
| 36 |
a look at the grml2usb manpage. |
| 37 |
Can be specified multiple times. |
| 38 |
-s URI Generate a small ISO file which downloads the squashfs |
| 39 |
file from the specified URI. Please note that due to |
| 40 |
restrictions in the bootprocess only IPs are allowed. |
| 41 |
Supported protocols are: http and ftp |
| 42 |
-t Directory Directory that should be used for temporary files |
| 43 |
during build, instead of using a temporary directory |
| 44 |
created by mktemp(1). |
| 45 |
|
| 46 |
Examples: |
| 47 |
$0 -s http://192.168.23.42:8000/grml/ -o small.iso grml-small-2025.12-amd64.iso |
| 48 |
|
| 49 |
Will generate a file small.iso which tries to download the squashfs file from |
| 50 |
http://192.168.23.42:8000/grml/ - the squashfs file is placed in the same |
| 51 |
output directory as the ISO file. |
| 52 |
" |
| 53 |
[ -n "$1" ] && exit $1 || exit 1 |
| 54 |
} |
| 55 |
# }}} |
| 56 |
|
| 57 |
# command line handling {{{ |
| 58 |
[[ $# -gt 2 ]] || usage 1 |
| 59 |
|
| 60 |
ISOFILE='' |
| 61 |
DIR='' |
| 62 |
FORCE='' |
| 63 |
URI='' |
| 64 |
typeset -a GRML2USB_OPTS |
| 65 |
while getopts fb:c:o:r:p:s:t: name; do |
| 66 |
case $name in |
| 67 |
o) ISOFILE="$OPTARG";; |
| 68 |
b) GRML2USB_OPTS+=(--bootoptions="$OPTARG");; |
| 69 |
c) DIR="$(readlink -f "$OPTARG")"; [ -n "$DIR" ] || { echo "Could not read $OPTARG - exiting" >&2 ; exit 1 ; } ;; |
| 70 |
f) FORCE='true';; |
| 71 |
r) GRML2USB_OPTS+=(--remove-bootoption="$OPTARG");; |
| 72 |
p) GRML2USB_OPTS+=("$OPTARG");; |
| 73 |
s) URI="$OPTARG";; |
| 74 |
t) WRKDIR="$(readlink -f "$OPTARG")";; |
| 75 |
?) usage 2;; |
| 76 |
esac |
| 77 |
done |
| 78 |
|
| 79 |
# test for specified URI |
| 80 |
if [ -n "$URI" ] ; then |
| 81 |
GRML2USB_OPTS+=(--bootoptions="fetch=$URI") |
| 82 |
fi |
| 83 |
|
| 84 |
# make sure -o is specified |
| 85 |
[ -n "$ISOFILE" ] || usage 1 |
| 86 |
|
| 87 |
# we don't to override any files by accident |
| 88 |
if [ -e "$ISOFILE" ] && [ -z "$FORCE" ]; then |
| 89 |
echo "Error: target file $ISOFILE exists already." >&2 |
| 90 |
exit 1 |
| 91 |
fi |
| 92 |
|
| 93 |
if [ -n "$DIR" ] && [ ! -d "$DIR" ] ; then |
| 94 |
echo "Error: specified parameter for -c is not a directory" >&2 |
| 95 |
exit 1 |
| 96 |
fi |
| 97 |
# }}} |
| 98 |
|
| 99 |
# we need root permissions for executing grml2usb {{{ |
| 100 |
if [[ $(id -u) != 0 ]]; then |
| 101 |
echo >&2 "Error: please run $0 as root." |
| 102 |
exit 1 |
| 103 |
fi |
| 104 |
# }}} |
| 105 |
|
| 106 |
# check for grml2usb {{{ |
| 107 |
if [ ! -x "$(command -v "$GRML2USB")" ] && [ ! -x "$GRML2USB" ] ; then |
| 108 |
echo "Error: Could not find grml2usb executable. Is /usr/sbin missing in PATH?" >&2 |
| 109 |
echo "Tip: run GRML2USB=/usr/sbin/grml2usb grml2iso ... as workaround" >&2 |
| 110 |
if [ -x "./$GRML2USB" ] ; then |
| 111 |
echo >&2 "If you executed grml2iso from the grml2usb repository use" |
| 112 |
echo >&2 "GRML2USB=./grml2usb $0 $*" |
| 113 |
fi |
| 114 |
exit 1 |
| 115 |
fi |
| 116 |
# }}} |
| 117 |
|
| 118 |
# variables {{{ |
| 119 |
ORIG_DIR="$(pwd)" |
| 120 |
|
| 121 |
# normalise path |
| 122 |
case $ISOFILE in |
| 123 |
/*) ;; |
| 124 |
*) ISOFILE=$ORIG_DIR/$ISOFILE ;; |
| 125 |
esac |
| 126 |
# }}} |
| 127 |
|
| 128 |
# ensure to properly set up working directory {{{ |
| 129 |
WRKDIR_EXISTED='false' |
| 130 |
if [ -z "$WRKDIR" ] ; then |
| 131 |
WRKDIR="$(mktemp -d)" |
| 132 |
else |
| 133 |
[ -d "$WRKDIR" ] && WRKDIR_EXISTED='true' |
| 134 |
fi |
| 135 |
|
| 136 |
GRML2USB_OPTS+=(--tmpdir="$WRKDIR") |
| 137 |
|
| 138 |
rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp" |
| 139 |
mkdir -p "$WRKDIR/cddir" |
| 140 |
# }}} |
| 141 |
|
| 142 |
# execute grml2usb with all ISOs you'd like to install {{{ |
| 143 |
# remove all parameters |
| 144 |
shift $(( OPTIND - 1 )) |
| 145 |
|
| 146 |
$GRML2USB "${GRML2USB_OPTS[@]}" "$@" "$WRKDIR/cddir" |
| 147 |
# }}} |
| 148 |
|
| 149 |
# change to working directory {{{ |
| 150 |
# make sure $WRKDIR is an absolute path, otherwise accessing files |
| 151 |
# in it will fail later in the code path if user provided a |
| 152 |
# relative directory |
| 153 |
WRKDIR=$(realpath $WRKDIR) |
| 154 |
cd "$WRKDIR/cddir" |
| 155 |
# }}} |
| 156 |
|
| 157 |
# detect boot method and set up boot arguments {{{ |
| 158 |
BOOT_ARGS="" |
| 159 |
USE_ISOLINUX=false |
| 160 |
USE_GRUB=false |
| 161 |
HOST_ARCH="$(uname -m)" |
| 162 |
|
| 163 |
# Check if syslinux/isolinux files are available |
| 164 |
# On arm64 hosts, prefer GRUB since isohybrid is not available |
| 165 |
if [ -d "$WRKDIR/cddir/boot/syslinux" ] && [ "$HOST_ARCH" != "aarch64" ] ; then |
| 166 |
echo "Found syslinux files, using isolinux for BIOS boot." |
| 167 |
USE_ISOLINUX=true |
| 168 |
mv "$WRKDIR"/cddir/boot/syslinux "$WRKDIR"/cddir/boot/isolinux |
| 169 |
echo "menu label ^Isolinux prompt" > "$WRKDIR"/cddir/boot/isolinux/promptname.cfg |
| 170 |
echo "include hd.cfg" >> "$WRKDIR"/cddir/boot/isolinux/grmlmain.cfg |
| 171 |
elif [ -d "$WRKDIR/cddir/boot/grub" ] ; then |
| 172 |
if [ -d "$WRKDIR/cddir/boot/syslinux" ] ; then |
| 173 |
echo "Found syslinux files, but using GRUB for BIOS boot (running on $HOST_ARCH)." |
| 174 |
rm -rf "$WRKDIR/cddir/boot/syslinux" |
| 175 |
else |
| 176 |
echo "No syslinux files found, using GRUB for BIOS boot." |
| 177 |
fi |
| 178 |
USE_GRUB=true |
| 179 |
else |
| 180 |
echo "Warning: Neither syslinux nor GRUB boot files found." >&2 |
| 181 |
fi |
| 182 |
|
| 183 |
if [ "$USE_ISOLINUX" = "true" ] ; then |
| 184 |
# isolinux-based BIOS boot |
| 185 |
if ! command -v isohybrid >/dev/null 2>&1 ; then |
| 186 |
echo "Error: isohybrid executable not found (install syslinux/isolinux/syslinux-utils?)." >&2 |
| 187 |
exit 1 |
| 188 |
fi |
| 189 |
BOOT_ARGS="-no-emul-boot -boot-load-size 4 -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat" |
| 190 |
|
| 191 |
if [ -r "${WRKDIR}/cddir/boot/efi.img" ] ; then |
| 192 |
echo "/boot/efi.img found, enabling (U)EFI boot support." |
| 193 |
if ! [ -r /usr/lib/ISOLINUX/isohdpfx.bin ] ; then |
| 194 |
echo "Error: /usr/lib/ISOLINUX/isohdpfx.bin not available, required for xorriso/isohybrid." >&2 |
| 195 |
echo "Hint: make sure isolinux is installed." >&2 |
| 196 |
exit 1 |
| 197 |
fi |
| 198 |
BOOT_ARGS+=" -eltorito-alt-boot -e boot/efi.img -no-emul-boot" |
| 199 |
BOOT_ARGS+=" -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -isohybrid-gpt-basdat -no-pad" |
| 200 |
fi |
| 201 |
|
| 202 |
elif [ "$USE_GRUB" = "true" ] ; then |
| 203 |
# GRUB-based BIOS boot |
| 204 |
GRUB_BIOS_AVAILABLE=false |
| 205 |
|
| 206 |
# Check if source ISO already has GRUB BIOS boot image |
| 207 |
if [ -r "boot/grub/i386-pc/eltorito.img" ] ; then |
| 208 |
echo "Using existing GRUB BIOS boot image from source ISO." |
| 209 |
GRUB_BIOS_AVAILABLE=true |
| 210 |
elif [ -r "boot/grub/i386-pc/cdboot.img" ] ; then |
| 211 |
echo "Using existing GRUB cdboot.img from source ISO." |
| 212 |
cp boot/grub/i386-pc/cdboot.img boot/grub/i386-pc/eltorito.img |
| 213 |
GRUB_BIOS_AVAILABLE=true |
| 214 |
elif [ -d "/usr/lib/grub/i386-pc" ] ; then |
| 215 |
# Try to generate GRUB BIOS boot image (requires i386-pc modules) |
| 216 |
if command -v grub-mkimage >/dev/null 2>&1 ; then |
| 217 |
echo "Generating GRUB BIOS boot image..." |
| 218 |
mkdir -p boot/grub/i386-pc |
| 219 |
if grub-mkimage -o boot/grub/i386-pc/eltorito.img -O i386-pc-eltorito -p /boot/grub \ |
| 220 |
biosdisk iso9660 linux configfile normal search search_fs_file search_label part_msdos part_gpt fat ext2 2>/dev/null ; then |
| 221 |
GRUB_BIOS_AVAILABLE=true |
| 222 |
else |
| 223 |
echo "Warning: Failed to generate GRUB BIOS boot image." >&2 |
| 224 |
fi |
| 225 |
fi |
| 226 |
fi |
| 227 |
|
| 228 |
if [ "$GRUB_BIOS_AVAILABLE" = "true" ] ; then |
| 229 |
# Find GRUB's boot.img for hybrid MBR (check ISO first, then system) |
| 230 |
GRUB_BOOT_IMG="" |
| 231 |
for path in boot/grub/i386-pc/boot.img /usr/lib/grub/i386-pc/boot.img /usr/share/grub/i386-pc/boot.img ; do |
| 232 |
if [ -r "$path" ] ; then |
| 233 |
GRUB_BOOT_IMG="$path" |
| 234 |
break |
| 235 |
fi |
| 236 |
done |
| 237 |
|
| 238 |
BOOT_ARGS="-no-emul-boot -boot-load-size 4 -boot-info-table -b boot/grub/i386-pc/eltorito.img -c boot/grub/boot.cat" |
| 239 |
BOOT_ARGS+=" --grub2-boot-info" |
| 240 |
|
| 241 |
if [ -r "${WRKDIR}/cddir/boot/efi.img" ] ; then |
| 242 |
echo "/boot/efi.img found, enabling (U)EFI boot support." |
| 243 |
BOOT_ARGS+=" -eltorito-alt-boot -e boot/efi.img -no-emul-boot" |
| 244 |
if [ -n "$GRUB_BOOT_IMG" ] ; then |
| 245 |
BOOT_ARGS+=" --grub2-mbr $GRUB_BOOT_IMG -isohybrid-gpt-basdat -no-pad" |
| 246 |
else |
| 247 |
echo "Warning: GRUB boot.img not found, ISO may not be hybrid-bootable from USB." >&2 |
| 248 |
BOOT_ARGS+=" -no-pad" |
| 249 |
fi |
| 250 |
elif [ -n "$GRUB_BOOT_IMG" ] ; then |
| 251 |
BOOT_ARGS+=" --grub2-mbr $GRUB_BOOT_IMG -no-pad" |
| 252 |
fi |
| 253 |
elif [ -r "${WRKDIR}/cddir/boot/efi.img" ] ; then |
| 254 |
# Fall back to EFI-only boot |
| 255 |
echo "Warning: GRUB i386-pc modules not available, falling back to EFI-only boot." >&2 |
| 256 |
echo "Hint: Install grub-pc-bin for BIOS boot support on non-x86 hosts." >&2 |
| 257 |
BOOT_ARGS="-e boot/efi.img -no-emul-boot -isohybrid-gpt-basdat -no-pad" |
| 258 |
else |
| 259 |
echo "Error: Cannot create bootable ISO - no BIOS or EFI boot method available." >&2 |
| 260 |
exit 1 |
| 261 |
fi |
| 262 |
|
| 263 |
else |
| 264 |
# EFI-only boot (e.g., arm64) |
| 265 |
if [ -r "${WRKDIR}/cddir/boot/efi.img" ] ; then |
| 266 |
echo "Using EFI-only boot." |
| 267 |
BOOT_ARGS="-e boot/efi.img -no-emul-boot -isohybrid-gpt-basdat -no-pad" |
| 268 |
else |
| 269 |
echo "Error: No bootable configuration found." >&2 |
| 270 |
exit 1 |
| 271 |
fi |
| 272 |
fi |
| 273 |
# }}} |
| 274 |
|
| 275 |
# adjust ISO for small output if necessary {{{ |
| 276 |
if [ -n "$URI" ] ; then |
| 277 |
bootloader_files=$(find . -name "*.cfg" -type f) |
| 278 |
bootloader_files+=" " |
| 279 |
bootloader_files+=$(find . -name "*.lst" -type f) |
| 280 |
output_dir=$(dirname "$ISOFILE") |
| 281 |
for squashfs in $(find . -name *.squashfs) ; do |
| 282 |
media_path="$(dirname "$squashfs")" |
| 283 |
filename="$(basename "$squashfs")" |
| 284 |
target="$output_dir/$filename" |
| 285 |
if [ -f "$target" ] && [ -z "$FORCE" ] ; then |
| 286 |
echo >&2 "Warning: $target already exists, and -force not specified, not overwriting" |
| 287 |
else |
| 288 |
mv $squashfs $target |
| 289 |
OUTPUT_FILES+=("$target") |
| 290 |
fi |
| 291 |
sed -i -e "s#^\(^.*$media_path.*\)\($URI\)\(.*$\)#\1$URI/$filename\3#g" $bootloader_files |
| 292 |
|
| 293 |
done |
| 294 |
fi |
| 295 |
# }}} |
| 296 |
|
| 297 |
# copy specified directory to cd {{{ |
| 298 |
if [ -n "$DIR" ] ; then |
| 299 |
echo >&2 "Copying ${DIR} to generated ISO" |
| 300 |
for param in GRML_NAME VERSION RELEASENAME DATE SHORT_NAME \ |
| 301 |
BOOTID RELEASE_INFO ; do |
| 302 |
EXCLUDE_PARAM="$EXCLUDE_PARAM --exclude **%${param}%**" |
| 303 |
done |
| 304 |
rsync -a ${DIR}/ $EXCLUDE_PARAM . |
| 305 |
fi |
| 306 |
|
| 307 |
# adjust files from overlay directory |
| 308 |
for GRML_VERSION_FILE in $(find . -name grml-version) ; do |
| 309 |
GRML_NAME=$(awk '{print $1}' "$GRML_VERSION_FILE") |
| 310 |
VERSION=$(awk '{print $2}' "$GRML_VERSION_FILE") |
| 311 |
RELEASENAME=$(sed 's/.*- \(.*\).*\[.*/\1/' "$GRML_VERSION_FILE") |
| 312 |
DATE=$(sed 's/.*\[\(.*\)].*/\1/' "$GRML_VERSION_FILE") |
| 313 |
SHORT_NAME="$(echo $GRML_NAME | tr -d ',./;\- ')" |
| 314 |
RELEASE_INFO="$GRML_NAME $VERSION - $RELEASENAME" |
| 315 |
BOOTID=$(cat conf/bootid.txt) |
| 316 |
|
| 317 |
for param in GRML_NAME VERSION RELEASENAME DATE SHORT_NAME \ |
| 318 |
RELEASE_INFO BOOTID ; do |
| 319 |
value="$(eval echo '$'"$param")" |
| 320 |
|
| 321 |
# copy parameterized files from the overlay directory |
| 322 |
for file in $(find ${DIR} -name "*%$param%*") ; do |
| 323 |
file=${file##$DIR/} |
| 324 |
target_dir="$(dirname ${file})" |
| 325 |
mkdir -p "$target_dir" || true |
| 326 |
cp -r ${DIR}/${file} ./${target_dir}/"$(basename ${file/\%${param}\%/$value})" |
| 327 |
done |
| 328 |
|
| 329 |
# adjust config files |
| 330 |
for file in ./boot/isolinux/*.cfg ./boot/isolinux/*.msg \ |
| 331 |
./boot/grub/*.cfg ; do |
| 332 |
sed -i "s/%$param%/$value/g" ${file} 2>/dev/null || true |
| 333 |
done |
| 334 |
done |
| 335 |
done |
| 336 |
# }}} |
| 337 |
|
| 338 |
# generate the CD/DVD ISO {{{ |
| 339 |
xorriso -as mkisofs -V 'grml-multiboot' -l -r -J $BOOT_ARGS \ |
| 340 |
-o "$ISOFILE" . |
| 341 |
# }}} |
| 342 |
|
| 343 |
# cleanup {{{ |
| 344 |
cd "$ORIG_DIR" |
| 345 |
sync |
| 346 |
rm -rf "$WRKDIR/cddir" "$WRKDIR/grub_tmp" |
| 347 |
[[ $WRKDIR_EXISTED = 'false' ]] && rmdir "$WRKDIR" |
| 348 |
echo "Generated $ISOFILE" |
| 349 |
if [ -n "$URI" ] ; then |
| 350 |
echo " |
| 351 |
Information: |
| 352 |
============== |
| 353 |
You requested to generate a small ISO image. Your generated |
| 354 |
ISO image $ISOFILE does _not_ contain the squashfs files from |
| 355 |
the source ISO images. |
| 356 |
|
| 357 |
You have to provide the extracted squashfs files under $URI. |
| 358 |
|
| 359 |
ISO image: $ISOFILE |
| 360 |
Squashfs files: ${OUTPUT_FILES[@]} |
| 361 |
URI: $URI |
| 362 |
" |
| 363 |
fi |
| 364 |
# }}} |
| 365 |
|
| 366 |
## EOF ######################################################################### |
| 367 |
# vim:foldmethod=marker ts=2 ft=sh ai expandtab tw=80 sw=2 |