Target system is CentOS 6.x but resizing operations will be done in %pre section of CentOS 7.6 kickstart.

I have already run resize2fs -M /dev/mapper/centos-home successfully. If I mount the volume, df says it's 100% full. This is good.

Now I need to reduce the size of the logical volume to match the size of the ext4 filesystem. I don't want to use rough estimates. I also don't need to conserve every possible extent. Within a >= 1GB range is fine. i.e. volume can be reduced to 1GB >= filesystem size.

The target volume does contain data I can't lose. I don't have shell (or any other) access to the system. The system has no CentOS repos available so I cannot upgrade it. I don't even have remote hands I can talk to. I'm using a different computer to build and test my install ISO on.

My %pre syntax/script needs to safely reduce /dev/mapper/centos-home so I can create a new root volume for CentOS 7.6 install. Yes, this entire proposition is nuts.

  • usedGB=$(df -h $homevol | tail -1 | awk '{print $2}' | awk '{printf("%d\n",$1 + 1)}') ; echo $usedGB prints '5' How do I use pass my variable $usedGB to lvreduce ?
    – mr.zog
    Apr 26, 2019 at 17:29
  • lvreduce -L -$usedGB\G /dev/mapper/centos-home WARNING: Reducing active logical volume to 15.00 GiB. . . . but echo $usedGB reports only 5GB . . .
    – mr.zog
    Apr 26, 2019 at 17:39
  • OK. I needed $usedGB\G not -$usedGB\G Had to remove the hyphen.
    – mr.zog
    Apr 26, 2019 at 17:42

2 Answers 2


Do it in one step.

/sbin/lvresize --resizefs --size -8g  /dev/mapper/centos-home

Where 8g is the size of the root volume you want to install.

Constant size may be acceptable because the size of your new install is predictable.

  • /sbin/lvresize --resizefs --size -$usedGB\g /dev/mapper/centos-root This is the cleanest way.
    – mr.zog
    Apr 26, 2019 at 19:15
  • If you have a script to calculate the maximum reduction, answer your own question with it. I find the constant reduction safer because if it cannot reduce by enough it aborts. Apr 27, 2019 at 12:03
  • I don't understand what you mean by "constant reduction."
    – mr.zog
    Apr 27, 2019 at 20:22
  • By constant reduction I mean hard coded reducing by 8g to simplify things. Consistent sizing of the volume, and if it fails there won't be enough space for the new volume. Your script to detect how much to reduce is a different approach. Apr 27, 2019 at 20:26

It's not really a script per se.

mkdir /tmp/rootlv
mount -t ext4 -o ro $rootvol /tmp/rootlv
usedROOT=$(df -h $rootvol |  tail -1 | awk '{print $2}' | awk '{printf("%d\n",$1 + 1)}')
umount /tmp/rootvol
/sbin/lvresize --resizefs --size $usedROOT\g  $rootvol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.