scripts/make.iso.preseed.sh

123 lines
4.1 KiB
Bash
Raw Normal View History

#!/bin/sh
# Notes {{{
# Mount or extract
# At the end, mounting or extracting the iso use the same disk space, but
# mount require root permissions, so i choose to extract the iso.
# }}}
# Vars {{{
DEBIAN_VERSION_NAME="stretch"
DEBIAN_VERSION_NB="9.6.0"
DEBIAN_ISO_DVD_URL="https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-${DEBIAN_VERSION_NB}-amd64-DVD-1.iso"
DEBIAN_ISO_NETINST_URL="https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-${DEBIAN_VERSION_NB}-amd64-netinst.iso"
DEBIAN_ISO_MINI_URL="http://ftp.nl.debian.org/debian/dists/${DEBIAN_VERSION_NAME}/main/installer-amd64/current/images/netboot/mini.iso"
2018-12-26 17:54:17 +01:00
TEMP_DIR=$(mktemp -d -t new-preseed-debian-iso-XXXXXX.tmp)
TEMP_ISO_DIR="${TEMP_DIR}/extracted-iso"
2018-12-26 17:54:17 +01:00
DEBIAN_DL_ISO_PATH="${TEMP_DIR}/debian.iso"
DEBIAN_PRESEED_ISO_PATH="$(dirname ${TEMP_DIR})/preseed-debian-${DEBIAN_VERSION_NB}-amd64.iso"
# }}}
#USE_DEBIAN_ISO="${DEBIAN_ISO_DVD_URL}"
USE_DEBIAN_ISO="${DEBIAN_ISO_NETINST_URL}"
#USE_DEBIAN_ISO="${DEBIAN_ISO_MINI_URL}"
# Pre-requisites {{{
if [ ! $(command -v 7z) ]; then
printf '%b\n' "ERROR: Please install '7z' bin:\\nsudo apt install p7zip-full"
exit 1
fi
if [ ! $(command -v gzip) ]; then
printf '%b\n' "ERROR: Please install 'gzip' bin:\\nsudo apt install gzip"
exit 1
fi
2018-12-26 15:20:14 +01:00
if [ ! $(command -v xorriso) ]; then
printf '%b\n' "ERROR: Please install 'xorriso' bin:\\nsudo apt install xorriso"
exit 1
fi
if [ ! -f /usr/lib/ISOLINUX/isohdpfx.bin ]; then
printf '%b\n' "ERROR: Please install 'isolinux' package:\\nsudo apt install isolinux"
2018-12-26 14:55:32 +01:00
exit 1
fi
# }}}
# Test vars {{{
case $# in
1 )
PRESEED_FILE="${1}"
;;
2 )
PRESEED_FILE="${1}"
USER_TEMP_DIR="${2}"
## Check user temp dir argument
if [ ! -d "${USER_TEMP_DIR}" ]; then
printf '%b\n' "ERROR: The first argument should be the path to a directory."
exit 1
fi
2018-12-26 17:54:17 +01:00
TEMP_DIR=$(mktemp -d -t debian-iso-XXXXXX.tmp -p "${USER_TEMP_DIR}")
;;
* )
printf '%b\n' "Usage:"
printf '%b\n' "$(basename $0) PRESEED_FILE_PATH [TEMP_DIR]"
printf '%b\n' "-------"
printf '%b\n' "PRESEED_FILE_PATH: Required"
printf '%b\n' "Should be the absolut path to the preseed file you want to add to the ISO."
printf '%b\n' "-------"
printf '%b\n' "TEMP_DIR: Optionnal"
printf '%b\n' "If you want to store temporary files and final ISO in a specific directory."
printf '%b\n' "Note: This script require ISO size×2 (eg for DVD. 3.4GB×2)."
printf '%b\n' "Otherwise \$TMPDIR will be used."
;;
esac
## Check preseed argument
if [ ! -f "${PRESEED_FILE}" ]; then
printf '%b\n' "ERROR: The first argument should be the path to preseed file to add."
exit 1
fi
# }}}
# Download and extract ISO {{{
wget -q "${USE_DEBIAN_ISO}" -O "${DEBIAN_DL_ISO_PATH}"
2018-12-26 17:54:17 +01:00
command 7z x -o"${TEMP_ISO_DIR}" -- "${DEBIAN_DL_ISO_PATH}" > /dev/null
rm -rf -- "${DEBIAN_DL_ISO_PATH}"
# }}}
# Manage initrd.gz file {{{
## Exclude initrd.gz files from xen and gtk directories
2018-12-26 17:54:17 +01:00
INITRD_GZ_PATH=$(find "${TEMP_ISO_DIR}" -type f \( -path '*xen*' -o -path '*gtk*' \) -prune -o -name "initrd.gz" -print -quit)
## Uncompress initrd.gz
gunzip -- "${INITRD_GZ_PATH}" || exit 1
## Add preseed file to initrd
2018-12-26 17:54:17 +01:00
INITRD_PATH=$(find "${TEMP_ISO_DIR}" -type f \( -path '*xen*' -o -path '*gtk*' \) -prune -o -name "initrd" -print -quit)
echo "${PRESEED_FILE}" | cpio --quiet -H newc -o -A -F "${INITRD_PATH}"
## Recompress to initrd.gz
gzip -- "${INITRD_PATH}" || exit 1
## Fix permissions in install directory
chmod -w -R -- $(dirname -- "${INITRD_GZ_PATH}")
# }}}
# Generate new md5sum {{{
2018-12-26 17:54:17 +01:00
cd -- "${TEMP_ISO_DIR}" || exit 2
## Exclude some unwanted files
find . -follow -type f -not \( -name "md5sum.txt" -o -name 'mkisofs' -o -name 'Bootable_NoEmulation.img' -o -path '*isolinux*' \) -exec md5sum {} \; > ./md5sum.txt
cd - || exit 2
# }}}
# Generate new bootable iso {{{
2018-12-26 15:20:14 +01:00
xorriso -as mkisofs -o "${DEBIAN_PRESEED_ISO_PATH}" \
-quiet -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
-c isolinux/boot.cat -b isolinux/isolinux.bin -no-emul-boot \
2018-12-26 17:54:17 +01:00
-boot-load-size 4 -boot-info-table "${TEMP_ISO_DIR}"
# }}}
2018-12-26 15:20:14 +01:00
printf '%b\n' "The new ISO image is available: ${DEBIAN_PRESEED_ISO_PATH}"
2018-12-26 17:54:17 +01:00
#rm -rf -- "${TEMP_ISO_DIR}"
exit 0