The  resize2fs  program  will  resize ext2, ext3, or ext4 file systems.  It can be used to enlarge or shrink an unmounted file system located on device.  Kernels early than 2.6 need filesystem umounted to do the resize, however, since the 2.6 kernel, linux supports on-line resize for filesystems  mounted  using  ext3  and ext4.).

Here is just few quick examples:

Create a test filesystem

In examle below, I will just the filesystem resizing with lvm, using the tratidition fdisk created partition are mostly same.

# lvcreate -L 10g vg0
  Logical volume "lvol0" created
# mkfs -L testfs /dev/mapper/vg0-lvol0             
mke2fs 1.41.12 (17-May-2010)
Filesystem label=testfs
...
# mkdir /home/testfs
# mount /dev/mapper/vg0-lvol0 /home/testfs
# df -l /home/testfs
Filesystem           1K-blocks     Used Available Use% Mounted on
/dev/mapper/vg0-lvol0    10321208    23028   9773892   1% /home/testfs

To increase filesystem size

To increase filesystem, the first thing need to do is increase lv(or partition) size, then increase filesystem.

# lvextend -L 20g /dev/mapper/vg0-lvol0 
  Size of logical volume vg0/lvol0 changed from 10.00 GiB (320 extents) to 20.00 GiB (640 extents).
  Logical volume lvol0 successfully resized
# resize2fs /dev/mapper/vg0-lvol0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg0-lvol0 is mounted on /home/testfs; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
resize2fs: Kernel does not support online resizing

 Oops, the filesystem was created to ext2, on line resizing is not supported.

 To increase ext2 filesystem, you need umount the filesystem, then run resize2fs.

For ext3 and ext4, they both support on-line resizing(on kernel 2.6 and higher).

# resize2fs /dev/mapper/vg0-lvol0 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg0-lvol0 is mounted on /home/testfs; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg0-lvol0 to 5242880 (4k) blocks.
The filesystem on /dev/mapper/vg0-lvol0 is now 5242880 blocks long.

To decrease filesystem size

To decrease a filesystem, decrease filesystem first, then reduce the partition accordingly, be careful not to decrease the partition too much.

Run fsck first, then resize2fs

# e2fsck -f /dev/mapper/vg0-lvol0
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
testfs: 11/1310720 files (0.0% non-contiguous), 89297/5242880 blocks

# resize2fs /dev/mapper/vg0-lvol0 10g
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg0-lvol0 to 2621440 (4k) blocks.
The filesystem on /dev/mapper/vg0-lvol0 is now 2621440 blocks long.

Reduce the lv size

# lvreduce -L 10g /dev/mapper/vg0-lvol0 
  WARNING: Reducing active logical volume to 10.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Size of logical volume vg0/lvol0 changed from 20.00 GiB (640 extents) to 10.00 GiB (320 extents).
  Logical volume lvol0 successfully resized

# e2fsck -f /dev/mapper/vg0-lvol0
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
testfs: 11/655360 files (0.0% non-contiguous), 46895/2621440 blocks

Use lvresize to resize both lv and filesystem size

To reduce filesystem to 10g

# lvresize -r -L 10g /dev/mapper/vg0-lvol0 
Do you want to unmount "/home/testfs"? [Y|n] y
fsck from util-linux-ng 2.17.2

testfs: 11/1310720 files (0.0% non-contiguous), 118578/5242880 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg0-lvol0 to 2621440 (4k) blocks.
The filesystem on /dev/mapper/vg0-lvol0 is now 2621440 blocks long.

  Size of logical volume vg0/lvol0 changed from 20.00 GiB (640 extents) to 10.00 GiB (320 extents).
  Logical volume lvol0 successfully resized

To increase the filesystem to 20g

# lvresize -r -L 20g /dev/mapper/vg0-lvol0 
  Size of logical volume vg0/lvol0 changed from 10.00 GiB (320 extents) to 20.00 GiB (640 extents).
  Logical volume lvol0 successfully resized
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg0-lvol0 is mounted on /home/testfs; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg0-lvol0 to 5242880 (4k) blocks.
The filesystem on /dev/mapper/vg0-lvol0 is now 5242880 blocks long.

 

Comments powered by CComment