Tuesday, April 8, 2014

how to shrink an LVM Volume (also in VMware created machine)

Today at work we enjoyed shrinking a VmWare machine made to big at the beginning.

You should have a recent backup...we did not, but don't blame me in case something goes wrong. A typo in one the following commands could easily destroy data !! 

Since all of the steps must be performed on an unmounted volume (If want to reduce a non-root volume unmount it) and we wanted to operate on the root one we simply booted from a iso CD of gparted (download the image and set the machine in VMWare to boot from there) then issue the following commands:
makes any logical volumes available to the Linux: # vgchange -a y

force a file system check on the volume in question:  # e2fsck -f /dev/something/root
Device names for LVM volumes follow the convention: /dev/<volume group>/<logical volume>.
In our case I’m going to shrink the one named root. This is a critical step; resizing a file system in an inconsistent state could have disastrous consequences. 
resize the actual file system: # resize2fs /dev/something/root 18G
Replace 18G with about 90% of the size you want the final volume to be. It’s critical that the new size is greater than or equal to the size of the file system. 
We were not sure about resizefs and lvreduce whether they’re using standard computer gigabytes (1024^3 bytes) or drive manufacturer gigabytes (1000^3 bytes) so we’ll just shrink the file system a bit more than necessary and expand it to use the full space available. 
reduce the size of the logical volume:# lvreduce -L 20G /dev/polar/root
use the actual size you want for the volume. 
grow the file system to all available space on the logical volume:# resize2fs /dev/polar/root
That’s it for the Linux side.

Now if VMWare has a Thin storage you can simply resize it from the Vsphere. 
If not I'll sketch the path to follow (but without snapshots):
  • shutdown the machine
  • add a new disk of the size of the just resized Volume 20G
  • boot it and mount the new disk
  • dd if=<old disk> of=<new disk> bs=4096 count=<proper number of blocks to cover the size of your new disk>
  • shutdown
  • from VSphere switch the 2 disks (the scsi device id)
  • boot to check that everything is working 
  • shutdown and remove the old disk from VSphere
hope you find it useful 
A

No comments:

Post a Comment