Fix btrfs resize: use online resize instead of unmount/remount
btrfs supports live filesystem resize, so there is no need to unmount a mounted btrfs partition before shrinking it. This also avoids umount failures when the partition is busy (e.g. /home with an active SSH session). ext4 still requires offline resize. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+20
-13
@@ -345,14 +345,14 @@ if [[ "$MODE" == "shrink" ]]; then
|
||||
echo " After shrink : ${CURR_START} MiB – ${NEW_END} MiB (filesystem: ${NEW_FS_MIB} MiB)"
|
||||
echo " New OEMDRV : ${OEMDRV_START} MiB – ${OEMDRV_END} MiB (${SHRINK_MIB} MiB)"
|
||||
|
||||
if [[ "$S_MNT" != "-" && -n "$S_MNT" ]]; then
|
||||
info "Unmounting $S_DEV from $S_MNT..."
|
||||
ORIG_MNT="$S_MNT"
|
||||
umount "$S_DEV" || die "Cannot unmount $S_DEV. Close all open files and try again."
|
||||
WAS_MOUNTED=1
|
||||
fi
|
||||
|
||||
if [[ "$S_FS" == "ext4" ]]; then
|
||||
# ext4 requires offline resize — unmount first
|
||||
if [[ "$S_MNT" != "-" && -n "$S_MNT" ]]; then
|
||||
info "Unmounting $S_DEV from $S_MNT..."
|
||||
ORIG_MNT="$S_MNT"
|
||||
umount "$S_DEV" || die "Cannot unmount $S_DEV. Close all open files and try again."
|
||||
WAS_MOUNTED=1
|
||||
fi
|
||||
info "Running e2fsck on $S_DEV..."
|
||||
e2fsck -f "$S_DEV" || die "e2fsck found errors on $S_DEV. Fix them before retrying."
|
||||
|
||||
@@ -360,13 +360,20 @@ if [[ "$MODE" == "shrink" ]]; then
|
||||
resize2fs "$S_DEV" "${NEW_FS_MIB}M" || die "resize2fs failed."
|
||||
|
||||
elif [[ "$S_FS" == "btrfs" ]]; then
|
||||
# btrfs supports online resize — use existing mount point if available,
|
||||
# otherwise mount temporarily
|
||||
info "Shrinking btrfs filesystem on $S_DEV to ${NEW_FS_MIB} MiB..."
|
||||
TMP_MNT=$(mktemp -d /tmp/oemdrv_btrfs.XXXXXX)
|
||||
mount "$S_DEV" "$TMP_MNT" || { rmdir "$TMP_MNT"; die "Cannot mount $S_DEV for btrfs resize."; }
|
||||
btrfs filesystem resize "${NEW_FS_MIB}m" "$TMP_MNT" \
|
||||
|| { umount "$TMP_MNT"; rmdir "$TMP_MNT"; die "btrfs filesystem resize failed."; }
|
||||
sync
|
||||
umount "$TMP_MNT" && rmdir "$TMP_MNT"
|
||||
if [[ "$S_MNT" != "-" && -n "$S_MNT" ]]; then
|
||||
btrfs filesystem resize "${NEW_FS_MIB}m" "$S_MNT" \
|
||||
|| die "btrfs filesystem resize failed."
|
||||
else
|
||||
TMP_MNT=$(mktemp -d /tmp/oemdrv_btrfs.XXXXXX)
|
||||
mount "$S_DEV" "$TMP_MNT" || { rmdir "$TMP_MNT"; die "Cannot mount $S_DEV for btrfs resize."; }
|
||||
btrfs filesystem resize "${NEW_FS_MIB}m" "$TMP_MNT" \
|
||||
|| { umount "$TMP_MNT"; rmdir "$TMP_MNT"; die "btrfs filesystem resize failed."; }
|
||||
sync
|
||||
umount "$TMP_MNT" && rmdir "$TMP_MNT"
|
||||
fi
|
||||
fi
|
||||
|
||||
info "Shrinking partition $S_PNUM to ${NEW_END} MiB in partition table..."
|
||||
|
||||
Reference in New Issue
Block a user