Linux resize2fs Command

Linux 命令大全Linux Command Collection


resize2fsis a utility in Linux systems for resizing ext2/ext3/ext4 file systems. It can expand or shrink the file system without losing data, and is usually used withLVM(Logical Volume Management) or disk partitioning tools.


Command Syntax

resize2fs [选项] 设备 [新大小]

Parameter Description

Parameter Description
Device Specify the device on which the file system to be resized resides (e.g./dev/sda1)
New Size Optional parameter, specifies the new size of the file system (unit can be K/M/G)

Common Options

Option Description
-d Display debug information
-f Force the operation
-p Display progress bar
-M Shrink the file system to the minimum possible size
-P Print the minimum possible size of the file system without performing the resize

Usage Scenarios

Scenario 1: Expanding the File System

After the underlying storage device (partition/LVM) has been expanded, the file system needs to be resized to use the newly added space:

Example

# 1. First expand the underlying device (partition or LVM)
# 2. Then resize the file system
resize2fs /dev/vg01/lv01

Scenario 2: Shrinking the File System

Example

# 1. First shrink the file system (Note: the file system needs to be unmounted first)
umount /mnt/data
e2fsck -f /dev/vg01/lv01
resize2fs /dev/vg01/lv01 10G
# 2. Then shrink the underlying device

Detailed Examples

Example 1: Expanding an ext4 File System

Example

# Check the current file system size
df -h /mnt/data

# Expand the LVM logical volume (assuming space has been prepared)
lvextend -L +5G /dev/vg01/lv01

# Resize the file system
resize2fs /dev/vg01/lv01

# Verify the new size
df -h /mnt/data

Example 2: Shrinking an ext4 File System

Example

# Unmount the file system
umount /mnt/data

# Force a file system check
e2fsck -f /dev/vg01/lv01

# Shrink the file system to 15GB
resize2fs /dev/vg01/lv01 15G

# Shrink the LVM logical volume (Note: it should be slightly larger than the file system)
lvreduce -L 15.5G /dev/vg01/lv01

# Remount
mount /dev/vg01/lv01 /mnt/data

Notes

  1. Data backup: Be sure to back up important data before resizing the file system
  2. File system check: Must be run before the shrinking operatione2fsck -f
  3. Order issue:
    • When expanding: first expand the device, then expand the file system
    • When shrinking: first shrink the file system, then shrink the device
  4. Online resizing: In some cases, the file system does not need to be unmounted, but it is recommended to unmount it for safer operation
  5. Minimum size: Use the-Poption to view the minimum size to which the file system can be shrunk

FAQ

Q1: Why can't resize2fs resize non-ext file systems?

A: resize2fsis a tool specifically designed for ext2/ext3/ext4 file systems. Other file systems such as XFS, Btrfs, etc. have their own resize tools.

Q2: Will the resize operation lose data?

A: Correct operation usually does not lose data, but any disk operation carries risks. It is strongly recommended to back up first.

Q3: How do I know the minimum size to which the file system can be shrunk?

A: Use the command:resize2fs -P /dev/your_device


Flowchart: resize2fs Workflow

Example

graph TD
A[Start] --> B{Operation type}
B -->|Expand| C[Expand underlying storage device]
C --> D[Run resize2fs to expand the file system]
B -->|Shrink| E[Unmount the file system]
E --> F[Run e2fsck check]
F --> G[Run resize2fs to shrink the file system]
G --> H[Shrink the underlying storage device]
D & H --> I[Verify new size]
I --> J[End]

Summary

resize2fsis a powerful tool for managing ext file system sizes. With reasonable use, you can flexibly adjust storage space. Remember the operation order and precautions to safely and efficiently complete file system resizing tasks.


Linux 命令大全Linux Command Collection

Other Extensions