Shrink an ext4 filesystem in RHEL 6/Centos 6

In a previous post I demonstrated how to increase the size of an ext4 filesystem in RHEL 6/CentOS 6. In this post I will demonstrate how to shrink or reduce the size of an ext4 filesystem in RHEL 6/CentOS 6. As part of this shrink process I will also remove one of the disks in the Volume Group that the filesystem we are shrinking is part of.

My Environment

Let me briefly confirm the environment I am using for this demo.  My server is a VMware VM and the OS is Centos 6.6, it presently contains 5 virtual disks:

20150614181018

All the virtual disks are in use and the system comprises multiple filesystems. We are going to use the filesystem /var/video to demonstrate how to shrink a filesystem.

The Shrink Process

Please note that you should stop any applications first and then unmount the filesystem before proceeding with the steps below. I would also ensure that you back up the system prior to any shrinking activity.

The process required to reduce a filesystem starts at the top layer, with the filesystem itself. It must be reduced, then the Logical Volume is reduced. If required the Volume Group can also be reduced if the ultimate goal is to remove Physical Volumes from the system.  The order of this process is shown below:

Filesystems --> Logical Volumes --> Volume Groups --> Physical Volumes

So without further ado, let’s crack on with the shrinking process.

1. Check the current size of the filesystem by running df -h:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  908M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G  4.4G  4.9G  48% /var/music
/dev/mapper/data-video
                      9.8G  4.4G  4.9G  47% /var/video
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
[root@lnx-svr-01 ~]#

To see the usage in Megabytes run df -m:

[root@lnx-svr-01 ~]# df -m
Filesystem           1M-blocks  Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                         17519   908     15715   6% /
tmpfs                      939     0       939   0% /dev/shm
/dev/sda1                  477    25       427   6% /boot
/dev/mapper/data-music
                          9952  4441      4999  48% /var/music
/dev/mapper/data-pictures
                          4888    10      4624   1% /var/pictures
/dev/mapper/data-video
                          9944  4430      5003  47% /var/video
[root@lnx-svr-01 ~]#

We can see that /var/video is 9.8GB in size and is using 47% of its total capacity. We want to reduce /var/video down to 4.5GB in size.

2. Run e2fsk first to perform a filesystem check:

[root@lnx-svr-01 ~]# e2fsck -f /dev/data/video
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
/dev/data/video: 12/655360 files (0.0% non-contiguous), 1207792/2619392 blocks

3. Run resize2fs to shrink the filesystem to your desired size:

[root@lnx-svr-01 ~]# resize2fs /dev/data/video 4800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/data/video to 1228800 (4k) blocks.
The filesystem on /dev/data/video is now 1228800 blocks long.

I have resized the device /dev/data/video to 4800MB. To change the unit of size to Gigabytes change M to G.

4. Run lvreduce to shrink the Logical Volume to a size slightly larger than the filesystem. In step 3 we shrunk the filesystem to 4800MB so we will shrink the Logical Volume video to a slightly larger size. We will therefore reduce it to 4900MB:

[root@lnx-svr-01 ~]# lvreduce -L 4900M /dev/data/video
  WARNING: Reducing active logical volume to 4.79 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce video? [y/n]: y
  Size of logical volume data/video changed from 9.99 GiB (2558 extents) to 4.79 GiB (1225 extents).
  Logical volume video successfully resized
[root@lnx-svr-01 ~]#

5. So the shrink process is complete. Let’s mount the filesystem:

[root@lnx-svr-01 ~]# mount /var/video/

6. Now run df -h to see the new size of the /var/video filesystem:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  908M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G  4.4G  4.9G  48% /var/music
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
/dev/mapper/data-video
                      4.5G  4.4G     0 100% /var/video
[root@lnx-svr-01 ~]#

7. We know that we have reduced the Logical Volume video from 9.8GB to 4.5GB, so we now have 5.3GB of free space in the Volume Group data. Run vgdisplay to confirm the amount of free space in the Volume Group data:

