resize2fs

ext2/ext3/ext4 file system resizer

sudo resize2fs [ -fFpPMbs ] [ -d debug-flags ] [ -S RAID-stride ] [ -z undo_file ] device [ newSize ]

Resizes ext2, ext3, or ext4 unmounted file system. (Run from /etc/init.d to resize root fs)
Can expand the size even if the filesystem is mounted,

newSize can be suffixed by s, K, M, or G, for 512 byte sectors, kilobytes, …, can not be larger than the size of the partition.
Default: blocksize of the filesystem.
Defaults to the size of the partition.

Does not change the size of the partition.
If using the logical volume manager lvm use lvextend.

To shrink an ext partition, first use resize2fs to shrink the size of filesystem(not online).
Then use parted or fdisk to shrink the size of the partition. Do not make it smaller than the new size of the filesystem!

Attempt to shrink root on raspberry pi buster from 6GB:

sudo resize2fs -F  /dev/mmcblk0p2 5500M
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
resize2fs: On-line shrinking not supported
On another system:
df                  # determine where SD card is mounted
sudo umount sdc*    # 
sudo e2fsck -f /dev/sdc2 #  requested by resize2fs
sudo resize2fs -F -d63 -p /dev/sdc2 5500M   # Flush buffers, debug flags

# get size of new filesystem in sectors
#echo `sudo dumpe2fs /dev/sdc2 |grep "Block count" *   # size of filesystem in Block size (4096)
# plus start

sudo parted /dev/sdc print unit s resizepart 2 sssss  # From (actually end = size -1  but hey!)
sudo parted /dev/sdc print unit Mi resizepart 2 5760  # From resize2fs start + size


Enlarge example:

    sudo  fdisk /dev/mmcblk0
    F                           # show Free space

    sudo parted /dev/mmcblk0 unit G print resizepart 2 END-Gbytes

    /bin/df -m
    Filesystem     1M-blocks  Used Available Use% Mounted on
    /dev/root           4960  4548       142  97% / 

 sudo fdisk /dev/mmcblk0
    Device         Boot    Start      End  Sectors  Size Id Type
    /dev/mmcblk0p2         98304 12551295 12452992    6G 83 Linux     5.93GB 

 sudo resize2fs -d16 -p /dev/mmcblk0p2 
    resize2fs 1.44.5 (15-Dec-2018)
    Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks = 1
    The filesystem on /dev/mmcblk0p2 is now 1556624 (4k) blocks long.

 /bin/df -m
    Filesystem     1M-blocks  Used Available Use% Mounted on
    /dev/root           5921  4548      1064  82% /
-P Print the number of file system blocks in the file system if it is shrunk and exit.
 sudo resize2fs -d63 -P /dev/mmcblk0p2
resize2fs 1.44.5 (15-Dec-2018)
fs has 61227 inodes, 8 groups required.
fs requires 598282 data blocks.
With 8 group(s), we have 253350 blocks available.
Added 11 extra group(s), blks_needed 598282, data_blocks 605480, last_start 322035
Last group's overhead is 7308
Need 276247 data blocks in last group
Final size of last group is 283555
Estimated blocks needed: 873379
Extents safety margin: 2001
Estimated minimum size of the filesystem: 875380

sudo resize2fs -P /dev/mmcblk0p2 
resize2fs 1.44.5 (15-Dec-2018)
 Estimated minimum size of the filesystem: 1,306,624
after raspberrypi buster install:
resize2fs 1.44.5 (15-Dec-2018)
Estimated minimum size of the filesystem: 470,820 
-p Prints out a percentage completion bars for each operation during an offline resize,
-M Minimize the file system size
 On-line shrinking not supported
-d flagsdebug flags. Computed by adding the numbers of the flags (all 2+4+8+16+32= 3E = 62
  •  2 - block relocations
  •  4 - inode relocations
  •  8 - moving the inode table
  • 16 - Print timing information
  • 32 - minimum filesystem size calculation
  • -f Forces overriding some safety checks
    -F Flush the filesystem device's buffer caches before beginning. Useful for doing time trials.
    -b Turns on  the 64bit feature, resizes the group descriptors , and moves other metadata
    -s Turns off the 64bit feature and frees blocks that are no longer in use.
    -S RAID-stride heuristically determines the RAID stride specified when the filesystem was created. Allows explicitl specifying a RAID stride setting to be used by resize2fs instead.
    -z undo_file Before overwriting a file system block, save the old contents .
    Used with e2undo(8) to restore the old contents of the file system.
    Default resize2fs-device.e2undo in the directory specified via $E2FSPROGS_UNDO_DIR .

    Cannot be used to recover from a power fail. reset or system crash.

    See: dumpe2fs, fdisk, parted, e2fsck(8), mke2fs(8), lvm(8), lvextend(8)