Skip to content

Commit c47f3e4

Browse files
committed
compression
1 parent aa396bb commit c47f3e4

File tree

1 file changed

+100
-9
lines changed

1 file changed

+100
-9
lines changed

mount.sh

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,27 @@ if [ "$ImgType" == "Canonical" ]; then
175175

176176
fi
177177

178-
# Remount after expansion
179-
echo "=== Remounting after expansion ==="
180-
sudo mount -o rw,loop,offset=$OFFSET "$ROOTFS_IMG" ./rootfs
181-
182178
if [ "$rubik" != true ]; then
179+
# First, attach as loop device with partition scan
180+
loopdev=$(sudo losetup --find --show --partscan "$ROOTFS_IMG")
181+
echo "Attached loop device for resize: ${loopdev}"
182+
183183
# Resize the filesystem to fill the partition
184-
echo "=== Resizing filesystem ==="
185-
sudo resize2fs "$(losetup -j "$ROOTFS_IMG" | cut -d: -f1 | head -n1)"
184+
echo "=== Resizing filesystem to fill partition ==="
185+
sudo e2fsck -f -y "${loopdev}p2" || [ $? -eq 1 ]
186+
sudo resize2fs "${loopdev}p2"
187+
188+
# Detach the loop device
189+
sudo losetup -d "${loopdev}"
190+
191+
# Now remount for the chroot work
192+
echo "=== Remounting after expansion ==="
193+
sudo mount -o rw,loop,offset=$OFFSET "$ROOTFS_IMG" ./rootfs
194+
else
195+
# For rubik images, just remount
196+
echo "=== Remounting after expansion ==="
197+
sudo mount -o rw,loop,offset=$OFFSET "$ROOTFS_IMG" ./rootfs
186198
fi
187-
elif [ "$ImgType" == "CustomIDE" ]; then
188-
echo "Image already customized, no expansion needed"
189-
fi
190199

191200
rm -f ImgType
192201

@@ -236,6 +245,88 @@ sudo chroot rootfs /bin/bash -c "
236245
fi
237246
"
238247

248+
echo "=== Resizing images to minimum ==="
249+
if [ "$rubik" != true ]; then
250+
# Attach loop device with partition scan so we can operate on the partition device
251+
LOOP_DEV=$(sudo losetup --find --show --partscan "$ROOTFS_IMG")
252+
echo "Attached loop device: $LOOP_DEV"
253+
254+
# Partition device (most systems create ${LOOP_DEV}p2)
255+
PART_DEV="${LOOP_DEV}p2"
256+
if [ ! -b "$PART_DEV" ]; then
257+
# Fallback: try to discover the partition device via lsblk
258+
PART_DEV=$(lsblk -ln -o PATH,TYPE "${LOOP_DEV}" | awk '$2=="part" {print $1; exit}')
259+
fi
260+
261+
if [ -z "$PART_DEV" ] || [ ! -b "$PART_DEV" ]; then
262+
echo "Error: could not find partition device for $LOOP_DEV"
263+
sudo losetup -d "$LOOP_DEV" || true
264+
else
265+
echo "Operating on partition device: $PART_DEV"
266+
267+
# Ensure filesystem is consistent
268+
sudo e2fsck -f -y "$PART_DEV" || [ $? -eq 1 ]
269+
270+
# Check for size mismatch and fix if needed
271+
set +e
272+
sudo e2fsck -f -y "$PART_DEV" 2>&1 | grep -q "Either the superblock or the partition table"
273+
if [ $? -eq 0 ]; then
274+
echo "Detected filesystem/partition size mismatch, forcing filesystem to partition size"
275+
# Get actual partition size in blocks
276+
PART_BLOCKS=$(sudo blockdev --getsz "$PART_DEV")
277+
PART_BLOCKS=$((PART_BLOCKS / 8)) # Convert 512-byte sectors to 4K blocks
278+
sudo resize2fs -f "$PART_DEV" ${PART_BLOCKS}
279+
# Run e2fsck again to verify
280+
sudo e2fsck -f -y "$PART_DEV" || [ $? -eq 1 ]
281+
fi
282+
set -e
283+
284+
# Shrink filesystem to its minimum size
285+
sudo resize2fs -M "$PART_DEV"
286+
287+
# Determine filesystem size (blocks * block size) in bytes
288+
blk_count=$(sudo dumpe2fs -h "$PART_DEV" 2>/dev/null | awk -F: '/Block count/ {gsub(/ /,"",$2); print $2}')
289+
blk_size=$(sudo dumpe2fs -h "$PART_DEV" 2>/dev/null | awk -F: '/Block size/ {gsub(/ /,"",$2); print $2}')
290+
if [ -n "$blk_count" ] && [ -n "$blk_size" ]; then
291+
required_bytes=$((blk_count * blk_size))
292+
echo "Filesystem minimum: $required_bytes bytes"
293+
294+
# Get partition start (in bytes)
295+
start_byte=$(sudo parted -s "$LOOP_DEV" unit B print | awk '/^ 2/ {gsub("B","",$2); print $2}')
296+
if [ -z "$start_byte" ]; then
297+
echo "Error: could not determine partition start"
298+
else
299+
# Add a small slack (1 MiB) to avoid off-by-one issues
300+
slack=1048576
301+
new_end=$((start_byte + required_bytes + slack))
302+
echo "Resizing partition 2 to end at ${new_end} bytes (start ${start_byte})"
303+
304+
# Resize partition to fit the shrunken filesystem (+slack)
305+
# Parted wants confirmation, that's why we use the yes pipe here
306+
yes | sudo parted ---pretend-input-tty "$LOOP_DEV" unit B resizepart 2 "${new_end}B" || true
307+
308+
# Inform kernel of partition table change
309+
sudo partprobe "$LOOP_DEV" || true
310+
311+
# Detach loop device before truncating the backing file
312+
sudo losetup -d "$LOOP_DEV"
313+
LOOP_DEV=""
314+
315+
# Truncate the image file to the new end (round up to 512-byte sector)
316+
sector_size=512
317+
last_sector=$(((new_end + sector_size - 1) / sector_size))
318+
new_image_size=$((last_sector * sector_size))
319+
echo "Truncating image to ${new_image_size} bytes"
320+
sudo truncate -s "$new_image_size" "$ROOTFS_IMG"
321+
fi
322+
else
323+
echo "Warning: could not determine filesystem block info; leaving image size unchanged"
324+
sudo losetup -d "$LOOP_DEV" || true
325+
LOOP_DEV=""
326+
fi
327+
fi
328+
fi
329+
239330
# Cleanup mounts
240331
sudo umount rootfs/dev || true
241332
sudo umount rootfs/run || true

0 commit comments

Comments
 (0)