From 46322535c1020fb4ee8392e540f5b65740842d30 Mon Sep 17 00:00:00 2001 From: Eric Hameleers Date: Sun, 7 Jan 2024 19:46:43 +0100 Subject: liveinit: move partition scan into a new function for potential reuse --- liveinit.tpl | 56 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/liveinit.tpl b/liveinit.tpl index e00d984..cc292a9 100644 --- a/liveinit.tpl +++ b/liveinit.tpl @@ -797,6 +797,37 @@ if [ "$RESCUE" = "" ]; then fi } + # Find partition on which a file resides: + # Function input: + # (param 1) Full path to the file we are looking for + # (param 2) Directory to mount the partition containing our file + # Use $(df $MYMNT |tail -1 |tr -s ' ' |cut -d' ' -f1) to find that partition, + # it will remain mounted on the provided mountpoint upon function return. + scan_part() { + local FILEPATH="$1" + local MYMNT="$2" + local ISOPART="" + local PARTFS="" + echo "${MARKER}: Scanning for '$FILEPATH'..." + for ISOPART in $(ret_partition $(blkid |cut -d: -f1)) $(ret_blockdev $(blkid |cut -d: -f1)) ; do + PARTFS=$(blkid $ISOPART |rev |cut -d'"' -f2 |rev) + mount -t $PARTFS -o ro $ISOPART ${MYMNT} + if [ -f "${MYMNT}/${FILEPATH}" ]; then + # Found our file! + unset ISOPART + break + else + umount $ISOPART + fi + done + if [ -n "$ISOPART" ]; then + echo "${MARKER}: Partition scan unable to find $(basename $FILEPATH), trouble ahead." + return 1 + else + return 0 + fi + } # End scan_part() + ## End support functions ## # We need a mounted filesystem here to be able to do a switch_root later, @@ -958,25 +989,12 @@ if [ "$RESCUE" = "" ]; then mkdir -p ${SUPERMNT} # if [ "$LIVEMEDIA" = "scandev" ]; then - # Scan partitions to find the one with the ISO and set LIVEMEDIA: - echo "${MARKER}: Scanning for '$LIVEPATH'..." - for ISOPART in $(ret_partition $(blkid |cut -d: -f1)) $(ret_blockdev $(blkid |cut -d: -f1)) ; do - PARTFS=$(blkid $ISOPART |rev |cut -d'"' -f2 |rev) - # Abuse the $SUPERMNT a bit, we will actually use it later: - mount -t $PARTFS -o ro $ISOPART ${SUPERMNT} - if [ -f ${SUPERMNT}/${LIVEPATH} ]; then - # Found our ISO! - LIVEMEDIA=$ISOPART - umount $ISOPART - unset ISOPART - break - else - umount $ISOPART - fi - done - if [ -n "$ISOPART" ]; then - echo "${MARKER}: Partition scan unable to find ISO, trouble ahead." - fi + # Scan partitions to find the one with the ISO and set LIVEMEDIA. + # Abuse the $SUPERMNT a bit, we will actually use it later. + # TODO: proper handling of scan_part return code. + scan_part ${LIVEPATH} ${SUPERMNT} + LIVEMEDIA="$(df ${SUPERMNT} 2>/dev/null |tail -1 |tr -s ' ' |cut -d' ' -f1)" + umount ${SUPERMNT} fi # At this point we know $LIVEMEDIA - either because the bootparameter # specified it or else because the 'scandev' found it for us. -- cgit v1.2.3