libreschool-pxeserver/root/srv/ipxe/check_image.sh

84 lines
2.3 KiB
Bash

#!/bin/bash
IMAGE=$1
IPXEDIR=/srv/ipxe
ISODIR=/srv/iso
MOUNTSDIR=/srv/mounts
IMAGEDIR=${IPXEDIR}/${IMAGE}
# error if image directory doesn't exists
[ ! -d "${IMAGEDIR}" ] && exit 1
# error if boot file not readable inside image directory
[ ! -r "${IMAGEDIR}/boot" ] && exit 2
# if no iso_checksum file, we need no iso, so we exit succesfully
[ ! -f "${IMAGEDIR}/iso_checksum" ] && exit 0
ISO_FILENAME=$(cat "${IMAGEDIR}/iso_checksum" | awk '{print $2;}')
# error if no filename inside iso_checksum
if [ "a$ISO_FILENAME" = "a" ] ; then
exit 3
fi
# checksum already checked and file exists, we're good
if [ -e "${ISODIR}/${ISO_FILENAME}.checksum_ok" ] ; then
if [ -r "${ISODIR}/${ISO_FILENAME}" ] ; then
exit 0
fi
fi
# checksum_ok file exists, but no iso... clear the stale file
if [ -e "${ISODIR}/${ISO_FILENAME}.checksum_ok" ] ; then
if [ ! -e "${ISODIR}/${ISO_FILENAME}" ] ; then
rm "${ISODIR}/${ISO_FILENAME}.checksum_ok"
fi
fi
# here we don't have a checksum_ok
# we have an iso, let's check it
cd "${ISODIR}"
if [ -e "${ISODIR}/${ISO_FILENAME}" ] ; then
sha256sum --quiet -c ${IMAGEDIR}/iso_checksum && touch "${ISODIR}/${ISO_FILENAME}.checksum_ok"
fi
# error if no iso url specified
if [ ! -r "${IMAGEDIR}/iso_url" ] ; then
exit 4
fi
ISO_URL=$(cat "${IMAGEDIR}/iso_url")
# the previous iso check wasn't ok, maybe an incomplete download, let's resume it
# error if download unsuccessful
if [ ! -e "${ISODIR}/${ISO_FILENAME}.checksum_ok" ] ; then
wget -c -O "${ISODIR}/${ISO_FILENAME}" "${ISO_URL}" || exit 5
# we have a complete iso now, let's check it
if [ -e "${ISODIR}/${ISO_FILENAME}" ] ; then
sha256sum --quiet -c ${IMAGEDIR}/iso_checksum && touch "${ISODIR}/${ISO_FILENAME}.checksum_ok" || rm "${ISODIR}/${ISO_FILENAME}"
# we remove the broken iso if the checksum doesn't match
fi
fi
# errore if download succesfull but checksum mismatch
if [ ! -e "${ISODIR}/${ISO_FILENAME}.checksum_ok" ] ; then
exit 6
fi
# create mount directory if doesn't exists
if [ ! -d "${MOUNTSDIR}/${ISO_FILENAME}" ] ; then
mkdir -p "${MOUNTSDIR}/${ISO_FILENAME}" || exit 7
fi
# if iso already mounted, we're good
mountpoint -q "${MOUNTSDIR}/${ISO_FILENAME}" && exit 0
# otherwise, mount the iso
mount "${ISODIR}/${ISO_FILENAME}" "${MOUNTSDIR}/${ISO_FILENAME}" || exit 8