Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux resize2fs command

Linux resize2fs command

Linux Command Reference Linux Command Reference


resize2fs is a practical tool in Linux for resizing ext2/ext3/ext4 filesystems. It can expand or shrink a filesystem without data loss, and is typically used together with LVM (Logical Volume Management) or disk partitioning tools.


Command syntax

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

Parameter Description

Parameters Description
Device Specify the device holding the filesystem to resize (such as /dev/sda1)
new size optional argument specifying the new size of the filesystem (units can be K/M/G)

Common options

Option Description
-d Display debugging information
-f Force the operation
-p display a progress bar
-M shrink the filesystem to the smallest possible size
-P print the smallest possible size of the filesystem without performing the resize

Use Cases

Scenario 1: Expand a filesystem

After the underlying storage device (partition/LVM) has been expanded, the filesystem needs to be resized to use the new space:

Example

1. First expand the underlying device (partition or LVM)

2. Then resize the filesystem

resize2fs

/

dev

/

vg01

/

lv01

Scenario 2: Shrink a filesystem

Example

1. First shrink the filesystem (note: the filesystem must 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: Expand an ext4 filesystem

Example

Check the current filesystem size

df

-h

/

mnt

/

data

Expand the LVM logical volume (assuming space is already available)

lvextend

-L

+5G

/

dev

/

vg01

/

lv01

Resize the filesystem

resize2fs

/

dev

/

vg01

/

lv01

Verify the new size

df

-h

/

mnt

/

data

Example 2: Shrink an ext4 filesystem

Example

Unmount the filesystem

umount

/

mnt

/

data

Force a filesystem check

e2fsck

-f

/

dev

/

vg01

/

lv01

Shrink the filesystem to 15GB

resize2fs

/

dev

/

vg01

/

lv01 15G

Shrink the LVM logical volume (note: it must be slightly larger than the filesystem)

lvreduce

-L

15.5G

/

dev

/

vg01

/

lv01

Remount

mount

/

dev

/

vg01

/

lv01

/

mnt

/

data


Notes

  1. Data backup: always back up important data before resizing a filesystem
  2. Filesystem check: must be run before a shrink operation e2fsck -f
  3. Order of operations:
  4. When expanding: expand the device first, then the filesystem
  5. When shrinking: shrink the filesystem first, then the device
  6. - When expanding: expand the device first, then the filesystem - When shrinking: shrink the filesystem first, then the device
    1. Online resizing: in some cases the filesystem does not need to be unmounted, but unmounting is recommended for safer operation
    2. Minimum size: When using -P option to view the smallest size to which the filesystem can be shrunk

    Frequently Asked Questions

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

    A: resize2fs is a tool designed specifically for ext2/ext3/ext4 filesystems. Other filesystems such as XFS and Btrfs have their own resizing tools.

    Q2: Does a resize operation lose data?

    A: Correct operation normally does not lose data, but any disk operation carries risk, so backing up first is strongly recommended.

    Q3: How do I know the smallest size a filesystem can be shrunk to?

    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 filesystem]

        B -->|Shrink| E[Unmount the filesystem]

        E --> F[Run e2fsck check]

        F --> G[Run resize2fs to shrink the filesystem]

        G --> H[Shrink the underlying storage device]

        D & H --> I[Verify the new size]

        I --> J[End]


    Summary

    resize2fs is a powerful tool for managing the size of ext filesystems; used properly, it can flexibly adjust storage space. Remember the order of operations and the precautions, and you can complete filesystem resizing tasks safely and efficiently.


    Linux Command Reference Linux Command Reference