#!/bin/sh
# Author: Didier Spaier, Paris, France
#
# Before running this script, adapt MIRROR and PATHTOISO below to your
# installation as well as other environments variables
#
#

CWD=$(pwd)
MIRROR=/data/slackware64-current
GENUINEISO=/data/images_iso/slackware64-current-mini-install.iso
MODIFIEDISO=/data/images_iso/modified_slackware64-current-mini-install.iso
mount $GENUINEISO /mnt
ISO=$(mktemp -d) || exit 1
TMP=$(mktemp -d) || exit 1
cp -r /mnt/* $ISO
KVER=$(grep kernels $ISO/kernels/VERSIONS.TXT|sed "s/.* //;s/.$//")
# Unpack the initrd
mkdir -p $TMP/initrd
cd $TMP/initrd
xz -cd < $ISO/isolinux/initrd.img | \
cpio -i -d -m -H newc --no-absolute-filenames
# Include all kernel modules
installpkg --root $TMP/initrd $MIRROR/slackware64/a/kernel-modules*txz
echo "Compressing the kernel modules. Please be patient..."
find ./lib/modules -type f -name "*.ko" -exec xz -9f -C crc32 {} \;
for i in $(find ./lib/modules -type l -name "*.ko") ; do
   ln -s $( readlink $i).xz $i.xz ; rm $i ;
done
echo "--- Kernel modules have been compressed ---"
echo "--- Running depmod ---"-
echo "/sbin/depmod -b $TMP/initrd $KVER"
/sbin/depmod -b $TMP/initrd $KVER
echo "--- depmod has be run ---"
# Repack the initrd
find . | cpio -o -H newc | xz -9fv -C crc32 > $ISO/isolinux/initrd.img
cd $ISO
# Rebuilt efiboot.img
size=$(BLOCKSIZE=1M du -s EFI|sed "s/\([[:digit:]][[:digit:]]\).*/\1/")
rsize=$(( $size + 1 ))
dd if=/dev/zero of=efiboot.img bs=1M count=$rsize
mkfs.fat efiboot.img
MOUNTPOINT=$(mktemp -d)
# Mount the image there:
mount -o loop efiboot.img $MOUNTPOINT
# Copy the the whole EFI directory to the mountpoint as elilo needs to
# find everything it deals with there.
# On the other hand, no need to include EFI in the ISO.
cp -a EFI $MOUNTPOINT
# Unmount and clean up:
umount $MOUNTPOINT
rmdir $MOUNTPOINT
# Move efiboot.img to ../../isolinux:
mv efiboot.img $ISO/isolinux
# Now $ISO contains all files to be put on the ISO image, so we can
EFIOPTIONS="-eltorito-alt-boot -e isolinux/efiboot.img -isohybrid-gpt-basdat -no-emul-boot"
xorriso -as mkisofs \
    -isohybrid-mbr /usr/share/syslinux/isohdpfx.bin \
    -hide-rr-moved \
    -U \
    -V "SLACK_MOD" \
    -J \
    -joliet-long \
    -r \
    -v \
    -x EFI\
    -o $MODIFIEDISO \
    -b isolinux/isolinux.bin \
    -c isolinux/boot.cat \
    -no-emul-boot \
    -boot-info-table $EFIOPTIONS . \
    2>${MODIFIEDISO%iso}log.txt
chown -R `stat -c %U:%G $CWD` $MODIFIEDISO ${MODIFIED%iso}log.txt $ISO $TMP
echo "** Completed. ***

If writing the ISO was successful an hybrid DVD/USB image:
$PATHTOISO is ready.
You may check then remove these directories
TMP=$TMP
ISO=$ISO"

