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 " 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)"
|
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
|
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..."
|
info "Running e2fsck on $S_DEV..."
|
||||||
e2fsck -f "$S_DEV" || die "e2fsck found errors on $S_DEV. Fix them before retrying."
|
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."
|
resize2fs "$S_DEV" "${NEW_FS_MIB}M" || die "resize2fs failed."
|
||||||
|
|
||||||
elif [[ "$S_FS" == "btrfs" ]]; then
|
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..."
|
info "Shrinking btrfs filesystem on $S_DEV to ${NEW_FS_MIB} MiB..."
|
||||||
TMP_MNT=$(mktemp -d /tmp/oemdrv_btrfs.XXXXXX)
|
if [[ "$S_MNT" != "-" && -n "$S_MNT" ]]; then
|
||||||
mount "$S_DEV" "$TMP_MNT" || { rmdir "$TMP_MNT"; die "Cannot mount $S_DEV for btrfs resize."; }
|
btrfs filesystem resize "${NEW_FS_MIB}m" "$S_MNT" \
|
||||||
btrfs filesystem resize "${NEW_FS_MIB}m" "$TMP_MNT" \
|
|| die "btrfs filesystem resize failed."
|
||||||
|| { umount "$TMP_MNT"; rmdir "$TMP_MNT"; die "btrfs filesystem resize failed."; }
|
else
|
||||||
sync
|
TMP_MNT=$(mktemp -d /tmp/oemdrv_btrfs.XXXXXX)
|
||||||
umount "$TMP_MNT" && rmdir "$TMP_MNT"
|
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
|
fi
|
||||||
|
|
||||||
info "Shrinking partition $S_PNUM to ${NEW_END} MiB in partition table..."
|
info "Shrinking partition $S_PNUM to ${NEW_END} MiB in partition table..."
|
||||||
|
|||||||
Reference in New Issue
Block a user