#!/bin/bash SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" ask() { USER_ANSWER="?"; while [ "$USER_ANSWER" != "n" ] && [ "$USER_ANSWER" != "y" ] ; do echo -n "$1 ? [y/n] " read -r -n 1 USER_ANSWER echo ""; done if [ "$USER_ANSWER" == "y" ] ; then return 0 else return 1 fi } check_tools() { package_list='xorriso isolinux python3-apt apt-utils python3-requests rsync wget' # shellcheck disable=SC2086 if apt-get -s install ${package_list} |grep ^Inst then echo "Some tools are needed :" echo "apt install ${package_list}" exit 0 fi } check_tools usage() { cat < "${wantedfile}" < "${excludedfile}" < ${temp_file} fi echo "##### extracting iso" if [ ! -e "${temp_dir}"/md5sum.txt ] then echo "extracting iso to ${temp_dir}" xorriso -osirrox on -indev "${ISOFILE}" -extract / "${temp_dir}" chmod -R +w "${temp_dir}" fi if [ ! -d "${myfiles}" ] then mkdir -p "${myfiles}" fi echo "### download wanted packages" if [ -e "${wantedfile}" ] && [ -e "${excludedfile}" ] then if python3 download_myfiles.py --isodir "${temp_dir}" \ --wanted "${wantedfile}" --excluded "${excludedfile}" \ --files "${myfiles}" --quiet then echo "### myfiles generation ok" else echo "### myfiles generation failure !!!" exit 1 fi else echo "download_myfiles.py disabled" fi echo "### add extra script content" if [ -e "${extra_script}" ] then eval "$(cat "${extra_script}")" fi if [[ ${OFFLINE} -gt 0 ]] then if ! grep local-apt-repository "${wantedfile}" then echo "ERROR: local-apt-repository is required in wanted file for offline mode" exit 1 else cat > "${myfiles}"/local-apt-repository.sh <<"EOEF" #!/usr/bin/env bash echo "### Activate apt cdrom" sed --expression "s/^[#]*deb cdrom:/deb [trusted=yes] cdrom:/" /etc/apt/sources.list sed --expression="s/^[#]*deb cdrom:/deb [trusted=yes] cdrom:/" --in-place /etc/apt/sources.list echo "### Prepare for local-apt-repository usage" local_apt_repository=/srv/local-apt-repository source_directory=/media/cdrom0 original_list=/tmp/original_list.txt cat > ${original_list} <> ${original_list} done < <(find ${source_directory} -name Packages.gz -print0) pushd ${source_directory} || return 1 while IFS= read -r -d '' packagepath do if ! grep "^${packagepath}$" ${original_list} then echo "### Package $(basename "${packagepath}") not in original cd. Copy in /local-apt-repository." mkdir --parents --verbose ${local_apt_repository}/"$(dirname "${packagepath}")" cp --verbose "${packagepath}" ${local_apt_repository}"/$(dirname "${packagepath}")"/"$(basename "${packagepath}")" fi done < <(find pool -name "*.deb" -print0) popd || return 1 apt --yes install "$(find ${local_apt_repository} -name "local-apt-repository*.deb")" apt clean apt update apt upgrade EOEF fi fi cat > "${myfiles}"/README.custom <<"EOF" === In offline mode, Mount cdrom ( mount /dev/sr0 ) And execute local-apt-repository.sh === EOF chmod -R +w "${temp_dir}" echo "### customize initrd with preseed" if [ -e "${preseed}" ] then # cpio on "out mode" don't allow to rename/basename file on the fly # so we need to put the file somewhere with the target name # and go (pushd) to the directoy considered as root (/) of the initrd cp --verbose "${preseed}" /tmp/preseed.cfg pushd /tmp || return 1 case $(uname -m) in i686) echo "### Detected install.386" gunzip --verbose "${temp_dir}/install.386/initrd.gz" echo preseed.cfg | cpio --format=newc --create --append --verbose --file="${temp_dir}"/install.386/initrd gzip --verbose "${temp_dir}"/install.386/initrd ls -l "${temp_dir}"/install.386/initrd.gz ;; x86_64) echo "### Detected install.amd" gunzip --verbose "${temp_dir}/install.amd/initrd.gz" echo preseed.cfg | cpio --format=newc --create --append --verbose --file="${temp_dir}"/install.amd/initrd gzip --verbose "${temp_dir}"/install.amd/initrd ls -l "${temp_dir}"/install.amd/initrd.gz ;; *) echo "Platform not implemented" exit 1 ;; esac popd || return 1 fi echo "### Add content of myfiles" rsync --verbose --recursive "${myfiles}"/ "${temp_dir}" echo "### Calculate md5sum.txt" chmod +w "${temp_dir}"/md5sum.txt pushd "${temp_dir}" || return 1 find . -follow -type f ! -name "md5sum.txt" -print0 | xargs -0 md5sum > md5sum.txt popd || return 1 chmod -R -w "${temp_dir}" echo "### Create iso" xorriso -as mkisofs -o "${destination}" \ -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \ -c isolinux/boot.cat -b isolinux/isolinux.bin \ -no-emul-boot -boot-load-size 4 \ -boot-info-table "${temp_dir}" } if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then main "$@" fi