You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
339 lines
12 KiB
339 lines
12 KiB
#! /usr/bin/env bash |
|
set -ueEo pipefail |
|
|
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" |
|
|
|
check_tools() |
|
{ |
|
if ! udisksctl status >/dev/null |
|
then |
|
echo "udisksctl is required" |
|
exit 1 |
|
fi |
|
if ! wget --version >/dev/null |
|
then |
|
echo "wget is required" |
|
exit 1 |
|
fi |
|
if ! jigdo-lite --version >/dev/null |
|
then |
|
echo "jigdo-lite is required" |
|
exit 1 |
|
fi |
|
} |
|
|
|
check_tools |
|
|
|
usage() { |
|
cat <<EOF |
|
|
|
Upgrade an existant official debian iso to last revision. |
|
|
|
options: |
|
-c|--configfile Specify a config file |
|
-d|--destination Path or filename to produce the new isofile |
|
-h|--help Show this message. |
|
-i|--isofile Isofile to upgrade |
|
-j|--jigdo Jigdo file local storage path |
|
-m|--mirror Download pacakge repository |
|
-p|--proxy specify a optional debian apt proxy |
|
-v Show more output. |
|
|
|
Configfile contents example: |
|
http_proxy=http://<ip>:<port> |
|
ISOFILE=$HOME/debian-13.0.0-amd64-netinst.iso |
|
JIGDO=$HOME/jigdo |
|
DESTINATION=$HOME |
|
MIRROR=http://deb.debian.org |
|
EOF |
|
} |
|
|
|
main() { |
|
|
|
cd "$SCRIPT_DIR" |
|
|
|
ISOFILE="" |
|
VERBOSITY=0 |
|
|
|
while [[ $# -gt 0 ]]; do |
|
case $1 in |
|
-c|--configfile) |
|
CONFIGFILE=$2 |
|
[[ -z ${CONFIGFILE} ]] && echo "missing argument $1" && exit 1 |
|
if [ -e "${CONFIGFILE}" ] |
|
then |
|
# shellcheck source=/dev/null |
|
source "${CONFIGFILE}" |
|
else |
|
echo "ERROR: specified configfile not found"* |
|
exit 1 |
|
fi |
|
shift |
|
shift |
|
;; |
|
-d|--destination) |
|
DESTINATION=$2 |
|
[[ -z ${DESTINATION} ]] && echo "missing argument $1" && exit 1 |
|
shift |
|
shift |
|
;; |
|
-h|--help) |
|
usage |
|
exit 0 |
|
;; |
|
-i|--isofile) |
|
ISOFILE=$2 |
|
[[ -z ${ISOFILE} ]] && echo "missing argument $1" && exit 1 |
|
shift |
|
shift |
|
;; |
|
-j|--jigdo) |
|
JIGDOPATH=$2 |
|
[[ -z ${JIGDOPATH} ]] && echo "missing argument $1" && exit 1 |
|
shift |
|
shift |
|
;; |
|
-m|--mirror) |
|
MIRROR=$2 |
|
[[ -z ${MIRROR} ]] && echo "missing argument $1" && exit 1 |
|
shift |
|
shift |
|
;; |
|
-p|--proxy) |
|
http_proxy=$2 |
|
[[ -z ${http_proxy} ]] && echo "missing argument $1" && exit 1 |
|
shift |
|
shift |
|
;; |
|
-v|--verbose) |
|
VERBOSITY=1 |
|
shift |
|
;; |
|
*) |
|
echo "ERROR: unknown option: $1" |
|
usage |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
if [ ! -e "$ISOFILE" ] |
|
then |
|
echo "ERROR: provide the source isofile." |
|
exit 1 |
|
fi |
|
|
|
DESTINATION=${DESTINATION:-$(pwd)} |
|
JIGDOPATH=${JIGDOPATH:-$HOME/jigdo} |
|
http_proxy=${http_proxy:-} |
|
if [ ! -d "${JIGDOPATH}" ] |
|
then |
|
mkdir -p "${JIGDOPATH}" |
|
fi |
|
MIRROR=${MIRROR:-http://deb.debian.org/} |
|
|
|
# debian-13.0.0-amd64-netinst.iso |
|
bnfile=$(basename "${ISOFILE}") |
|
distribution=$( echo "${bnfile}" |cut --delimiter='-' --fields=1 ) |
|
majorversion=$( echo "${bnfile}" |cut --delimiter='-' --fields=2 |cut --delimiter='.' --fields=1 ) |
|
minorversion=$( echo "${bnfile}" |cut --delimiter='-' --fields=2 |cut --delimiter='.' --fields=2 ) |
|
incrementalversion=$( echo "${bnfile}" |cut --delimiter='-' --fields=2 |cut --delimiter='.' --fields=3 ) |
|
architecture=$( echo "${bnfile}" |cut --delimiter='-' --fields=3) |
|
flavor=$( echo "${bnfile}" |cut --delimiter='-' --fields=4 |cut --delimiter='.' --fields=1 ) |
|
extension=$( echo "${bnfile}" |cut --delimiter='-' --fields=4 |cut --delimiter='.' --fields=2 ) |
|
|
|
if [[ ${VERBOSITY} -gt 0 ]] |
|
then |
|
echo "isofile = ${ISOFILE}" |
|
echo "reconstructed = ${distribution}-${majorversion}.${minorversion}.${incrementalversion}-${architecture}-${flavor}.iso" |
|
fi |
|
|
|
if [ "${extension}" != "iso" ] |
|
then |
|
echo "Not implemented for extension : ${extension}" |
|
exit 1 |
|
fi |
|
|
|
if [ "${distribution}" != "debian" ] |
|
then |
|
echo "Not implemented for distribution : ${distribution}" |
|
exit 1 |
|
fi |
|
|
|
case ${flavor} in |
|
netinst) |
|
cdimagejigdodirectory="jigdo-cd" |
|
;; |
|
*) |
|
echo "Not implemented for flavor : ${flavor}" |
|
exit 1 |
|
;; |
|
esac |
|
|
|
case ${majorversion} in |
|
13) |
|
# https://cdimage.debian.org/cdimage/release/current/amd64/jigdo-cd/debian-13.5.0-amd64-netinst.jigdo |
|
jigdorooturl="https://cdimage.debian.org/cdimage/release/current" |
|
cdimagetargeturl="${jigdorooturl}/${architecture}/${cdimagejigdodirectory}" |
|
;; |
|
12) |
|
# https://cdimage.debian.org/cdimage/archive/ |
|
# https://cdimage.debian.org/cdimage/archive/12.14.0/i386/jigdo-cd/debian-12.14.0-i386-netinst.jigdo |
|
jigdorooturl=https://cdimage.debian.org/cdimage/archive |
|
# compute the target url is required for archive section |
|
cdimagearchiveindex=/tmp/jigdo-${majorversion}-index.html |
|
if wget ${jigdorooturl} --output-document "${cdimagearchiveindex}" |
|
then |
|
targetversion=$( grep "[DIR]" "${cdimagearchiveindex}" | grep "a href=\"${majorversion}" | cut --delimiter='.' --fields=2 | sort --numeric-sort | tail --lines=1 ) |
|
cdimagetargeturl="${jigdorooturl}/${majorversion}.${targetversion}.${incrementalversion}/${architecture}/${cdimagejigdodirectory}" |
|
else |
|
echo "download error ${cdimagetargeturl}/SHA512SUMS" |
|
echo "check your network connection" |
|
exit 1 |
|
fi |
|
;; |
|
*) |
|
echo "Not implemented" |
|
exit 1 |
|
esac |
|
|
|
sha512filelist=${JIGDOPATH}/jigdo-${distribution}-${majorversion}-last-${architecture}-${flavor}-sha512sum |
|
|
|
if [[ ${VERBOSITY} -ge 0 ]] |
|
then |
|
echo "isofile = ${ISOFILE}" |
|
echo "reconstructed = ${distribution}-${majorversion}.${minorversion}.${incrementalversion}-${architecture}-${flavor}.iso" |
|
echo "cdimagetargeturl = ${cdimagetargeturl}" |
|
echo "sha512filelist = ${sha512filelist}" |
|
fi |
|
|
|
if wget "${cdimagetargeturl}/SHA512SUMS" --output-document "${sha512filelist}" |
|
then |
|
# retrieve the cdimage "directory checksum" to extract last "current" version |
|
targetjigdoname=$( grep "${distribution}-${majorversion}.*-${architecture}-${flavor}.jigdo" "${sha512filelist}" | cut --delimiter=' ' --fields=3 ) |
|
targettemplatename=$( grep "${distribution}-${majorversion}.*-${architecture}-${flavor}.template" "${sha512filelist}" | cut --delimiter=' ' --fields=3 ) |
|
targetversion=$( echo "${targetjigdoname}" | cut --delimiter='-' --fields=2 | cut --delimiter='.' --fields=2 ) |
|
if [[ ${minorversion} -gt ${targetversion} ]] |
|
then |
|
echo "Rule violated : your version is more advanced than target" |
|
echo "Not implemented" |
|
exit 1 |
|
elif [[ ${minorversion} -lt ${targetversion} ]] |
|
then |
|
# download jigdo if not already done or bad checksum |
|
jigdofilepath=$(find "${JIGDOPATH}" -name "${distribution}-${majorversion}.${targetversion}.${incrementalversion}-${architecture}-${flavor}.jigdo") |
|
if [[ ! -e "${jigdofilepath}" ]] || ! sha512sum --ignore-missing --check "${sha512filelist}" "${jigdofilepath}" |
|
then |
|
if wget "${cdimagetargeturl}/${targetjigdoname}" --output-document "${JIGDOPATH}/${targetjigdoname}" |
|
then |
|
jigdofilepath="${JIGDOPATH}/${targetjigdoname}" |
|
pushd "${JIGDOPATH}" || exit 1 |
|
echo "sha512sum --ignore-missing --check ${sha512filelist}" |
|
if ! sha512sum --ignore-missing --check "${sha512filelist}" |
|
then |
|
echo "download error : bad checksum : ${cdimagetargeturl}/${targetjigdoname}" |
|
echo "check your network connection" |
|
exit 1 |
|
fi |
|
popd |
|
fi |
|
else |
|
echo "jigdo file ${jigdofilepath} is ok" |
|
fi |
|
# download template if not already done or bad checksum |
|
templatefilepath=$(find "${JIGDOPATH}" -name "${distribution}-${majorversion}.${targetversion}.${incrementalversion}-${architecture}-${flavor}.template") |
|
if [[ ! -e "${templatefilepath}" ]] || ! sha512sum --ignore-missing --check "${sha512filelist}" "${templatefilepath}" |
|
then |
|
if wget "${cdimagetargeturl}/${targettemplatename}" --output-document "${JIGDOPATH}/${targettemplatename}" |
|
then |
|
templatefilepath="${JIGDOPATH}/${targettemplatename}" |
|
pushd "${JIGDOPATH}" || exit 1 |
|
echo "sha512sum --ignore-missing --check ${sha512filelist}" |
|
if ! sha512sum --ignore-missing --check "${sha512filelist}" |
|
then |
|
echo "download error : bad checksum : ${cdimagetargeturl}/${targettemplatename}" |
|
echo "check your network connection" |
|
exit 1 |
|
fi |
|
popd |
|
fi |
|
else |
|
echo "template file ${templatefilepath} is ok" |
|
fi |
|
else |
|
echo "Already on the last version" |
|
exit 0 |
|
fi |
|
else |
|
echo "download error ${cdimagetargeturl}/SHA512SUMS" |
|
echo "check your network connection" |
|
exit 1 |
|
fi |
|
|
|
# mount loop image |
|
looplog=/tmp/udisks_loop-setup_$(basename "${ISOFILE}").log |
|
mountlog=/tmp/udisks_mount_$(basename "${ISOFILE}").log |
|
|
|
if ! ( [ ! -d "/media/$(whoami)" ] && ! find "/media/$(whoami)" -maxdepth 1 -type d -name "*${majorversion}.${minorversion}.${incrementalversion}*${architecture}*" ) |
|
then |
|
if udisksctl loop-setup --read-only --file="${ISOFILE}" | tee "${looplog}" |
|
then |
|
blockdevice=$( cut --delimiter=' ' --fields=5 "${looplog}" | cut --delimiter='.' --fields=1 ) |
|
devicename=$( echo "${blockdevice}" | cut --delimiter='/' --fields=3 ) |
|
else |
|
echo "ERROR : loop-setup" |
|
exit 1 |
|
fi |
|
if [[ ! "${blockdevice}" =~ ^/dev/loop*$ ]] && lsblk | grep "^${devicename}" |
|
then |
|
if udisksctl mount --block-device "${blockdevice}p1" | tee "${mountlog}" |
|
then |
|
if ! grep --quiet "^Mounted" "${mountlog}" |
|
then |
|
echo "ERROR : mount" |
|
exit 1 |
|
fi |
|
else |
|
echo "ERROR : mount" |
|
exit 1 |
|
fi |
|
else |
|
echo "ERROR : loop-setup" |
|
echo "It may not work by ssh or systemd-run because of polkit" |
|
echo "See https://github.com/coldfix/udiskie/wiki/Permissions" |
|
exit 1 |
|
fi |
|
fi |
|
|
|
# MOUNTPATH=$( find "/media/$(whoami)" -maxdepth 1 -type d -name "*${majorversion}.${minorversion}.${incrementalversion}*${architecture}*" ) |
|
|
|
if [ -n "${http_proxy}" ] |
|
then |
|
proxyoption="-e http_proxy=${http_proxy}" |
|
else |
|
proxyoption="" |
|
fi |
|
cat > "${HOME}"/.jigdo-lite <<EOF |
|
jigdo='' |
|
debianMirror='${MIRROR}/debian/' |
|
nonusMirror='${MIRROR}/debian-non-US/' |
|
tmpDir='${JIGDOPATH}/db' |
|
jigdoOpts='--cache jigdo-file-cache.db' |
|
wgetOpts='-nv --passive-ftp --dot-style=mega --continue --timeout=30 ${proxyoption} ' |
|
scanMenu='$( find "/media/$(whoami)" -maxdepth 1 -type d -name "*${majorversion}.${minorversion}.${incrementalversion}*${architecture}*" )/' |
|
filesPerFetch='30' |
|
EOF |
|
|
|
if jigdo-lite --noask "${jigdofilepath}" |
|
then |
|
echo "completed" |
|
else |
|
echo "Error: something was wrong" |
|
exit 1 |
|
fi |
|
|
|
} |
|
|
|
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
|
main "$@" |
|
fi
|
|
|