use a single partitioned loop device (#641)

This commit is contained in:
rfc6919 2022-11-11 19:56:04 +11:00 committed by GitHub
parent c3083ecd50
commit 1d13a292a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 64 deletions

View File

@ -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..."
echo "Creating loop device..."
cnt=0
until BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_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 BOOT_DEV. Retrying..."
echo "Error in losetup. Retrying..."
sleep 5
else
echo "ERROR: losetup for BOOT_DEV failed; exiting"
echo "ERROR: losetup failed; exiting"
exit 1
fi
done
echo "Mounting ROOT_DEV..."
cnt=0
until ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}"); do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for ROOT_DEV. Retrying..."
sleep 5
else
echo "ERROR: losetup for ROOT_DEV 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

View File

@ -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..."
echo "Creating loop device..."
cnt=0
until BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_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 BOOT_DEV. Retrying..."
echo "Error in losetup. Retrying..."
sleep 5
else
echo "ERROR: losetup for BOOT_DEV failed; exiting"
echo "ERROR: losetup failed; exiting"
exit 1
fi
done
echo "Mounting ROOT_DEV..."
cnt=0
until ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}"); do
if [ $cnt -lt 5 ]; then
cnt=$((cnt + 1))
echo "Error in losetup for ROOT_DEV. Retrying..."
sleep 5
else
echo "ERROR: losetup for ROOT_DEV 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}"

View File

@ -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}")"
fi
sleep 1
losetup -d "${LOOP_DEV}"
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
done
losetup -d "$LOOP_DEVICE"
fi
}
export -f unmount_image