New script to insert a preseed into an iso image

This commit is contained in:
Jeremy Gardais 2018-12-26 14:28:04 +01:00
parent 31d5b0c669
commit 04542158ec
1 changed files with 109 additions and 0 deletions

109
make.iso.preseed.sh Executable file
View File

@ -0,0 +1,109 @@
#!/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"
TEMP_DIR_ISO=$(mktemp -d -t debian-iso-XXXXXX.tmp)
DEBIAN_DL_ISO_PATH="${TEMP_DIR_ISO}/debian.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
# }}}
# 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
TEMP_DIR_ISO=$(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}"
command 7z x -o"${TEMP_DIR_ISO}" -- "${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
INITRD_GZ_PATH=$(find "${TEMP_DIR_ISO}" -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
INITRD_PATH=$(find "${TEMP_DIR_ISO}" -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 {{{
cd -- "${TEMP_DIR_ISO}" || 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 {{{
# }}}
# If extract image
#rm -rf -- "${TEMP_DIR_ISO}
exit 0