[root@lnx-svr-01 ~]# vgdisplay /dev/data
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.97 GiB
  PE Size               4.00 MiB
  Total PE              6392
  Alloc PE / Size       5059 / 19.76 GiB
  Free  PE / Size       1333 / 5.21 GiB
  VG UUID               IglQep-hydn-lm2D-Dc1N-LsL4-edpi-BzgMuE

[root@lnx-svr-01 ~]#

We can see that there are 1333 extents or 5.21GB of free space in the Volume Group data.

8. Run pvdisplay to show all Physical Volumes on the system:

[root@lnx-svr-01 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               2
  Allocated PE          1276
  PV UUID               OpA0C8-wv7K-0cZT-3zHW-GXeb-YLM8-hWbRjL

  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               data
  PV Size               10.00 GiB / not usable 4.70 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2558
  Free PE               0
  Allocated PE          2558
  PV UUID               VC7As8-pB3U-GGqa-l0xg-NIVj-jv7l-dTGGHl

  --- Physical volume ---
  PV Name               /dev/sdd1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               53
  Allocated PE          1225
  PV UUID               xKOs8i-UQy1-mpvu-8Hhl-zjF4-FYjb-w4DwSu

  --- Physical volume ---
  PV Name               /dev/sde1
  VG Name               data
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               1278
  Allocated PE          0
  PV UUID               s1XEBf-PCbp-fygl-yVc0-BySI-32FM-mu3laE

  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               tx5fGZ-LURm-Bqgq-Jxqz-AuJD-IWgl-YMxPle

[root@lnx-svr-01 ~]#

We can see that Volume Group data comprises of /dev/sdb1/dev/sde1. The only Physical Volume that has no data on it is /dev/sde1. We know this because it has no Allocated PEs (Physical Extents) and also that the Free PE equals = Total PE, meaning that all the extents that comprise /dev/sde1 are free:

Total PE              1278
Free PE               1278
Allocated PE          0

9. So if no data actually resides on Physical Volume /dev/sde1 we we need to remove it from Volume Group data. To do this run vgreduce per the below:

[root@lnx-svr-01 ~]# vgreduce data /dev/sde1
  Removed "/dev/sde1" from volume group "data"
[root@lnx-svr-01 ~]#

10. To remove Physical Volume /dev/sde1 run pvremove per the below:

[root@lnx-svr-01 ~]# pvremove /dev/sde1
  Labels on physical volume "/dev/sde1" successfully wiped
[root@lnx-svr-01 ~]#

So we are at a stage where the virtual disk that maps to /dev/sde can be removed. Although I know that Hard Disk 5 maps to /dev/sde (because I added it in a previous post) that doesn’t mean that the order of disks in Linux matches up with the order of disks in VMware – often they don’t match up.

11. To verify the virtual disk to linux disk mapping run the ls command per the below to list all the disks in the system:

[root@lnx-svr-01 video]# ls -l /sys/block/sd*
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sda -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:0/end_device-2:0/target2:0:0/2:0:0:0/block/sda
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdb -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:1/end_device-2:1/target2:0:1/2:0:1:0/block/sdb
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdc -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:2/end_device-2:2/target2:0:2/2:0:2:0/block/sdc
lrwxrwxrwx. 1 root root 0 Jun 14 15:02 /sys/block/sdd -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:3/end_device-2:3/target2:0:3/2:0:3:0/block/sdd
lrwxrwxrwx. 1 root root 0 Jun 14 18:05 /sys/block/sde -> ../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/port-2:4/end_device-2:4/target2:0:4/2:0:4:0/block/sde
[root@lnx-svr-01 video]#

From the above we see the bottom line relates to disk /sde. The important bit is at the end of the line; 2:0:4:0/block/sde. The numbers highlighted in red represent the SCSI-controller-no:Target-no which we require to identify the virtual disk in VMware. This corresponds to Hard Disk 5 in the VM:

20150622134432

So we definitely know that Hard Disk 5 maps to /dev/sde. As it is has been removed from LVM we can remove it from the VM now:

20150611165654

The Happy Ending

Process over, you can breathe a sigh of relief! I will re-iterate that you should ensure that you have up-to-date backups of the system prior to any shrinking activity – you can never be too safe.

Reference(s):
How to Find corresponding VMDK in Linux/UNIX

LVM – Setup and configure Filesystems in RHEL 6/Centos 6 – Part 4

Welcome to Part 4 of this series of 4 articles detailing how to setup and configure multiple filesystems running on LVM partitions. Each article will cover a specific topic, per the below:

Part 1 – Setup and configure Physical Volumes
Part 2 – Setup and configure Volume Groups
Part 3 – Setup and configure Logical Volumes
Part 4 – Setup and configure Filesystems

In Part 4 we are going to we will:

  • Format the logical volumes with the ext4 filesystem.
  • Mount the logical volumes create in Part 3 as filesystems.
  • Add these filesystems into /etc/fstab to ensure they are mounted during the boot process.

To format the music logical volume with the ext4 filesystem run:

mkfs.ext4 /dev/data/music

The resulting output is:

[root@lnx-svr-01 ~]# mkfs.ext4 /dev/data/music
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@lnx-svr-01 ~]#

