Skip to content

Commit

Permalink
use a single partitioned loop device (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc6919 committed Nov 11, 2022
1 parent c3083ec commit 1d13a29
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 64 deletions.
32 changes: 6 additions & 26 deletions export-image/prerun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,21 @@ if [ "${NO_PRERUN_QCOW2}" = "0" ]; then
parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"

PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)

ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)

echo "Mounting BOOT_DEV..."
cnt=0
until BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}"); do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for BOOT_DEV. Retrying..."
sleep 5
else
echo "ERROR: losetup for BOOT_DEV failed; exiting"
exit 1
fi
done

echo "Mounting ROOT_DEV..."
echo "Creating loop device..."
cnt=0
until ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}"); do
until LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for ROOT_DEV. Retrying..."
echo "Error in losetup. Retrying..."
sleep 5
else
echo "ERROR: losetup for ROOT_DEV failed; exiting"
echo "ERROR: losetup failed; exiting"
exit 1
fi
done

echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
BOOT_DEV="${LOOP_DEV}p1"
ROOT_DEV="${LOOP_DEV}p2"

ROOT_FEATURES="^huge_file"
for FEATURE in 64bit; do
Expand Down
32 changes: 6 additions & 26 deletions export-noobs/prerun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,21 @@ unmount_image "${IMG_FILE}"

rm -rf "${NOOBS_DIR}"

PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)

ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)

echo "Mounting BOOT_DEV..."
cnt=0
until BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}"); do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for BOOT_DEV. Retrying..."
sleep 5
else
echo "ERROR: losetup for BOOT_DEV failed; exiting"
exit 1
fi
done

echo "Mounting ROOT_DEV..."
echo "Creating loop device..."
cnt=0
until ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}"); do
until LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for ROOT_DEV. Retrying..."
echo "Error in losetup. Retrying..."
sleep 5
else
echo "ERROR: losetup for ROOT_DEV failed; exiting"
echo "ERROR: losetup failed; exiting"
exit 1
fi
done

echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
BOOT_DEV="${LOOP_DEV}p1"
ROOT_DEV="${LOOP_DEV}p2"

mkdir -p "${STAGE_WORK_DIR}/rootfs"
mkdir -p "${NOOBS_DIR}"
Expand Down
20 changes: 8 additions & 12 deletions scripts/common
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,15 @@ export -f unmount
unmount_image(){
sync
sleep 1
local LOOP_DEVICES
LOOP_DEVICES=$(losetup --list | grep "$(basename "${1}")" | cut -f1 -d' ')
for LOOP_DEV in ${LOOP_DEVICES}; do
if [ -n "${LOOP_DEV}" ]; then
local MOUNTED_DIR
MOUNTED_DIR=$(mount | grep "$(basename "${LOOP_DEV}")" | head -n 1 | cut -f 3 -d ' ')
if [ -n "${MOUNTED_DIR}" ] && [ "${MOUNTED_DIR}" != "/" ]; then
unmount "$(dirname "${MOUNTED_DIR}")"
LOOP_DEVICE=$(losetup --list | grep "$1" | cut -f1 -d' ')
if [ -n "$LOOP_DEVICE" ]; then
for part in "$LOOP_DEVICE"p*; do
if DIR=$(findmnt -n -o target -S "$part"); then
unmount "$DIR"
fi
sleep 1
losetup -d "${LOOP_DEV}"
fi
done
done
losetup -d "$LOOP_DEVICE"
fi
}
export -f unmount_image

Expand Down

0 comments on commit 1d13a29

Please sign in to comment.