Shrinking an ext4 File System and the Logical Volume Containing It

The following procedure will reduce both the ext4 file system and logical volume sizes by 10 GB. Initially the file system uses the entire size of the logical volume. Obviously, there needs to be at least 10 GB of disk space currently available. This procedure needs to be done with the file system unmounted. This should also work fine on ext2 and ext3.

root@ubuntu:/tmp# df -h /dev/mapper/mysql_s1_prod1_raid10-data
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/mysql_s1_prod1_raid10-data
                      241G  647M  228G   1% /local/mysql-s1-prod1_data
root@ubuntu:/tmp# umount /local/mysql-s1-prod1_data
root@ubuntu:/tmp# pvscan
  PV /dev/mapper/mysql-s1-prod0-t1-v1_fujitsu2-27   VG mysql_s1_prod0_raid1    lvm2 [78.12 GiB / 0    free]
  PV /dev/mapper/mysql-s1-prod1-t1-v1_fujitsu2-32   VG mysql_s1_prod1_raid1    lvm2 [78.12 GiB / 0    free]
  PV /dev/mapper/mysql-s1-prod0-t1-v2_fujitsu2-28   VG mysql_s1_prod0_raid10   lvm2 [244.14 GiB / 10.00 GiB free]
  PV /dev/mapper/mysql-s1-prod1-t1-v2_fujitsu2-35   VG mysql_s1_prod1_raid10   lvm2 [244.14 GiB / 0    free]
  PV /dev/sda2                                      VG system                  lvm2 [135.84 GiB / 102.32 GiB free]
  Total: 5 [780.36 GiB] / in use: 5 [780.36 GiB] / in no VG: 0 [0   ]
root@ubuntu:/tmp# lvscan
  ACTIVE            '/dev/mysql_s1_prod0_raid1/data' [78.12 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod1_raid1/data' [78.12 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod0_raid10/data' [234.14 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod1_raid10/data' [244.14 GiB] inherit
  ACTIVE            '/dev/system/root' [9.31 GiB] inherit
  ACTIVE            '/dev/system/var' [4.66 GiB] inherit
  ACTIVE            '/dev/system/tmp' [2.79 GiB] inherit
  ACTIVE            '/dev/system/swap' [2.79 GiB] inherit
  ACTIVE            '/dev/system/home' [4.66 GiB] inherit
  ACTIVE            '/dev/system/opt' [9.31 GiB] inherit
root@ubuntu:/tmp# e2fsck -fy /dev/mysql_s1_prod1_raid10/data
e2fsck 1.41.11 (14-Mar-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
/dev/mysql_s1_prod1_raid10/data: 127/16007168 files (2.4% non-contiguous), 1170118/63998976 blocks
root@ubuntu:/tmp# resize2fs -Mp /dev/mapper/mysql_s1_prod1_raid10-data
resize2fs 1.41.11 (14-Mar-2010)
Resizing the filesystem on /dev/mapper/mysql_s1_prod1_raid10-data to 291412 (4k) blocks.
Begin pass 2 (max = 88941)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 1954)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 11)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/mysql_s1_prod1_raid10-data is now 291412 blocks long.
root@ubuntu:/tmp# e2fsck -fy /dev/mysql_s1_prod1_raid10/data
e2fsck 1.41.11 (14-Mar-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
/dev/mysql_s1_prod1_raid10/data: 127/73728 files (2.4% non-contiguous), 160138/291412 blocks
root@ubuntu:/tmp# lvreduce -L -10G /dev/mapper/mysql_s1_prod1_raid10-data
  WARNING: Reducing active logical volume to 234.14 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce data? [y/n]: y
  Reducing logical volume data to 234.14 GiB
  Logical volume data successfully resized
root@ubuntu:/tmp# resize2fs /dev/mapper/mysql_s1_prod1_raid10-data
resize2fs 1.41.11 (14-Mar-2010)
Please run 'e2fsck -f /dev/mapper/mysql_s1_prod1_raid10-data' first.
root@ubuntu:/tmp# resize2fs /dev/mapper/mysql_s1_prod1_raid10-data
resize2fs 1.41.11 (14-Mar-2010)
Resizing the filesystem on /dev/mapper/mysql_s1_prod1_raid10-data to 61377536 (4k) blocks.
The filesystem on /dev/mapper/mysql_s1_prod1_raid10-data is now 61377536 blocks long.
root@ubuntu:/tmp# e2fsck -fy /dev/mysql_s1_prod1_raid10/data
e2fsck 1.41.11 (14-Mar-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
/dev/mysql_s1_prod1_raid10/data: 127/15351808 files (2.4% non-contiguous), 1128998/61377536 blocks
root@ubuntu:/tmp# df -h /dev/mapper/mysql_s1_prod1_raid10-data
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/mysql_s1_prod1_raid10-data
                      231G  647M  219G   1% /local/mysql-s1-prod1_data
root@ubuntu:/tmp# pvscan
  PV /dev/mapper/mysql-s1-prod0-t1-v1_fujitsu2-27   VG mysql_s1_prod0_raid1    lvm2 [78.12 GiB / 0    free]
  PV /dev/mapper/mysql-s1-prod1-t1-v1_fujitsu2-32   VG mysql_s1_prod1_raid1    lvm2 [78.12 GiB / 0    free]
  PV /dev/mapper/mysql-s1-prod0-t1-v2_fujitsu2-28   VG mysql_s1_prod0_raid10   lvm2 [244.14 GiB / 10.00 GiB free]
  PV /dev/mapper/mysql-s1-prod1-t1-v2_fujitsu2-35   VG mysql_s1_prod1_raid10   lvm2 [244.14 GiB / 10.00 GiB free]
  PV /dev/sda2                                      VG system                  lvm2 [135.84 GiB / 102.32 GiB free]
  Total: 5 [780.36 GiB] / in use: 5 [780.36 GiB] / in no VG: 0 [0   ]
root@ubuntu:/tmp# lvscan
  ACTIVE            '/dev/mysql_s1_prod0_raid1/data' [78.12 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod1_raid1/data' [78.12 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod0_raid10/data' [234.14 GiB] inherit
  ACTIVE            '/dev/mysql_s1_prod1_raid10/data' [234.14 GiB] inherit
  ACTIVE            '/dev/system/root' [9.31 GiB] inherit
  ACTIVE            '/dev/system/var' [4.66 GiB] inherit
  ACTIVE            '/dev/system/tmp' [2.79 GiB] inherit
  ACTIVE            '/dev/system/swap' [2.79 GiB] inherit
  ACTIVE            '/dev/system/home' [4.66 GiB] inherit
  ACTIVE            '/dev/system/opt' [9.31 GiB] inherit

Leave a Reply

Your email address will not be published. Required fields are marked *