Perform the same steps for the video and pictures logical volumes.

Now that the filesystems have been created, we need to mount them. First, We will create the folders upon which the filesystems will be mounted, their relationships to the corresponding folders (mount point) is:

music -------> /var/music
video -------> /var/video
pictures ----> /var/pictures

Run mkdir to create the folder above:

mkdir /var/music /var/video /var/pictures

Mount the newly created filesystems by running the mount command per the below:

mount /dev/data/music /var/music
mount /dev/data/video /var/video
mount /dev/data/pictures /var/pictures

Then run df -h to see the currently mounted filesystems:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  903M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G   23M  9.2G   1% /var/music
/dev/mapper/data-video
                      4.8G   10M  4.6G   1% /var/video
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
[root@lnx-svr-01 ~]#

We can see from the above that our filesystems are listed and mounted. However, we mounted the filesystems manually, we need them to mount automatically when the system boots up, so in order to enable this we need to add them to /etc/fstab.

So to proceed, run vi to edit /etc/fstab:

vi /etc/fstab

This is what is looks like at present:

# /etc/fstab
# Created by anaconda on Sat May 30 19:17:27 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=db505cff-1d69-4f12-b22a-9d4335dc2275 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

Add the following lines into the file:

/dev/data/music         /var/music              ext4    rw,noatime      0 0
/dev/data/video         /var/video              ext4    rw,noatime      0 0
/dev/data/pictures      /var/pictures           ext4    rw,noatime      0 0

So it looks like this:

# /etc/fstab
# Created by anaconda on Sat May 30 19:17:27 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=db505cff-1d69-4f12-b22a-9d4335dc2275 /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/data/music         /var/music              ext4    rw,noatime      0 0
/dev/data/video         /var/video              ext4    rw,noatime      0 0
/dev/data/pictures      /var/pictures           ext4    rw,noatime      0 0

Save and exit the file, then reboot the system by running the shutdown command per the below:

[root@lnx-svr-01 ~]# shutdown -r now

Broadcast message from root@lnx-svr-01.vsysad.local
        (/dev/pts/0) at 14:35 ...

The system is going down for reboot NOW!
[root@lnx-svr-01 ~]#

Once the server comes back up, login and run df -h to confirm that all of our filesystems are mounted:

[root@lnx-svr-01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       18G  903M   16G   6% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot
/dev/mapper/data-music
                      9.8G   23M  9.2G   1% /var/music
/dev/mapper/data-video
                      4.8G   10M  4.6G   1% /var/video
/dev/mapper/data-pictures
                      4.8G   10M  4.6G   1% /var/pictures
[root@lnx-svr-01 ~]#

There we have it, three filesystems running on LVM partitions which are ready for production use. I hope this was easy to follow and understand. Please also take time to read the references at the bottom, they are excellent resources and were bookmarked by myself for reference whenever I needed to check anything LVM related.

References:
How to Create LVM Partition in RHEL 6 / CentoOS
Linux Basics – LVM (Logical Volume Manager) Tutorial
How to Install LVM on Linux and Disk Operations
A Beginner’s Guide To LVM
How To Create LVM Using vgcreate, lvcreate, and lvextend lvm2 Commands