How to carefully reduce an LVM logical volume and do the calculations
Hello!
Let’s imagine that we want reduce the size of an LVM partiton (logical volume) by 30 GiB.
They are 6 steps to follow:
- Backup your data (very important in case of calculation error!)
- Unmount the file system for reducing.
- Check the file system after unmount.
- Reduce the file system.
- Reduce the Logical Volume size than Current size.
- Recheck the file system for error.
I won’t explain how to do the 2 first steps, they are easy…
Check the file system
$ sudo e2fsck -ff /dev/mapper/MyVG-homeLV
e2fsck 1.44.1 (24-Mar-2018)
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/mapper/MyVG-homeLV: 510791/17571840 files (1.5% non-contiguous), 31792698/70269952 blocks
Must pass in every 5 steps of file-system check.
Reduce the file system
We have to do some calculation. And to do so, we need to know block count and block size of the partition. We can get them like this:
$ sudo dumpe2fs -h /dev/mapper/MyVG-homeLV
dumpe2fs 1.44.1 (24-Mar-2018)
[...]
Inode count: 19537920
Block count: 78134272
Reserved block count: 3906713
Free blocks: 46217093
Free inodes: 19027129
First block: 0
Block size: 4096
Fragment size: 4096
[...]
By calculate [block count] * [block size]
, we get the size of our partition in bytes:
>>> 78134272*4096
320037978112
As I said before, to we want reduce the size of the partition by 30 GiB which is equal to 32212254720 bytes:
>>> 30*1024**3
32212254720
So our new partition size is…
>>> 320037978112-32212254720
287825723392
… 287825723392 bytes which is equal to 281079808 KiB:
>>> 287825723392/1024
281079808
So we can now set the partition new size:
$ sudo resize2fs /dev/mapper/MyVG-homeLV 281079808K
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/MyVG-homeLV to 70269952 (4k) blocks.
The filesystem on /dev/mapper/MyVG-homeLV is now 70269952 (4k) blocks long.
Reduce the Logical Volume size
We need to know the LVM volume group PE size:
$ sudo vgdisplay
[...]
PE Size 4.00 MiB
Total PE 83471
Alloc PE / Size 83471 / <326.06 GiB
Free PE / Size 0 / 0
VG UUID 8ce6vr-vmIt-XPis-bbAx-X9B0-7WYd-EhyPWf
The PE size is 4 MiB.
30 GiB is equal to 30720 MiB:
>>> 30*1024
30720
So 30 GiB is equal to 7680 PE:
>>> 30720/4
7680.0
Now, we can reduce the logical volume using PE size:
$ sudo lvreduce -l -7680 /dev/mapper/MyVG-homeLV
WARNING: Reducing active logical volume to <268.06 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce MyVG/homeLV? [y/n]: y
Size of logical volume MyVG/homeLV changed from <298.06 GiB (76303 extents) to <268.06 GiB (68623 extents).
Logical volume MyVG/homeLV successfully resized.
Recheck the file system for error
$ sudo resize2fs /dev/mapper/MyVG-homeLV
resize2fs 1.44.1 (24-Mar-2018)
The filesystem is already 70269952 (4k) blocks long. Nothing to do!$ sudo e2fsck -ff /dev/mapper/MyVG-homeLV
e2fsck 1.44.1 (24-Mar-2018)
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/mapper/MyVG-homeLV: 510791/17571840 files (1.5% non-contiguous), 31792698/70269952 blocks
If these 2 commands didn’t report any error, then it’s fine.
Hope that it helped you! 😃